Merge branch 'MDL-41914-weekly' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 7 Oct 2013 14:06:32 +0000 (16:06 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 7 Oct 2013 14:06:32 +0000 (16:06 +0200)
lib/db/install.xml
lib/db/services.php
lib/db/upgrade.php
user/externallib.php
user/tests/externallib_test.php
version.php

index f8f65cc..64cb1cb 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20130927" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20131004" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
         <KEY NAME="fk_backpackid" TYPE="foreign" FIELDS="backpackid" REFTABLE="badge_backpack" REFFIELDS="id"/>
       </KEYS>
     </TABLE>
+    <TABLE NAME="user_devices" COMMENT="This table stores user's mobile devices information in order to send PUSH notifications">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
+        <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+        <FIELD NAME="appid" TYPE="char" LENGTH="128" NOTNULL="true" SEQUENCE="false" COMMENT="the app id, usually something like com.moodle.moodlemobile"/>
+        <FIELD NAME="name" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="the device name, occam or iPhone etc.."/>
+        <FIELD NAME="model" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="the device model, Nexus 4 or iPad 1,1"/>
+        <FIELD NAME="platform" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="the device platform, Android or iOS etc"/>
+        <FIELD NAME="version" TYPE="char" LENGTH="32" NOTNULL="true" SEQUENCE="false" COMMENT="The device version, 6.1.2, 4.2.2 etc.."/>
+        <FIELD NAME="pushid" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="the device PUSH token/key/identifier/registration id"/>
+        <FIELD NAME="uuid" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The device vendor UUID"/>
+        <FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+        <KEY NAME="pushid-userid" TYPE="unique" FIELDS="pushid, userid"/>
+        <KEY NAME="pushid-platform" TYPE="unique" FIELDS="pushid, platform"/>
+        <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
+      </KEYS>
+    </TABLE>
   </TABLES>
 </XMLDB>
index 9781298..fe87360 100644 (file)
@@ -437,6 +437,15 @@ $functions = array(
         'capabilities'=> 'moodle/user:update',
     ),
 
+    'core_user_add_user_device' => array(
+        'classname'   => 'core_user_external',
+        'methodname'  => 'add_user_device',
+        'classpath'   => 'user/externallib.php',
+        'description' => 'Store mobile user devices information for PUSH Notifications.',
+        'type'        => 'write',
+        'capabilities'=> '',
+    ),
+
     // === enrol related functions ===
 
     'core_enrol_get_enrolled_users_with_capability' => array(
@@ -895,7 +904,8 @@ $services = array(
             'moodle_user_get_users_by_courseid',
             'moodle_message_send_instantmessages',
             'core_course_get_contents',
-            'core_get_component_strings'),
+            'core_get_component_strings',
+            'core_user_add_user_device'),
         'enabled' => 0,
         'restrictedusers' => 0,
         'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE,
index 5409977..3b6c4c1 100644 (file)
@@ -2576,5 +2576,37 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2013092700.01);
     }
 
+    if ($oldversion < 2013100400.01) {
+        // Add user_devices core table.
+
+        // Define field id to be added to user_devices.
+        $table = new xmldb_table('user_devices');
+
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
+        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
+        $table->add_field('appid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'userid');
+        $table->add_field('name', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'appid');
+        $table->add_field('model', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'name');
+        $table->add_field('platform', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'model');
+        $table->add_field('version', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'platform');
+        $table->add_field('pushid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'version');
+        $table->add_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'pushid');
+        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'uuid');
+        $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated');
+
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+        $table->add_key('pushid-userid', XMLDB_KEY_UNIQUE, array('pushid', 'userid'));
+        $table->add_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform'));
+        $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
+
+        if (!$dbman->table_exists($table)) {
+            $dbman->create_table($table);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2013100400.01);
+
+    }
+
     return true;
 }
