MDL-40062 mod_forum: Add discussion_updated event

This commit is contained in:
Dan Poltawski 2014-02-17 14:04:27 +08:00
parent a52c04d549
commit 97802bea3a
4 changed files with 202 additions and 0 deletions

View File

@ -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/>.
/**
* The mod_forum discussion updated event.
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_forum discussion updated event.
*
* @property-read array $other Extra information about the event.
* -int forumid: The id of the forum the discussion is in
*
* @package mod_forum
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class discussion_updated extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$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() {
return "The user {$this->userid} has updated the discussion {$this->objectid}.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventdiscussionupdated', 'mod_forum');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/forum/discuss.php', array('d' => $this->objectid));
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['forumid'])) {
throw new \coding_exception('forumid must be set in $other.');
}
if ($this->contextlevel != CONTEXT_MODULE) {
throw new \coding_exception('Context passed must be module context.');
}
if (!isset($this->objectid)) {
throw new \coding_exception('objectid must be set to the discussionid.');
}
}
}

View File

@ -146,6 +146,7 @@ $string['editedpostupdated'] = '{$a}\'s post was updated';
$string['editing'] = 'Editing';
$string['eventcoursesearched'] = 'Course searched';
$string['eventdiscussioncreated'] = 'Discussion created';
$string['eventdiscussionupdated'] = 'Discussion updated';
$string['eventdiscussiondeleted'] = 'Discussion deleted';
$string['eventdiscussionmoved'] = 'Discussion moved';
$string['eventdiscussionviewed'] = 'Discussion viewed';

View File

@ -481,6 +481,16 @@ if (!empty($forum)) { // User is starting a new discussion in a forum
forum_discussion_update_last_post($newid);
// Fire events to reflect the split..
$params = array(
'context' => $modcontext,
'objectid' => $discussion->id,
'other' => array(
'forumid' => $forum->id,
)
);
$event = \mod_forum\event\discussion_updated::create($params);
$event->trigger();
$params = array(
'context' => $modcontext,
'objectid' => $newid,

View File

@ -201,6 +201,98 @@ class mod_forum_events_testcase extends advanced_testcase {
$this->assertNotEmpty($event->get_name());
}
/**
* Ensure discussion_updated event validates that forumid is set.
*/
public function test_discussion_updated_forumid_validation() {
$course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$context = context_module::instance($forum->cmid);
$params = array(
'context' => $context,
);
$this->setExpectedException('coding_exception', 'forumid must be set in $other.');
\mod_forum\event\discussion_updated::create($params);
}
/**
* Ensure discussion_updated event validates that discussionid is set.
*/
public function test_discussion_updated_objectid_validation() {
$course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$context = context_module::instance($forum->cmid);
$params = array(
'context' => $context,
'other' => array('forumid' => $forum->id)
);
$this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
\mod_forum\event\discussion_updated::create($params);
}
/**
* Ensure discussion_created event validates that the context is the correct level.
*/
public function test_discussion_updated_context_validation() {
$course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$params = array(
'context' => context_system::instance(),
'other' => array('forumid' => $forum->id),
);
$this->setExpectedException('coding_exception', 'Context passed must be module context.');
\mod_forum\event\discussion_updated::create($params);
}
/**
* Test discussion_created event.
*/
public function test_discussion_updated() {
// Setup test data.
$course = $this->getDataGenerator()->create_course();
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
$user = $this->getDataGenerator()->create_user();
// 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);
$context = context_module::instance($forum->cmid);
$params = array(
'context' => $context,
'objectid' => $discussion->id,
'other' => array('forumid' => $forum->id),
);
// Create the event.
$event = \mod_forum\event\discussion_updated::create($params);
// Trigger and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Check that the event contains the expected values.
$this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
$this->assertEquals($context, $event->get_context());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
}
/**
* Ensure discussion_deleted event validates that forumid is set.
*/