Merge branch 'MDL-40062-master' of git://github.com/danpoltawski/moodle
authorDamyon Wiese <damyon@moodle.com>
Mon, 17 Feb 2014 06:37:18 +0000 (14:37 +0800)
committerDamyon Wiese <damyon@moodle.com>
Mon, 17 Feb 2014 06:37:18 +0000 (14:37 +0800)
31 files changed:
mod/forum/classes/event/course_module_instance_list_viewed.php [new file with mode: 0644]
mod/forum/classes/event/course_searched.php [new file with mode: 0644]
mod/forum/classes/event/discussion_created.php [new file with mode: 0644]
mod/forum/classes/event/discussion_deleted.php [new file with mode: 0644]
mod/forum/classes/event/discussion_moved.php [new file with mode: 0644]
mod/forum/classes/event/discussion_updated.php [new file with mode: 0644]
mod/forum/classes/event/discussion_viewed.php [new file with mode: 0644]
mod/forum/classes/event/forum_viewed.php [new file with mode: 0644]
mod/forum/classes/event/post_created.php [new file with mode: 0644]
mod/forum/classes/event/post_deleted.php [new file with mode: 0644]
mod/forum/classes/event/post_updated.php [new file with mode: 0644]
mod/forum/classes/event/readtracking_disabled.php [new file with mode: 0644]
mod/forum/classes/event/readtracking_enabled.php [new file with mode: 0644]
mod/forum/classes/event/subscribers_viewed.php [new file with mode: 0644]
mod/forum/classes/event/subscription_created.php [new file with mode: 0644]
mod/forum/classes/event/subscription_deleted.php [new file with mode: 0644]
mod/forum/classes/event/userreport_viewed.php [new file with mode: 0644]
mod/forum/discuss.php
mod/forum/index.php
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/markposts.php
mod/forum/post.php
mod/forum/search.php
mod/forum/settracking.php
mod/forum/subscribe.php
mod/forum/subscribers.php
mod/forum/tests/events_test.php [new file with mode: 0644]
mod/forum/user.php
mod/forum/version.php
mod/forum/view.php

diff --git a/mod/forum/classes/event/course_module_instance_list_viewed.php b/mod/forum/classes/event/course_module_instance_list_viewed.php
new file mode 100644 (file)
index 0000000..d0bfc30
--- /dev/null
@@ -0,0 +1,38 @@
+<?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 instance list viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum instance list viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
+    // No need for any code here as everything is handled by the parent class.
+}
diff --git a/mod/forum/classes/event/course_searched.php b/mod/forum/classes/event/course_searched.php
new file mode 100644 (file)
index 0000000..1861c8d
--- /dev/null
@@ -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/>.
+
+/**
+ * The mod_forum course searched 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 course searched event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     -string searchterm: The searchterm used on forum search
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_searched extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        $searchterm = s($this->other['searchterm']);
+        return "The user {$this->userid} has searched the course {$this->courseid} for ".
+            "forum posts containing '{$searchterm}''";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventcoursesearched', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/search.php',
+            array('id' => $this->courseid, 'search' => $this->other['searchterm']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'search', $logurl, $this->other['searchterm']);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['searchterm'])) {
+            throw new \coding_exception('searchterm must be set in $other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_COURSE) {
+            throw new \coding_exception('Context passed must be course.');
+        }
+    }
+
+}
+
diff --git a/mod/forum/classes/event/discussion_created.php b/mod/forum/classes/event/discussion_created.php
new file mode 100644 (file)
index 0000000..a52ca51
--- /dev/null
@@ -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/>.
+
+/**
+ * The mod_forum discussion created event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum discussion created 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_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_discussions';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has created a discussion in the forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventdiscussioncreated', '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));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'add discussion', $logurl, $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in $other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('objectid must be set to the discussionid.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/discussion_deleted.php b/mod/forum/classes/event/discussion_deleted.php
new file mode 100644 (file)
index 0000000..425d017
--- /dev/null
@@ -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/>.
+
+/**
+ * The mod_forum discussion deleted 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 deleted 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_deleted extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $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 deleted the forum discussion {$this->objectid} ".
+            "in forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventdiscussiondeleted', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('id' => $this->contextinstanceid));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'delete discussion', 'view.php?id=' . $this->contextinstanceid,
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in $other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('objectid must be set to the discussionid.');
+        }
+    }
+}
+
diff --git a/mod/forum/classes/event/discussion_moved.php b/mod/forum/classes/event/discussion_moved.php
new file mode 100644 (file)
index 0000000..66f82a3
--- /dev/null
@@ -0,0 +1,114 @@
+<?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 moved 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 moved event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int fromforumid: The id of the forum the discussion is being moved from
+ *     - int toforumid: The id of the forum the discussion is being moved to
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class discussion_moved 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 moved the forum discussion {$this->objectid} ".
+            "from forum {$this->other['fromforumid']} to forum {$this->other['toforumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventdiscussionmoved', '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));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'move discussion', 'discuss.php?d=' . $this->objectid,
+            $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['fromforumid'])) {
+            throw new \coding_exception('fromforumid must be set in $other.');
+        }
+
+        if (!isset($this->other['toforumid'])) {
+            throw new \coding_exception('toforumid 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.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/discussion_updated.php b/mod/forum/classes/event/discussion_updated.php
new file mode 100644 (file)
index 0000000..6b84628
--- /dev/null
@@ -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.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/discussion_viewed.php b/mod/forum/classes/event/discussion_viewed.php
new file mode 100644 (file)
index 0000000..c1f911d
--- /dev/null
@@ -0,0 +1,105 @@
+<?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 viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum discussion viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class discussion_viewed extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+        $this->data['objecttable'] = 'forum_discussions';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has viewed the forum discussion {$this->objectid}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventdiscussionviewed', '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));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'view discussion', 'discuss.php?d=' . $this->objectid,
+            $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        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.');
+        }
+    }
+
+}
+
diff --git a/mod/forum/classes/event/forum_viewed.php b/mod/forum/classes/event/forum_viewed.php
new file mode 100644 (file)
index 0000000..2df0826
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum forum viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum forum viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class forum_viewed extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+        $this->data['objecttable'] = 'forum';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has viewed the forum {$this->objectid}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventforumviewed', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('d' => $this->objectid));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'view forum', 'view.php?f=' . $this->objectid,
+            $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        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.');
+        }
+
+    }
+
+}
+
diff --git a/mod/forum/classes/event/post_created.php b/mod/forum/classes/event/post_created.php
new file mode 100644 (file)
index 0000000..79aa997
--- /dev/null
@@ -0,0 +1,131 @@
+<?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 post created event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum post created event.
+ * @property-read array $other Extra information about the event.
+ *     - int discussionid: The discussion id the post is part of.
+ *     - int forumid: The forum id the post is part of.
+ *     - string forumtype: The type of forum the post is part of.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class post_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_posts';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has created a post in the discussion {$this->other['discussionid']} ".
+            " in forum {$this->other['forumid']}.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventpostcreated', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        if ($this->other['forumtype'] == 'single') {
+            // Single discussion forums are an exception. We show
+            // the forum itself since it only has one discussion
+            // thread.
+            $url = new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+        } else {
+            $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $this->other['discussionid']));
+        }
+        $url->set_anchor('p'.$this->objectid);
+        return $url;
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'add post', $logurl, $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('objectid must be set to the postid.');
+        }
+
+        if (!isset($this->other['discussionid'])) {
+            throw new \coding_exception('discussionid must be set in other.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if (!isset($this->other['forumtype'])) {
+            throw new \coding_exception('forumtype must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/post_deleted.php b/mod/forum/classes/event/post_deleted.php
new file mode 100644 (file)
index 0000000..a73fcc7
--- /dev/null
@@ -0,0 +1,130 @@
+<?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 post deleted 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 post deleted event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int discussionid: The discussion id the post is part of.
+ *     - int forumid: The forum id the post is part of.
+ *     - string forumtype: The type of forum the post is part of.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class post_deleted extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'forum_posts';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has deleted the post {$this->objectid} ".
+            " in discussion {$this->other['discussionid']}.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventpostdeleted', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        if ($this->other['forumtype'] == 'single') {
+            // Single discussion forums are an exception. We show
+            // the forum itself since it only has one discussion
+            // thread.
+            $url = new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+        } else {
+            $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $this->other['discussionid']));
+        }
+        return $url;
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'delete post', $logurl, $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('objectid must be set to the postid.');
+        }
+
+        if (!isset($this->other['discussionid'])) {
+            throw new \coding_exception('discussionid must be set in other.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if (!isset($this->other['forumtype'])) {
+            throw new \coding_exception('forumtype must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/post_updated.php b/mod/forum/classes/event/post_updated.php
new file mode 100644 (file)
index 0000000..5bb1f02
--- /dev/null
@@ -0,0 +1,131 @@
+<?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 post 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 post updated event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int discussionid: The discussion id the post is part of.
+ *     - int forumid: The forum id the post is part of.
+ *     - string forumtype: The type of forum the post is part of.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class post_updated extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+        $this->data['objecttable'] = 'forum_posts';
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has created updated the post {$this->objectid} ".
+            " in discussion {$this->other['discussionid']}.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventpostupdated', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        if ($this->other['forumtype'] == 'single') {
+            // Single discussion forums are an exception. We show
+            // the forum itself since it only has one discussion
+            // thread.
+            $url = new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+        } else {
+            $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $this->other['discussionid']));
+        }
+        $url->set_anchor('p'.$this->objectid);
+        return $url;
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'update post', $logurl, $this->objectid, $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('objectid must be set to the postid.');
+        }
+
+        if (!isset($this->other['discussionid'])) {
+            throw new \coding_exception('discussionid must be set in other.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if (!isset($this->other['forumtype'])) {
+            throw new \coding_exception('forumtype must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/readtracking_disabled.php b/mod/forum/classes/event/readtracking_disabled.php
new file mode 100644 (file)
index 0000000..f44f1f1
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int forumid: The id of the forum which readtracking has been disabled on.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class readtracking_disabled extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "Read tracking has been disabled for user {$this->relateduserid} in forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventreadtrackingdisabled', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'stop tracking', 'view.php?f=' . $this->other['forumid'],
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('relateduserid must be set.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/readtracking_enabled.php b/mod/forum/classes/event/readtracking_enabled.php
new file mode 100644 (file)
index 0000000..1d1f314
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum read tracking enabled 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 read tracking enabled event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int forumid: The id of the forum which readtracking has been enabled on.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class readtracking_enabled extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "Read tracking has been enabled for user {$this->relateduserid} in forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventreadtrackingenabled', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'start tracking', 'view.php?f=' . $this->other['forumid'],
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('relateduserid must be set.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/subscribers_viewed.php b/mod/forum/classes/event/subscribers_viewed.php
new file mode 100644 (file)
index 0000000..c3e8a3a
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum subscribers list viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum subscribers list viewed event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int forumid: The id of the forum which the subscriberslist has been viewed.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class subscribers_viewed extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has viewed the subscribers list for forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventsubscribersviewed', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/subscribers.php', array('id' => $this->other['forumid']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'view subscribers', 'subscribers.php?id=' . $this->other['forumid'],
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+
+}
+
diff --git a/mod/forum/classes/event/subscription_created.php b/mod/forum/classes/event/subscription_created.php
new file mode 100644 (file)
index 0000000..94c1c61
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int forumid: The id of the forum which has been subscribed to.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class subscription_created extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->relateduserid} was subscribed to the forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventsubscriptioncreated', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'subscribe', 'view.php?f=' . $this->other['forumid'],
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('relateduserid must be set.');
+        }
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/subscription_deleted.php b/mod/forum/classes/event/subscription_deleted.php
new file mode 100644 (file)
index 0000000..73832ce
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum subscription created event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - int forumid: The id of the forum which has been unsusbcribed from.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class subscription_deleted extends \core\event\base {
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->relateduserid} was unsubscribed the forum {$this->other['forumid']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventsubscriptiondeleted', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/mod/forum/view.php', array('id' => $this->other['forumid']));
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        return array($this->courseid, 'forum', 'unsubscribe', 'view.php?f=' . $this->other['forumid'],
+            $this->other['forumid'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('relateduserid must be set.');
+        }
+
+        if (!isset($this->other['forumid'])) {
+            throw new \coding_exception('forumid must be set in other.');
+        }
+
+        if ($this->contextlevel != CONTEXT_MODULE) {
+            throw new \coding_exception('Context passed must be module context.');
+        }
+    }
+}
diff --git a/mod/forum/classes/event/userreport_viewed.php b/mod/forum/classes/event/userreport_viewed.php
new file mode 100644 (file)
index 0000000..91b3cc9
--- /dev/null
@@ -0,0 +1,127 @@
+<?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 userreport viewed event.
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace mod_forum\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * The mod_forum userreport viewed event.
+ *
+ * @property-read array $other Extra information about the event.
+ *     - string reportmode: The mode the report has been viewed in (posts or discussions).
+ *
+ * @package    mod_forum
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class userreport_viewed extends \core\event\base {
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user {$this->userid} has viewed the userreport for user {$this->relateduserid} in ".
+            "course {$this->courseid} with viewing mode {$this->other['reportmode']}";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventuserreportviewed', 'mod_forum');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+
+        $url = new \moodle_url('/mod/forum/user.php', array('id' => $this->relateduserid,
+            'mode' => $this->other['reportmode']));
+
+        if ($this->courseid != SITEID) {
+            $url->param('course', $this->courseid);
+        }
+
+        return $url;
+    }
+
+    /**
+     * Return the legacy event log data.
+     *
+     * @return array|null
+     */
+    protected function get_legacy_logdata() {
+        // The legacy log table expects a relative path to /mod/forum/.
+        $logurl = substr($this->get_url()->out_as_local_url(), strlen('/mod/forum/'));
+
+        return array($this->courseid, 'forum', 'user report', $logurl, $this->relateduserid);
+    }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('relateduserid must be set.');
+        }
+        if (!isset($this->other['reportmode'])) {
+            throw new \coding_exception('reportmode must be set in other.');
+        }
+
+        switch ($this->contextlevel)
+        {
+            case CONTEXT_COURSE:
+            case CONTEXT_SYSTEM:
+                // OK, expected context level.
+                break;
+            default:
+                // Unexpected contextlevel.
+                throw new \coding_exception('Context passed must be system or course.');
+        }
+    }
+
+}
+
index fc821f5..d7e522e 100644 (file)
             print_error('cannotmovenotvisible', 'forum', $return);
         }
 
-        require_capability('mod/forum:startdiscussion', context_module::instance($cmto->id));
+        $destinationctx = context_module::instance($cmto->id);
+        require_capability('mod/forum:startdiscussion', $destinationctx);
 
         if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
             echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
         }
         $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
         $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
-        add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id);
+
+        $params = array(
+            'context' => $destinationctx,
+            'objectid' => $discussion->id,
+            'other' => array(
+                'fromforumid' => $forum->id,
+                'toforumid' => $forumto->id,
+            )
+        );
+        $event = \mod_forum\event\discussion_moved::create($params);
+        $event->add_record_snapshot('forum_discussions', $discussion);
+        $event->add_record_snapshot('forum', $forum);
+        $event->add_record_snapshot('forum', $forumto);
+        $event->trigger();
 
         // Delete the RSS files for the 2 forums to force regeneration of the feeds
         require_once($CFG->dirroot.'/mod/forum/rsslib.php');
         redirect($return.'&moved=-1&sesskey='.sesskey());
     }
 
-    add_to_log($course->id, 'forum', 'view discussion', "discuss.php?d=$discussion->id", $discussion->id, $cm->id);
+    $params = array(
+        'context' => $modcontext,
+        'objectid' => $discussion->id,
+    );
+    $event = \mod_forum\event\discussion_viewed::create($params);
+    $event->add_record_snapshot('forum_discussions', $discussion);
+    $event->add_record_snapshot('forum', $forum);
+    $event->trigger();
 
     unset($SESSION->fromdiscussion);
 
index f86c646..e282ddb 100644 (file)
@@ -51,7 +51,11 @@ $coursecontext = context_course::instance($course->id);
 
 unset($SESSION->fromdiscussion);
 
-add_to_log($course->id, 'forum', 'view forums', "index.php?id=$course->id");
+$params = array(
+    'context' => context_course::instance($course->id)
+);
+$event = \mod_forum\event\course_module_instance_list_viewed::create($params);
+$event->trigger();
 
 $strforums       = get_string('forums', 'forum');
 $strforum        = get_string('forum', 'forum');
@@ -197,10 +201,8 @@ if (!is_null($subscribe)) {
     $returnto = forum_go_back_to("index.php?id=$course->id");
     $shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
     if ($subscribe) {
-        add_to_log($course->id, 'forum', 'subscribeall', "index.php?id=$course->id", $course->id);
         redirect($returnto, get_string('nowallsubscribed', 'forum', $shortname), 1);
     } else {
-        add_to_log($course->id, 'forum', 'unsubscribeall', "index.php?id=$course->id", $course->id);
         redirect($returnto, get_string('nowallunsubscribed', 'forum', $shortname), 1);
     }
 }
index 7b6c321..8267591 100644 (file)
@@ -144,6 +144,22 @@ $string['edit'] = 'Edit';
 $string['editedby'] = 'Edited by {$a->name} - original submission {$a->date}';
 $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';
+$string['eventforumviewed'] = 'Forum viewed';
+$string['eventuserreportviewed'] = 'User report viewed';
+$string['eventpostcreated'] = 'Post created';
+$string['eventpostdeleted'] = 'Post deleted';
+$string['eventpostupdated'] = 'Post updated';
+$string['eventreadtrackingdisabled'] = 'Read tracking disabled';
+$string['eventreadtrackingenabled'] = 'Read tracking enabled';
+$string['eventsubscribersviewed'] = 'Subscribers viewed';
+$string['eventsubscriptioncreated'] = 'Subscription created';
+$string['eventsubscriptiondeleted'] = 'Subscription deleted';
 $string['emaildigestcompleteshort'] = 'Complete posts';
 $string['emaildigestdefault'] = 'Default ({$a})';
 $string['emaildigestoffshort'] = 'No digest';
