MDL-45743 forum: trigger an event when locking/unlocking a discussion
This commit is contained in:
@@ -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']];
|
||||
}
|
||||
}
|
||||
@@ -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'));
|
||||
|
||||
@@ -236,6 +236,7 @@ $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';
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user