Skip to content

Commit

Permalink
MDL-44342 airnotifier: Added support for the new workflow of register…
Browse files Browse the repository at this point in the history
…ing devices

And also for request access keys to Airnotifier
  • Loading branch information
jleyva committed Apr 7, 2014
1 parent 08ca1a5 commit 3570f5e
Show file tree
Hide file tree
Showing 15 changed files with 532 additions and 456 deletions.
198 changes: 198 additions & 0 deletions message/output/airnotifier/classes/manager.php
@@ -0,0 +1,198 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Airnotifier manager class
*
* @package message_airnotifier
* @category external
* @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.7
*/


/**
* Airnotifier helper manager class
*
* @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_airnotifier_manager {

/**
* Include the relevant javascript and language strings for the device
* toolbox YUI module
*
* @return bool
*/
public function include_device_ajax() {
global $PAGE, $CFG;

if (!$CFG->enableajax) {
return false;
}

$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();
}

// Include toolboxes.
$PAGE->requires->yui_module('moodle-message_airnotifier-toolboxes', 'M.message.init_device_toolbox', array(array(
'ajaxurl' => $config->resturl,
'config' => $config,
))
);

// Required strings for the javascript.
$PAGE->requires->strings_for_js(array('deletecheckdevicename'), 'message_airnotifier');
$PAGE->requires->strings_for_js(array('show', 'hide'), 'moodle');

return true;
}

/**
* Return the user devices for a specific app.
*
* @param string $appname the app name .
* @param int $userid if empty take the current user.
* @return array all the devices
*/
public function get_user_devices($appname, $userid = null) {
global $USER, $DB;

if (empty($userid)) {
$userid = $USER->id;
}

$devices = array();

$params = array('appid' => $appname, 'userid' => $userid);

// 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.

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);
}
$device->id = $airnotifierdev->id;
$device->enable = $airnotifierdev->enable;
$devices[] = $device;
}
}
}
return $devices;
}

/**
* Request and access key to Airnotifier
*
* @return mixed The access key or false in case of error
*/
public function request_accesskey() {
global $CFG, $USER;

require_once($CFG->libdir . '/filelib.php');

// Sending the request access key request to Airnotifier.
$serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/accesskeys/';
// We use an APP Key "none", it can be anything.
$header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname,
'X-AN-APP-KEY: none');
$curl = new curl();
$curl->setHeader($header);

// Site ids are stored as secrets in md5 in the Moodle public hub.
$params = array(
'url' => $CFG->wwwroot,
'siteid' => md5($CFG->siteidentifier),
'contact' => $USER->email,
'description' => $CFG->wwwroot
);
$resp = $curl->post($serverurl, $params);

if ($key = json_decode($resp, true)) {
if (!empty($key['accesskey'])) {
return $key['accesskey'];
}
}
return false;
}

/**
* Create a device token in the Airnotifier instance
* @param string $token The token to be created
* @return bool True if all was right
*/
private function create_token($token) {
global $CFG;

if (!$this->is_system_configured()) {
return false;
}

require_once($CFG->libdir . '/filelib.php');

$serverurl = $CFG->airnotifierurl . ':' . $CFG->airnotifierport . '/tokens/' . $token;
$header = array('Accept: application/json', 'X-AN-APP-NAME: ' . $CFG->airnotifierappname,
'X-AN-APP-KEY: ' . $CFG->airnotifieraccesskey);
$curl = new curl;
$curl->setHeader($header);
$params = array();
$resp = $curl->post($serverurl, $params);

if ($resp = json_decode($resp, true)) {
if (!empty($resp['status'])) {
return $resp['status'] == 'ok' || $resp['status'] == 'token exists';
}
}
return false;
}

/**
* Tests whether the airnotifier settings have been configured
* @return boolean true if airnotifier is configured
*/
public function is_system_configured() {
global $CFG;

return (!empty($CFG->airnotifierurl) && !empty($CFG->airnotifierport) &&
!empty($CFG->airnotifieraccesskey) && !empty($CFG->airnotifierappname) &&
!empty($CFG->airnotifiermobileappname));
}

}

