Skip to content

Commit

Permalink
MDL-44321 events: cleaning up course_module_viewed event
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Feb 28, 2014
1 parent 76e4de3 commit fbc4b77
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 102 deletions.
4 changes: 4 additions & 0 deletions lib/classes/event/course_module_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ protected function validate_data() {
if (empty($this->objectid) || empty($this->objecttable)) {
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.');
}
}

}
2 changes: 2 additions & 0 deletions mod/book/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
'objectid' => $book->id
);
$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->trigger();

Expand Down
3 changes: 2 additions & 1 deletion mod/choice/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@
$eventdata = array();
$eventdata['objectid'] = $choice->id;
$eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;

$event = \mod_choice\event\course_module_viewed::create($eventdata);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->trigger();

/// Check to see if groups are being used in this choice
Expand Down
2 changes: 2 additions & 0 deletions mod/data/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
'objectid' => $data->id
);
$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->trigger();

Expand Down
3 changes: 0 additions & 3 deletions mod/feedback/classes/event/course_module_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ protected function validate_data() {
if (!isset($this->other['anonymous'])) {
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.
parent::validate_data();
Expand Down
34 changes: 2 additions & 32 deletions mod/feedback/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,39 +176,11 @@ public function test_response_deleted_event_exceptions() {

$context = context_module::instance($this->eventcm->id);

// Test not setting instanceid.
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.
// Test not setting other['anonymous'].
try {
\mod_feedback\event\response_submitted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'instanceid' => $this->eventfeedback->id)
'objectid' => $this->eventfeedbackcompleted->id
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['anonymous']");
Expand Down Expand Up @@ -252,8 +224,6 @@ public function test_response_submitted_event() {
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($USER->id, $event->relateduserid);
$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->setUser($this->eventuser);
$this->assertFalse($event->can_view());
Expand Down
6 changes: 2 additions & 4 deletions mod/feedback/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@
'objectid' => $feedback->id,
'context' => $context,
'other' => array(
'cmid' => $cm->id,
'instanceid' => $feedback->id,
'anonymous' => $feedback->anonymous
)
));
)
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('feedback', $feedback);
Expand Down
2 changes: 2 additions & 0 deletions mod/folder/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
'objectid' => $folder->id
);
$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->trigger();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @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.
Expand All @@ -46,31 +46,13 @@ protected function init() {
$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
*
* @return \moodle_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));
}

/**
Expand All @@ -83,24 +65,5 @@ protected function get_legacy_logdata() {
$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.');
}

}

}

1 change: 0 additions & 1 deletion mod/forum/lang/en/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
$string['eventdiscussiondeleted'] = 'Discussion deleted';
$string['eventdiscussionmoved'] = 'Discussion moved';
$string['eventdiscussionviewed'] = 'Discussion viewed';
$string['eventforumviewed'] = 'Forum viewed';
$string['eventuserreportviewed'] = 'User report viewed';
$string['eventpostcreated'] = 'Post created';
$string['eventpostdeleted'] = 'Post deleted';
Expand Down
22 changes: 11 additions & 11 deletions mod/forum/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,9 @@ public function test_discussion_viewed() {
}

/**
* 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();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$context = context_module::instance($forum->cmid);
Expand All @@ -606,14 +606,14 @@ public function test_forum_viewed_objectid_validation() {
'context' => $context,
);

$this->setExpectedException('coding_exception', 'objectid must be set to the forumid.');
\mod_forum\event\forum_viewed::create($params);
$this->setExpectedException('coding_exception', 'must define objectid');
\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();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));

Expand All @@ -623,13 +623,13 @@ public function test_forum_viewed_context_validation() {
);

$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.
$course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
Expand All @@ -641,7 +641,7 @@ public function test_forum_viewed() {
'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.
$sink = $this->redirectEvents();
Expand All @@ -651,7 +651,7 @@ public function test_forum_viewed() {
$event = reset($events);

// 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());
$expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
$this->assertEventLegacyLogData($expected, $event);
Expand Down
4 changes: 3 additions & 1 deletion mod/forum/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@
'context' => $context,
'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->trigger();

Expand Down
5 changes: 3 additions & 2 deletions mod/lesson/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,10 @@
// Trigger module viewed event.
$event = \mod_lesson\event\course_module_viewed::create(array(
'objectid' => $lesson->id,
'context' => $context,
'courseid' => $course->id
'context' => $context
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->trigger();

// This is where several messages (usually warnings) are displayed
Expand Down
2 changes: 2 additions & 0 deletions mod/lti/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
'objectid' => $lti->id
);
$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->trigger();

Expand Down
2 changes: 2 additions & 0 deletions mod/resource/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
'objectid' => $resource->id
);
$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->trigger();

Expand Down
1 change: 1 addition & 0 deletions mod/scorm/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'objectid' => $scorm->id,
'context' => $contextmodule,
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('scorm', $scorm);
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
Expand Down
5 changes: 3 additions & 2 deletions mod/url/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@

$params = array(
'context' => $context,
'objectid' => $url->id,
'courseid' => $course->id
'objectid' => $url->id
);
$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->trigger();

Expand Down
9 changes: 4 additions & 5 deletions mod/wiki/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,13 @@
)
);
$event->add_record_snapshot('wiki_pages', $page);
$event->trigger();
} else if($id) {
$event = \mod_wiki\event\course_module_viewed::create(
array(
'context' => $context,
'objectid' => $wiki->id
)
);
$event->add_record_snapshot('wiki', $wiki);
$event->trigger();
} else if($wid && $title) {
$event = \mod_wiki\event\page_viewed::create(
array(
Expand All @@ -320,9 +317,11 @@
)
);
$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_content();
Expand Down
1 change: 0 additions & 1 deletion mod/workshop/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
$eventdata = array();
$eventdata['objectid'] = $workshop->id;
$eventdata['context'] = $workshop->context;
$eventdata['courseid'] = $course->id;

$PAGE->set_url($workshop->view_url());
$event = \mod_workshop\event\course_module_viewed::create($eventdata);
Expand Down

0 comments on commit fbc4b77

Please sign in to comment.