From 98fb4d413b621b325e2aebf5de91179e02b87973 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 3 Apr 2014 15:13:56 +0200 Subject: [PATCH] MDL-44342 airnotifier: Add support for Android notifications --- .../output/airnotifier/classes/manager.php | 49 ++++++++----------- .../message_output_airnotifier.php | 8 +++ 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/message/output/airnotifier/classes/manager.php b/message/output/airnotifier/classes/manager.php index bbb7b0f77a5..e08b5cfdef3 100755 --- a/message/output/airnotifier/classes/manager.php +++ b/message/output/airnotifier/classes/manager.php @@ -24,6 +24,7 @@ * @since Moodle 2.7 */ +defined('MOODLE_INTERNAL') || die; /** * Airnotifier helper manager class @@ -47,16 +48,8 @@ class message_airnotifier_manager { } $config = new stdClass(); - - // The URL to use for resource changes. - if (!isset($config->resturl)) { - $config->resturl = '/message/output/airnotifier/rest.php'; - } - - // Any additional parameters which need to be included on page submission. - if (!isset($config->pageparams)) { - $config->pageparams = array(); - } + $config->resturl = '/message/output/airnotifier/rest.php'; + $config->pageparams = array(); // Include toolboxes. $PAGE->requires->yui_module('moodle-message_airnotifier-toolboxes', 'M.message.init_device_toolbox', array(array( @@ -92,28 +85,29 @@ class message_airnotifier_manager { // First, we look all the devices registered for this user in the Moodle core. // We are going to allow only ios devices (since these are the ones that supports PUSH notifications). - if ($userdevices = $DB->get_records('user_devices', $params)) { - foreach ($userdevices as $device) { - if (core_text::strtolower($device->platform) == 'ios') { - // Check if the device is known by airnotifier. - if (!$airnotifierdev = $DB->get_record('message_airnotifier_devices', array('userdeviceid' => $device->id))) { - // We have to create the device token in airnotifier. + $userdevices = $DB->get_records('user_devices', $params); + foreach ($userdevices as $device) { + if (core_text::strtolower($device->platform)) { + // Check if the device is known by airnotifier. + if (!$airnotifierdev = $DB->get_record('message_airnotifier_devices', + array('userdeviceid' => $device->id))) { - if (! $this->create_token($device->pushid)) { - continue; - } - - $airnotifierdev = new stdClass; - $airnotifierdev->userdeviceid = $device->id; - $airnotifierdev->enable = 1; - $airnotifierdev->id = $DB->insert_record('message_airnotifier_devices', $airnotifierdev); + // We have to create the device token in airnotifier. + if (! $this->create_token($device->pushid)) { + continue; } - $device->id = $airnotifierdev->id; - $device->enable = $airnotifierdev->enable; - $devices[] = $device; + + $airnotifierdev = new stdClass; + $airnotifierdev->userdeviceid = $device->id; + $airnotifierdev->enable = 1; + $airnotifierdev->id = $DB->insert_record('message_airnotifier_devices', $airnotifierdev); } + $device->id = $airnotifierdev->id; + $device->enable = $airnotifierdev->enable; + $devices[] = $device; } } + return $devices; } @@ -195,4 +189,3 @@ class message_airnotifier_manager { } } - diff --git a/message/output/airnotifier/message_output_airnotifier.php b/message/output/airnotifier/message_output_airnotifier.php index 299e4ad6397..bd0b9e908e5 100755 --- a/message/output/airnotifier/message_output_airnotifier.php +++ b/message/output/airnotifier/message_output_airnotifier.php @@ -63,9 +63,11 @@ class message_output_airnotifier extends message_output { // Mandatory notification data that need to be sent in the payload. They have variable length. // We need to take them in consideration to calculate the maximum message size. + // Since at this point we don't know the device, we use a 10 chars device platform. $notificationdata = array( "site" => $siteid, "type" => $eventdata->component . '_' . $eventdata->name, + "device" => "xxxxxxxxxx", "userfrom" => fullname($eventdata->userfrom)); // Calculate the size of the message knowing Apple payload must be lower than 256 bytes. @@ -111,6 +113,7 @@ class message_output_airnotifier extends message_output { 'site' => $siteid, 'type' => $eventdata->component . '_' . $eventdata->name, 'userfrom' => fullname($eventdata->userfrom), + 'device' => $devicetoken->platform, 'token' => $devicetoken->pushid); $resp = $curl->post($serverurl, $params); } @@ -126,6 +129,11 @@ class message_output_airnotifier extends message_output { public function config_form($preferences) { global $CFG, $OUTPUT, $USER, $PAGE; + $systemcontext = context_system::instance(); + if (!has_capability('message/airnotifier:managedevice', $systemcontext)) { + return get_string('nopermissiontomanagedevices', 'message_airnotifier'); + } + if (!$this->is_system_configured()) { return get_string('notconfigured', 'message_airnotifier'); } else {