MDL-41185 fix url in course_viewed event after course deleted

Includes a minor fix for course invalidation after deleting of course,
this improvement should not affect normal execution, that is why
it was not backported.
This commit is contained in:
Petr Skoda
2014-04-22 11:52:37 +08:00
parent f49151c30b
commit 0cd189bdf3
3 changed files with 18 additions and 2 deletions
+6 -2
View File
@@ -70,14 +70,18 @@ class course_viewed extends \core\event\base {
/**
* Get URL related to the action.
*
* @return \moodle_url
* @return \moodle_url|null
*/
public function get_url() {
$sectionid = null;
if (isset($this->other['coursesectionid'])) {
$sectionid = $this->other['coursesectionid'];
}
return \course_get_url($this->courseid, $sectionid);
try {
return \course_get_url($this->courseid, $sectionid);
} catch (\Exception $e) {
return null;
}
}
/**
+5
View File
@@ -4933,6 +4933,11 @@ function delete_course($courseorid, $showfeedback = true) {
$DB->delete_records("course", array("id" => $courseid));
$DB->delete_records("course_format_options", array("courseid" => $courseid));
// Reset all course related caches here.
if (class_exists('format_base', false)) {
format_base::reset_course_cache($courseid);
}
// Trigger a course deleted event.
$event = \core\event\course_deleted::create(array(
'objectid' => $course->id,
+7
View File
@@ -245,14 +245,21 @@ class core_events_testcase extends advanced_testcase {
// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$loggeddata = $event->get_data();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\core\event\course_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'course', 'view section', 'view.php?id=' . $course->id . '&sectionid='
. $sectionid, $sectionid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
delete_course($course->id, false);
$restored = \core\event\base::restore($loggeddata, array('origin' => 'web', 'ip' => '127.0.0.1'));
$this->assertInstanceOf('\core\event\course_viewed', $restored);
$this->assertNull($restored->get_url());
}
}