MDL-40062 mod_forum: post events
authorDan Poltawski <dan@moodle.com>
Mon, 10 Feb 2014 01:54:06 +0000 (09:54 +0800)
committerDan Poltawski <dan@moodle.com>
Mon, 17 Feb 2014 06:05:54 +0000 (14:05 +0800)
* post_created
* post_updated
* post_deleted

Prune post (split discussion) has been converted to multiple events as
splitting a post actually means new discussion created.

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/lang/en/forum.php
mod/forum/post.php

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.');
+        }
+    }
+}
index 5b30113..ad599c9 100644 (file)
@@ -151,9 +151,12 @@ $string['eventdiscussionmoved'] = 'Discussion moved';
 $string['eventdiscussionviewed'] = 'Discussion viewed';
 $string['eventforumviewed'] = 'Forum viewed';
 $string['eventuserreportviewed'] = 'User report viewed';
-$string['eventsubscribersviewed'] = 'Subscribers 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';
index 33cb325..ebcb22b 100644 (file)
@@ -361,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 {
@@ -464,8 +480,29 @@ 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' => $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"));
 
@@ -692,8 +729,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);
 
@@ -735,8 +789,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);