Commit fbc4b778 authored by Marina Glancy's avatar Marina Glancy
Browse files

MDL-44321 events: cleaning up course_module_viewed event

parent 76e4de31
...@@ -96,6 +96,10 @@ abstract class course_module_viewed extends base { ...@@ -96,6 +96,10 @@ abstract class course_module_viewed extends base {
if (empty($this->objectid) || empty($this->objecttable)) { if (empty($this->objectid) || empty($this->objecttable)) {
throw new \coding_exception('course_module_viewed event must define objectid and object table.'); throw new \coding_exception('course_module_viewed event must define objectid and object table.');
} }
// Make sure the context level is set to module.
if ($this->contextlevel != CONTEXT_MODULE) {
throw new \coding_exception('Context passed must be module context.');
}
} }
} }
...@@ -80,6 +80,8 @@ if ($chapterid == '0') { // Go to first chapter if no given. ...@@ -80,6 +80,8 @@ if ($chapterid == '0') { // Go to first chapter if no given.
'objectid' => $book->id 'objectid' => $book->id
); );
$event = \mod_book\event\course_module_viewed::create($params); $event = \mod_book\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('book', $book); $event->add_record_snapshot('book', $book);
$event->trigger(); $event->trigger();
......
...@@ -81,9 +81,10 @@ ...@@ -81,9 +81,10 @@
$eventdata = array(); $eventdata = array();
$eventdata['objectid'] = $choice->id; $eventdata['objectid'] = $choice->id;
$eventdata['context'] = $context; $eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;
$event = \mod_choice\event\course_module_viewed::create($eventdata); $event = \mod_choice\event\course_module_viewed::create($eventdata);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->trigger(); $event->trigger();
/// Check to see if groups are being used in this choice /// Check to see if groups are being used in this choice
......
...@@ -260,6 +260,8 @@ ...@@ -260,6 +260,8 @@
'objectid' => $data->id 'objectid' => $data->id
); );
$event = \mod_data\event\course_module_viewed::create($params); $event = \mod_data\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('data', $data); $event->add_record_snapshot('data', $data);
$event->trigger(); $event->trigger();
......
...@@ -92,9 +92,6 @@ class course_module_viewed extends \core\event\course_module_viewed { ...@@ -92,9 +92,6 @@ class course_module_viewed extends \core\event\course_module_viewed {
if (!isset($this->other['anonymous'])) { if (!isset($this->other['anonymous'])) {
throw new \coding_exception("Field other['anonymous'] cannot be empty"); throw new \coding_exception("Field other['anonymous'] cannot be empty");
} }
if (!isset($this->other['cmid'])) {
throw new \coding_exception("Field other['cmid'] cannot be empty");
}
// Call parent validations. // Call parent validations.
parent::validate_data(); parent::validate_data();
......
...@@ -176,39 +176,11 @@ class mod_feedback_events_testcase extends advanced_testcase { ...@@ -176,39 +176,11 @@ class mod_feedback_events_testcase extends advanced_testcase {
$context = context_module::instance($this->eventcm->id); $context = context_module::instance($this->eventcm->id);
// Test not setting instanceid. // Test not setting other['anonymous'].
try {
\mod_feedback\event\response_deleted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'anonymous' => 2)
));
$this->assertEventContextNotUsed($event);
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting cmid.
try {
\mod_feedback\event\response_deleted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('instanceid' => $this->eventfeedback->id, 'anonymous' => 2)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['cmid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['cmid'] cannot be empty", $e->getMessage());
}
// Test not setting anonymous.
try { try {
\mod_feedback\event\response_submitted::create(array( \mod_feedback\event\response_submitted::create(array(
'context' => $context, 'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id, 'objectid' => $this->eventfeedbackcompleted->id
'other' => array('cmid' => $this->eventcm->id, 'instanceid' => $this->eventfeedback->id)
)); ));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without $this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['anonymous']"); other['anonymous']");
...@@ -252,8 +224,6 @@ class mod_feedback_events_testcase extends advanced_testcase { ...@@ -252,8 +224,6 @@ class mod_feedback_events_testcase extends advanced_testcase {
$this->assertEquals($USER->id, $event->userid); $this->assertEquals($USER->id, $event->userid);
$this->assertEquals($USER->id, $event->relateduserid); $this->assertEquals($USER->id, $event->relateduserid);
$this->assertEquals('feedback_completed', $event->objecttable); $this->assertEquals('feedback_completed', $event->objecttable);
$this->assertEquals($this->eventcm->id, $event->other['cmid']);
$this->assertEquals($this->eventcm->instance, $event->other['instanceid']);
$this->assertEquals(FEEDBACK_ANONYMOUS_YES, $event->other['anonymous']); $this->assertEquals(FEEDBACK_ANONYMOUS_YES, $event->other['anonymous']);
$this->setUser($this->eventuser); $this->setUser($this->eventuser);
$this->assertFalse($event->can_view()); $this->assertFalse($event->can_view());
......
...@@ -101,11 +101,9 @@ $event = \mod_feedback\event\course_module_viewed::create(array( ...@@ -101,11 +101,9 @@ $event = \mod_feedback\event\course_module_viewed::create(array(
'objectid' => $feedback->id, 'objectid' => $feedback->id,
'context' => $context, 'context' => $context,
'other' => array( 'other' => array(
'cmid' => $cm->id,
'instanceid' => $feedback->id,
'anonymous' => $feedback->anonymous 'anonymous' => $feedback->anonymous
) )
)); ));
$event->add_record_snapshot('course_modules', $cm); $event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course); $event->add_record_snapshot('course', $course);
$event->add_record_snapshot('feedback', $feedback); $event->add_record_snapshot('feedback', $feedback);
......
...@@ -54,6 +54,8 @@ $params = array( ...@@ -54,6 +54,8 @@ $params = array(
'objectid' => $folder->id 'objectid' => $folder->id
); );
$event = \mod_folder\event\course_module_viewed::create($params); $event = \mod_folder\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('folder', $folder); $event->add_record_snapshot('folder', $folder);
$event->trigger(); $event->trigger();
......
...@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die(); ...@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2014 Dan Poltawski <dan@moodle.com> * @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
class forum_viewed extends \core\event\base { class course_module_viewed extends \core\event\course_module_viewed {
/** /**
* Init method. * Init method.
...@@ -46,31 +46,13 @@ class forum_viewed extends \core\event\base { ...@@ -46,31 +46,13 @@ class forum_viewed extends \core\event\base {
$this->data['objecttable'] = 'forum'; $this->data['objecttable'] = 'forum';
} }
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user {$this->userid} has viewed the forum {$this->objectid}";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventforumviewed', 'mod_forum');
}
/** /**
* Get URL related to the action * Get URL related to the action
* *
* @return \moodle_url * @return \moodle_url
*/ */
public function get_url() { public function get_url() {
return new \moodle_url('/mod/forum/view.php', array('d' => $this->objectid)); return new \moodle_url('/mod/forum/view.php', array('f' => $this->objectid));
} }
/** /**
...@@ -83,24 +65,5 @@ class forum_viewed extends \core\event\base { ...@@ -83,24 +65,5 @@ class forum_viewed extends \core\event\base {
$this->objectid, $this->contextinstanceid); $this->objectid, $this->contextinstanceid);
} }
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if ($this->contextlevel != CONTEXT_MODULE) {
throw new \coding_exception('Context passed must be module context.');
}
if (!isset($this->objectid)) {
throw new \coding_exception('objectid must be set to the forumid.');
}
}
} }
...@@ -150,7 +150,6 @@ $string['eventdiscussionupdated'] = 'Discussion updated'; ...@@ -150,7 +150,6 @@ $string['eventdiscussionupdated'] = 'Discussion updated';
$string['eventdiscussiondeleted'] = 'Discussion deleted'; $string['eventdiscussiondeleted'] = 'Discussion deleted';
$string['eventdiscussionmoved'] = 'Discussion moved'; $string['eventdiscussionmoved'] = 'Discussion moved';
$string['eventdiscussionviewed'] = 'Discussion viewed'; $string['eventdiscussionviewed'] = 'Discussion viewed';
$string['eventforumviewed'] = 'Forum viewed';
$string['eventuserreportviewed'] = 'User report viewed'; $string['eventuserreportviewed'] = 'User report viewed';
$string['eventpostcreated'] = 'Post created'; $string['eventpostcreated'] = 'Post created';
$string['eventpostdeleted'] = 'Post deleted'; $string['eventpostdeleted'] = 'Post deleted';
......
...@@ -595,9 +595,9 @@ class mod_forum_events_testcase extends advanced_testcase { ...@@ -595,9 +595,9 @@ class mod_forum_events_testcase extends advanced_testcase {
} }
/** /**
* Ensure forum_viewed event validates that the forumid is set. * Ensure course_module_viewed event validates that the forumid is set.
*/ */
public function test_forum_viewed_objectid_validation() { public function test_course_module_viewed_objectid_validation() {
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$context = context_module::instance($forum->cmid); $context = context_module::instance($forum->cmid);
...@@ -606,14 +606,14 @@ class mod_forum_events_testcase extends advanced_testcase { ...@@ -606,14 +606,14 @@ class mod_forum_events_testcase extends advanced_testcase {
'context' => $context, 'context' => $context,
); );
$this->setExpectedException('coding_exception', 'objectid must be set to the forumid.'); $this->setExpectedException('coding_exception', 'must define objectid');
\mod_forum\event\forum_viewed::create($params); \mod_forum\event\course_module_viewed::create($params);
} }
/** /**
* Ensure forum_viewed event validates that the contextlevel is correct. * Ensure course_module_viewed event validates that the contextlevel is correct.
*/ */
public function test_forum_viewed_context_validation() { public function test_course_module_viewed_context_validation() {
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
...@@ -623,13 +623,13 @@ class mod_forum_events_testcase extends advanced_testcase { ...@@ -623,13 +623,13 @@ class mod_forum_events_testcase extends advanced_testcase {
); );
$this->setExpectedException('coding_exception', 'Context passed must be module context.'); $this->setExpectedException('coding_exception', 'Context passed must be module context.');
\mod_forum\event\forum_viewed::create($params); \mod_forum\event\course_module_viewed::create($params);
} }
/** /**
* Test the forum_viewed event. * Test the course_module_viewed event.
*/ */
public function test_forum_viewed() { public function test_course_module_viewed() {
// Setup test data. // Setup test data.
$course = $this->getDataGenerator()->create_course(); $course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
...@@ -641,7 +641,7 @@ class mod_forum_events_testcase extends advanced_testcase { ...@@ -641,7 +641,7 @@ class mod_forum_events_testcase extends advanced_testcase {
'objectid' => $forum->id, 'objectid' => $forum->id,
); );
$event = \mod_forum\event\forum_viewed::create($params); $event = \mod_forum\event\course_module_viewed::create($params);
// Trigger and capture the event. // Trigger and capture the event.
$sink = $this->redirectEvents(); $sink = $this->redirectEvents();
...@@ -651,7 +651,7 @@ class mod_forum_events_testcase extends advanced_testcase { ...@@ -651,7 +651,7 @@ class mod_forum_events_testcase extends advanced_testcase {
$event = reset($events); $event = reset($events);
// Checking that the event contains the expected values. // Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_forum\event\forum_viewed', $event); $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context()); $this->assertEquals($context, $event->get_context());
$expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid); $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
$this->assertEventLegacyLogData($expected, $event); $this->assertEventLegacyLogData($expected, $event);
......
...@@ -133,7 +133,9 @@ ...@@ -133,7 +133,9 @@
'context' => $context, 'context' => $context,
'objectid' => $forum->id 'objectid' => $forum->id
); );
$event = \mod_forum\event\forum_viewed::create($params); $event = \mod_forum\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('forum', $forum); $event->add_record_snapshot('forum', $forum);
$event->trigger(); $event->trigger();
......
...@@ -287,9 +287,10 @@ if ($pageid != LESSON_EOL) { ...@@ -287,9 +287,10 @@ if ($pageid != LESSON_EOL) {
// Trigger module viewed event. // Trigger module viewed event.
$event = \mod_lesson\event\course_module_viewed::create(array( $event = \mod_lesson\event\course_module_viewed::create(array(
'objectid' => $lesson->id, 'objectid' => $lesson->id,
'context' => $context, 'context' => $context
'courseid' => $course->id
)); ));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->trigger(); $event->trigger();
// This is where several messages (usually warnings) are displayed // This is where several messages (usually warnings) are displayed
......
...@@ -100,6 +100,8 @@ $params = array( ...@@ -100,6 +100,8 @@ $params = array(
'objectid' => $lti->id 'objectid' => $lti->id
); );
$event = \mod_lti\event\course_module_viewed::create($params); $event = \mod_lti\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('lti', $lti); $event->add_record_snapshot('lti', $lti);
$event->trigger(); $event->trigger();
......
...@@ -57,6 +57,8 @@ $params = array( ...@@ -57,6 +57,8 @@ $params = array(
'objectid' => $resource->id 'objectid' => $resource->id
); );
$event = \mod_resource\event\course_module_viewed::create($params); $event = \mod_resource\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('resource', $resource); $event->add_record_snapshot('resource', $resource);
$event->trigger(); $event->trigger();
......
...@@ -118,6 +118,7 @@ $event = \mod_scorm\event\course_module_viewed::create(array( ...@@ -118,6 +118,7 @@ $event = \mod_scorm\event\course_module_viewed::create(array(
'objectid' => $scorm->id, 'objectid' => $scorm->id,
'context' => $contextmodule, 'context' => $contextmodule,
)); ));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('scorm', $scorm); $event->add_record_snapshot('scorm', $scorm);
$event->add_record_snapshot('course_modules', $cm); $event->add_record_snapshot('course_modules', $cm);
$event->trigger(); $event->trigger();
......
...@@ -48,10 +48,11 @@ require_capability('mod/url:view', $context); ...@@ -48,10 +48,11 @@ require_capability('mod/url:view', $context);
$params = array( $params = array(
'context' => $context, 'context' => $context,
'objectid' => $url->id, 'objectid' => $url->id
'courseid' => $course->id
); );
$event = \mod_url\event\course_module_viewed::create($params); $event = \mod_url\event\course_module_viewed::create($params);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('url', $url); $event->add_record_snapshot('url', $url);
$event->trigger(); $event->trigger();
......
...@@ -296,7 +296,6 @@ if($pageid) { ...@@ -296,7 +296,6 @@ if($pageid) {
) )
); );
$event->add_record_snapshot('wiki_pages', $page); $event->add_record_snapshot('wiki_pages', $page);
$event->trigger();
} else if($id) { } else if($id) {
$event = \mod_wiki\event\course_module_viewed::create( $event = \mod_wiki\event\course_module_viewed::create(
array( array(
...@@ -304,8 +303,6 @@ if($pageid) { ...@@ -304,8 +303,6 @@ if($pageid) {
'objectid' => $wiki->id 'objectid' => $wiki->id
) )
); );
$event->add_record_snapshot('wiki', $wiki);
$event->trigger();
} else if($wid && $title) { } else if($wid && $title) {
$event = \mod_wiki\event\page_viewed::create( $event = \mod_wiki\event\page_viewed::create(
array( array(
...@@ -320,9 +317,11 @@ if($pageid) { ...@@ -320,9 +317,11 @@ if($pageid) {
) )
); );
$event->add_record_snapshot('wiki_pages', $page); $event->add_record_snapshot('wiki_pages', $page);
$event->add_record_snapshot('wiki', $wiki);
$event->trigger();
} }
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('wiki', $wiki);
$event->trigger();
$wikipage->print_header(); $wikipage->print_header();
$wikipage->print_content(); $wikipage->print_content();
......
...@@ -61,7 +61,6 @@ $completion->set_module_viewed($cm); ...@@ -61,7 +61,6 @@ $completion->set_module_viewed($cm);
$eventdata = array(); $eventdata = array();
$eventdata['objectid'] = $workshop->id; $eventdata['objectid'] = $workshop->id;
$eventdata['context'] = $workshop->context; $eventdata['context'] = $workshop->context;
$eventdata['courseid'] = $course->id;
$PAGE->set_url($workshop->view_url()); $PAGE->set_url($workshop->view_url());
$event = \mod_workshop\event\course_module_viewed::create($eventdata); $event = \mod_workshop\event\course_module_viewed::create($eventdata);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment