<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>
'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(
'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,
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;
}
);
}
+ /**
+ * 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(),
+ )
+ );
+ }
+
}
/**
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;
+}
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.