MDL-54928 notifications: Retrieve username from DB if empty
[moodle.git] / message / output / airnotifier / message_output_airnotifier.php
CommitLineData
08ca1a5b 1<?php
08ca1a5b 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
3570f5e2
JL
17/**
18 * Airnotifier message processor to send messages to the APNS provider: airnotfier. (https://github.com/dongsheng/airnotifier)
19 *
20 * @package message_airnotifier
21 * @category external
22 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 2.7
25 */
26
27
08ca1a5b 28require_once($CFG->dirroot . '/message/output/lib.php');
08ca1a5b 29
30/**
3570f5e2 31 * Message processor class
08ca1a5b 32 *
33 * @package message_airnotifier
34 * @copyright 2012 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class message_output_airnotifier extends message_output {
38
39 /**
40 * Processes the message and sends a notification via airnotifier
41 *
42 * @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
43 * @return true if ok, false if error
44 */
3570f5e2 45 public function send_message($eventdata) {
3ce71f4e 46 global $CFG, $DB;
7cfe4419 47 require_once($CFG->libdir . '/filelib.php');
08ca1a5b 48
49 if (!empty($CFG->noemailever)) {
3570f5e2 50 // Hidden setting for development sites, set in config.php if needed.
08ca1a5b 51 debugging('$CFG->noemailever active, no airnotifier message sent.', DEBUG_MINIMAL);
52 return true;
53 }
54
3570f5e2
JL
55 // Skip any messaging suspended and deleted users.
56 if ($eventdata->userto->auth === 'nologin' or
57 $eventdata->userto->suspended or
58 $eventdata->userto->deleted) {
08ca1a5b 59 return true;
60 }
61
3ce71f4e
DP
62 // If username is empty we try to retrieve it, since it's required to generate the siteid.
63 if (empty($eventdata->userto->username)) {
64 $eventdata->userto->username = $DB->get_field('user', 'username', array('id' => $eventdata->userto->id));
65 }
66
3570f5e2
JL
67 // Site id, to map with Moodle Mobile stored sites.
68 $siteid = md5($CFG->wwwroot . $eventdata->userto->username);
69
29ad8f97
JL
70 // Airnotifier can handle custom requests using processors (that are Airnotifier plugins).
71 // In the extra parameter we indicate which processor to use and also additional data to be handled by the processor.
72 // Here we clone the eventdata object because will be deleting/adding attributes.
73 $extra = clone $eventdata;
74
75 // Delete attributes that may content private information.
76 if (!empty($eventdata->userfrom)) {
77 $extra->userfromid = $eventdata->userfrom->id;
78 $extra->userfromfullname = fullname($eventdata->userfrom);
79 unset($extra->userfrom);
08ca1a5b 80 }
29ad8f97
JL
81 $extra->usertoid = $eventdata->userto->id;
82 unset($extra->userto);
83
84 $extra->processor = 'moodle';
85 $extra->site = $siteid;
86 $extra->date = (!empty($eventdata->timecreated)) ? $eventdata->timecreated : time();
87 $extra->notification = (!empty($eventdata->notification)) ? 1 : 0;
08ca1a5b 88
5785421a
JL
89 // Site name.
90 $site = get_site();
6d782e33
JL
91 $extra->sitefullname = clean_param(format_string($site->fullname), PARAM_NOTAGS);
92 $extra->siteshortname = clean_param(format_string($site->shortname), PARAM_NOTAGS);
93
94 // Clean HTML, push notifications must arrive clean.
95 if (!empty($extra->smallmessage)) {
96 $extra->smallmessage = clean_param($extra->smallmessage, PARAM_NOTAGS);
97 }
98 if (!empty($extra->fullmessage)) {
99 $extra->fullmessage = clean_param($extra->fullmessage, PARAM_NOTAGS);
100 }
101 if (!empty($extra->fullmessagehtml)) {
102 $extra->fullmessagehtml = clean_param($extra->fullmessagehtml, PARAM_NOTAGS);
103 }
5785421a 104
3570f5e2
JL
105 // We are sending to message to all devices.
106 $airnotifiermanager = new message_airnotifier_manager();
107 $devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $eventdata->userto->id);
08ca1a5b 108
109 foreach ($devicetokens as $devicetoken) {
110
3570f5e2
JL
111 if (!$devicetoken->enable) {
112 continue;
113 }
114
115 // Sending the message to the device.
b159589d 116 $serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/api/v2/push/';
08ca1a5b 117 $header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname,
118 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey);
119 $curl = new curl;
120 $curl->setHeader($header);
29ad8f97 121
3570f5e2 122 $params = array(
98fb4d41 123 'device' => $devicetoken->platform,
29ad8f97
JL
124 'token' => $devicetoken->pushid,
125 'extra' => $extra
126 );
127
128 // JSON POST raw body request.
129 $resp = $curl->post($serverurl, json_encode($params));
08ca1a5b 130 }
131
132 return true;
133 }
134
135 /**
136 * Creates necessary fields in the messaging config form.
137 *
138 * @param array $preferences An array of user preferences
139 */
3570f5e2 140 public function config_form($preferences) {
08ca1a5b 141 global $CFG, $OUTPUT, $USER, $PAGE;
142
98fb4d41
JL
143 $systemcontext = context_system::instance();
144 if (!has_capability('message/airnotifier:managedevice', $systemcontext)) {
145 return get_string('nopermissiontomanagedevices', 'message_airnotifier');
146 }
147
08ca1a5b 148 if (!$this->is_system_configured()) {
149 return get_string('notconfigured', 'message_airnotifier');
150 } else {
151
152 $PAGE->requires->css('/message/output/airnotifier/style.css');
153
3570f5e2
JL
154 $airnotifiermanager = new message_airnotifier_manager();
155 $devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $USER->id);
08ca1a5b 156
157 if (!empty($devicetokens)) {
158 $output = '';
159
160 foreach ($devicetokens as $devicetoken) {
161
162 if ($devicetoken->enable) {
163 $hideshowiconname = 't/hide';
164 $dimmed = '';
165 } else {
166 $hideshowiconname = 't/show';
167 $dimmed = 'dimmed_text';
168 }
169
170 $hideshowicon = $OUTPUT->pix_icon($hideshowiconname, get_string('showhide', 'message_airnotifier'));
3570f5e2 171 $name = "{$devicetoken->name} {$devicetoken->model} {$devicetoken->platform} {$devicetoken->version}";
08ca1a5b 172 $hideurl = new moodle_url('message/output/airnotifier/action.php',
173 array('hide' => !$devicetoken->enable, 'deviceid' => $devicetoken->id,
174 'sesskey' => sesskey()));
08ca1a5b 175
3570f5e2
JL
176 $output .= html_writer::start_tag('li', array('id' => $devicetoken->id,
177 'class' => 'airnotifierdevice ' . $dimmed)) . "\n";
178 $output .= html_writer::label($name, 'deviceid-' . $devicetoken->id, array('class' => 'devicelabel ')) . ' ' .
179 html_writer::link($hideurl, $hideshowicon, array('class' => 'hidedevice', 'alt' => 'show/hide')) . "\n";
08ca1a5b 180 $output .= html_writer::end_tag('li') . "\n";
181 }
182
3570f5e2 183 // Include the AJAX script to automatically trigger the action.
08ca1a5b 184 $airnotifiermanager->include_device_ajax();
185
186 $output = html_writer::tag('ul', $output, array('id' => 'airnotifierdevices'));
187 } else {
188 $output = get_string('nodevices', 'message_airnotifier');
189 }
190 return $output;
191 }
192 }
193
194 /**
195 * Parses the submitted form data and saves it into preferences array.
196 *
197 * @param stdClass $form preferences form class
198 * @param array $preferences preferences array
199 */
3570f5e2
JL
200 public function process_form($form, &$preferences) {
201 return true;
08ca1a5b 202 }
203
204 /**
205 * Loads the config data from database to put on the form during initial form display
206 *
207 * @param array $preferences preferences array
208 * @param int $userid the user id
209 */
3570f5e2
JL
210 public function load_data(&$preferences, $userid) {
211 return true;
08ca1a5b 212 }
213
214 /**
215 * Tests whether the airnotifier settings have been configured
216 * @return boolean true if airnotifier is configured
217 */
3570f5e2
JL
218 public function is_system_configured() {
219 $airnotifiermanager = new message_airnotifier_manager();
220 return $airnotifiermanager->is_system_configured();
08ca1a5b 221 }
222}
223