Skip to content

Commit

Permalink
MDL-44342 airnotifier: Initial work, first version of the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mouneyrac authored and jleyva committed Apr 7, 2014
1 parent 1a727e1 commit 08ca1a5
Show file tree
Hide file tree
Showing 15 changed files with 1,278 additions and 0 deletions.
37 changes: 37 additions & 0 deletions message/output/airnotifier/db/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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/>.

/**
* Capability definitions for airnotifier.
*
* @package message_airnotifier
* @category access
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$capabilities = array(

'message/airnotifier:managedevice' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_USER,
'archetypes' => array(
'user' => CAP_ALLOW
)
)
);
38 changes: 38 additions & 0 deletions message/output/airnotifier/db/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 message processor installation code
*
* @package message_airnotifier
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Install the Airnotifier message processor
*/
function xmldb_message_airnotifier_install() {
global $DB;

$result = true;

$provider = new stdClass();
$provider->name = 'airnotifier';
$DB->insert_record('message_processors', $provider);
return $result;
}
26 changes: 26 additions & 0 deletions message/output/airnotifier/db/install.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="message/output/airnotifier/db" VERSION="20120706" COMMENT="XMLDB file for airnotifier Moodle tables"
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">
<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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
44 changes: 44 additions & 0 deletions message/output/airnotifier/db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 external functions and service definitions.
*
* @package message_airnotifier
* @category webservice
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$functions = array(
'message_airnotifier_add_user_device' => array(
'classname' => 'message_airnotifier_external',
'methodname' => 'add_user_device',
'classpath' => 'message/output/airnotifier/externallib.php',
'description' => 'Add device to user device list',
'type' => 'write',
),

'message_airnotifier_get_access_key' => array(
'classname' => 'message_airnotifier_external',
'methodname' => 'get_access_key',
'classpath' => 'message/output/airnotifier/externallib.php',
'description' => 'Get the mobile device access key with specified permissions',
'type' => 'read',
),
);

68 changes: 68 additions & 0 deletions message/output/airnotifier/db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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/>.

/**
* Upgrade code for airnotifier message processor
*
* @package message_airnotifier
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* Upgrade code for the airnotifier message processor
*
* @param int $oldversion The version that we are upgrading from
*/
function xmldb_message_airnotifier_upgrade($oldversion) {
global $CFG, $DB;

$dbman = $DB->get_manager();


if ($oldversion < 2012070500.05) {

// Define table user_devices to be created
$table = new xmldb_table('airnotifier_user_devices');

// Adding fields to table user_devices
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('appname', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('devicename', XMLDB_TYPE_CHAR, '255', null, null, null, null);
$table->add_field('devicetype', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('deviceos', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('deviceosversion', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('devicebrand', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('devicenotificationtoken', XMLDB_TYPE_CHAR, '255', null, null, null, null);
$table->add_field('deviceuid', XMLDB_TYPE_CHAR, '255', null, null, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('enable', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');

// Adding keys to table user_devices
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));

// Conditionally launch create table for user_devices
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached
upgrade_plugin_savepoint(true, 2012070500.05);
}

return true;
}

150 changes: 150 additions & 0 deletions message/output/airnotifier/externallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?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/>.

/**
* external API for airnotifier web services
*
* @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.4
*/
class message_airnotifier_external extends external_api {

/**
* Returns description of add_user_device() parameters
*
* @return external_function_parameters
* @since Moodle 2.4
*/
public static function add_user_device_parameters() {
return new external_function_parameters(
array('device' => new external_single_structure(
array(
'appname' => new external_value( PARAM_TEXT, 'the app name'),
'devicename' => new external_value( PARAM_TEXT, 'Device name: "Jerome\'s iPhone"', VALUE_OPTIONAL),
'devicetype' => new external_value( PARAM_TEXT, 'iPhone 3GS, Google Nexus S, ...', VALUE_OPTIONAL),
'deviceos' => new external_value( PARAM_TEXT, 'iOS, Android, ...', VALUE_OPTIONAL),
'deviceosversion' => new external_value( PARAM_TEXT, 'OS version number', VALUE_OPTIONAL),
'devicebrand' => new external_value( PARAM_TEXT, 'the device brand (Apple, Samsung, ...)', VALUE_OPTIONAL),
'devicenotificationtoken' => new external_value( PARAM_RAW, 'the device token used to send notification for the specified app'),
'deviceuid' => new external_value( PARAM_RAW, 'the device unique device id if it exists', VALUE_OPTIONAL),
), 'the device information - Important: type, os, osversion and brand will be saved in lowercase for fast searching'
)
));
}

/**
* Add a device to the user device list
*
* @param array $device
* @return int device id
* @since Moodle 2.4
*/
public static function add_user_device($device) {
global $USER, $CFG;

$params = self::validate_parameters(self::add_user_device_parameters(),
array('device'=>$device));

// Ensure the current user is allowed to run this function
$context = context_user::instance($USER->id);
self::validate_context($context);
require_capability('message/airnotifier:managedevice', $context);

$device['userid'] = $USER->id;

require_once($CFG->dirroot . "/message/output/airnotifier/lib.php");
$airnotifiermanager = new airnotifier_manager();
$device['id'] = $airnotifiermanager->add_user_device($device);

return $device['id'];
}

/**
* Returns description of add_user_device() result value
*
* @return external_single_structure
* @since Moodle 2.4
*/
public static function add_user_device_returns() {
return new external_value( PARAM_INT, 'Device id in the Moodle database');
}

/**
* Returns description of get_access_key() parameters
*
* @return external_function_parameters
* @since Moodle 2.4
*/
public static function get_access_key_parameters() {
return new external_function_parameters(
array('permissions' => new external_multiple_structure(
new external_value( PARAM_ALPHA, 'the permission'),
'Allowed permissions: createtoken (not yet implemented: deletetoken, accessobjects,
sendnotification, sendbroadcast)',
VALUE_DEFAULT, array()
)
));
}

/**
* Get access key with specified permissions
*
* @param array $permissions the permission that the access key should
* @return string access key
* @since Moodle 2.4
*/
public static function get_access_key($permissions = array()) {
global $CFG;

$params = self::validate_parameters(self::get_access_key_parameters(),
array('permissions'=>$permissions));

// Check that user can use the requested permission.
foreach ($params['permissions'] as $perm) {
switch ($perm) {
case 'createtoken':
// Any mobile device / user should have this permission.
// No need to check anything for this permission.

break;

default:
throw new moodle_exception('permissionnotimplemented');
break;
}
}

// Look for access key that have exactly the same permissions.
// TODO: This mobile device access key should be retrieve by web service from
// moodle.org or airnotifer when the admin enables mobile on Moodle.
$accesskey = $CFG->airnotifierdeviceaccesskey;

return $accesskey;
}

/**
* Returns description of add_user_device() result value
*
* @return external_single_structure
* @since Moodle 2.4
*/
public static function get_access_key_returns() {
return new external_value( PARAM_ALPHANUMEXT, 'access key');
}
}
Loading

0 comments on commit 08ca1a5

Please sign in to comment.