Merge branch '45743-forum-trigger-more-events-master' of https://github.com/DSI-Universite-Rennes2/moodle

This commit is contained in:
Huong Nguyen
2023-08-02 15:05:14 +07:00
8 changed files with 325 additions and 13 deletions
@@ -0,0 +1,111 @@
<?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/>.
namespace mod_forum\event;
use coding_exception;
use moodle_url;
/**
* The mod_forum discussion lock updated event.
*
* @package mod_forum
* @copyright 2022 Université Rennes 2 {@link https://www.univ-rennes2.fr}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class discussion_lock_updated extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init(): void {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'forum_discussions';
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "The user with id '$this->userid' {$this->other['status']} the discussion: $this->objectid".
" in the forum: {$this->other['forumid']}";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('eventdiscussionlockupdated', 'mod_forum');
}
/**
* Get URL related to the action
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/mod/forum/discuss.php', ['d' => $this->objectid]);
}
/**
* Custom validation.
*
* @throws coding_exception
* @return void
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->other['forumid'])) {
throw new coding_exception('forumid must be set in $other.');
}
if (!isset($this->other['status'])) {
throw new \coding_exception('The \'status\' value must be set in other.');
}
if (!in_array($this->other['status'], ['locked', 'unlocked'], true)) {
throw new \coding_exception('The \'status\' value must be \'locked\' or \'unlocked\'.');
}
if ($this->contextlevel != CONTEXT_MODULE) {
throw new coding_exception('Context passed must be module context.');
}
if (!isset($this->objectid)) {
throw new coding_exception('objectid must be set to the discussionid.');
}
}
/**
* Forum discussion object id mappings.
*
* @return array
*/
public static function get_objectid_mapping(): array {
return ['db' => 'forum_discussions', 'restore' => 'forum_discussion'];
}
/**
* Forum id mappings.
*
* @return array
*/
public static function get_other_mapping(): array {
return ['forumid' => ['db' => 'forum', 'restore' => 'forum']];
}
}
@@ -0,0 +1,99 @@
<?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/>.
namespace mod_forum\event;
use coding_exception;
use moodle_url;
/**
* The mod_forum subscription mode updated event.
*
* @package mod_forum
* @copyright 2022 Université Rennes 2 {@link https://www.univ-rennes2.fr}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class subscription_mode_updated extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init(): void {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'forum';
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "The user with id '$this->userid' has updated the forum subscription
from mode '{$this->other['oldvalue']}' to mode '{$this->other['newvalue']}' in the forum id '$this->objectid'";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name(): string {
return get_string('eventforumsubscriptionupdated', 'mod_forum');
}
/**
* Get URL related to the action
*
* @return moodle_url
*/
public function get_url(): moodle_url {
return new moodle_url('/mod/forum/subscribers.php', ['id' => $this->objectid]);
}
/**
* Custom validation.
*
* @throws coding_exception
* @return void
*/
protected function validate_data(): void {
parent::validate_data();
if (!isset($this->other['oldvalue'])) {
throw new coding_exception('oldvalue must be set in $other.');
}
if (!isset($this->other['newvalue'])) {
throw new coding_exception('newvalue must be set in $other.');
}
if ($this->contextlevel != CONTEXT_MODULE) {
throw new coding_exception('Context passed must be module context.');
}
if (!isset($this->objectid)) {
throw new coding_exception('objectid must be set to the forumid.');
}
}
/**
* Forum object id mappings.
*
* @return array
*/
public static function get_objectid_mapping(): array {
return ['db' => 'forum', 'restore' => 'forum'];
}
}
+24 -3
View File
@@ -176,11 +176,32 @@ class subscriptions {
*
* @param \stdClass $forum The record of the forum to set
* @param int $status The new subscription state
* @return bool
* @return bool true
* @throws dml_exception A DML specific exception is thrown for any errors.
*/
public static function set_subscription_mode($forumid, $status = 1) {
public static function set_subscription_mode($forum, $status = FORUM_FORCESUBSCRIBE): bool {
global $DB;
return $DB->set_field("forum", "forcesubscribe", $status, array("id" => $forumid));
if (is_numeric($forum)) {
debugging(__METHOD__.': Argument #1 ($forum) must be a stdClass record of a forum', DEBUG_DEVELOPER);
$forum = $DB->get_record("forum", ["id" => $forum], '*', MUST_EXIST);
}
$DB->set_field("forum", "forcesubscribe", $status, ["id" => $forum->id]);
if ($forum->forcesubscribe != $status) {
// Trigger event if subscription mode has been changed.
$event = \mod_forum\event\subscription_mode_updated::create([
"context" => forum_get_context($forum->id),
"objectid" => $forum->id,
"other" => ["oldvalue" => $forum->forcesubscribe, "newvalue" => $status],
]);
$event->add_record_snapshot("forum", $forum);
$event->trigger();
}
return true;
}
/**
+12
View File
@@ -1747,6 +1747,18 @@ class mod_forum_external extends external_api {
$discussion->toggle_locked_state($lockedvalue);
$response = $discussionvault->update_discussion($discussion);
$discussion = !$response ? $response : $discussion;
// Trigger an event.
$params = [
'context' => $forum->get_context(),
'objectid' => $discussion->get_id(),
'other' => ['forumid' => $forum->get_id(), 'status' => $lockedvalue ? 'locked' : 'unlocked'],
];
$event = \mod_forum\event\discussion_lock_updated::create($params);
$discussionrecord = $DB->get_record('forum_discussions', ['id' => $discussion->get_id()]);
$event->add_record_snapshot('forum_discussions', $discussionrecord);
$event->trigger();
$exporterfactory = mod_forum\local\container::get_exporter_factory();
$exporter = $exporterfactory->get_discussion_exporter($USER, $forum, $discussion);
return $exporter->export($PAGE->get_renderer('mod_forum'));
+2
View File
@@ -236,12 +236,14 @@ $string['eventcoursesearched'] = 'Course searched';
$string['eventdiscussioncreated'] = 'Discussion created';
$string['eventdiscussionupdated'] = 'Discussion updated';
$string['eventdiscussiondeleted'] = 'Discussion deleted';
$string['eventdiscussionlockupdated'] = 'Discussion lock updated';
$string['eventdiscussionmoved'] = 'Discussion moved';
$string['eventdiscussionviewed'] = 'Discussion viewed';
$string['eventdiscussionsubscriptioncreated'] = 'Discussion subscription created';
$string['eventdiscussionsubscriptiondeleted'] = 'Discussion subscription deleted';
$string['eventdiscussionpinned'] = 'Discussion pinned';
$string['eventdiscussionunpinned'] = 'Discussion unpinned';
$string['eventforumsubscriptionupdated'] = 'Subscription mode updated';
$string['eventuserreportviewed'] = 'User report viewed';
$string['eventpostcreated'] = 'Post created';
$string['eventpostdeleted'] = 'Post deleted';
+4 -4
View File
@@ -126,7 +126,7 @@ if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context
require_sesskey();
switch ($mode) {
case FORUM_CHOOSESUBSCRIBE : // 0
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_CHOOSESUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_CHOOSESUBSCRIBE);
redirect(
$subscribersurl,
get_string('everyonecannowchoose', 'forum'),
@@ -135,7 +135,7 @@ if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context
);
break;
case FORUM_FORCESUBSCRIBE : // 1
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_FORCESUBSCRIBE);
redirect(
$subscribersurl,
get_string('everyoneisnowsubscribed', 'forum'),
@@ -144,7 +144,7 @@ if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context
);
break;
case FORUM_INITIALSUBSCRIBE : // 2
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_INITIALSUBSCRIBE);
if ($forum->forcesubscribe <> FORUM_INITIALSUBSCRIBE) {
// Reload the forum again to get the updated forcesubscribe field.
$forum = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
@@ -161,7 +161,7 @@ if (!is_null($mode) and has_capability('mod/forum:managesubscriptions', $context
);
break;
case FORUM_DISALLOWSUBSCRIBE : // 3
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_DISALLOWSUBSCRIBE);
redirect(
$subscribersurl,
get_string('noonecansubscribenow', 'forum'),
+25
View File
@@ -1901,6 +1901,8 @@ class externallib_test extends externallib_advanced_testcase {
/*
* Test set_lock_state.
*
* @covers \mod_forum\event\discussion_lock_updated
*/
public function test_set_lock_state() {
global $DB;
@@ -1916,6 +1918,7 @@ class externallib_test extends externallib_advanced_testcase {
$record->course = $course->id;
$record->type = 'news';
$forum = self::getDataGenerator()->create_module('forum', $record);
$context = \context_module::instance($forum->cmid);
$record = new \stdClass();
$record->course = $course->id;
@@ -1937,16 +1940,38 @@ class externallib_test extends externallib_advanced_testcase {
// Set the lock.
self::setAdminUser();
$sink = $this->redirectEvents(); // Capturing the event.
$result = mod_forum_external::set_lock_state($forum->id, $discussion->id, 0);
$result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
$this->assertTrue($result['locked']);
$this->assertNotEquals(0, $result['times']['locked']);
// Check that the event contains the expected values.
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\discussion_lock_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$this->assertStringContainsString(' locked the discussion:', $event->get_description());
// Unset the lock.
$sink = $this->redirectEvents(); // Capturing the event.
$result = mod_forum_external::set_lock_state($forum->id, $discussion->id, time());
$result = external_api::clean_returnvalue(mod_forum_external::set_lock_state_returns(), $result);
$this->assertFalse($result['locked']);
$this->assertEquals('0', $result['times']['locked']);
// Check that the event contains the expected values.
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\discussion_lock_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$this->assertStringContainsString(' unlocked the discussion:', $event->get_description());
}
/*
+48 -6
View File
@@ -58,6 +58,11 @@ class subscriptions_test extends \advanced_testcase {
\mod_forum\subscriptions::reset_discussion_cache();
}
/**
* Test subscription modes modifications.
*
* @covers \mod_forum\event\subscription_mode_updated
*/
public function test_subscription_modes() {
global $DB;
@@ -68,6 +73,7 @@ class subscriptions_test extends \advanced_testcase {
$options = array('course' => $course->id);
$forum = $this->getDataGenerator()->create_module('forum', $options);
$context = \context_module::instance($forum->cmid);
// Create a user enrolled in the course as a student.
list($user) = $this->helper_create_users($course, 1);
@@ -75,33 +81,69 @@ class subscriptions_test extends \advanced_testcase {
// Must be logged in as the current user.
$this->setUser($user);
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
$sink = $this->redirectEvents(); // Capturing the event.
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_FORCESUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertEquals(FORUM_FORCESUBSCRIBE, \mod_forum\subscriptions::get_subscription_mode($forum));
$this->assertTrue(\mod_forum\subscriptions::is_forcesubscribed($forum));
$this->assertFalse(\mod_forum\subscriptions::is_subscribable($forum));
$this->assertFalse(\mod_forum\subscriptions::subscription_disabled($forum));
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\subscription_mode_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$sink = $this->redirectEvents(); // Capturing the event.
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_DISALLOWSUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertEquals(FORUM_DISALLOWSUBSCRIBE, \mod_forum\subscriptions::get_subscription_mode($forum));
$this->assertTrue(\mod_forum\subscriptions::subscription_disabled($forum));
$this->assertFalse(\mod_forum\subscriptions::is_subscribable($forum));
$this->assertFalse(\mod_forum\subscriptions::is_forcesubscribed($forum));
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_INITIALSUBSCRIBE);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\subscription_mode_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$sink = $this->redirectEvents(); // Capturing the event.
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_INITIALSUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertEquals(FORUM_INITIALSUBSCRIBE, \mod_forum\subscriptions::get_subscription_mode($forum));
$this->assertTrue(\mod_forum\subscriptions::is_subscribable($forum));
$this->assertFalse(\mod_forum\subscriptions::subscription_disabled($forum));
$this->assertFalse(\mod_forum\subscriptions::is_forcesubscribed($forum));
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_CHOOSESUBSCRIBE);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\subscription_mode_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
$sink = $this->redirectEvents(); // Capturing the event.
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_CHOOSESUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertEquals(FORUM_CHOOSESUBSCRIBE, \mod_forum\subscriptions::get_subscription_mode($forum));
$this->assertTrue(\mod_forum\subscriptions::is_subscribable($forum));
$this->assertFalse(\mod_forum\subscriptions::subscription_disabled($forum));
$this->assertFalse(\mod_forum\subscriptions::is_forcesubscribed($forum));
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
$this->assertInstanceOf('\mod_forum\event\subscription_mode_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
@@ -1436,12 +1478,12 @@ class subscriptions_test extends \advanced_testcase {
// Subscribption disabled.
$this->setUser($student->id);
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_DISALLOWSUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_DISALLOWSUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertFalse((boolean)\mod_forum\subscriptions::get_user_default_subscription($forum, $context, $cm, $discussion->id));
$this->assertFalse((boolean)\mod_forum\subscriptions::get_user_default_subscription($forum, $context, $cm, null));
\mod_forum\subscriptions::set_subscription_mode($forum->id, FORUM_FORCESUBSCRIBE);
\mod_forum\subscriptions::set_subscription_mode($forum, FORUM_FORCESUBSCRIBE);
$forum = $DB->get_record('forum', array('id' => $forum->id));
$this->assertTrue((boolean)\mod_forum\subscriptions::get_user_default_subscription($forum, $context, $cm, $discussion->id));
$this->assertTrue((boolean)\mod_forum\subscriptions::get_user_default_subscription($forum, $context, $cm, null));