4 changes: 2 additions & 2 deletions message/output/airnotifier/db/install.php
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -27,12 +26,13 @@
* Install the Airnotifier message processor
*/
function xmldb_message_airnotifier_install() {
global $DB;
global $CFG, $DB;

$result = true;

$provider = new stdClass();
$provider->name = 'airnotifier';
$DB->insert_record('message_processors', $provider);

return $result;
}
19 changes: 6 additions & 13 deletions message/output/airnotifier/db/install.xml
@@ -1,25 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="message/output/airnotifier/db" VERSION="20120706" COMMENT="XMLDB file for airnotifier Moodle tables"
<XMLDB PATH="message/output/airnotifier/db" VERSION="20140129" COMMENT="XMLDB file for Moodle message/output/airnotifier"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="airnotifier_user_devices" COMMENT="Mobile devices associated to users">
<TABLE NAME="message_airnotifier_devices" COMMENT="Store information about the devices registered in Airnotifier for PUSH notifications">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="appname"/>
<FIELD NAME="appname" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="The name of the app: mymoodle, ..." PREVIOUS="id" NEXT="devicename"/>
<FIELD NAME="devicename" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="For example Jerome's iPhone" PREVIOUS="appname" NEXT="devicetype"/>
<FIELD NAME="devicetype" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="For example iPhone 4S, Google Nexus..." PREVIOUS="devicename" NEXT="deviceos"/>
<FIELD NAME="deviceos" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="For example ios, android..." PREVIOUS="devicetype" NEXT="deviceosversion"/>
<FIELD NAME="deviceosversion" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="5.1, 2.3.3..." PREVIOUS="deviceos" NEXT="devicebrand"/>
<FIELD NAME="devicebrand" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="Apple, Samsung..." PREVIOUS="deviceosversion" NEXT="devicenotificationtoken"/>
<FIELD NAME="devicenotificationtoken" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Apple give one token per device/app to be able to send push notification to the device." PREVIOUS="devicebrand" NEXT="deviceuid"/>
<FIELD NAME="deviceuid" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="A unique ID to identify the device. Freedom for the client to send whatever he wants and the plugin to manage them in concordance." PREVIOUS="devicenotificationtoken" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="the user owning the device" PREVIOUS="deviceuid" NEXT="enable"/>
<FIELD NAME="enable" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="the user can disable on of his/her device." PREVIOUS="userid"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userdeviceid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The user device id in the user_devices table"/>
<FIELD NAME="enable" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="The user can enable/disable his devices"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="userdeviceid" TYPE="foreign-unique" FIELDS="userdeviceid" REFTABLE="user_devices" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
Expand Down
15 changes: 7 additions & 8 deletions message/output/airnotifier/db/services.php
Expand Up @@ -25,20 +25,19 @@
*/

$functions = array(
'message_airnotifier_add_user_device' => array(
'message_airnotifier_is_system_configured' => array(
'classname' => 'message_airnotifier_external',
'methodname' => 'add_user_device',
'methodname' => 'is_system_configured',
'classpath' => 'message/output/airnotifier/externallib.php',
'description' => 'Add device to user device list',
'type' => 'write',
'description' => 'Check whether the airnotifier settings have been configured',
'type' => 'read',
),

'message_airnotifier_get_access_key' => array(
'message_airnotifier_are_notification_preferences_configured' => array(
'classname' => 'message_airnotifier_external',
'methodname' => 'get_access_key',
'methodname' => 'are_notification_preferences_configured',
'classpath' => 'message/output/airnotifier/externallib.php',
'description' => 'Get the mobile device access key with specified permissions',
'description' => 'Check if the users have notification preferences configured yet',
'type' => 'read',
),
);

68 changes: 0 additions & 68 deletions message/output/airnotifier/db/upgrade.php

This file was deleted.

0 comments on commit 3570f5e

Please sign in to comment.