MDL-40062 mod_forum: subscription events

Events for forum mail subscriptions

* subscription_created
* subscription_deleted
* subscribers_viewed
This commit is contained in:
Dan Poltawski 2014-02-07 15:38:51 +08:00
parent 71595d0053
commit de2770501e
7 changed files with 358 additions and 6 deletions

View File

@ -0,0 +1,107 @@
<?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 subscribers list viewed 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 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 <dan@moodle.com>
* @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.');
}
}
}

View File

@ -0,0 +1,106 @@
<?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 has been subscribed to.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @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.');
}
}
}

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 has been unsusbcribed from.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @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.');
}
}
}

View File

@ -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';

View File

@ -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;
}
/**

View File

@ -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);
}

View File

@ -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);