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