2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Airnotifier message processor to send messages to the APNS provider: airnotfier. (https://github.com/dongsheng/airnotifier)
20 * @package message_airnotifier
22 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once($CFG->dirroot . '/message/output/lib.php');
31 * Message processor class
33 * @package message_airnotifier
34 * @copyright 2012 Jerome Mouneyrac
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class message_output_airnotifier extends message_output {
40 * Processes the message and sends a notification via airnotifier
42 * @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
43 * @return true if ok, false if error
45 public function send_message($eventdata) {
47 require_once($CFG->libdir . '/filelib.php');
49 if (!empty($CFG->noemailever)) {
50 // Hidden setting for development sites, set in config.php if needed.
51 debugging('$CFG->noemailever active, no airnotifier message sent.', DEBUG_MINIMAL);
55 // Skip any messaging suspended and deleted users.
56 if ($eventdata->userto->auth === 'nologin' or
57 $eventdata->userto->suspended or
58 $eventdata->userto->deleted) {
62 // Site id, to map with Moodle Mobile stored sites.
63 $siteid = md5($CFG->wwwroot . $eventdata->userto->username);
65 // Airnotifier can handle custom requests using processors (that are Airnotifier plugins).
66 // In the extra parameter we indicate which processor to use and also additional data to be handled by the processor.
67 // Here we clone the eventdata object because will be deleting/adding attributes.
68 $extra = clone $eventdata;
70 // Delete attributes that may content private information.
71 if (!empty($eventdata->userfrom)) {
72 $extra->userfromid = $eventdata->userfrom->id;
73 $extra->userfromfullname = fullname($eventdata->userfrom);
74 unset($extra->userfrom);
76 $extra->usertoid = $eventdata->userto->id;
77 unset($extra->userto);
79 $extra->processor = 'moodle';
80 $extra->site = $siteid;
81 $extra->date = (!empty($eventdata->timecreated)) ? $eventdata->timecreated : time();
82 $extra->notification = (!empty($eventdata->notification)) ? 1 : 0;
86 $extra->sitefullname = clean_param(format_string($site->fullname), PARAM_NOTAGS);
87 $extra->siteshortname = clean_param(format_string($site->shortname), PARAM_NOTAGS);
89 // Clean HTML, push notifications must arrive clean.
90 if (!empty($extra->smallmessage)) {
91 $extra->smallmessage = clean_param($extra->smallmessage, PARAM_NOTAGS);
93 if (!empty($extra->fullmessage)) {
94 $extra->fullmessage = clean_param($extra->fullmessage, PARAM_NOTAGS);
96 if (!empty($extra->fullmessagehtml)) {
97 $extra->fullmessagehtml = clean_param($extra->fullmessagehtml, PARAM_NOTAGS);
100 // We are sending to message to all devices.
101 $airnotifiermanager = new message_airnotifier_manager();
102 $devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $eventdata->userto->id);
104 foreach ($devicetokens as $devicetoken) {
106 if (!$devicetoken->enable) {
110 // Sending the message to the device.
111 $serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/api/v2/push/';
112 $header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname,
113 'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey);
115 $curl->setHeader($header);
118 'device' => $devicetoken->platform,
119 'token' => $devicetoken->pushid,
123 // JSON POST raw body request.
124 $resp = $curl->post($serverurl, json_encode($params));
131 * Creates necessary fields in the messaging config form.
133 * @param array $preferences An array of user preferences
135 public function config_form($preferences) {
136 global $CFG, $OUTPUT, $USER, $PAGE;
138 $systemcontext = context_system::instance();
139 if (!has_capability('message/airnotifier:managedevice', $systemcontext)) {
140 return get_string('nopermissiontomanagedevices', 'message_airnotifier');
143 if (!$this->is_system_configured()) {
144 return get_string('notconfigured', 'message_airnotifier');
147 $PAGE->requires->css('/message/output/airnotifier/style.css');
149 $airnotifiermanager = new message_airnotifier_manager();
150 $devicetokens = $airnotifiermanager->get_user_devices($CFG->airnotifiermobileappname, $USER->id);
152 if (!empty($devicetokens)) {
155 foreach ($devicetokens as $devicetoken) {
157 if ($devicetoken->enable) {
158 $hideshowiconname = 't/hide';
161 $hideshowiconname = 't/show';
162 $dimmed = 'dimmed_text';
165 $hideshowicon = $OUTPUT->pix_icon($hideshowiconname, get_string('showhide', 'message_airnotifier'));
166 $name = "{$devicetoken->name} {$devicetoken->model} {$devicetoken->platform} {$devicetoken->version}";
167 $hideurl = new moodle_url('message/output/airnotifier/action.php',
168 array('hide' => !$devicetoken->enable, 'deviceid' => $devicetoken->id,
169 'sesskey' => sesskey()));
171 $output .= html_writer::start_tag('li', array('id' => $devicetoken->id,
172 'class' => 'airnotifierdevice ' . $dimmed)) . "\n";
173 $output .= html_writer::label($name, 'deviceid-' . $devicetoken->id, array('class' => 'devicelabel ')) . ' ' .
174 html_writer::link($hideurl, $hideshowicon, array('class' => 'hidedevice', 'alt' => 'show/hide')) . "\n";
175 $output .= html_writer::end_tag('li') . "\n";
178 // Include the AJAX script to automatically trigger the action.
179 $airnotifiermanager->include_device_ajax();
181 $output = html_writer::tag('ul', $output, array('id' => 'airnotifierdevices'));
183 $output = get_string('nodevices', 'message_airnotifier');
190 * Parses the submitted form data and saves it into preferences array.
192 * @param stdClass $form preferences form class
193 * @param array $preferences preferences array
195 public function process_form($form, &$preferences) {
200 * Loads the config data from database to put on the form during initial form display
202 * @param array $preferences preferences array
203 * @param int $userid the user id
205 public function load_data(&$preferences, $userid) {
210 * Tests whether the airnotifier settings have been configured
211 * @return boolean true if airnotifier is configured
213 public function is_system_configured() {
214 $airnotifiermanager = new message_airnotifier_manager();
215 return $airnotifiermanager->is_system_configured();