index 787ddb5..6cc2e04 100644 (file)
@@ -988,6 +988,106 @@ class core_user_external extends external_api {
         return null;
     }
 
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters
+     * @since Moodle 2.6
+     */
+    public static function add_user_device_parameters() {
+        return new external_function_parameters(
+            array(
+                'appid'     => new external_value(PARAM_NOTAGS, 'the app id, usually something like com.moodle.moodlemobile'),
+                'name'      => new external_value(PARAM_NOTAGS, 'the device name, \'occam\' or \'iPhone\' etc.'),
+                'model'     => new external_value(PARAM_NOTAGS, 'the device model \'Nexus4\' or \'iPad1,1\' etc.'),
+                'platform'  => new external_value(PARAM_NOTAGS, 'the device platform \'iOS\' or \'Android\' etc.'),
+                'version'   => new external_value(PARAM_NOTAGS, 'the device version \'6.1.2\' or \'4.2.2\' etc.'),
+                'pushid'    => new external_value(PARAM_RAW, 'the device PUSH token/key/identifier/registration id'),
+                'uuid'      => new external_value(PARAM_RAW, 'the device UUID')
+            )
+        );
+    }
+
+    /**
+     * Add a user device in Moodle database (for PUSH notifications usually).
+     *
+     * @param string $appid The app id, usually something like com.moodle.moodlemobile.
+     * @param string $name The device name, occam or iPhone etc.
+     * @param string $model The device model Nexus4 or iPad1.1 etc.
+     * @param string $platform The device platform iOs or Android etc.
+     * @param string $version The device version 6.1.2 or 4.2.2 etc.
+     * @param string $pushid The device PUSH token/key/identifier/registration id.
+     * @param string $uuid The device UUID.
+     * @return array List of possible warnings.
+     * @since Moodle 2.6
+     */
+    public static function add_user_device($appid, $name, $model, $platform, $version, $pushid, $uuid) {
+        global $CFG, $USER, $DB;
+        require_once($CFG->dirroot . "/user/lib.php");
+
+        $params = self::validate_parameters(self::add_user_device_parameters(),
+                array('appid' => $appid,
+                      'name' => $name,
+                      'model' => $model,
+                      'platform' => $platform,
+                      'version' => $version,
+                      'pushid' => $pushid,
+                      'uuid' => $uuid
+                      ));
+
+        $warnings = array();
+
+        // Prevent duplicate keys for users.
+        if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'userid' => $USER->id))) {
+            $warnings['warning'][] = array(
+                'item' => $params['pushid'],
+                'warningcode' => 'existingkeyforthisuser',
+                'message' => 'This key is already stored for this user'
+            );
+            return $warnings;
+        }
+
+        // The same key can't exists for the same platform.
+        if ($DB->get_record('user_devices', array('pushid' => $params['pushid'], 'platform' => $params['platform']))) {
+            $warnings['warning'][] = array(
+                'item' => $params['pushid'],
+                'warningcode' => 'existingkeyforplatform',
+                'message' => 'This key is already stored for other device using the same platform'
+            );
+            return $warnings;
+        }
+
+        $userdevice = new stdclass;
+        $userdevice->userid     = $USER->id;
+        $userdevice->appid      = $params['appid'];
+        $userdevice->name       = $params['name'];
+        $userdevice->model      = $params['model'];
+        $userdevice->platform   = $params['platform'];
+        $userdevice->version    = $params['version'];
+        $userdevice->pushid     = $params['pushid'];
+        $userdevice->uuid       = $params['uuid'];
+        $userdevice->timecreated  = time();
+        $userdevice->timemodified = $userdevice->timecreated;
+
+        if (!$DB->insert_record('user_devices', $userdevice)) {
+            throw new moodle_exception("There was a problem saving in the database the device with key: " . $params['pushid']);
+        }
+
+        return $warnings;
+    }
+
+    /**
+     * Returns description of method result value.
+     *
+     * @return external_multiple_structure
+     * @since Moodle 2.6
+     */
+    public static function add_user_device_returns() {
+        return new external_multiple_structure(
+           new external_warnings()
+        );
+    }
+
 }
 
  /**
index 011cc71..9405a77 100644 (file)
@@ -712,4 +712,33 @@ class core_user_externallib_testcase extends externallib_advanced_testcase {
         $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
         $this->assertNotEmpty($file);
     }
+
+    /**
+     * Test add user device
+     */
+    public function test_add_user_device() {
+        global $USER, $CFG, $DB;
+
+        $this->resetAfterTest(true);
+
+        $device = array(
+                'appid' => 'com.moodle.moodlemobile',
+                'name' => 'occam',
+                'model' => 'Nexus 4',
+                'platform' => 'Android',
+                'version' => '4.2.2',
+                'pushid' => 'apushdkasdfj4835',
+                'uuid' => 'asdnfl348qlksfaasef859'
+                );
+
+        // Call the external function.
+        core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
+                                            $device['version'], $device['pushid'], $device['uuid']);
+
+        $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
+        $created = (array) $created;
+
+        $this->assertEquals($device, array_intersect_key((array)$created, $device));
+    }
+
 }
index 7f9a43a..9415705 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2013100400.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2013100400.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.