Skip to content

Commit

Permalink
Merge branch 'w17_MDL-41185_m27_courselog' of git://github.com/skodak…
Browse files Browse the repository at this point in the history
…/moodle
  • Loading branch information
marinaglancy committed Apr 22, 2014
2 parents 23a9a56 + 0cd189b commit 9ff40d8
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 11 deletions.
8 changes: 7 additions & 1 deletion course/user.php
Expand Up @@ -105,7 +105,13 @@
$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");

Expand Down
15 changes: 8 additions & 7 deletions course/view.php
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,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);

Expand Down
6 changes: 3 additions & 3 deletions index.php
Expand Up @@ -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')) {
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
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
112 changes: 112 additions & 0 deletions lib/classes/event/course_user_report_viewed.php
@@ -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.');
}
}
}
118 changes: 118 additions & 0 deletions lib/classes/event/course_viewed.php
@@ -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.');
}
}
}
5 changes: 5 additions & 0 deletions lib/moodlelib.php
Expand Up @@ -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,
Expand Down

0 comments on commit 9ff40d8

Please sign in to comment.