Skip to content

Commit

Permalink
MDL-41185 events: Added events for different course view pages
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve authored and skodak committed Apr 21, 2014
1 parent 95f7bd8 commit 68a7235
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 9 deletions.
9 changes: 8 additions & 1 deletion course/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@
$mode = reset($modes);
}

add_to_log($course->id, "course", "user report", "user.php?id=$course->id&user=$user->id&mode=$mode", "$user->id");
$eventdata = array();
$eventdata['courseid'] = $id;
$eventdata['context'] = $coursecontext;
$eventdata['userid'] = $user->id;
$eventdata['other'] = array();
$eventdata['other']['mode'] = $mode;
$event = \core\event\course_user_report_viewed::create($eventdata);
$event->trigger();

$stractivityreport = get_string("activityreport");

Expand Down
19 changes: 12 additions & 7 deletions course/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@

require_once($CFG->dirroot.'/calendar/lib.php'); /// This is after login because it needs $USER

$logparam = 'id='. $course->id;
$loglabel = 'view';
$infoid = $course->id;
if ($section and $section > 0) {
$loglabel = 'view section';

// Get section details and check it exists.
$modinfo = get_fast_modinfo($course);
Expand All @@ -111,10 +107,7 @@
// correct error message shown.
require_capability('moodle/course:viewhiddensections', $context);
}
$infoid = $coursesections->id;
$logparam .= '&sectionid='. $infoid;
}
add_to_log($course->id, 'course', $loglabel, "view.php?". $logparam, $infoid);

// Fix course format if it is no longer installed
$course->format = course_get_format($course)->get_format();
Expand Down Expand Up @@ -288,6 +281,18 @@

echo html_writer::end_tag('div');

// Trigger course viewed event.
$eventparams = array();
$eventparams['context'] = $context;
$eventparams['courseid'] = $course->id;
$eventparams['userid'] = $USER->id;
if (!empty($section)) {
$eventparams['other'] = array();
$eventparams['other']['coursesectionid'] = $section;
}
$event = \core\event\course_viewed::create($eventparams);
$event->trigger();

// Include course AJAX
include_course_ajax($course, $modnamesused);

Expand Down
4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
}

if (isloggedin()) {
add_to_log(SITEID, 'course', 'view', 'view.php?id='.SITEID, SITEID);
$eventparams = array('courseid' => SITEID, 'context' => context_course::instance(SITEID));
$event = \core\event\course_viewed::create($eventparams);
$event->trigger();
}

/// If the hub plugin is installed then we let it take over the homepage here
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@
$string['eventcourseupdated'] = 'Course updated';
$string['eventcoursesectionupdated'] = ' Course section updated';
$string['eventcoursemoduleinstancelistviewed'] = 'Course module instance list viewed';
$string['eventcourseuserreportviewed'] = 'Course user report viewed';
$string['eventcourseviewed'] = 'Course viewed';
$string['eventemailfailed'] = 'Email failed to send';
$string['eventname'] = 'Event name';
$string['eventunknownlogged'] = 'Unknown event';
Expand Down
104 changes: 104 additions & 0 deletions lib/classes/event/course_user_report_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Course user report viewed event.
*
* @package core
* @copyright 2014 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* Course user report viewed event class.
*
* Class for event to be triggered when a course user report is viewed.
* @property-read array $other Extra information about the event.
* -string mode: Mode is used to show the user different data.
*
* @package core
* @since Moodle 2.7
* @copyright 2014 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_user_report_viewed extends \core\event\base {

/**
* Init method.
*
* Please override this in extending class and specify objecttable.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "A user with the id '$this->userid' viewed the user report in the course '$this->courseid'";
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventcourseuserreportviewed', 'core');
}

/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url("/course/user.php", array('id' => $this->courseid, 'user' => $this->userid,
'mode' => $this->other['mode']));
}

/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'course', 'user report', 'user.php?id=' . $this->courseid . '&amp;user='
. $this->userid . '&amp;mode=' . $this->other['mode'], $this->userid);
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
// Make sure this class is never used without proper object details.
if (!isset($this->other['mode'])) {
throw new \coding_exception('mode needs to be set in $other.');
}
}
}
106 changes: 106 additions & 0 deletions lib/classes/event/course_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Course viewed event.
*
* @package core
* @copyright 2014 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* Course viewed event class.
*
* Class for event to be triggered when a course is viewed.
* @property-read array $other Extra information about the event.
* -int coursesectionid: The course section ID (Optional!).
*
* @package core
* @since Moodle 2.7
* @copyright 2014 Adrian Greeve <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_viewed extends \core\event\base {

/**
* Init method.
*
* Please override this in extending class and specify objecttable.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "A user with the id '$this->userid' viewed the course '$this->courseid'";
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventcourseviewed', 'core');
}

/**
* Get URL related to the action.
*
* @return \moodle_url
*/
public function get_url() {
return \course_get_url($this->courseid, $this->other['coursesectionid']);
}

/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
if (!empty($this->other['coursesectionid'])) {
return array($this->courseid, 'course', 'view section', 'view.php?id=' . $this->courseid . '&amp;sectionid='
. $this->other['coursesectionid'], $this->other['coursesectionid']);
} else {
return array($this->courseid, 'course', 'view', 'view.php?id=' . $this->courseid, $this->courseid);
}
}

/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context passed must be course context.');
}
}
}
83 changes: 83 additions & 0 deletions lib/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,87 @@ public function test_email_failed() {
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}

/**
* There is no api involved so the best we can do is test legacy data by triggering event manually.
*/
public function test_course_user_report_viewed() {

$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);

$eventparams = array();
$eventparams['courseid'] = $course->id;
$eventparams['context'] = $context;
$eventparams['userid'] = $user->id;
$eventparams['other'] = array();
$eventparams['other']['mode'] = 'grade';
$event = \core\event\course_user_report_viewed::create($eventparams);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);

$this->assertInstanceOf('\core\event\course_user_report_viewed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'course', 'user report', 'user.php?id=' . $course->id . '&amp;user='
. $user->id . '&amp;mode=grade', $user->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}

/**
* There is no api involved so the best we can do is test legacy data by triggering event manually.
*/
public function test_course_viewed() {

$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$context = context_course::instance($course->id);

// First try with no optional parameters.
$eventparams = array();
$eventparams['courseid'] = $course->id;
$eventparams['context'] = $context;
$eventparams['userid'] = $user->id;
$event = \core\event\course_viewed::create($eventparams);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$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', 'view.php?id=' . $course->id, $course->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);

// Now try with optional parameters.
$eventparams = array();
$eventparams['courseid'] = $course->id;
$eventparams['context'] = $context;
$eventparams['userid'] = $user->id;
$eventparams['other'] = array();
$sectionid = 34;
$eventparams['other']['coursesectionid'] = $sectionid;
$event = \core\event\course_viewed::create($eventparams);

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$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 . '&amp;sectionid='
. $sectionid, $sectionid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}

0 comments on commit 68a7235

Please sign in to comment.