MDL-54973 message: Taking care of support and noreply users on search
[moodle.git] / message / classes / search / message_received.php
CommitLineData
26c0d75f
DG
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 * Search area for received messages.
19 *
20 * @package core_message
21 * @copyright 2016 Devang Gaur
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_message\search;
25
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Search area for received messages.
31 *
32 * @package core_message
33 * @copyright 2016 Devang Gaur
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class message_received extends base_message {
37
df762928
DM
38 /**
39 * Returns recordset containing message records.
40 *
41 * @param int $modifiedfrom timestamp
42 * @return \moodle_recordset
43 */
44 public function get_recordset_by_timestamp($modifiedfrom = 0) {
45 global $DB;
46
47 // We don't want to index messages received from noreply and support users.
48 $params = array('modifiedfrom' => $modifiedfrom, 'noreplyuser' => \core_user::NOREPLY_USER,
49 'supportuser' => \core_user::SUPPORT_USER);
50 return $DB->get_recordset_select('message_read', 'timecreated >= :modifiedfrom AND
51 useridto != :noreplyuser AND useridto != :supportuser', $params, 'timecreated ASC');
52 }
53
26c0d75f
DG
54 /**
55 * Returns the document associated with this message record.
56 *
57 * @param stdClass $record
58 * @param array $options
59 * @return \core_search\document
60 */
61 public function get_document($record, $options = array()) {
62 return parent::get_document($record, array('user1id' => $record->useridto, 'user2id' => $record->useridfrom));
63 }
64
65 /**
66 * Whether the user can access the document or not.
67 *
68 * @param int $id The message instance id.
69 * @return int
70 */
71 public function check_access($id) {
72 global $CFG, $DB, $USER;
73
74 if (!$CFG->messaging) {
75 return \core_search\manager::ACCESS_DENIED;
76 }
77
78 $message = $DB->get_record('message_read', array('id' => $id));
79 if (!$message) {
80 return \core_search\manager::ACCESS_DELETED;
81 }
82
df762928
DM
83 $userfrom = \core_user::get_user($message->useridfrom, 'id, deleted');
84 $userto = \core_user::get_user($message->useridto, 'id, deleted');
26c0d75f
DG
85
86 if (!$userfrom || !$userto || $userfrom->deleted || $userto->deleted) {
87 return \core_search\manager::ACCESS_DELETED;
88 }
89
90 if ($USER->id != $userto->id) {
91 return \core_search\manager::ACCESS_DENIED;
92 }
93
94 if ($message->timeusertodeleted != 0) {
95 return \core_search\manager::ACCESS_DELETED;
96 }
97
98 return \core_search\manager::ACCESS_GRANTED;
99 }
100
df762928 101}