Merge branch 'w17_MDL-41185_m27_courselog' of git://github.com/skodak/moodle

This commit is contained in:
Marina Glancy 2014-04-22 12:26:24 +08:00
commit 9ff40d83b5
8 changed files with 339 additions and 11 deletions

View file

@ -105,7 +105,13 @@ if (!in_array($mode, $modes)) {
$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(
'context' => $coursecontext,
'relateduserid' => $user->id,
'other' => array('mode' => $mode),
);
$event = \core\event\course_user_report_viewed::create($eventdata);
$event->trigger();
$stractivityreport = get_string("activityreport");

View file

@ -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);
@ -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();
@ -288,6 +281,14 @@
echo html_writer::end_tag('div');
// Trigger course viewed event.
$eventdata = array('context' => $context);
if (!empty($section)) {
$eventdata['other'] = array('coursesectionid' => $section);
}
$event = \core\event\course_viewed::create($eventdata);
$event->trigger();
// Include course AJAX
include_course_ajax($course, $modnamesused);

View file

@ -75,9 +75,9 @@
}
}
if (isloggedin()) {
add_to_log(SITEID, 'course', 'view', 'view.php?id='.SITEID, SITEID);
}
$eventparams = array('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
if (file_exists($CFG->dirroot.'/local/hub/lib.php') and get_config('local_hub', 'hubenabled')) {

View file

@ -737,6 +737,8 @@ $string['eventcourserestored'] = 'Course restored';
$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';

View file

@ -0,0 +1,112 @@
<?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 <adrian@moodle.com>
* @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 <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_user_report_viewed extends \core\event\base {
/**
* Init method.
*
* @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' for user '$this->relateduserid'";
}
/**
* 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->relateduserid,
'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->relateduserid . '&amp;mode=' . $this->other['mode'], $this->relateduserid);
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context passed must be course context.');
}
if (empty($this->relateduserid)) {
throw new \coding_exception('relateduserid needs to be set.');
}
// 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.');
}
}
}

View file

@ -0,0 +1,118 @@
<?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 <adrian@moodle.com>
* @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 <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_viewed extends \core\event\base {
/**
* Init method.
*
* @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|null
*/
public function get_url() {
$sectionid = null;
if (isset($this->other['coursesectionid'])) {
$sectionid = $this->other['coursesectionid'];
}
try {
return \course_get_url($this->courseid, $sectionid);
} catch (\Exception $e) {
return null;
}
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
if ($this->courseid == SITEID and !isloggedin()) {
// We did not log frontpage access in older Moodle versions.
return null;
}
if (isset($this->other['coursesectionid'])) {
return array($this->courseid, 'course', 'view section', 'view.php?id=' . $this->courseid . '&amp;sectionid='
. $this->other['coursesectionid'], $this->other['coursesectionid']);
}
return array($this->courseid, 'course', 'view', 'view.php?id=' . $this->courseid, $this->courseid);
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if ($this->contextlevel != CONTEXT_COURSE) {
throw new \coding_exception('Context passed must be course context.');
}
}
}

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,

View file

@ -178,4 +178,88 @@ class core_events_testcase extends advanced_testcase {
$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['context'] = $context;
$eventparams['relateduserid'] = $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['context'] = $context;
$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.
$sectionid = 34;
$eventparams = array();
$eventparams['context'] = $context;
$eventparams['other'] = array('coursesectionid' => $sectionid);
$event = \core\event\course_viewed::create($eventparams);
// 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 . '&amp;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());
}
}