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/>.
16
17/**
18 * Message sent event.
19 *
20 * @package    core
21 * @copyright  2014 Mark Nelson <markn@moodle.com>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core\event;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Message sent event class.
31 *
32 * @property-read array $other {
33 *      Extra information about event.
34 *
35 *      - int courseid: the id of the related course.
36 * }
37 *
38 * @package    core
39 * @since      Moodle 2.7
40 * @copyright  2014 Mark Nelson <markn@moodle.com>
41 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class message_sent extends base {
44    /**
45     * Create event using ids.
46     * @param int $userfromid
47     * @param int $usertoid
48     * @param int $messageid
49     * @param int $courseid course id the event is related with.
50     * @return message_sent
51     */
52    public static function create_from_ids(int $userfromid, int $usertoid, int $messageid, int $courseid) {
53        // We may be sending a message from the 'noreply' address, which means we are not actually sending a
54        // message from a valid user. In this case, we will set the userid to 0.
55        // Check if the userid is valid.
56        if (!\core_user::is_real_user($userfromid)) {
57            $userfromid = 0;
58        }
59
60        $event = self::create(array(
61            'objectid' => $messageid,
62            'userid' => $userfromid,
63            'context' => \context_system::instance(),
64            'relateduserid' => $usertoid,
65            'other' => array(
66                'courseid' => $courseid
67            )
68        ));
69
70        return $event;
71    }
72
73    /**
74     * Init method.
75     */
76    protected function init() {
77        $this->data['objecttable'] = 'messages';
78        $this->data['crud'] = 'c';
79        $this->data['edulevel'] = self::LEVEL_OTHER;
80    }
81
82    /**
83     * Returns localised general event name.
84     *
85     * @return string
86     */
87    public static function get_name() {
88        return get_string('eventmessagesent', 'message');
89    }
90
91    /**
92     * Returns relevant URL.
93     *
94     * @return \moodle_url
95     */
96    public function get_url() {
97        return new \moodle_url('/message/index.php', array('user1' => $this->userid, 'user2' => $this->relateduserid));
98    }
99
100    /**
101     * Returns description of what happened.
102     *
103     * @return string
104     */
105    public function get_description() {
106        // Check if we are sending from a valid user.
107        if (\core_user::is_real_user($this->userid)) {
108            return "The user with id '$this->userid' sent a message to the user with id '$this->relateduserid'.";
109        }
110
111        return "A message was sent by the system to the user with id '$this->relateduserid'.";
112    }
113
114    /**
115     * Return legacy data for add_to_log().
116     *
117     * @return array
118     */
119    protected function get_legacy_logdata() {
120        // The add_to_log function was only ever called when we sent a message from one user to another. We do not want
121        // to return the legacy log data if we are sending a system message, so check that the userid is valid.
122        if (\core_user::is_real_user($this->userid)) {
123            $messageid = $this->other['messageid'] ?? $this->objectid; // For BC we may have 'messageid' in other.
124            return array(SITEID, 'message', 'write', 'index.php?user=' . $this->userid . '&id=' . $this->relateduserid .
125                '&history=1#m' . $messageid, $this->userid);
126        }
127
128        return null;
129    }
130
131    /**
132     * Custom validation.
133     *
134     * @throws \coding_exception
135     * @return void
136     */
137    protected function validate_data() {
138        parent::validate_data();
139
140        if (!isset($this->relateduserid)) {
141            throw new \coding_exception('The \'relateduserid\' must be set.');
142        }
143
144        if (!isset($this->other['courseid'])) {
145            throw new \coding_exception('The \'courseid\' value must be set in other.');
146        }
147    }
148
149    public static function get_objectid_mapping() {
150        return array('db' => 'messages', 'restore' => base::NOT_MAPPED);
151    }
152
153    public static function get_other_mapping() {
154        $othermapped = array();
155        $othermapped['courseid'] = array('db' => 'course', 'restore' => base::NOT_MAPPED);
156        return $othermapped;
157    }
158}
159