diff --git a/mod/forum/classes/event/post_created.php b/mod/forum/classes/event/post_created.php new file mode 100644 index 00000000000..79aa997ee21 --- /dev/null +++ b/mod/forum/classes/event/post_created.php @@ -0,0 +1,131 @@ +. + +/** + * The mod_forum post created event. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @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 + * @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 index 00000000000..a73fcc7e68e --- /dev/null +++ b/mod/forum/classes/event/post_deleted.php @@ -0,0 +1,130 @@ +. + +/** + * The mod_forum post deleted event. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @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 + * @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 index 00000000000..5bb1f026d81 --- /dev/null +++ b/mod/forum/classes/event/post_updated.php @@ -0,0 +1,131 @@ +. + +/** + * The mod_forum post updated event. + * + * @package mod_forum + * @copyright 2014 Dan Poltawski + * @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 + * @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/lang/en/forum.php b/mod/forum/lang/en/forum.php index 5b301132842..ad599c9a964 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -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'; diff --git a/mod/forum/post.php b/mod/forum/post.php index 33cb32562de..ebcb22b401f 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -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&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&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);