MDL-54708 message: notification popover respects message preferences
[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 /**
607454d6
RW
38 * Do nothing on send_message.
39 *
fe983847 40 * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
3b120e46 41 * @return true if ok, false if error
42 */
fe983847 43 public function send_message($eventdata) {
ada7695d
RW
44 global $DB;
45
46 //hold onto the popup processor id because /admin/cron.php sends a lot of messages at once
47 static $processorid = null;
48
49 //prevent users from getting popup notifications of messages to themselves (happens with forum notifications)
50 if ($eventdata->userfrom->id != $eventdata->userto->id) {
51 if (empty($processorid)) {
52 $processor = $DB->get_record('message_processors', array('name'=>'popup'));
53 $processorid = $processor->id;
54 }
55 $procmessage = new stdClass();
56 $procmessage->unreadmessageid = $eventdata->savedmessageid;
57 $procmessage->processorid = $processorid;
58
59 //save this message for later delivery
60 $DB->insert_record('message_working', $procmessage);
61
62 if ($eventdata->notification) {
63 if (!$DB->record_exists('message_popup', ['messageid' => $eventdata->savedmessageid, 'isread' => 0])) {
64 $record = new StdClass();
65 $record->messageid = $eventdata->savedmessageid;
66 $record->isread = 0;
67
68 $DB->insert_record('message_popup', $record);
69 }
70 }
71 }
72
3b120e46 73 return true;
74 }
1d422980 75
6fbd60ef
AD
76 /**
77 * Creates necessary fields in the messaging config form.
78 *
79 * @param array $preferences An array of user preferences
80 */
f3d095f8 81 function config_form($preferences) {
a813a748 82 return null;
d18b1bbd 83 }
1d422980 84
6fbd60ef
AD
85 /**
86 * Parses the submitted form data and saves it into preferences array.
87 *
88 * @param stdClass $form preferences form class
89 * @param array $preferences preferences array
90 */
f3d095f8 91 public function process_form($form, &$preferences) {
3b120e46 92 return true;
93 }
6fbd60ef
AD
94
95 /**
96 * Loads the config data from database to put on the form during initial form display
97 *
98 * @param array $preferences preferences array
99 * @param int $userid the user id
100 */
f3d095f8 101 public function load_data(&$preferences, $userid) {
c8621a02 102 global $USER;
3b120e46 103 return true;
104 }
ada7695d
RW
105
106 /**
107 * Handles the message_viewed event to keep data in sync.
108 *
109 * @param \core\event\base $event The event data
110 */
111 public static function message_viewed(\core\event\base $event) {
112 global $DB;
113
114 if ($record = $DB->get_record('message_popup', ['messageid' => $event->other['messageid']])) {
115 // The id can change when the moving to the message_read table.
116 $record->messageid = $event->objectid;
117 $record->isread = 1;
118 $DB->update_record('message_popup', $record);
119 }
120 }
6fbd60ef 121}