MDL-40062 mod_forum: discussion events
[moodle.git] / mod / forum / classes / event / discussion_viewed.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * The mod_forum discussion viewed event.
19  *
20  * @package    mod_forum
21  * @copyright  2014 Dan Poltawski <dan@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 namespace mod_forum\event;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * The mod_forum discussion viewed event.
31  *
32  * @package    mod_forum
33  * @copyright  2014 Dan Poltawski <dan@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class discussion_viewed extends \core\event\base {
38     /**
39      * Init method.
40      *
41      * @return void
42      */
43     protected function init() {
44         $this->data['crud'] = 'r';
45         $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
46         $this->data['objecttable'] = 'forum_discussions';
47     }
49     /**
50      * Returns description of what happened.
51      *
52      * @return string
53      */
54     public function get_description() {
55         return "The user {$this->userid} has viewed the forum discussion {$this->objectid}";
56     }
58     /**
59      * Return localised event name.
60      *
61      * @return string
62      */
63     public static function get_name() {
64         return get_string('eventdiscussionviewed', 'mod_forum');
65     }
67     /**
68      * Get URL related to the action
69      *
70      * @return \moodle_url
71      */
72     public function get_url() {
73         return new \moodle_url('/mod/forum/discuss.php', array('d' => $this->objectid));
74     }
76     /**
77      * Return the legacy event log data.
78      *
79      * @return array|null
80      */
81     protected function get_legacy_logdata() {
82         return array($this->courseid, 'forum', 'view discussion', 'discuss.php?d=' . $this->objectid,
83             $this->objectid, $this->contextinstanceid);
84     }
86     /**
87      * Custom validation.
88      *
89      * @throws \coding_exception
90      * @return void
91      */
92     protected function validate_data() {
93         parent::validate_data();
95         if ($this->contextlevel != CONTEXT_MODULE) {
96             throw new \coding_exception('Context passed must be module context.');
97         }
99         if (!isset($this->objectid)) {
100             throw new \coding_exception('objectid must be set to the discussionid.');
101         }
102     }