Skip to content

Commit

Permalink
Merge branch 'MDL-70058-403' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_403_STABLE
  • Loading branch information
junpataleta committed Feb 7, 2024
2 parents 4f45925 + 22e5c8a commit 219899c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
36 changes: 33 additions & 3 deletions course/externallib.php
Expand Up @@ -818,6 +818,26 @@ public static function get_courses_returns() {
);
}

/**
* Return array of all editable course custom fields indexed by their shortname
*
* @param \context $context
* @param int $courseid
* @return \core_customfield\field_controller[]
*/
public static function get_editable_customfields(\context $context, int $courseid = 0): array {
$result = [];

$handler = \core_course\customfield\course_handler::create();
$handler->set_parent_context($context);

foreach ($handler->get_editable_fields($courseid) as $field) {
$result[$field->get('shortname')] = $field;
}

return $result;
}

/**
* Returns description of method parameters
*
Expand Down Expand Up @@ -990,8 +1010,13 @@ public static function create_courses($courses) {

// Custom fields.
if (!empty($course['customfields'])) {
$customfields = self::get_editable_customfields($context);
foreach ($course['customfields'] as $field) {
$course['customfield_'.$field['shortname']] = $field['value'];
if (array_key_exists($field['shortname'], $customfields)) {
// Ensure we're populating the element form fields correctly.
$controller = \core_customfield\data_controller::create(0, null, $customfields[$field['shortname']]);
$course[$controller->get_form_element_name()] = $field['value'];
}
}
}

Expand Down Expand Up @@ -1203,10 +1228,15 @@ public static function update_courses($courses) {
}
}

// Prepare list of custom fields.
// Custom fields.
if (isset($course['customfields'])) {
$customfields = self::get_editable_customfields($context, $course['id']);
foreach ($course['customfields'] as $field) {
$course['customfield_' . $field['shortname']] = $field['value'];
if (array_key_exists($field['shortname'], $customfields)) {
// Ensure we're populating the element form fields correctly.
$controller = \core_customfield\data_controller::create(0, null, $customfields[$field['shortname']]);
$course[$controller->get_form_element_name()] = $field['value'];
}
}
}

Expand Down
67 changes: 48 additions & 19 deletions course/tests/externallib_test.php
Expand Up @@ -453,10 +453,12 @@ public function test_create_courses() {
// Custom fields.
$fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']);

$customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text',
'categoryid' => $fieldcategory->get('id'),
'configdata' => ['visibility' => \core_course\customfield\course_handler::VISIBLETOALL]];
$field = self::getDataGenerator()->create_custom_field($customfield);
$fieldtext = self::getDataGenerator()->create_custom_field([
'categoryid' => $fieldcategory->get('id'), 'name' => 'Text', 'shortname' => 'text', 'type' => 'text',
]);
$fieldtextarea = self::getDataGenerator()->create_custom_field([
'categoryid' => $fieldcategory->get('id'), 'name' => 'Textarea', 'shortname' => 'textarea', 'type' => 'textarea',
]);

// Set the required capabilities by the external function
$contextid = context_system::instance()->id;
Expand Down Expand Up @@ -508,7 +510,10 @@ public function test_create_courses() {
$course4['fullname'] = 'Test course with custom fields';
$course4['shortname'] = 'Testcoursecustomfields';
$course4['categoryid'] = $category->id;
$course4['customfields'] = [['shortname' => $customfield['shortname'], 'value' => 'Test value']];
$course4['customfields'] = [
['shortname' => $fieldtext->get('shortname'), 'value' => 'And I want to tell you so much'],
['shortname' => $fieldtextarea->get('shortname'), 'value' => 'I love you'],
];
$courses = array($course4, $course1, $course2, $course3);

$createdcourses = core_course_external::create_courses($courses);
Expand Down Expand Up @@ -580,7 +585,10 @@ public function test_create_courses() {

$handler = core_course\customfield\course_handler::create();
$customfields = $handler->export_instance_data_object($createdcourse['id']);
$this->assertEquals((object)['test' => 'Test value'], $customfields);
$this->assertEquals((object) [
'text' => 'And I want to tell you so much',
'textarea' => '<div class="text_to_html">I love you</div>',
], $customfields);
} else {
throw new moodle_exception('Unexpected shortname');
}
Expand Down Expand Up @@ -1876,12 +1884,17 @@ public function test_update_courses() {

// Course with custom fields.
$fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']);
$customfield = ['shortname' => 'test', 'name' => 'Custom field', 'type' => 'text',
'categoryid' => $fieldcategory->get('id'),
'configdata' => ['visibility' => \core_course\customfield\course_handler::VISIBLETOALL, 'locked' => 1]];
$field = self::getDataGenerator()->create_custom_field($customfield);

$originalcourse3 = self::getDataGenerator()->create_course(['customfield_test' => 'Test value']);
$fieldtext = self::getDataGenerator()->create_custom_field([
'categoryid' => $fieldcategory->get('id'), 'name' => 'Text', 'shortname' => 'text', 'type' => 'text', 'configdata' => [
'locked' => 1,
],
]);
$fieldtextarea = self::getDataGenerator()->create_custom_field([
'categoryid' => $fieldcategory->get('id'), 'name' => 'Textarea', 'shortname' => 'textarea', 'type' => 'textarea',
]);

$originalcourse3 = self::getDataGenerator()->create_course();
self::getDataGenerator()->enrol_user($USER->id, $originalcourse3->id, $roleid);

// Course values to be updated.
Expand Down Expand Up @@ -1914,8 +1927,11 @@ public function test_update_courses() {
$course2['forcetheme'] = 'classic';

$course3['id'] = $originalcourse3->id;
$updatedcustomfieldvalue = ['shortname' => 'test', 'value' => 'Updated test value'];
$course3['customfields'] = [$updatedcustomfieldvalue];
$course3['customfields'] = [
['shortname' => $fieldtext->get('shortname'), 'value' => 'I long to see the sunlight in your hair'],
['shortname' => $fieldtextarea->get('shortname'), 'value' => 'And tell you time and time again'],
];

$courses = array($course1, $course2, $course3);

$updatedcoursewarnings = core_course_external::update_courses($courses);
Expand Down Expand Up @@ -1956,7 +1972,10 @@ public function test_update_courses() {
}

$this->assertEquals($course2['enablecompletion'], $courseinfo->enablecompletion);
$this->assertEquals(['test' => null], (array)$customfields);
$this->assertEquals((object) [
'text' => null,
'textarea' => null,
], $customfields);
} else if ($course['id'] == $course1['id']) {
$this->assertEquals($course1['fullname'], $courseinfo->fullname);
$this->assertEquals($course1['shortname'], $courseinfo->shortname);
Expand All @@ -1966,9 +1985,15 @@ public function test_update_courses() {
$this->assertEquals(5, course_get_format($course['id'])->get_last_section_number());
$this->assertEquals(0, $courseinfo->newsitems);
$this->assertEquals(FORMAT_MOODLE, $courseinfo->summaryformat);
$this->assertEquals(['test' => null], (array)$customfields);
$this->assertEquals((object) [
'text' => null,
'textarea' => null,
], $customfields);
} else if ($course['id'] == $course3['id']) {
$this->assertEquals(['test' => $updatedcustomfieldvalue['value']], (array)$customfields);
$this->assertEquals((object) [
'text' => 'I long to see the sunlight in your hair',
'textarea' => '<div class="text_to_html">And tell you time and time again</div>',
], $customfields);
} else {
throw new moodle_exception('Unexpected shortname');
}
Expand Down Expand Up @@ -2106,14 +2131,18 @@ public function test_update_courses() {
$this->setUser($user);
self::getDataGenerator()->enrol_user($user->id, $course3['id'], $roleid);

$newupdatedcustomfieldvalue = ['shortname' => 'test', 'value' => 'New updated value'];
$course3['customfields'] = [$newupdatedcustomfieldvalue];
$course3['customfields'] = [
['shortname' => 'text', 'value' => 'New updated value'],
];

core_course_external::update_courses([$course3]);

// Custom field was not updated.
$customfields = \core_course\customfield\course_handler::create()->export_instance_data_object($course3['id']);
$this->assertEquals(['test' => $updatedcustomfieldvalue['value']], (array)$customfields);
$this->assertEquals((object) [
'text' => 'I long to see the sunlight in your hair',
'textarea' => '<div class="text_to_html">And tell you time and time again</div>',
], $customfields);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions customfield/field/textarea/classes/data_controller.php
Expand Up @@ -88,7 +88,14 @@ public function instance_form_save(\stdClass $datanew) {
if (!property_exists($datanew, $fieldname)) {
return;
}

// Normalise form data, for cases it's come from an external source.
$fromform = $datanew->$fieldname;
if (!is_array($fromform)) {
$fromform = ['text' => $fromform];
$fromform['format'] = $this->get('id') ? $this->get('valueformat') :
$this->get_field()->get_configdata_property('defaultvalueformat');
}

if (!$this->get('id')) {
$this->data->set('value', '');
Expand Down
1 change: 1 addition & 0 deletions customfield/tests/generator/lib.php
Expand Up @@ -124,6 +124,7 @@ public function create_field($record) : field_controller {
'locked' => 0,
'visibility' => 2,
'defaultvalue' => '',
'defaultvalueformat' => FORMAT_MOODLE,
'displaysize' => 0,
'maxlength' => 0,
'ispassword' => 0,
Expand Down

0 comments on commit 219899c

Please sign in to comment.