From de2770501e73364018575ffe369b865ceb4ddb0d Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Fri, 7 Feb 2014 15:38:51 +0800 Subject: [PATCH] MDL-40062 mod_forum: subscription events Events for forum mail subscriptions * subscription_created * subscription_deleted * subscribers_viewed --- .../classes/event/subscribers_viewed.php | 107 +++++++++++++++++ .../classes/event/subscription_created.php | 106 +++++++++++++++++ .../classes/event/subscription_deleted.php | 108 ++++++++++++++++++ mod/forum/lang/en/forum.php | 3 + mod/forum/lib.php | 31 ++++- mod/forum/subscribe.php | 2 - mod/forum/subscribers.php | 7 +- 7 files changed, 358 insertions(+), 6 deletions(-) create mode 100644 mod/forum/classes/event/subscribers_viewed.php create mode 100644 mod/forum/classes/event/subscription_created.php create mode 100644 mod/forum/classes/event/subscription_deleted.php diff --git a/mod/forum/classes/event/subscribers_viewed.php b/mod/forum/classes/event/subscribers_viewed.php new file mode 100644 index 00000000000..c3e8a3aefe9 --- /dev/null +++ b/mod/forum/classes/event/subscribers_viewed.php @@ -0,0 +1,107 @@ +. + +/** + * The mod_forum subscribers list viewed 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 subscribers list viewed event. + * + * @property-read array $other Extra information about the event. + * - int forumid: The id of the forum which the subscriberslist has been viewed. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class subscribers_viewed extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['edulevel'] = self::LEVEL_OTHER; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return "The user {$this->userid} has viewed the subscribers list for forum {$this->other['forumid']}"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventsubscribersviewed', 'mod_forum'); + } + + /** + * Get URL related to the action + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/forum/subscribers.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', 'view subscribers', 'subscribers.php?id=' . $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->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/subscription_created.php b/mod/forum/classes/event/subscription_created.php new file mode 100644 index 00000000000..94c1c6100a9 --- /dev/null +++ b/mod/forum/classes/event/subscription_created.php @@ -0,0 +1,106 @@ +. + +/** + * 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 has been subscribed to. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class subscription_created 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 "The user {$this->relateduserid} was subscribed to the forum {$this->other['forumid']}"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventsubscriptioncreated', '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', 'subscribe', '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/subscription_deleted.php b/mod/forum/classes/event/subscription_deleted.php new file mode 100644 index 00000000000..73832cedb24 --- /dev/null +++ b/mod/forum/classes/event/subscription_deleted.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 has been unsusbcribed from. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class subscription_deleted 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 "The user {$this->relateduserid} was unsubscribed the forum {$this->other['forumid']}"; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventsubscriptiondeleted', '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', 'unsubscribe', '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 3ecb8699af6..f9d06c37e68 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -151,6 +151,9 @@ $string['eventdiscussionmoved'] = 'Discussion moved'; $string['eventdiscussionviewed'] = 'Discussion viewed'; $string['eventforumviewed'] = 'Forum viewed'; $string['eventuserreportviewed'] = 'User report viewed'; +$string['eventsubscribersviewed'] = 'Subscribers viewed'; +$string['eventsubscriptioncreated'] = 'Subscription created'; +$string['eventsubscriptiondeleted'] = 'Subscription deleted'; $string['emaildigestcompleteshort'] = 'Complete posts'; $string['emaildigestdefault'] = 'Default ({$a})'; $string['emaildigestoffshort'] = 'No digest'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c55b1eddddf..a66bcf2a051 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -4815,7 +4815,19 @@ function forum_subscribe($userid, $forumid) { $sub->userid = $userid; $sub->forum = $forumid; - return $DB->insert_record("forum_subscriptions", $sub); + $result = $DB->insert_record("forum_subscriptions", $sub); + + $cm = get_coursemodule_from_instance('forum', $forumid); + $params = array( + 'context' => context_module::instance($cm->id), + 'relateduserid' => $userid, + 'other' => array('forumid' => $forumid), + + ); + $event = \mod_forum\event\subscription_created::create($params); + $event->trigger(); + + return $result; } /** @@ -4827,8 +4839,21 @@ function forum_subscribe($userid, $forumid) { */ function forum_unsubscribe($userid, $forumid) { global $DB; - return ($DB->delete_records('forum_digests', array('userid' => $userid, 'forum' => $forumid)) - && $DB->delete_records('forum_subscriptions', array('userid' => $userid, 'forum' => $forumid))); + + $DB->delete_records('forum_digests', array('userid' => $userid, 'forum' => $forumid)); + $DB->delete_records('forum_subscriptions', array('userid' => $userid, 'forum' => $forumid)); + + $cm = get_coursemodule_from_instance('forum', $forumid); + $params = array( + 'context' => context_module::instance($cm->id), + 'relateduserid' => $userid, + 'other' => array('forumid' => $forumid), + + ); + $event = \mod_forum\event\subscription_deleted::create($params); + $event->trigger(); + + return true; } /** diff --git a/mod/forum/subscribe.php b/mod/forum/subscribe.php index 1bd52774f61..37aea54d4a7 100644 --- a/mod/forum/subscribe.php +++ b/mod/forum/subscribe.php @@ -149,7 +149,6 @@ if (forum_is_subscribed($user->id, $forum->id)) { } require_sesskey(); if (forum_unsubscribe($user->id, $forum->id)) { - add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id); redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1); } else { print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]); @@ -174,6 +173,5 @@ if (forum_is_subscribed($user->id, $forum->id)) { } require_sesskey(); forum_subscribe($user->id, $forum->id); - add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id); redirect($returnto, get_string("nowsubscribed", "forum", $info), 1); } diff --git a/mod/forum/subscribers.php b/mod/forum/subscribers.php index 87c3b5429ef..eda984f8a17 100644 --- a/mod/forum/subscribers.php +++ b/mod/forum/subscribers.php @@ -54,7 +54,12 @@ if (!has_capability('mod/forum:viewsubscribers', $context)) { unset($SESSION->fromdiscussion); -add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id); +$params = array( + 'context' => $context, + 'other' => array('forumid' => $forum->id), +); +$event = \mod_forum\event\subscribers_viewed::create($params); +$event->trigger(); $forumoutput = $PAGE->get_renderer('mod_forum'); $currentgroup = groups_get_activity_group($cm);