index 2bad212..a66bcf2 100644 (file)
@@ -773,8 +773,6 @@ function forum_cron() {
                 if (!$mailresult){
                     mtrace("Error: mod/forum/lib.php forum_cron(): Could not send out mail for id $post->id to user $userto->id".
                          " ($userto->email) .. not trying again.");
-                    add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#p$post->id",
-                               substr(format_string($post->subject,true),0,30), $cm->id, $userto->id);
                     $errorcount[$post->id]++;
                 } else {
                     $mailcount[$post->id]++;
@@ -1073,9 +1071,8 @@ function forum_cron() {
                 $mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml, $attachment, $attachname);
 
                 if (!$mailresult) {
-                    mtrace("ERROR!");
-                    echo "Error: mod/forum/cron.php: Could not send out digest mail to user $userto->id ($userto->email)... not trying again.\n";
-                    add_to_log($course->id, 'forum', 'mail digest error', '', '', $cm->id, $userto->id);
+                    mtrace("ERROR: mod/forum/cron.php: Could not send out digest mail to user $userto->id ".
+                        "($userto->email)... not trying again.");
                 } else {
                     mtrace("success.");
                     $usermailcount++;
@@ -4818,7 +4815,19 @@ function forum_subscribe($userid, $forumid) {
     $sub->userid  = $userid;
     $sub->forum = $forumid;
 
-    return $DB->insert_record("forum_subscriptions", $sub);
+    $result = $DB->insert_record("forum_subscriptions", $sub);
+
+    $cm = get_coursemodule_from_instance('forum', $forumid);
+    $params = array(
+        'context' => context_module::instance($cm->id),
+        'relateduserid' => $userid,
+        'other' => array('forumid' => $forumid),
+
+    );
+    $event  = \mod_forum\event\subscription_created::create($params);
+    $event->trigger();
+
+    return $result;
 }
 
 /**
@@ -4830,8 +4839,21 @@ function forum_subscribe($userid, $forumid) {
  */
 function forum_unsubscribe($userid, $forumid) {
     global $DB;
-    return ($DB->delete_records('forum_digests', array('userid' => $userid, 'forum' => $forumid))
-            && $DB->delete_records('forum_subscriptions', array('userid' => $userid, 'forum' => $forumid)));
+
+    $DB->delete_records('forum_digests', array('userid' => $userid, 'forum' => $forumid));
+    $DB->delete_records('forum_subscriptions', array('userid' => $userid, 'forum' => $forumid));
+
+    $cm = get_coursemodule_from_instance('forum', $forumid);
+    $params = array(
+        'context' => context_module::instance($cm->id),
+        'relateduserid' => $userid,
+        'other' => array('forumid' => $forumid),
+
+    );
+    $event = \mod_forum\event\subscription_deleted::create($params);
+    $event->trigger();
+
+    return true;
 }
 
 /**
index a47f049..b26278a 100644 (file)
@@ -81,9 +81,7 @@ if ($mark == 'read') {
             print_error('invaliddiscussionid', 'forum');
         }
 
-        if (forum_tp_mark_discussion_read($user, $d)) {
-            add_to_log($course->id, "discussion", "mark read", "view.php?f=$forum->id", $d, $cm->id);
-        }
+        forum_tp_mark_discussion_read($user, $d);
     } else {
         // Mark all messages read in current group
         $currentgroup = groups_get_activity_group($cm);
@@ -92,15 +90,12 @@ if ($mark == 'read') {
             // may return 0
             $currentgroup=false;
         }
-        if (forum_tp_mark_forum_read($user, $forum->id,$currentgroup)) {
-            add_to_log($course->id, "forum", "mark read", "view.php?f=$forum->id", $forum->id, $cm->id);
-        }
+        forum_tp_mark_forum_read($user, $forum->id, $currentgroup);
     }
 
 /// FUTURE - Add ability to mark them as unread.
 //    } else { // subscribe
 //        if (forum_tp_start_tracking($forum->id, $user->id)) {
-//            add_to_log($course->id, "forum", "mark unread", "view.php?f=$forum->id", $forum->id, $cm->id);
 //            redirect($returnto, get_string("nowtracking", "forum", $info), 1);
 //        } else {
 //            print_error("Could not start tracking that forum", $_SERVER["HTTP_REFERER"]);
index f1e9988..ad1deab 100644 (file)
@@ -335,8 +335,17 @@ if (!empty($forum)) {      // User is starting a new discussion in a forum
                 }
                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
 
-                add_to_log($discussion->course, "forum", "delete discussion",
-                           "view.php?id=$cm->id", "$forum->id", $cm->id);
+                $params = array(
+                    'objectid' => $discussion->id,
+                    'context' => $modcontext,
+                    'other' => array(
+                        'forumid' => $forum->id,
+                    )
+                );
+
+                $event = \mod_forum\event\discussion_deleted::create($params);
+                $event->add_record_snapshot('forum_discussions', $discussion);
+                $event->trigger();
 
                 redirect("view.php?f=$discussion->forum");
 
@@ -352,7 +361,23 @@ if (!empty($forum)) {      // User is starting a new discussion in a forum
                     $discussionurl = "discuss.php?d=$post->discussion";
                 }
 
-                add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
+                $params = array(
+                    'context' => $modcontext,
+                    'objectid' => $post->id,
+                    'other' => array(
+                        'discussionid' => $discussion->id,
+                        'forumid' => $forum->id,
+                        'forumtype' => $forum->type,
+                    )
+                );
+
+                if ($post->userid !== $USER->id) {
+                    $params['relateduserid'] = $post->userid;
+                }
+                $event = \mod_forum\event\post_deleted::create($params);
+                $event->add_record_snapshot('forum_posts', $post);
+                $event->add_record_snapshot('forum_discussions', $discussion);
+                $event->trigger();
 
                 redirect(forum_go_back_to($discussionurl));
             } else {
@@ -455,8 +480,39 @@ if (!empty($forum)) {      // User is starting a new discussion in a forum
         forum_discussion_update_last_post($discussion->id);
         forum_discussion_update_last_post($newid);
 
-        add_to_log($discussion->course, "forum", "prune post",
-                       "discuss.php?d=$newid", "$post->id", $cm->id);
+        // 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,
+            'other' => array(
+                'forumid' => $forum->id,
+            )
+        );
+        $event = \mod_forum\event\discussion_created::create($params);
+        $event->trigger();
+
+        $params = array(
+            'context' => $modcontext,
+            'objectid' => $post->id,
+            'other' => array(
+                'discussionid' => $newid,
+                'forumid' => $forum->id,
+                'forumtype' => $forum->type,
+            )
+        );
+        $event = \mod_forum\event\post_updated::create($params);
+        $event->add_record_snapshot('forum_discussions', $discussion);
+        $event->trigger();
 
         redirect(forum_go_back_to("discuss.php?d=$newid"));
 
@@ -683,8 +739,25 @@ if ($fromform = $mform_post->get_data()) {
         } else {
             $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
         }
-        add_to_log($course->id, "forum", "update post",
-                "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
+
+        $params = array(
+            'context' => $modcontext,
+            'objectid' => $fromform->id,
+            'other' => array(
+                'discussionid' => $discussion->id,
+                'forumid' => $forum->id,
+                'forumtype' => $forum->type,
+            )
+        );
+
+        if ($realpost->userid !== $USER->id) {
+            $params['relateduserid'] = $realpost->userid;
+        }
+
+        $event = \mod_forum\event\post_updated::create($params);
+        $event->add_record_snapshot('forum_posts', $fromform);
+        $event->add_record_snapshot('forum_discussions', $discussion);
+        $event->trigger();
 
         redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
 
@@ -726,8 +799,20 @@ if ($fromform = $mform_post->get_data()) {
             } else {
                 $discussionurl = "discuss.php?d=$discussion->id";
             }
-            add_to_log($course->id, "forum", "add post",
-                      "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
+
+            $params = array(
+                'context' => $modcontext,
+                'objectid' => $fromform->id,
+                'other' => array(
+                    'discussionid' => $discussion->id,
+                    'forumid' => $forum->id,
+                    'forumtype' => $forum->type,
+                )
+            );
+            $event = \mod_forum\event\post_created::create($params);
+            $event->add_record_snapshot('forum_posts', $fromform);
+            $event->add_record_snapshot('forum_discussions', $discussion);
+            $event->trigger();
 
             // Update completion state
             $completion=new completion_info($course);
@@ -773,8 +858,16 @@ if ($fromform = $mform_post->get_data()) {
         $message = '';
         if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
 
-            add_to_log($course->id, "forum", "add discussion",
-                    "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
+            $params = array(
+                'context' => $modcontext,
+                'objectid' => $discussion->id,
+                'other' => array(
+                    'forumid' => $forum->id,
+                )
+            );
+            $event = \mod_forum\event\discussion_created::create($params);
+            $event->add_record_snapshot('forum_discussions', $discussion);
+            $event->trigger();
 
             $timemessage = 2;
             if (!empty($message)) { // if we're printing stuff about the file upload
index c7cc504..4f257b1 100644 (file)
@@ -112,7 +112,13 @@ if (!$course = $DB->get_record('course', array('id'=>$id))) {
 
 require_course_login($course);
 
-add_to_log($course->id, "forum", "search", "search.php?id=$course->id&amp;search=".urlencode($search), $search);
+$params = array(
+    'context' => $PAGE->context,
+    'other' => array('searchterm' => $search)
+);
+
+$event = \mod_forum\event\course_searched::create($params);
+$event->trigger();
 
 $strforums = get_string("modulenameplural", "forum");
 $strsearch = get_string("search", "forum");
index 9a39722..f8b8587 100644 (file)
@@ -58,9 +58,17 @@ if (!forum_tp_can_track_forums($forum)) {
 $info = new stdClass();
 $info->name  = fullname($USER);
 $info->forum = format_string($forum->name);
+
+$eventparams = array(
+    'context' => context_module::instance($cm->id),
+    'relateduserid' => $USER->id,
+    'other' => array('forumid' => $forum->id),
+);
+
 if (forum_tp_is_tracked($forum) ) {
     if (forum_tp_stop_tracking($forum->id)) {
-        add_to_log($course->id, "forum", "stop tracking", "view.php?f=$forum->id", $forum->id, $cm->id);
+        $event = \mod_forum\event\readtracking_disabled::create($eventparams);
+        $event->trigger();
         redirect($returnto, get_string("nownottracking", "forum", $info), 1);
     } else {
         print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);
@@ -68,7 +76,8 @@ if (forum_tp_is_tracked($forum) ) {
 
 } else { // subscribe
     if (forum_tp_start_tracking($forum->id)) {
-        add_to_log($course->id, "forum", "start tracking", "view.php?f=$forum->id", $forum->id, $cm->id);
+        $event = \mod_forum\event\readtracking_enabled::create($eventparams);
+        $event->trigger();
         redirect($returnto, get_string("nowtracking", "forum", $info), 1);
     } else {
         print_error('cannottrack', '', $_SERVER["HTTP_REFERER"]);
index 1bd5277..37aea54 100644 (file)
@@ -149,7 +149,6 @@ if (forum_is_subscribed($user->id, $forum->id)) {
     }
     require_sesskey();
     if (forum_unsubscribe($user->id, $forum->id)) {
-        add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
         redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1);
     } else {
         print_error('cannotunsubscribe', 'forum', $_SERVER["HTTP_REFERER"]);
@@ -174,6 +173,5 @@ if (forum_is_subscribed($user->id, $forum->id)) {
     }
     require_sesskey();
     forum_subscribe($user->id, $forum->id);
-    add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id);
     redirect($returnto, get_string("nowsubscribed", "forum", $info), 1);
 }
index 87c3b54..eda984f 100644 (file)
@@ -54,7 +54,12 @@ if (!has_capability('mod/forum:viewsubscribers', $context)) {
 
 unset($SESSION->fromdiscussion);
 
-add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id);
+$params = array(
+    'context' => $context,
+    'other' => array('forumid' => $forum->id),
+);
+$event = \mod_forum\event\subscribers_viewed::create($params);
+$event->trigger();
 
 $forumoutput = $PAGE->get_renderer('mod_forum');
 $currentgroup = groups_get_activity_group($cm);
diff --git a/mod/forum/tests/events_test.php b/mod/forum/tests/events_test.php
new file mode 100644 (file)
index 0000000..105b42a
--- /dev/null
@@ -0,0 +1,1750 @@
+<?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/>.
+
+/**
+ * Tests for forum events.
+ *
+ * @package    mod_forum
+ * @category   test
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Tests for forum events.
+ *
+ * @package    mod_forum
+ * @category   test
+ * @copyright  2014 Dan Poltawski <dan@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class mod_forum_events_testcase extends advanced_testcase {
+
+    /**
+     * Tests set up.
+     */
+    public function setUp() {
+        $this->resetAfterTest();
+    }
+
+    /**
+     * Ensure course_searched event validates that searchterm is set.
+     */
+    public function test_course_searched_searchterm_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $coursectx = context_course::instance($course->id);
+        $params = array(
+            'context' => $coursectx,
+        );
+
+        $this->setExpectedException('coding_exception', 'searchterm must be set in $other.');
+        \mod_forum\event\course_searched::create($params);
+    }
+
+    /**
+     * Ensure course_searched event validates that context is the correct level.
+     */
+    public function test_course_searched_context_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('searchterm' => 'testing'),
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be course.');
+        \mod_forum\event\course_searched::create($params);
+    }
+
+    /**
+     * Test course_searched event.
+     */
+    public function test_course_searched() {
+
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $coursectx = context_course::instance($course->id);
+        $searchterm = 'testing123';
+
+        $params = array(
+            'context' => $coursectx,
+            'other' => array('searchterm' => $searchterm),
+        );
+
+        // Create event.
+        $event = \mod_forum\event\course_searched::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+         // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
+        $this->assertEquals($coursectx, $event->get_context());
+        $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure discussion_created event validates that forumid is set.
+     */
+    public function test_discussion_created_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_created::create($params);
+    }
+
+    /**
+     * Ensure discussion_created event validates that discussionid is set.
+     */
+    public function test_discussion_created_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_created::create($params);
+    }
+
+    /**
+     * Ensure discussion_created event validates that the context is the correct level.
+     */
+    public function test_discussion_created_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_created::create($params);
+    }
+
+    /**
+     * Test discussion_created event.
+     */
+    public function test_discussion_created() {
+
+        // 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_created::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_created', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $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.
+     */
+    public function test_discussion_deleted_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_deleted::create($params);
+    }
+
+    /**
+     * Ensure discussion_deleted event validates that discussionid is set.
+     */
+    public function test_discussion_deleted_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_deleted::create($params);
+    }
+
+    /**
+     * Ensure discussion_deleted event validates that context is of the correct level.
+     */
+    public function test_discussion_deleted_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_deleted::create($params);
+    }
+
+    /**
+     * Test discussion_deleted event.
+     */
+    public function test_discussion_deleted() {
+
+        // 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),
+        );
+
+        $event = \mod_forum\event\discussion_deleted::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $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_deleted', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure discussion_moved event validates that fromforumid is set.
+     */
+    public function test_discussion_moved_fromforumid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $context = context_module::instance($toforum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'other' => array('toforumid' => $toforum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'fromforumid must be set in $other.');
+        \mod_forum\event\discussion_moved::create($params);
+    }
+
+    /**
+     * Ensure discussion_moved event validates that toforumid is set.
+     */
+    public function test_discussion_moved_toforumid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $context = context_module::instance($toforum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'other' => array('fromforumid' => $fromforum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'toforumid must be set in $other.');
+        \mod_forum\event\discussion_moved::create($params);
+    }
+
+    /**
+     * Ensure discussion_moved event validates that the discussionid is set.
+     */
+    public function test_discussion_moved_objectid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $context = context_module::instance($toforum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
+        \mod_forum\event\discussion_moved::create($params);
+    }
+
+    /**
+     * Ensure discussion_moved event validates that the context level is correct.
+     */
+    public function test_discussion_moved_context_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $toforum = $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'] = $fromforum->id;
+        $record['userid'] = $user->id;
+        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $discussion->id,
+            'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\discussion_moved::create($params);
+    }
+
+    /**
+     * Test discussion_moved event.
+     */
+    public function test_discussion_moved() {
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+        $toforum = $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'] = $fromforum->id;
+        $record['userid'] = $user->id;
+        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+        $context = context_module::instance($toforum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'objectid' => $discussion->id,
+            'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
+        );
+
+        $event = \mod_forum\event\discussion_moved::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $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_moved', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
+            $discussion->id, $toforum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure discussion_viewed event validates that the discussionid is set
+     */
+    public function test_discussion_viewed_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,
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
+        \mod_forum\event\discussion_viewed::create($params);
+    }
+
+    /**
+     * Ensure discussion_viewed event validates that the contextlevel is correct.
+     */
+    public function test_discussion_viewed_context_validation() {
+        $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);
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $discussion->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\discussion_viewed::create($params);
+    }
+
+    /**
+     * Test discussion_viewed event.
+     */
+    public function test_discussion_viewed() {
+        // 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,
+        );
+
+        $event = \mod_forum\event\discussion_viewed::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $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_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
+            $discussion->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure forum_viewed event validates that the forumid is set.
+     */
+    public function test_forum_viewed_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,
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the forumid.');
+        \mod_forum\event\forum_viewed::create($params);
+    }
+
+    /**
+     * Ensure forum_viewed event validates that the contextlevel is correct.
+     */
+    public function test_forum_viewed_context_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $forum->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\forum_viewed::create($params);
+    }
+
+    /**
+     * Test the forum_viewed event.
+     */
+    public function test_forum_viewed() {
+        // Setup test data.
+        $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,
+            'objectid' => $forum->id,
+        );
+
+        $event = \mod_forum\event\forum_viewed::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\forum_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure subscription_created event validates that the forumid is set.
+     */
+    public function test_subscription_created_forumid_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\subscription_created::create($params);
+    }
+
+    /**
+     * Ensure subscription_created event validates that the relateduserid is set.
+     */
+    public function test_subscription_created_relateduserid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $forum->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'relateduserid must be set.');
+        \mod_forum\event\subscription_created::create($params);
+    }
+
+    /**
+     * Ensure subscription_created event validates that the contextlevel is correct.
+     */
+    public function test_subscription_created_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\subscription_created::create($params);
+    }
+
+    /**
+     * Test the subscription_created event.
+     */
+    public function test_subscription_created() {
+        // Setup test data.
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $event = \mod_forum\event\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);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure subscription_deleted event validates that the forumid is set.
+     */
+    public function test_subscription_deleted_forumid_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\subscription_deleted::create($params);
+    }
+
+    /**
+     * Ensure subscription_deleted event validates that the relateduserid is set.
+     */
+    public function test_subscription_deleted_relateduserid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $forum->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'relateduserid must be set.');
+        \mod_forum\event\subscription_deleted::create($params);
+    }
+
+    /**
+     * Ensure subscription_deleted event validates that the contextlevel is correct.
+     */
+    public function test_subscription_deleted_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\subscription_deleted::create($params);
+    }
+
+    /**
+     * Test the subscription_deleted event.
+     */
+    public function test_subscription_deleted() {
+        // Setup test data.
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $event = \mod_forum\event\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);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     * Ensure readtracking_enabled event validates that the forumid is set.
+     */
+    public function test_readtracking_enabled_forumid_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\readtracking_enabled::create($params);
+    }
+
+    /**
+     * Ensure readtracking_enabled event validates that the relateduserid is set.
+     */
+    public function test_readtracking_enabled_relateduserid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $forum->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'relateduserid must be set.');
+        \mod_forum\event\readtracking_enabled::create($params);
+    }
+
+    /**
+     * Ensure readtracking_enabled event validates that the contextlevel is correct.
+     */
+    public function test_readtracking_enabled_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\readtracking_enabled::create($params);
+    }
+
+    /**
+     * Test the readtracking_enabled event.
+     */
+    public function test_readtracking_enabled() {
+        // Setup test data.
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $event = \mod_forum\event\readtracking_enabled::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure readtracking_disabled event validates that the forumid is set.
+     */
+    public function test_readtracking_disabled_forumid_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\readtracking_disabled::create($params);
+    }
+
+    /**
+     *  Ensure readtracking_disabled event validates that the relateduserid is set.
+     */
+    public function test_readtracking_disabled_relateduserid_validation() {
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $forum->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'relateduserid must be set.');
+        \mod_forum\event\readtracking_disabled::create($params);
+    }
+
+    /**
+     *  Ensure readtracking_disabled event validates that the contextlevel is correct
+     */
+    public function test_readtracking_disabled_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\readtracking_disabled::create($params);
+    }
+
+    /**
+     *  Test the readtracking_disabled event.
+     */
+    public function test_readtracking_disabled() {
+        // Setup test data.
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $event = \mod_forum\event\readtracking_disabled::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure subscribers_viewed event validates that the forumid is set.
+     */
+    public function test_subscribers_viewed_forumid_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\subscribers_viewed::create($params);
+    }
+
+    /**
+     *  Ensure subscribers_viewed event validates that the contextlevel is correct.
+     */
+    public function test_subscribers_viewed_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $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),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context.');
+        \mod_forum\event\subscribers_viewed::create($params);
+    }
+
+    /**
+     *  Test the subscribers_viewed event.
+     */
+    public function test_subscribers_viewed() {
+        // Setup test data.
+        $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),
+        );
+
+        $event = \mod_forum\event\subscribers_viewed::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure userreport_viewed event validates that the reportmode is set.
+     */
+    public function test_userreport_viewed_reportmode_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+
+        $params = array(
+            'context' => context_course::instance($course->id),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'reportmode must be set in other.');
+        \mod_forum\event\userreport_viewed::create($params);
+    }
+
+    /**
+     *  Ensure userreport_viewed event validates that the contextlevel is correct.
+     */
+    public function test_userreport_viewed_contextlevel_validation() {
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
+
+        $params = array(
+            'context' => context_module::instance($forum->id),
+            'other' => array('reportmode' => 'posts'),
+            'relateduserid' => $user->id,
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be system or course.');
+        \mod_forum\event\userreport_viewed::create($params);
+    }
+
+    /**
+     *  Ensure userreport_viewed event validates that the relateduserid is set.
+     */
+    public function test_userreport_viewed_relateduserid_validation() {
+
+        $params = array(
+            'context' => context_system::instance(),
+            'other' => array('reportmode' => 'posts'),
+        );
+
+        $this->setExpectedException('coding_exception', 'relateduserid must be set.');
+        \mod_forum\event\userreport_viewed::create($params);
+    }
+
+    /**
+     * Test the userreport_viewed event.
+     */
+    public function test_userreport_viewed() {
+        // Setup test data.
+        $user = $this->getDataGenerator()->create_user();
+        $course = $this->getDataGenerator()->create_course();
+        $context = context_course::instance($course->id);
+
+        $params = array(
+            'context' => $context,
+            'relateduserid' => $user->id,
+            'other' => array('reportmode' => 'discussions'),
+        );
+
+        $event = \mod_forum\event\userreport_viewed::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\userreport_viewed', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'user report',
+            "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure post_created event validates that the postid is set.
+     */
+    public function test_post_created_postid_validation() {
+        $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);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
+        \mod_forum\event\post_created::create($params);
+    }
+
+    /**
+     *  Ensure post_created event validates that the discussionid is set.
+     */
+    public function test_post_created_discussionid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
+        \mod_forum\event\post_created::create($params);
+    }
+
+    /**
+     *  Ensure post_created event validates that the forumid is set.
+     */
+    public function test_post_created_forumid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\post_created::create($params);
+    }
+
+    /**
+     *  Ensure post_created event validates that the forumtype is set.
+     */
+    public function test_post_created_forumtype_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
+        \mod_forum\event\post_created::create($params);
+    }
+
+    /**
+     *  Ensure post_created event validates that the contextlevel is correct.
+     */
+    public function test_post_created_context_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context');
+        \mod_forum\event\post_created::create($params);
+    }
+
+    /**
+     * Test the post_created event.
+     */
+    public function test_post_created() {
+        // 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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $context = context_module::instance($forum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $event = \mod_forum\event\post_created::create($params);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\post_created', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
+            $forum->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure post_deleted event validates that the postid is set.
+     */
+    public function test_post_deleted_postid_validation() {
+        $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);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
+        \mod_forum\event\post_deleted::create($params);
+    }
+
+    /**
+     *  Ensure post_deleted event validates that the discussionid is set.
+     */
+    public function test_post_deleted_discussionid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
+        \mod_forum\event\post_deleted::create($params);
+    }
+
+    /**
+     *  Ensure post_deleted event validates that the forumid is set.
+     */
+    public function test_post_deleted_forumid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\post_deleted::create($params);
+    }
+
+    /**
+     *  Ensure post_deleted event validates that the forumtype is set.
+     */
+    public function test_post_deleted_forumtype_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
+        \mod_forum\event\post_deleted::create($params);
+    }
+
+    /**
+     *  Ensure post_deleted event validates that the contextlevel is correct.
+     */
+    public function test_post_deleted_context_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context');
+        \mod_forum\event\post_deleted::create($params);
+    }
+
+    /**
+     * Test post_deleted event.
+     */
+    public function test_post_deleted() {
+        // 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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $context = context_module::instance($forum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $event = \mod_forum\event\post_deleted::create($params);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+
+    /**
+     *  Ensure post_updated event validates that the postid is set.
+     */
+    public function test_post_updated_postid_validation() {
+        $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);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
+        \mod_forum\event\post_updated::create($params);
+    }
+
+    /**
+     *  Ensure post_updated event validates that the discussionid is set.
+     */
+    public function test_post_updated_discussionid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
+        \mod_forum\event\post_updated::create($params);
+    }
+
+    /**
+     *  Ensure post_updated event validates that the forumid is set.
+     */
+    public function test_post_updated_forumid_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumid must be set in other.');
+        \mod_forum\event\post_updated::create($params);
+    }
+
+    /**
+     *  Ensure post_updated event validates that the forumtype is set.
+     */
+    public function test_post_updated_forumtype_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_module::instance($forum->cmid),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
+        );
+
+        $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
+        \mod_forum\event\post_updated::create($params);
+    }
+
+    /**
+     *  Ensure post_updated event validates that the contextlevel is correct.
+     */
+    public function test_post_updated_context_validation() {
+        $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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $params = array(
+            'context' => context_system::instance(),
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $this->setExpectedException('coding_exception', 'Context passed must be module context');
+        \mod_forum\event\post_updated::create($params);
+    }
+
+    /**
+     * Test post_updated event.
+     */
+    public function test_post_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);
+
+        // Add a post.
+        $record = array();
+        $record['discussion'] = $discussion->id;
+        $record['userid'] = $user->id;
+        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
+
+        $context = context_module::instance($forum->cmid);
+
+        $params = array(
+            'context' => $context,
+            'objectid' => $post->id,
+            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
+        );
+
+        $event = \mod_forum\event\post_updated::create($params);
+
+        // Trigger and capturing the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
+        $this->assertEquals($context, $event->get_context());
+        $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
+            $post->id, $forum->cmid);
+        $this->assertEventLegacyLogData($expected, $event);
+        $this->assertEventContextNotUsed($event);
+
+        $this->assertNotEmpty($event->get_name());
+    }
+}
index 0059ae8..6c5c3c5 100644 (file)
@@ -62,8 +62,6 @@ if ($perpage != 5) {
     $url->param('perpage', $perpage);
 }
 
