Skip to content

Commit

Permalink
fixup! fixup! MDL-82715 customfield_number: Add automatically populat…
Browse files Browse the repository at this point in the history
…ed providers.
  • Loading branch information
ilyatregubov committed Sep 9, 2024
1 parent fd295ba commit ffe68f4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion customfield/field/number/amd/build/recalculate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion customfield/field/number/amd/build/recalculate.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion customfield/field/number/amd/src/recalculate.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function init() {
initialised = true;

document.addEventListener('click', (e) => {
e.preventDefault();
const target = e.target.closest(SELECTORS.wrapper + " " + SELECTORS.link);
if (!target) {
return;
Expand All @@ -54,6 +53,7 @@ export function init() {
if (!el) {
return;
}
e.preventDefault();
const fieldid = target.dataset.fieldid;
const instanceid = target.dataset.instanceid;

Expand Down
18 changes: 7 additions & 11 deletions customfield/field/number/classes/field_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public function config_form_definition(MoodleQuickForm $mform): void {
$mform->addElement('header', 'specificsettings', get_string('specificsettings', 'customfield_number'));
$mform->setExpanded('specificsettings');

$this->add_field_type_select($mform);
// Add form config elements for each provider.
foreach ($this->get_providers() as $provider) {
$provider->config_form_definition($mform);
}

// Default value.
$mform->addElement('float', 'configdata[defaultvalue]', get_string('defaultvalue', 'core_customfield'));
if ($this->get_configdata_property('defaultvalue') === null) {
Expand Down Expand Up @@ -102,9 +108,8 @@ public function config_form_definition(MoodleQuickForm $mform): void {

// Display format settings.
// TODO: Change this after MDL-82996 fixed.
$randelname = 'str_' . random_string();
$randelname = 'str_display_format';
$mform->addGroup([], $randelname, html_writer::tag('h4', get_string('headerdisplaysettings', 'customfield_number')));
$mform->hideIf($randelname, 'configdata[fieldtype]', 'ne', '');

// Display template.
$mform->addElement('text', 'configdata[display]', get_string('display', 'customfield_number'),
Expand All @@ -114,7 +119,6 @@ public function config_form_definition(MoodleQuickForm $mform): void {
if ($this->get_configdata_property('display') === null) {
$mform->setDefault('configdata[display]', '{value}');
}
$mform->hideIf('configdata[display]', 'configdata[fieldtype]', 'ne', '');

// Display when zero.
$mform->addElement('text', 'configdata[displaywhenzero]', get_string('displaywhenzero', 'customfield_number'),
Expand All @@ -124,14 +128,6 @@ public function config_form_definition(MoodleQuickForm $mform): void {
if ($this->get_configdata_property('displaywhenzero') === null) {
$mform->setDefault('configdata[displaywhenzero]', 0);
}

$this->add_field_type_select($mform);

// Add form config elements for each provider.
foreach ($this->get_providers() as $provider) {
$provider->config_form_definition($mform);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public function config_form_definition(MoodleQuickForm $mform): void {
$mform->hideIf('configdata[activitytypes]', 'configdata[fieldtype]', 'ne', get_class($this));
$mform->addElement('advcheckbox', 'configdata[nofactivities_zero]', get_string('displaywhenzero', 'customfield_number'));
$mform->hideIf('configdata[nofactivities_zero]', 'configdata[fieldtype]', 'ne', get_class($this));

$mform->hideIf('configdata[displaywhenzero]', 'configdata[fieldtype]', 'eq', get_class($this));
$mform->hideIf('configdata[display]', 'configdata[fieldtype]', 'eq', get_class($this));
$mform->hideIf('str_display_format', 'configdata[fieldtype]', 'eq', get_class($this));
$mform->hideIf('inputformat', 'configdata[fieldtype]', 'eq', get_class($this));
}

/**
Expand Down
95 changes: 47 additions & 48 deletions customfield/field/number/tests/external/recalculate_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,70 @@
*/
final class recalculate_test extends \externallib_advanced_testcase {

/** @var stdClass */
private $course = null;
/** @var \core_customfield\field_controller */
private $field = null;

/**
* Tests set up.
* Tests when teacher can not edit locked field.
*/
public function setUp(): void {
public function test_execute_no_permission(): void {
global $DB;
parent::setUp();

$this->resetAfterTest();
[$course, $field] = $this->prepare_course();
$configdata = [
'fieldtype' => 'customfield_number\\local\\numberproviders\\nofactivities',
'activitytypes' => ['assign', 'forum'],
'locked' => 1,
];
$field->set('configdata', json_encode($configdata));
$field->save();

$context = \context_course::instance($course->id);
$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$this->unassignUserCapability('moodle/course:changelockedcustomfields', $context->id, $roleid);

$this->expectException(moodle_exception::class);
$this->expectExceptionMessage(get_string('update'));
recalculate::execute($field->get('id'), (int)$course->id);
}

/**
* Tests when all data is valid.
*/
public function test_execute(): void {
$this->resetAfterTest();
[$course, $field] = $this->prepare_course();
$result = recalculate::execute($field->get('id'), (int)$course->id);
$result = external_api::clean_returnvalue(recalculate::execute_returns(), $result);
$this->assertEquals(3.0, $result['value']);
}

/**
* Create a course with number custom field.
* @return array An array with the course object and field object.
*/
private function prepare_course(): array {
global $DB;

$this->course = $this->getDataGenerator()->create_course();
$course = $this->getDataGenerator()->create_course();

// Add teacher to a course.
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($teacher->id, $this->course->id, $roleids['editingteacher']);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);

$this->getDataGenerator()->create_module('assign', ['course' => $this->course->id, 'name' => 'Assign1', 'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $this->course->id, 'name' => 'Assign2', 'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $this->course->id, 'name' => 'Assign3', 'visible' => 0]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign1', 'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign2', 'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign3', 'visible' => 0]);

$this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id, 'name' => 'Quiz1', 'visible' => 1]);
$this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id, 'name' => 'Quiz2', 'visible' => 0]);
$this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz1', 'visible' => 1]);
$this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz2', 'visible' => 0]);

$this->getDataGenerator()->create_module('forum', ['course' => $this->course->id, 'name' => 'Forum1', 'visible' => 1]);
$this->getDataGenerator()->create_module('forum', ['course' => $course->id, 'name' => 'Forum1', 'visible' => 1]);

/** @var core_customfield_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');

$category = $generator->create_category();
$this->field = $generator->create_field(
$field = $generator->create_field(
[
'categoryid' => $category->get('id'),
'shortname' => 'myfield', 'type' => 'number',
Expand All @@ -85,38 +114,8 @@ public function setUp(): void {
]
);
$this->setUser($teacher);
}

/**
* Tests when teacher can not edit locked field.
*/
public function test_execute_no_permission(): void {
global $DB;

$configdata = [
'fieldtype' => 'customfield_number\\local\\numberproviders\\nofactivities',
'activitytypes' => ['assign', 'forum'],
'locked' => 1,
];
$this->field->set('configdata', json_encode($configdata));
$this->field->save();

$context = \context_course::instance($this->course->id);
$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$this->unassignUserCapability('moodle/course:changelockedcustomfields', $context->id, $roleid);

$this->expectException(moodle_exception::class);
$this->expectExceptionMessage(get_string('update'));
recalculate::execute($this->field->get('id'), (int)$this->course->id);
}

/**
* Tests when all data is valid.
*/
public function test_execute(): void {
$result = recalculate::execute($this->field->get('id'), (int)$this->course->id);
$result = external_api::clean_returnvalue(recalculate::execute_returns(), $result);
$this->assertEquals(3.0, $result['value']);
return [$course, $field];
}

}

0 comments on commit ffe68f4

Please sign in to comment.