mirror of
https://github.com/moodle/moodle.git
synced 2025-03-22 00:20:37 +01:00
MDL-1626 mod_forum: Add functions to change user discussion subscription state
This commit is contained in:
parent
59075a4349
commit
e3bbfb52c4
112
mod/forum/classes/event/discussion_subscription_created.php
Normal file
112
mod/forum/classes/event/discussion_subscription_created.php
Normal file
@ -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/>.
|
||||
|
||||
/**
|
||||
* The mod_forum discussion_subscription created event.
|
||||
*
|
||||
* @package mod_forum
|
||||
* @copyright 2014 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace mod_forum\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The mod_forum discussion_subscription created event class.
|
||||
*
|
||||
* @property-read array $other {
|
||||
* Extra information about the event.
|
||||
*
|
||||
* - int forumid: The id of the forum which the discussion is in.
|
||||
* - int discussion: The id of the discussion which has been subscribed to.
|
||||
* }
|
||||
*
|
||||
* @package mod_forum
|
||||
* @since Moodle 2.8
|
||||
* @copyright 2014 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class discussion_subscription_created extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['crud'] = 'c';
|
||||
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
|
||||
$this->data['objecttable'] = 'forum_discussion_subs';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with id '$this->userid' subscribed the user with id '$this->relateduserid' to the discussion " .
|
||||
" with id '{$this->other['discussion']}' in the forum with the course module id '$this->contextinstanceid'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_name() {
|
||||
return get_string('eventdiscussionsubscriptioncreated', 'mod_forum');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url('/mod/forum/subscribe.php', array(
|
||||
'id' => $this->other['forumid'],
|
||||
'd' => $this->other['discussion'],
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @return void
|
||||
*/
|
||||
protected function validate_data() {
|
||||
parent::validate_data();
|
||||
|
||||
if (!isset($this->relateduserid)) {
|
||||
throw new \coding_exception('The \'relateduserid\' must be set.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['forumid'])) {
|
||||
throw new \coding_exception('The \'forumid\' value must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['discussion'])) {
|
||||
throw new \coding_exception('The \'discussion\' value must be set in other.');
|
||||
}
|
||||
|
||||
if ($this->contextlevel != CONTEXT_MODULE) {
|
||||
throw new \coding_exception('Context level must be CONTEXT_MODULE.');
|
||||
}
|
||||
}
|
||||
}
|
112
mod/forum/classes/event/discussion_subscription_deleted.php
Normal file
112
mod/forum/classes/event/discussion_subscription_deleted.php
Normal file
@ -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/>.
|
||||
|
||||
/**
|
||||
* The mod_forum discussion_subscription deleted event.
|
||||
*
|
||||
* @package mod_forum
|
||||
* @copyright 2014 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace mod_forum\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The mod_forum discussion_subscription deleted event class.
|
||||
*
|
||||
* @property-read array $other {
|
||||
* Extra information about the event.
|
||||
*
|
||||
* - int forumid: The id of the forum which the discussion is in.
|
||||
* - int discussion: The id of the discussion which has been unsubscribed from.
|
||||
* }
|
||||
*
|
||||
* @package mod_forum
|
||||
* @since Moodle 2.8
|
||||
* @copyright 2014 Andrew Nicols <andrew@nicols.co.uk>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class discussion_subscription_deleted extends \core\event\base {
|
||||
/**
|
||||
* Init method.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function init() {
|
||||
$this->data['crud'] = 'd';
|
||||
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
|
||||
$this->data['objecttable'] = 'forum_discussion_subs';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of what happened.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with id '$this->userid' unsubscribed the user with id '$this->relateduserid' from the discussion " .
|
||||
" with id '{$this->other['discussion']}' in the forum with the course module id '$this->contextinstanceid'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return localised event name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_name() {
|
||||
return get_string('eventdiscussionsubscriptiondeleted', 'mod_forum');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL related to the action.
|
||||
*
|
||||
* @return \moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new \moodle_url('/mod/forum/subscribe.php', array(
|
||||
'id' => $this->other['forumid'],
|
||||
'd' => $this->other['discussion'],
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom validation.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
* @return void
|
||||
*/
|
||||
protected function validate_data() {
|
||||
parent::validate_data();
|
||||
|
||||
if (!isset($this->relateduserid)) {
|
||||
throw new \coding_exception('The \'relateduserid\' must be set.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['forumid'])) {
|
||||
throw new \coding_exception('The \'forumid\' value must be set in other.');
|
||||
}
|
||||
|
||||
if (!isset($this->other['discussion'])) {
|
||||
throw new \coding_exception('The \'discussion\' value must be set in other.');
|
||||
}
|
||||
|
||||
if ($this->contextlevel != CONTEXT_MODULE) {
|
||||
throw new \coding_exception('Context level must be CONTEXT_MODULE.');
|
||||
}
|
||||
}
|
||||
}
|
@ -33,6 +33,21 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class subscriptions {
|
||||
|
||||
/**
|
||||
* The status value for a subscribed discussion.
|
||||
*
|
||||
* @const int
|
||||
*/
|
||||
const FORUM_DISCUSSION_SUBSCRIBED = 1;
|
||||
|
||||
/**
|
||||
* The status value for an unsubscribed discussion.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const FORUM_DISCUSSION_UNSUBSCRIBED = -1;
|
||||
|
||||
/**
|
||||
* The subscription cache for forums.
|
||||
*
|
||||
@ -55,13 +70,43 @@ class subscriptions {
|
||||
protected static $fetchedforums = array();
|
||||
|
||||
/**
|
||||
* Whether a user is subscribed to this forum.
|
||||
* The subscription cache for forum discussions.
|
||||
*
|
||||
* The first level key is the user ID
|
||||
* The second level is the forum ID
|
||||
* The third level key is the discussion ID
|
||||
* The value is then the users preference (int)
|
||||
*
|
||||
* @var array[]
|
||||
*/
|
||||
protected static $forumdiscussioncache = array();
|
||||
|
||||
/**
|
||||
* The list of forums which have been wholly retrieved for the forum discussion subscription cache.
|
||||
*
|
||||
* This allows for prior caching of an entire forum to reduce the
|
||||
* number of DB queries in a subscription check loop.
|
||||
*
|
||||
* @var bool[]
|
||||
*/
|
||||
protected static $discussionfetchedforums = array();
|
||||
|
||||
/**
|
||||
* Whether a user is subscribed to this forum, or a discussion within
|
||||
* the forum.
|
||||
*
|
||||
* If a discussion is specified, then report whether the user is
|
||||
* subscribed to posts to this particular discussion, taking into
|
||||
* account the forum preference.
|
||||
*
|
||||
* If it is not specified then only the forum preference is considered.
|
||||
*
|
||||
* @param int $userid The user ID
|
||||
* @param \stdClass $forum The record of the forum to test
|
||||
* @param int $discussionid The ID of the discussion to check
|
||||
* @return boolean
|
||||
*/
|
||||
private static function is_subscribed($userid, $forum) {
|
||||
public static function is_subscribed($userid, $forum, $discussionid = null) {
|
||||
// If forum is force subscribed and has allowforcesubscribe, then user is subscribed.
|
||||
$cm = get_coursemodule_from_instance('forum', $forum->id);
|
||||
if ($cm && self::is_forcesubscribed($forum) &&
|
||||
@ -69,6 +114,28 @@ class subscriptions {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($discussionid === null) {
|
||||
return self::is_subscribed_to_forum($userid, $forum);
|
||||
}
|
||||
|
||||
$subscriptions = self::fetch_discussion_subscription($forum->id, $userid);
|
||||
|
||||
// Check whether there is a record for this discussion subscription.
|
||||
if (isset($subscriptions[$discussionid])) {
|
||||
return ($subscriptions[$discussionid] == self::FORUM_DISCUSSION_SUBSCRIBED);
|
||||
}
|
||||
|
||||
return self::is_subscribed_to_forum($userid, $forum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a user is subscribed to this forum.
|
||||
*
|
||||
* @param int $userid The user ID
|
||||
* @param \stdClass $forum The record of the forum to test
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function is_subscribed_to_forum($userid, $forum) {
|
||||
return self::fetch_subscription_cache($forum->id, $userid);
|
||||
}
|
||||
|
||||
@ -148,14 +215,18 @@ class subscriptions {
|
||||
}
|
||||
list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED, 'c');
|
||||
|
||||
// get all forums from the user's courses that they are subscribed to and which are not set to forced
|
||||
$sql = "SELECT f.id, cm.id as cm, cm.visible
|
||||
// Get all forums from the user's courses that they are subscribed to and which are not set to forced.
|
||||
// It is possible for users to be subscribed to a forum in subscription disallowed mode so they must be listed
|
||||
// here so that that can be unsubscribed from.
|
||||
$sql = "SELECT f.id, cm.id as cm, cm.visible, f.course
|
||||
FROM {forum} f
|
||||
JOIN {course_modules} cm ON cm.instance = f.id
|
||||
JOIN {modules} m ON m.name = :modulename AND m.id = cm.module
|
||||
LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
|
||||
WHERE f.forcesubscribe <> :forcesubscribe AND fs.id IS NOT NULL
|
||||
AND cm.course $coursesql";
|
||||
JOIN {course_modules} cm ON cm.instance = f.id
|
||||
JOIN {modules} m ON m.name = :modulename AND m.id = cm.module
|
||||
LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
|
||||
WHERE f.forcesubscribe <> :forcesubscribe
|
||||
AND fs.id IS NOT NULL
|
||||
AND cm.course
|
||||
$coursesql";
|
||||
$params = array_merge($courseparams, array(
|
||||
'modulename'=>'forum',
|
||||
'userid' => $USER->id,
|
||||
@ -163,11 +234,11 @@ class subscriptions {
|
||||
));
|
||||
$forums = $DB->get_recordset_sql($sql, $params);
|
||||
|
||||
$unsubscribableforums = array(); // Array to return
|
||||
$unsubscribableforums = array();
|
||||
foreach($forums as $forum) {
|
||||
if (empty($forum->visible)) {
|
||||
// The forum is hidden - check if the user can view the forum.
|
||||
$context = context_module::instance($forum->cm);
|
||||
$context = \context_module::instance($forum->cm);
|
||||
if (!has_capability('moodle/course:viewhiddenactivities', $context)) {
|
||||
// The user can't see the hidden forum to cannot unsubscribe.
|
||||
continue;
|
||||
@ -315,9 +386,11 @@ class subscriptions {
|
||||
* @param int $groupid The group id if restricting subscriptions to a group of users, or 0 for all.
|
||||
* @param context_module $context the forum context, to save re-fetching it where possible.
|
||||
* @param string $fields requested user fields (with "u." table prefix).
|
||||
* @param boolean $includediscussionsubscriptions Whether to take discussion subscriptions and unsubscriptions into consideration.
|
||||
* @return array list of users.
|
||||
*/
|
||||
public static function fetch_subscribed_users($forum, $groupid = 0, $context = null, $fields = null) {
|
||||
public static function fetch_subscribed_users($forum, $groupid = 0, $context = null, $fields = null,
|
||||
$includediscussionsubscriptions = false) {
|
||||
global $CFG, $DB;
|
||||
|
||||
if (empty($fields)) {
|
||||
@ -354,13 +427,33 @@ class subscriptions {
|
||||
list($esql, $params) = get_enrolled_sql($context, '', $groupid, true);
|
||||
$params['forumid'] = $forum->id;
|
||||
|
||||
$sql = "SELECT $fields
|
||||
FROM {user} u
|
||||
JOIN ($esql) je ON je.id = u.id
|
||||
JOIN {forum_subscriptions} s ON s.userid = u.id
|
||||
WHERE
|
||||
s.forum = :forumid
|
||||
ORDER BY u.email ASC";
|
||||
if ($includediscussionsubscriptions) {
|
||||
$params['dforumid'] = $forum->id;
|
||||
$params['unsubscribed'] = self::FORUM_DISCUSSION_UNSUBSCRIBED;
|
||||
|
||||
$sql = "SELECT $fields
|
||||
FROM {user} u
|
||||
WHERE u.id IN (
|
||||
SELECT u2.id FROM
|
||||
{user} u2
|
||||
JOIN ($esql) je ON je.id = u.id
|
||||
LEFT JOIN {forum_subscriptions} s ON s.userid = u2.id
|
||||
LEFT JOIN {forum_discussion_subs} ds ON ds.userid = u2.id
|
||||
WHERE
|
||||
s.forum = :forumid
|
||||
OR
|
||||
(ds.forum = :dforumid AND ds.preference <> :unsubscribed)
|
||||
)
|
||||
ORDER BY u.email ASC";
|
||||
} else {
|
||||
$sql = "SELECT $fields
|
||||
FROM {user} u
|
||||
JOIN ($esql) je ON je.id = u.id
|
||||
JOIN {forum_subscriptions} s ON s.userid = u.id
|
||||
WHERE
|
||||
s.forum = :forumid
|
||||
ORDER BY u.email ASC";
|
||||
}
|
||||
$results = $DB->get_records_sql($sql, $params);
|
||||
}
|
||||
|
||||
@ -370,6 +463,100 @@ class subscriptions {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the discussion subscription data for the specified userid and forum.
|
||||
*
|
||||
* This is returned as an array of discussions for that forum which contain the preference in a stdClass.
|
||||
*
|
||||
* @param int $forumid The forum to retrieve a cache for
|
||||
* @param int $userid The user ID
|
||||
* @return array of stdClass objects with one per discussion in the forum.
|
||||
*/
|
||||
public static function fetch_discussion_subscription($forumid, $userid = null) {
|
||||
self::fill_discussion_subscription_cache($forumid, $userid);
|
||||
|
||||
if (!isset(self::$forumdiscussioncache[$userid]) || !isset(self::$forumdiscussioncache[$userid][$forumid])) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return self::$forumdiscussioncache[$userid][$forumid];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the discussion subscription data for the specified userid and forum.
|
||||
*
|
||||
* If the userid is not specified, then all discussion subscription data for that forum is fetched in a single query
|
||||
* and used for subsequent lookups without requiring further database queries.
|
||||
*
|
||||
* @param int $forumid The forum to retrieve a cache for
|
||||
* @param int $userid The user ID
|
||||
* @return void
|
||||
*/
|
||||
public static function fill_discussion_subscription_cache($forumid, $userid = null) {
|
||||
global $DB;
|
||||
|
||||
if (!isset(self::$discussionfetchedforums[$forumid])) {
|
||||
// This forum hasn't been fetched as a whole yet.
|
||||
if (isset($userid)) {
|
||||
if (!isset(self::$forumdiscussioncache[$userid])) {
|
||||
self::$forumdiscussioncache[$userid] = array();
|
||||
}
|
||||
|
||||
if (!isset(self::$forumdiscussioncache[$userid][$forumid])) {
|
||||
$subscriptions = $DB->get_recordset('forum_discussion_subs', array(
|
||||
'userid' => $userid,
|
||||
'forum' => $forumid,
|
||||
), null, 'id, discussion, preference');
|
||||
foreach ($subscriptions as $id => $data) {
|
||||
self::add_to_discussion_cache($forumid, $userid, $data->discussion, $data->preference);
|
||||
}
|
||||
$subscriptions->close();
|
||||
}
|
||||
} else {
|
||||
$subscriptions = $DB->get_recordset('forum_discussion_subs', array(
|
||||
'forum' => $forumid,
|
||||
), null, 'id, userid, discussion, preference');
|
||||
foreach ($subscriptions as $id => $data) {
|
||||
self::add_to_discussion_cache($forumid, $data->userid, $data->discussion, $data->preference);
|
||||
}
|
||||
self::$discussionfetchedforums[$forumid] = true;
|
||||
$subscriptions->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified discussion and user preference to the discussion
|
||||
* subscription cache.
|
||||
*
|
||||
* @param int $forumid The ID of the forum that this preference belongs to
|
||||
* @param int $userid The ID of the user that this preference belongs to
|
||||
* @param int $discussion The ID of the discussion that this preference relates to
|
||||
* @param int $preference The preference to store
|
||||
*/
|
||||
protected static function add_to_discussion_cache($forumid, $userid, $discussion, $preference) {
|
||||
if (!isset(self::$forumdiscussioncache[$userid])) {
|
||||
self::$forumdiscussioncache[$userid] = array();
|
||||
}
|
||||
|
||||
if (!isset(self::$forumdiscussioncache[$userid][$forumid])) {
|
||||
self::$forumdiscussioncache[$userid][$forumid] = array();
|
||||
}
|
||||
|
||||
self::$forumdiscussioncache[$userid][$forumid][$discussion] = $preference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the discussion cache.
|
||||
*
|
||||
* This cache is used to reduce the number of database queries when
|
||||
* checking forum discussion subscription states.
|
||||
*/
|
||||
public static function reset_discussion_cache() {
|
||||
self::$forumdiscussioncache = array();
|
||||
self::$discussionfetchedforums = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the forum cache.
|
||||
*
|
||||
@ -388,10 +575,12 @@ class subscriptions {
|
||||
* @param \stdClass $forum The forum record for this forum.
|
||||
* @param \context_module|null $context Module context, may be omitted if not known or if called for the current
|
||||
* module set in page.
|
||||
* @param boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
|
||||
* discussion subscriptions are removed too.
|
||||
* @return bool|int Returns true if the user is already subscribed, or the forum_subscriptions ID if the user was
|
||||
* successfully subscribed.
|
||||
*/
|
||||
public static function subscribe_user($userid, $forum, $context = null) {
|
||||
public static function subscribe_user($userid, $forum, $context = null, $userrequest = false) {
|
||||
global $DB;
|
||||
|
||||
if (self::is_subscribed($userid, $forum)) {
|
||||
@ -404,6 +593,22 @@ class subscriptions {
|
||||
|
||||
$result = $DB->insert_record("forum_subscriptions", $sub);
|
||||
|
||||
if ($userrequest) {
|
||||
$discussionsubscriptions = $DB->get_recordset('forum_discussion_subs', array('userid' => $userid, 'forum' => $forum->id));
|
||||
$DB->delete_records('forum_discussion_subs',
|
||||
array('userid' => $userid, 'forum' => $forum->id, 'preference' => self::FORUM_DISCUSSION_SUBSCRIBED));
|
||||
|
||||
// Reset the subscription caches for this forum.
|
||||
// We know that the there were previously entries and there aren't any more.
|
||||
if (isset(self::$forumdiscussioncache[$userid]) && isset(self::$forumdiscussioncache[$userid][$forum->id])) {
|
||||
foreach (self::$forumdiscussioncache[$userid][$forum->id] as $discussionid => $preference) {
|
||||
if ($preference == self::FORUM_DISCUSSION_SUBSCRIBED) {
|
||||
unset(self::$forumdiscussioncache[$userid][$forum->id][$discussionid]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the cache for this forum.
|
||||
self::$forumcache[$userid][$forum->id] = true;
|
||||
|
||||
@ -416,6 +621,12 @@ class subscriptions {
|
||||
|
||||
);
|
||||
$event = event\subscription_created::create($params);
|
||||
if ($userrequest && $discussionsubscriptions) {
|
||||
foreach ($discussionsubscriptions as $subscription) {
|
||||
$event->add_record_snapshot('forum_discussion_subs', $subscription);
|
||||
}
|
||||
$discussionsubscriptions->close();
|
||||
}
|
||||
$event->trigger();
|
||||
|
||||
return $result;
|
||||
@ -428,9 +639,11 @@ class subscriptions {
|
||||
* @param \stdClass $forum The forum record for this forum.
|
||||
* @param \context_module|null $context Module context, may be omitted if not known or if called for the current
|
||||
* module set in page.
|
||||
* @param boolean $userrequest Whether the user requested this change themselves. This has an effect on whether
|
||||
* discussion subscriptions are removed too.
|
||||
* @return boolean Always returns true.
|
||||
*/
|
||||
public static function unsubscribe_user($userid, $forum, $context = null) {
|
||||
public static function unsubscribe_user($userid, $forum, $context = null, $userrequest = false) {
|
||||
global $DB;
|
||||
|
||||
$sqlparams = array(
|
||||
@ -442,6 +655,17 @@ class subscriptions {
|
||||
if ($forumsubscription = $DB->get_record('forum_subscriptions', $sqlparams)) {
|
||||
$DB->delete_records('forum_subscriptions', array('id' => $forumsubscription->id));
|
||||
|
||||
if ($userrequest) {
|
||||
$discussionsubscriptions = $DB->get_recordset('forum_discussion_subs', $sqlparams);
|
||||
$DB->delete_records('forum_discussion_subs',
|
||||
array('userid' => $userid, 'forum' => $forum->id, 'preference' => self::FORUM_DISCUSSION_UNSUBSCRIBED));
|
||||
|
||||
// We know that the there were previously entries and there aren't any more.
|
||||
if (isset(self::$forumdiscussioncache[$userid]) && isset(self::$forumdiscussioncache[$userid][$forum->id])) {
|
||||
self::$forumdiscussioncache[$userid][$forum->id] = array();
|
||||
}
|
||||
}
|
||||
|
||||
// Reset the cache for this forum.
|
||||
self::$forumcache[$userid][$forum->id] = false;
|
||||
|
||||
@ -455,10 +679,141 @@ class subscriptions {
|
||||
);
|
||||
$event = event\subscription_deleted::create($params);
|
||||
$event->add_record_snapshot('forum_subscriptions', $forumsubscription);
|
||||
if ($userrequest && $discussionsubscriptions) {
|
||||
foreach ($discussionsubscriptions as $subscription) {
|
||||
$event->add_record_snapshot('forum_discussion_subs', $subscription);
|
||||
}
|
||||
$discussionsubscriptions->close();
|
||||
}
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes the user to the specified discussion.
|
||||
*
|
||||
* @param int $userid The userid of the user being subscribed
|
||||
* @param \stdClass $discussion The discussion to subscribe to
|
||||
* @param \context_module|null $context Module context, may be omitted if not known or if called for the current
|
||||
* module set in page.
|
||||
* @return boolean Whether a change was made
|
||||
*/
|
||||
public static function subscribe_user_to_discussion($userid, $discussion, $context = null) {
|
||||
global $DB;
|
||||
|
||||
// First check whether the user is subscribed to the discussion already.
|
||||
$subscription = $DB->get_record('forum_discussion_subs', array('userid' => $userid, 'discussion' => $discussion->id));
|
||||
if ($subscription) {
|
||||
if ($subscription->preference == self::FORUM_DISCUSSION_SUBSCRIBED) {
|
||||
// The user is already subscribed to the discussion. Ignore.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// No discussion-level subscription. Check for a forum level subscription.
|
||||
if ($DB->record_exists('forum_subscriptions', array('userid' => $userid, 'forum' => $discussion->forum))) {
|
||||
if ($subscription && $subscription->preference == self::FORUM_DISCUSSION_UNSUBSCRIBED) {
|
||||
// The user is subscribed to the forum, but unsubscribed from the discussion, delete the discussion preference.
|
||||
$DB->delete_records('forum_discussion_subs', array('id' => $subscription->id));
|
||||
unset(self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id]);
|
||||
} else {
|
||||
// The user is already subscribed to the forum. Ignore.
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ($subscription) {
|
||||
$subscription->preference = self::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
$DB->update_record('forum_discussion_subs', $subscription);
|
||||
} else {
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $userid;
|
||||
$subscription->forum = $discussion->forum;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = self::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$context = forum_get_context($discussion->forum, $context);
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $userid,
|
||||
'other' => array(
|
||||
'forumid' => $discussion->forum,
|
||||
'discussion' => $discussion->id,
|
||||
),
|
||||
|
||||
);
|
||||
$event = event\discussion_subscription_created::create($params);
|
||||
$event->trigger();
|
||||
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Unsubscribes the user from the specified discussion.
|
||||
*
|
||||
* @param int $userid The userid of the user being unsubscribed
|
||||
* @param \stdClass $discussion The discussion to unsubscribe from
|
||||
* @param \context_module|null $context Module context, may be omitted if not known or if called for the current
|
||||
* module set in page.
|
||||
* @return boolean Whether a change was made
|
||||
*/
|
||||
public static function unsubscribe_user_from_discussion($userid, $discussion, $context = null) {
|
||||
global $DB;
|
||||
|
||||
// First check whether the user's subscription preference for this discussion.
|
||||
$subscription = $DB->get_record('forum_discussion_subs', array('userid' => $userid, 'discussion' => $discussion->id));
|
||||
if ($subscription) {
|
||||
if ($subscription->preference == self::FORUM_DISCUSSION_UNSUBSCRIBED) {
|
||||
// The user is already unsubscribed from the discussion. Ignore.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// No discussion-level preference. Check for a forum level subscription.
|
||||
if (!$DB->record_exists('forum_subscriptions', array('userid' => $userid, 'forum' => $discussion->forum))) {
|
||||
if ($subscription && $subscription->preference == self::FORUM_DISCUSSION_SUBSCRIBED) {
|
||||
// The user is not subscribed to the forum, but subscribed from the discussion, delete the discussion subscription.
|
||||
$DB->delete_records('forum_discussion_subs', array('id' => $subscription->id));
|
||||
unset(self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id]);
|
||||
} else {
|
||||
// The user is not subscribed from the forum. Ignore.
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ($subscription) {
|
||||
$subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED;
|
||||
$DB->update_record('forum_discussion_subs', $subscription);
|
||||
} else {
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $userid;
|
||||
$subscription->forum = $discussion->forum;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = self::FORUM_DISCUSSION_UNSUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
}
|
||||
self::$forumdiscussioncache[$userid][$discussion->forum][$discussion->id] = false;
|
||||
}
|
||||
|
||||
$context = forum_get_context($discussion->forum, $context);
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $userid,
|
||||
'other' => array(
|
||||
'forumid' => $discussion->forum,
|
||||
'discussion' => $discussion->id,
|
||||
),
|
||||
|
||||
);
|
||||
$event = event\discussion_subscription_deleted::create($params);
|
||||
$event->trigger();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -195,9 +195,9 @@ if (!is_null($subscribe)) {
|
||||
$subscribed = \mod_forum\subscriptions::is_subscribed($USER->id, $forum);
|
||||
$canmanageactivities = has_capability('moodle/course:manageactivities', $coursecontext, $USER->id);
|
||||
if (($canmanageactivities || \mod_forum\subscriptions::is_subscribable($forum)) && $subscribe && !$subscribed && $cansub) {
|
||||
\mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext);
|
||||
\mod_forum\subscriptions::subscribe_user($USER->id, $forum, $modcontext, true);
|
||||
} else if (!$subscribe && $subscribed) {
|
||||
\mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext);
|
||||
\mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, $modcontext, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -150,6 +150,8 @@ $string['eventdiscussionupdated'] = 'Discussion updated';
|
||||
$string['eventdiscussiondeleted'] = 'Discussion deleted';
|
||||
$string['eventdiscussionmoved'] = 'Discussion moved';
|
||||
$string['eventdiscussionviewed'] = 'Discussion viewed';
|
||||
$string['eventdiscussionsubscriptioncreated'] = 'Discussion subscription created';
|
||||
$string['eventdiscussionsubscriptiondeleted'] = 'Discussion subscription deleted';
|
||||
$string['eventuserreportviewed'] = 'User report viewed';
|
||||
$string['eventpostcreated'] = 'Post created';
|
||||
$string['eventpostdeleted'] = 'Post deleted';
|
||||
|
@ -546,7 +546,7 @@ function forum_cron() {
|
||||
// caching subscribed users of each forum
|
||||
if (!isset($subscribedusers[$forumid])) {
|
||||
$modcontext = context_module::instance($coursemodules[$forumid]->id);
|
||||
if ($subusers = \mod_forum\subscriptions::fetch_subscribed_users($forums[$forumid], 0, $modcontext, 'u.*')) {
|
||||
if ($subusers = \mod_forum\subscriptions::fetch_subscribed_users($forums[$forumid], 0, $modcontext, 'u.*', true)) {
|
||||
foreach ($subusers as $postuser) {
|
||||
// this user is subscribed to this forum
|
||||
$subscribedusers[$forumid][$postuser->id] = $postuser->id;
|
||||
|
@ -113,7 +113,7 @@ if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context
|
||||
if ($forum->forcesubscribe <> FORUM_INITIALSUBSCRIBE) {
|
||||
$users = \mod_forum\subscriptions::get_potential_subscribers($context, 0, 'u.id, u.email', '');
|
||||
foreach ($users as $user) {
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum, $context);
|
||||
}
|
||||
}
|
||||
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
|
||||
@ -147,7 +147,7 @@ if (\mod_forum\subscriptions::is_subscribed($user->id, $forum)) {
|
||||
exit;
|
||||
}
|
||||
require_sesskey();
|
||||
if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) {
|
||||
if (\mod_forum\subscriptions::unsubscribe_user($user->id, $forum, $context, true)) {
|
||||
redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
|
||||
} else {
|
||||
print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
|
||||
@ -170,6 +170,6 @@ if (\mod_forum\subscriptions::is_subscribed($user->id, $forum)) {
|
||||
exit;
|
||||
}
|
||||
require_sesskey();
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum, $context, true);
|
||||
redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
|
||||
}
|
||||
|
@ -1824,4 +1824,873 @@ class mod_forum_events_testcase extends advanced_testcase {
|
||||
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created() {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
// Trigger the event by subscribing the user to the forum discussion.
|
||||
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
|
||||
|
||||
|
||||
$cm = get_coursemodule_from_instance('forum', $discussion->forum);
|
||||
$context = \context_module::instance($cm->id);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
$url = new \moodle_url('/mod/forum/subscribe.php', array(
|
||||
'id' => $forum->id,
|
||||
'd' => $discussion->id
|
||||
));
|
||||
|
||||
$this->assertEquals($url, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created_validation() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$event = \mod_forum\event\discussion_subscription_created::create($params);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
$event->trigger();
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test contextlevel validation of discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created_validation_contextlevel() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
$params = array(
|
||||
'context' => \context_course::instance($course->id),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
// Without an invalid context.
|
||||
$this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
|
||||
\mod_forum\event\discussion_subscription_created::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test discussion validation of discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created_validation_discussion() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
// Without the discussion.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
|
||||
\mod_forum\event\discussion_subscription_created::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test forumid validation of discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created_validation_forumid() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
// Without the forumid.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
|
||||
\mod_forum\event\discussion_subscription_created::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test relateduserid validation of discussion_subscription_created event.
|
||||
*/
|
||||
public function test_discussion_subscription_created_validation_relateduserid() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
// Without the relateduserid.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
|
||||
\mod_forum\event\discussion_subscription_created::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted() {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
// Trigger the event by unsubscribing the user to the forum discussion.
|
||||
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
|
||||
|
||||
|
||||
$cm = get_coursemodule_from_instance('forum', $discussion->forum);
|
||||
$context = \context_module::instance($cm->id);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
$url = new \moodle_url('/mod/forum/subscribe.php', array(
|
||||
'id' => $forum->id,
|
||||
'd' => $discussion->id
|
||||
));
|
||||
|
||||
$this->assertEquals($url, $event->get_url());
|
||||
$this->assertEventContextNotUsed($event);
|
||||
$this->assertNotEmpty($event->get_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted_validation() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
$params = array(
|
||||
'context' => $context,
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$event = \mod_forum\event\discussion_subscription_deleted::create($params);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
$event->trigger();
|
||||
$events = $sink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Without an invalid context.
|
||||
$params['context'] = \context_course::instance($course->id);
|
||||
$this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
|
||||
\mod_forum\event\discussion_deleted::create($params);
|
||||
|
||||
// Without the discussion.
|
||||
unset($params['discussion']);
|
||||
$this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.');
|
||||
\mod_forum\event\discussion_deleted::create($params);
|
||||
|
||||
// Without the forumid.
|
||||
unset($params['forumid']);
|
||||
$this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
|
||||
\mod_forum\event\discussion_deleted::create($params);
|
||||
|
||||
// Without the relateduserid.
|
||||
unset($params['relateduserid']);
|
||||
$this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.');
|
||||
\mod_forum\event\discussion_deleted::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test contextlevel validation of discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted_validation_contextlevel() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
$params = array(
|
||||
'context' => \context_course::instance($course->id),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
// Without an invalid context.
|
||||
$this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
|
||||
\mod_forum\event\discussion_subscription_deleted::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test discussion validation of discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted_validation_discussion() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
// Without the discussion.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
|
||||
\mod_forum\event\discussion_subscription_deleted::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test forumid validation of discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted_validation_forumid() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
// Without the forumid.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'relateduserid' => $user->id,
|
||||
'other' => array(
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
|
||||
\mod_forum\event\discussion_subscription_deleted::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test relateduserid validation of discussion_subscription_deleted event.
|
||||
*/
|
||||
public function test_discussion_subscription_deleted_validation_relateduserid() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// The user is not subscribed to the forum. Insert a new discussion subscription.
|
||||
$subscription = new \stdClass();
|
||||
$subscription->userid = $user->id;
|
||||
$subscription->forum = $forum->id;
|
||||
$subscription->discussion = $discussion->id;
|
||||
$subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
|
||||
|
||||
$subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
|
||||
|
||||
$context = context_module::instance($forum->cmid);
|
||||
|
||||
// Without the relateduserid.
|
||||
$params = array(
|
||||
'context' => context_module::instance($forum->cmid),
|
||||
'objectid' => $subscription->id,
|
||||
'other' => array(
|
||||
'forumid' => $forum->id,
|
||||
'discussion' => $discussion->id,
|
||||
)
|
||||
);
|
||||
|
||||
$this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
|
||||
\mod_forum\event\discussion_subscription_deleted::create($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the correct context is used in the events when subscribing
|
||||
* users.
|
||||
*/
|
||||
public function test_forum_subscription_page_context_valid() {
|
||||
global $CFG, $PAGE;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
// Setup test data.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$user = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
||||
|
||||
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
|
||||
$forum = $this->getDataGenerator()->create_module('forum', $options);
|
||||
$quiz = $this->getDataGenerator()->create_module('quiz', $options);
|
||||
|
||||
// Add a discussion.
|
||||
$record = array();
|
||||
$record['course'] = $course->id;
|
||||
$record['forum'] = $forum->id;
|
||||
$record['userid'] = $user->id;
|
||||
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// Add a post.
|
||||
$record = array();
|
||||
$record['discussion'] = $discussion->id;
|
||||
$record['userid'] = $user->id;
|
||||
$post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
|
||||
|
||||
// Set up the default page event to use this forum.
|
||||
$PAGE = new moodle_page();
|
||||
$cm = get_coursemodule_from_instance('forum', $discussion->forum);
|
||||
$context = \context_module::instance($cm->id);
|
||||
$PAGE->set_context($context);
|
||||
$PAGE->set_cm($cm, $course, $forum);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
// Trigger the event by subscribing the user to the forum.
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user to the forum.
|
||||
\mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by subscribing the user to the discussion.
|
||||
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user from the discussion.
|
||||
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Now try with the context for a different module (quiz).
|
||||
$PAGE = new moodle_page();
|
||||
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
||||
$quizcontext = \context_module::instance($cm->id);
|
||||
$PAGE->set_context($quizcontext);
|
||||
$PAGE->set_cm($cm, $course, $quiz);
|
||||
|
||||
// Trigger and capturing the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
// Trigger the event by subscribing the user to the forum.
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user to the forum.
|
||||
\mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by subscribing the user to the discussion.
|
||||
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user from the discussion.
|
||||
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Now try with the course context - the module context should still be used.
|
||||
$PAGE = new moodle_page();
|
||||
$coursecontext = \context_course::instance($course->id);
|
||||
$PAGE->set_context($coursecontext);
|
||||
|
||||
// Trigger the event by subscribing the user to the forum.
|
||||
\mod_forum\subscriptions::subscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user to the forum.
|
||||
\mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by subscribing the user to the discussion.
|
||||
\mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
// Trigger the event by unsubscribing the user from the discussion.
|
||||
\mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
|
||||
|
||||
$events = $sink->get_events();
|
||||
$sink->clear();
|
||||
$this->assertCount(1, $events);
|
||||
$event = reset($events);
|
||||
|
||||
// Checking that the event contains the expected values.
|
||||
$this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
|
||||
$this->assertEquals($context, $event->get_context());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -24,6 +24,9 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/forum/lib.php');
|
||||
|
||||
class mod_forum_lib_testcase extends advanced_testcase {
|
||||
|
||||
public function setUp() {
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -50,7 +50,7 @@ if (data_submitted() and $confirm and confirm_sesskey()) {
|
||||
$forums = \mod_forum\subscriptions::get_unsubscribable_forums();
|
||||
|
||||
foreach($forums as $forum) {
|
||||
\mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm));
|
||||
\mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true);
|
||||
}
|
||||
$DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user