-add_to_log(($isspecificcourse)?$courseid:SITEID, "forum", "user report", 'user.php?'.$url->get_query_string(), $userid);
-
 $user = $DB->get_record("user", array("id" => $userid), '*', MUST_EXIST);
 $usercontext = context_user::instance($user->id, MUST_EXIST);
 // Check if the requested user is the guest user
@@ -117,6 +115,15 @@ if ($isspecificcourse) {
     $courses = forum_get_courses_user_posted_in($user, $discussionsonly);
 }
 
+
+$params = array(
+    'context' => $PAGE->context,
+    'relateduserid' => $user->id,
+    'other' => array('reportmode' => $mode),
+);
+$event = \mod_forum\event\userreport_viewed::create($params);
+$event->trigger();
+
 // Get the posts by the requested user that the current user can access.
 $result = forum_get_posts_by_user($user, $courses, $isspecificcourse, $discussionsonly, ($page * $perpage), $perpage);
 
index 8f874d5..1a966f2 100644 (file)
@@ -25,7 +25,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2013110500;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2014021700;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2013110500;       // Requires this Moodle version
 $plugin->component = 'mod_forum';      // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 60;
index ec69112..8081f64 100644 (file)
 /// find out current groups mode
     groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/forum/view.php?id=' . $cm->id);
 
-/// Okay, we can show the discussions. Log the forum view.
-    if ($cm->id) {
-        add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id);
-    } else {
-        add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id");
-    }
+    $params = array(
+        'context' => $context,
+        'objectid' => $forum->id
+    );
+    $event = \mod_forum\event\forum_viewed::create($params);
+    $event->add_record_snapshot('forum', $forum);
+    $event->trigger();
 
     $SESSION->fromdiscussion = qualified_me();   // Return here if we post or set subscription etc