Skip to content

Commit

Permalink
Merge branch 'MDL-48716-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	lib/db/services.php
	lib/db/upgrade.php
	version.php
  • Loading branch information
andrewnicols committed Feb 25, 2015
2 parents 1725c55 + f494a81 commit db23cd0
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,9 @@
<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>
Expand Down
12 changes: 11 additions & 1 deletion lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,15 @@
'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(
Expand Down Expand Up @@ -995,7 +1004,8 @@
'core_message_search_contacts',
'core_message_get_blocked_users',
'gradereport_user_get_grades_table',
'core_group_get_course_user_groups'
'core_group_get_course_user_groups',
'core_user_remove_user_device',
),
'enabled' => 0,
'restrictedusers' => 0,
Expand Down
9 changes: 9 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4201,5 +4201,14 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2015022401.00);
}

if ($oldversion < 2015022500.00) {
$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, 2015022500.00);
}

return true;
}
70 changes: 70 additions & 0 deletions user/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,76 @@ public static function add_user_device_returns() {
);
}

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

}

/**
Expand Down
25 changes: 25 additions & 0 deletions user/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
54 changes: 54 additions & 0 deletions user/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,4 +751,58 @@ public function test_add_user_device() {
$this->assertEquals($device, array_intersect_key((array)$created, $device));
}

/**
* Test remove user device
*/
public function test_remove_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' => 'ABCDE3723ksdfhasfaasef859'
);

// A device with the same properties except the appid and pushid.
$device2 = $device;
$device2['pushid'] = "0987654321";
$device2['appid'] = "other.app.com";

// Create a user device using the external API function.
core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
$device['version'], $device['pushid'], $device['uuid']);

// Create the same device but for a different app.
core_user_external::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'],
$device2['version'], $device2['pushid'], $device2['uuid']);

// Try to remove a device that does not exist.
$result = core_user_external::remove_user_device('1234567890');
$result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
$this->assertFalse($result['removed']);
$this->assertCount(1, $result['warnings']);

// Try to remove a device that does not exist for an existing app.
$result = core_user_external::remove_user_device('1234567890', $device['appid']);
$result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
$this->assertFalse($result['removed']);
$this->assertCount(1, $result['warnings']);

// Remove an existing device for an existing app. This will remove one of the two devices.
$result = core_user_external::remove_user_device($device['uuid'], $device['appid']);
$result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
$this->assertTrue($result['removed']);

// Remove all the devices. This must remove the remaining device.
$result = core_user_external::remove_user_device($device['uuid']);
$result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
$this->assertTrue($result['removed']);
}

}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

$version = 2015022402.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015022500.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit db23cd0

Please sign in to comment.