MDL-48716 webservices: New ws core_user_remove_user_device
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 9 Feb 2015 17:06:10 +0000 (18:06 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 19 Feb 2015 14:15:18 +0000 (15:15 +0100)
lib/db/install.xml
lib/db/services.php
lib/db/upgrade.php
user/externallib.php
user/lib.php
version.php

index 5815826..844fcaf 100644 (file)
         <KEY NAME="pushid-userid" TYPE="unique" FIELDS="pushid, userid"/>
         <KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
       </KEYS>
+      <INDEXES>
+        <INDEX NAME="uuid-userid" UNIQUE="false" FIELDS="uuid, userid" COMMENT="Index on uuid and userid"/>
+      </INDEXES>
     </TABLE>
     <TABLE NAME="user_password_resets" COMMENT="table tracking password reset confirmation tokens">
       <FIELDS>
index 9aa27c1..99640aa 100644 (file)
@@ -465,6 +465,15 @@ $functions = array(
         'capabilities'=> '',
     ),
 
+    'core_user_remove_user_device' => array(
+        'classname'     => 'core_user_external',
+        'methodname'    => 'remove_user_device',
+        'classpath'     => 'user/externallib.php',
+        'description'   => 'Remove a user device from the Moodle database.',
+        'type'          => 'write',
+        'capabilities'  => '',
+    ),
+
     // === enrol related functions ===
 
     'core_enrol_get_enrolled_users_with_capability' => array(
@@ -985,7 +994,8 @@ $services = array(
             'core_message_get_contacts',
             'core_message_search_contacts',
             'core_message_get_blocked_users',
-            'gradereport_user_get_grades_table'
+            'gradereport_user_get_grades_table',
+            'core_user_remove_user_device'
             ),
         'enabled' => 0,
         'restrictedusers' => 0,
index 2b03cf6..522656a 100644 (file)
@@ -4177,5 +4177,14 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2015021100.00);
     }
 
+    if ($oldversion < 2015021900.01) {
+        $table = new xmldb_table('user_devices');
+        $index = new xmldb_index('uuid-userid', XMLDB_INDEX_NOTUNIQUE, array('uuid', 'userid'));
+        if (!$dbman->index_exists($table, $index)) {
+            $dbman->add_index($table, $index);
+        }
+        upgrade_main_savepoint(true, 2015021900.01);
+    }
+
     return true;
 }
index 813f5fe..0eb2418 100644 (file)
@@ -1159,6 +1159,76 @@ class core_user_external extends external_api {
         );
     }
 
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters
+     * @since Moodle 2.9
+     */
+    public static function remove_user_device_parameters() {
+        return new external_function_parameters(
+            array(
+                'uuid'  => new external_value(PARAM_RAW, 'the device UUID'),
+                'appid' => new external_value(PARAM_NOTAGS,
+                                                'the app id, if empty devices matching the UUID for the user will be removed',
+                                                VALUE_DEFAULT, ''),
+            )
+        );
+    }
+
+    /**
+     * Remove a user device from the Moodle database (for PUSH notifications usually).
+     *
+     * @param string $uuid The device UUID.
+     * @param string $appid The app id, opitonal parameter. If empty all the devices fmatching the UUID or the user will be removed.
+     * @return array List of possible warnings and removal status.
+     * @since Moodle 2.9
+     */
+    public static function remove_user_device($uuid, $appid = "") {
+        global $CFG;
+        require_once($CFG->dirroot . "/user/lib.php");
+
+        $params = self::validate_parameters(self::remove_user_device_parameters(), array('uuid' => $uuid, 'appid' => $appid));
+
+        $context = context_system::instance();
+        self::validate_context($context);
+
+        // Warnings array, it can be empty at the end but is mandatory.
+        $warnings = array();
+
+        $removed = user_remove_user_device($params['uuid'], $params['appid']);
+
+        if (!$removed) {
+            $warnings[] = array(
+                'item' => $params['uuid'],
+                'warningcode' => 'devicedoesnotexist',
+                'message' => 'The device doesn\'t exists in the database'
+            );
+        }
+
+        $result = array(
+            'removed' => $removed,
+            'warnings' => $warnings
+        );
+
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value.
+     *
+     * @return external_multiple_structure
+     * @since Moodle 2.9
+     */
+    public static function remove_user_device_returns() {
+        return new external_single_structure(
+            array(
+                'removed' => new external_value(PARAM_BOOL, 'True if removed, false if not removed because it doesn\'t exists'),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
+
 }
 
  /**
index e876dc3..737c793 100644 (file)
@@ -977,3 +977,28 @@ function user_is_previously_used_password($userid, $password) {
 
     return $reused;
 }
+
+/**
+ * Remove a user device from the Moodle database (for PUSH notifications usually).
+ *
+ * @param string $uuid The device UUID.
+ * @param string $appid The app id. If empty all the devices matching the UUID for the user will be removed.
+ * @return bool true if removed, false if the device didn't exists in the database
+ * @since Moodle 2.9
+ */
+function user_remove_user_device($uuid, $appid = "") {
+    global $DB, $USER;
+
+    $conditions = array('uuid' => $uuid, 'userid' => $USER->id);
+    if (!empty($appid)) {
+        $conditions['appid'] = $appid;
+    }
+
+    if (!$DB->count_records('user_devices', $conditions)) {
+        return false;
+    }
+
+    $DB->delete_records('user_devices', $conditions);
+
+    return true;
+}
index 98b3972..31ec4fc 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2015021900.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2015021900.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.