Merge branch 'master_MDL-69848' of https://github.com/golenkovm/moodle
[moodle.git] / message / output / popup / message_output_popup.php
CommitLineData
3b120e46 1<?php
eb5334ff 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 2 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/>.
3b120e46 16
17/**
607454d6 18 * Popup message processor
3b120e46 19 *
6fbd60ef
AD
20 * @package message_popup
21 * @copyright 2008 Luis Rodrigues
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
3b120e46 23 */
24
1fcf0ca8 25require_once(__DIR__ . '/../../../config.php'); //included from messagelib (how to fix?)
3b120e46 26require_once($CFG->dirroot.'/message/output/lib.php');
27
6fbd60ef
AD
28/**
29 * The popup message processor
30 *
31 * @package message_popup
32 * @copyright 2008 Luis Rodrigues and Martin Dougiamas
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
ada7695d 35class message_output_popup extends message_output {
3b120e46 36
1d422980 37 /**
e78523bf
MN
38 * Adds notifications to the 'message_popup_notifications' table if applicable.
39 *
40 * The reason for this is because we may not want to show all notifications in the notification popover. This
41 * can happen if the popup processor was disabled when the notification was sent. If the processor is disabled this
42 * function is never called so the notification will never be added to the 'message_popup_notifications' table.
43 * Essentially this table is used to filter what notifications to display from the 'notifications' table.
607454d6 44 *
fe983847 45 * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
3b120e46 46 * @return true if ok, false if error
47 */
fe983847 48 public function send_message($eventdata) {
e78523bf
MN
49 global $DB;
50
51 // Prevent users from getting popup notifications from themselves (happens with forum notifications).
52 if ($eventdata->userfrom->id != $eventdata->userto->id && $eventdata->notification) {
53 if (!$DB->record_exists('message_popup_notifications', ['notificationid' => $eventdata->savedmessageid])) {
54 $record = new stdClass();
55 $record->notificationid = $eventdata->savedmessageid;
56
57 $DB->insert_record('message_popup_notifications', $record);
58 }
59 }
60
3b120e46 61 return true;
62 }
1d422980 63
6fbd60ef
AD
64 /**
65 * Creates necessary fields in the messaging config form.
66 *
67 * @param array $preferences An array of user preferences
68 */
f3d095f8 69 function config_form($preferences) {
a813a748 70 return null;
d18b1bbd 71 }
1d422980 72
6fbd60ef
AD
73 /**
74 * Parses the submitted form data and saves it into preferences array.
75 *
76 * @param stdClass $form preferences form class
77 * @param array $preferences preferences array
78 */
f3d095f8 79 public function process_form($form, &$preferences) {
3b120e46 80 return true;
81 }
6fbd60ef
AD
82
83 /**
84 * Loads the config data from database to put on the form during initial form display
85 *
86 * @param array $preferences preferences array
87 * @param int $userid the user id
88 */
f3d095f8 89 public function load_data(&$preferences, $userid) {
c8621a02 90 global $USER;
3b120e46 91 return true;
92 }
ada7695d 93
7d69958e
RW
94 /**
95 * Don't show this processor on the message preferences page. The user can't disable
96 * the notifications for user-to-user messaging.
97 *
98 * @return bool
99 */
100 public function has_message_preferences() {
101 return false;
102 }
103
685daf1a
MN
104 /**
105 * Determines if this processor should process a message regardless of user preferences or site settings.
106 *
107 * @return bool
108 */
109 public function force_process_messages() {
110 global $CFG;
111
112 return !empty($CFG->messaging);
113 }
54cb5666
PH
114
115 /**
116 * Remove all popup notifications up to specified time
117 *
118 * @param int $notificationdeletetime
119 * @return void
120 */
121 public function cleanup_all_notifications(int $notificationdeletetime): void {
122 global $DB;
123
124 $DB->delete_records_select('message_popup_notifications',
125 'notificationid IN (SELECT id FROM {notifications} WHERE timecreated < ?)', [$notificationdeletetime]);
126 }
127
128 /**
129 * Remove read popup notifications up to specified time
130 *
131 * @param int $notificationdeletetime
132 * @return void
133 */
134 public function cleanup_read_notifications(int $notificationdeletetime): void {
135 global $DB;
136
137 $DB->delete_records_select('message_popup_notifications',
138 'notificationid IN (SELECT id FROM {notifications} WHERE timeread < ?)', [$notificationdeletetime]);
139 }
6fbd60ef 140}