MDL-40062 mod_forum: read tracking events

* readtracking_enabled
* readtracking_disabled
This commit is contained in:
Dan Poltawski 2014-02-10 15:03:30 +08:00
parent de2770501e
commit e70ebc55f8
5 changed files with 230 additions and 3 deletions

View File

@ -0,0 +1,108 @@
<?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/>.
/**
* The mod_forum subscription created event.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_forum subscription created event.
*
* @property-read array $other Extra information about the event.
* - int forumid: The id of the forum which readtracking has been disabled on.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class readtracking_disabled extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "Read tracking has been disabled for user {$this->relateduserid} in forum {$this->other['forumid']}";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreadtrackingdisabled', 'mod_forum');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'forum', 'stop tracking', 'view.php?f=' . $this->other['forumid'],
$this->other['forumid'], $this->contextinstanceid);
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->relateduserid)) {
throw new \coding_exception('relateduserid must be set.');
}
if (!isset($this->other['forumid'])) {
throw new \coding_exception('forumid must be set in other.');
}
if ($this->contextlevel != CONTEXT_MODULE) {
throw new \coding_exception('Context passed must be module context.');
}
}
}

View File

@ -0,0 +1,108 @@
<?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/>.
/**
* The mod_forum read tracking enabled event.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_forum read tracking enabled event.
*
* @property-read array $other Extra information about the event.
* - int forumid: The id of the forum which readtracking has been enabled on.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class readtracking_enabled extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "Read tracking has been enabled for user {$this->relateduserid} in forum {$this->other['forumid']}";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreadtrackingenabled', 'mod_forum');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
}
/**
* Return the legacy event log data.
*
* @return array|null
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'forum', 'start tracking', 'view.php?f=' . $this->other['forumid'],
$this->other['forumid'], $this->contextinstanceid);
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->relateduserid)) {
throw new \coding_exception('relateduserid must be set.');
}
if (!isset($this->other['forumid'])) {
throw new \coding_exception('forumid must be set in other.');
}
if ($this->contextlevel != CONTEXT_MODULE) {
throw new \coding_exception('Context passed must be module context.');
}
}
}

View File

@ -152,6 +152,8 @@ $string['eventdiscussionviewed'] = 'Discussion viewed';
$string['eventforumviewed'] = 'Forum viewed';
$string['eventuserreportviewed'] = 'User report viewed';
$string['eventsubscribersviewed'] = 'Subscribers viewed';
$string['eventreadtrackingdisabled'] = 'Read tracking disabled';
$string['eventreadtrackingenabled'] = 'Read tracking enabled';
$string['eventsubscriptioncreated'] = 'Subscription created';
$string['eventsubscriptiondeleted'] = 'Subscription deleted';
$string['emaildigestcompleteshort'] = 'Complete posts';

View File

@ -90,7 +90,7 @@ if ($mark == 'read') {
// may return 0
$currentgroup=false;
}
forum_tp_mark_forum_read($user, $forum->id,$currentgroup);
forum_tp_mark_forum_read($user, $forum->id, $currentgroup);
}
/// FUTURE - Add ability to mark them as unread.

View File

@ -58,9 +58,17 @@ if (!forum_tp_can_track_forums($forum)) {
$info = new stdClass();
$info->name = fullname($USER);
$info->forum = format_string($forum->name);
$eventparams = array(
'context' => context_module::instance($cm->id),
'relateduserid' => $USER->id,
'other' => array('forumid' => $forum->id),
);
if (forum_tp_is_tracked($forum) ) {
if (forum_tp_stop_tracking($forum->id)) {
add_to_log($course->id, "forum", "stop tracking", "view.php?f=$forum->id", $forum->id, $cm->id);
$event = \mod_forum\event\readtracking_disabled::create($eventparams);
$event->trigger();
redirect($returnto, get_string("nownottracking", "forum", $info), 1);
} else {
print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);
@ -68,7 +76,8 @@ if (forum_tp_is_tracked($forum) ) {
} else { // subscribe
if (forum_tp_start_tracking($forum->id)) {
add_to_log($course->id, "forum", "start tracking", "view.php?f=$forum->id", $forum->id, $cm->id);
$event = \mod_forum\event\readtracking_enabled::create($eventparams);
$event->trigger();
redirect($returnto, get_string("nowtracking", "forum", $info), 1);
} else {
print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);