From e70ebc55f81af44fab8e5d35864be3cf1f324379 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Mon, 10 Feb 2014 15:03:30 +0800 Subject: [PATCH] MDL-40062 mod_forum: read tracking events * readtracking_enabled * readtracking_disabled --- .../classes/event/readtracking_disabled.php | 108 ++++++++++++++++++ .../classes/event/readtracking_enabled.php | 108 ++++++++++++++++++ mod/forum/lang/en/forum.php | 2 + mod/forum/markposts.php | 2 +- mod/forum/settracking.php | 13 ++- 5 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 mod/forum/classes/event/readtracking_disabled.php create mode 100644 mod/forum/classes/event/readtracking_enabled.php diff --git a/mod/forum/classes/event/readtracking_disabled.php b/mod/forum/classes/event/readtracking_disabled.php new file mode 100644 index 00000000000..f44f1f1e04e --- /dev/null +++ b/mod/forum/classes/event/readtracking_disabled.php @@ -0,0 +1,108 @@ +. + +/** + * The mod_forum subscription created event. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @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 + * @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.'); + } + } +} diff --git a/mod/forum/classes/event/readtracking_enabled.php b/mod/forum/classes/event/readtracking_enabled.php new file mode 100644 index 00000000000..1d1f3142cf6 --- /dev/null +++ b/mod/forum/classes/event/readtracking_enabled.php @@ -0,0 +1,108 @@ +. + +/** + * The mod_forum read tracking enabled event. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @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 + * @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.'); + } + } +} diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index f9d06c37e68..5b301132842 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -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'; diff --git a/mod/forum/markposts.php b/mod/forum/markposts.php index fd58338c7ce..b26278a98f4 100644 --- a/mod/forum/markposts.php +++ b/mod/forum/markposts.php @@ -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. diff --git a/mod/forum/settracking.php b/mod/forum/settracking.php index 9a39722194a..f8b85877c51 100644 --- a/mod/forum/settracking.php +++ b/mod/forum/settracking.php @@ -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"]); -- 2.43.0