mirror of
https://github.com/moodle/moodle.git
synced 2025-04-18 23:15:38 +02:00
MDL-41914 New webservice core_user_add_user_device
This commit is contained in:
parent
e10df76f49
commit
6a403810b9
@ -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,
|
||||
|
@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user