Merge branch 'MDL-65369_master' of https://github.com/marxjohnson/moodle

This commit is contained in:
Adrian Greeve 2019-06-13 10:25:21 +08:00 committed by Eloy Lafuente (stronk7)
commit fa5bed66de
3 changed files with 60 additions and 4 deletions

View File

@ -2201,7 +2201,8 @@ function move_courses($courseids, $categoryid) {
'objectid' => $course->id,
'context' => context_course::instance($course->id),
'other' => array('shortname' => $dbcourse->shortname,
'fullname' => $dbcourse->fullname)
'fullname' => $dbcourse->fullname,
'updatedfields' => array('category' => $category->id))
));
$event->set_legacy_logdata(array($course->id, 'course', 'move', 'edit.php?id=' . $course->id, $course->id));
$event->trigger();
@ -2513,8 +2514,6 @@ function create_course($data, $editoroptions = NULL) {
function update_course($data, $editoroptions = NULL) {
global $DB, $CFG;
$data->timemodified = time();
// Prevent changes on front page course.
if ($data->id == SITEID) {
throw new moodle_exception('invalidcourse', 'error');
@ -2523,6 +2522,28 @@ function update_course($data, $editoroptions = NULL) {
$oldcourse = course_get_format($data->id)->get_course();
$context = context_course::instance($oldcourse->id);
// Capture the updated fields for the log data.
$updatedfields = [];
foreach (get_object_vars($oldcourse) as $field => $value) {
if ($field == 'summary_editor') {
if (($data->$field)['text'] !== $value['text']) {
// The summary might be very long, we don't wan't to fill up the log record with the full text.
$updatedfields[$field] = '(updated)';
}
} else if ($field == 'tags') {
// Tags might not have the same array keys, just check the values.
if (array_values($data->$field) !== array_values($value)) {
$updatedfields[$field] = $data->$field;
}
} else {
if (isset($data->$field) && $data->$field != $value) {
$updatedfields[$field] = $data->$field;
}
}
}
$data->timemodified = time();
if ($editoroptions) {
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
}
@ -2629,7 +2650,8 @@ function update_course($data, $editoroptions = NULL) {
'objectid' => $course->id,
'context' => context_course::instance($course->id),
'other' => array('shortname' => $course->shortname,
'fullname' => $course->fullname)
'fullname' => $course->fullname,
'updatedfields' => $updatedfields)
));
$event->set_legacy_logdata(array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id));

View File

@ -1800,6 +1800,39 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertEventContextNotUsed($event);
}
/**
* Test that triggering a course_updated event logs changes.
*/
public function test_course_updated_event_with_changes() {
global $DB;
$this->resetAfterTest();
// Create a course.
$course = $this->getDataGenerator()->create_course((object)['visible' => 1]);
$editedcourse = $DB->get_record('course', ['id' => $course->id]);
$editedcourse->visible = 0;
// Update course and catch course_updated event.
$sink = $this->redirectEvents();
update_course($editedcourse);
$events = $sink->get_events();
$sink->close();
$event = array_shift($events);
$this->assertInstanceOf('\core\event\course_updated', $event);
$otherdata = [
'shortname' => $course->shortname,
'fullname' => $course->fullname,
'updatedfields' => [
'visible' => 0
]
];
$this->assertEquals($otherdata, $event->other);
}
/**
* Test that triggering a course_deleted event works as expected.
*/

View File

@ -34,6 +34,7 @@ defined('MOODLE_INTERNAL') || die();
*
* - string shortname: (optional) shortname of course.
* - string fullname: (optional) fullname of course.
* - string updatedfields: (optional) array of course table fields edited in this event, ['fieldname' => 'newvalue']
* }
*
* @package core