From 3221718eda0e3666f357978cd9d629c6da077504 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Mon, 9 Feb 2015 18:06:10 +0100 Subject: [PATCH] MDL-48716 webservices: New ws core_user_remove_user_device --- lib/db/install.xml | 3 ++ lib/db/services.php | 12 +++++++- lib/db/upgrade.php | 9 ++++++ user/externallib.php | 70 ++++++++++++++++++++++++++++++++++++++++++++ user/lib.php | 25 ++++++++++++++++ version.php | 2 +- 6 files changed, 119 insertions(+), 2 deletions(-) diff --git a/lib/db/install.xml b/lib/db/install.xml index 58158267513..844fcaf9225 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -2982,6 +2982,9 @@ + + + diff --git a/lib/db/services.php b/lib/db/services.php index 9aa27c11d66..99640aa7ef9 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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, diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 2b03cf69b5e..522656a4a78 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/user/externallib.php b/user/externallib.php index 813f5fee24e..0eb2418780f 100644 --- a/user/externallib.php +++ b/user/externallib.php @@ -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(), + ) + ); + } + } /** diff --git a/user/lib.php b/user/lib.php index e876dc3d5d6..737c79328b7 100644 --- a/user/lib.php +++ b/user/lib.php @@ -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; +} diff --git a/version.php b/version.php index 98b3972cd71..31ec4fc87a6 100644 --- a/version.php +++ b/version.php @@ -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. -- 2.43.0