mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
Merge branch 'MDL-56045-master' of git://github.com/jleyva/moodle
This commit is contained in:
commit
6036184fab
@ -186,4 +186,28 @@ class message_airnotifier_manager {
|
||||
!empty($CFG->airnotifiermobileappname));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables a registered user device so it can receive Push notifications
|
||||
*
|
||||
* @param int $deviceid the device id
|
||||
* @param bool $enable true to enable it, false to disable it
|
||||
* @return bool true if the device was enabled, false in case of error
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function enable_device($deviceid, $enable) {
|
||||
global $DB, $USER;
|
||||
|
||||
if (!$device = $DB->get_record('message_airnotifier_devices', array('id' => $deviceid), '*')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check that the device belongs to the current user.
|
||||
if (!$userdevice = $DB->get_record('user_devices', array('id' => $device->userdeviceid, 'userid' => $USER->id), '*')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$device->enable = $enable;
|
||||
return $DB->update_record('message_airnotifier_devices', $device);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -42,4 +42,21 @@ $functions = array(
|
||||
'type' => 'read',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'message_airnotifier_get_user_devices' => array(
|
||||
'classname' => 'message_airnotifier_external',
|
||||
'methodname' => 'get_user_devices',
|
||||
'classpath' => 'message/output/airnotifier/externallib.php',
|
||||
'description' => 'Return the list of mobile devices that are registered in Moodle for the given user',
|
||||
'type' => 'read',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'message_airnotifier_enable_device' => array(
|
||||
'classname' => 'message_airnotifier_external',
|
||||
'methodname' => 'enable_device',
|
||||
'classpath' => 'message/output/airnotifier/externallib.php',
|
||||
'description' => 'Enables or disables a registered user device so it can receive Push notifications',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'message/airnotifier:managedevice',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
);
|
||||
|
@ -212,4 +212,177 @@ class message_airnotifier_external extends external_api {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function get_user_devices_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'appid' => new external_value(PARAM_NOTAGS, 'App unique id (usually a reversed domain)'),
|
||||
'userid' => new external_value(PARAM_INT, 'User id, 0 for current user', VALUE_DEFAULT, 0)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of mobile devices that are registered in Moodle for the given user.
|
||||
*
|
||||
* @param string $appid app unique id (usually a reversed domain)
|
||||
* @param integer $userid the user id, 0 for current user
|
||||
* @return array warnings and devices
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function get_user_devices($appid, $userid = 0) {
|
||||
global $USER;
|
||||
|
||||
$params = self::validate_parameters(
|
||||
self::get_user_devices_parameters(),
|
||||
array(
|
||||
'appid' => $appid,
|
||||
'userid' => $userid,
|
||||
)
|
||||
);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
|
||||
if (empty($params['userid'])) {
|
||||
$user = $USER;
|
||||
} else {
|
||||
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
|
||||
core_user::require_active_user($user);
|
||||
// Allow only admins to retrieve other users devices.
|
||||
if ($user->id != $USER->id) {
|
||||
require_capability('moodle/site:config', $context);
|
||||
}
|
||||
}
|
||||
|
||||
$warnings = array();
|
||||
$devices = array();
|
||||
// Check if mobile notifications are enabled.
|
||||
if (!self::is_system_configured()) {
|
||||
$warnings[] = array(
|
||||
'item' => 'user',
|
||||
'itemid' => $user->id,
|
||||
'warningcode' => 'systemnotconfigured',
|
||||
'message' => 'Mobile notifications are not configured'
|
||||
);
|
||||
} else {
|
||||
// We catch exceptions here because get_user_devices may try to connect to Airnotifier.
|
||||
try {
|
||||
$manager = new message_airnotifier_manager();
|
||||
$devices = $manager->get_user_devices($appid, $user->id);
|
||||
} catch (Exception $e) {
|
||||
$warnings[] = array(
|
||||
'item' => 'user',
|
||||
'itemid' => $user->id,
|
||||
'warningcode' => 'errorgettingdevices',
|
||||
'message' => $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'devices' => $devices,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function get_user_devices_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'devices' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array (
|
||||
'id' => new external_value(PARAM_INT, 'Device id (in the message_airnotifier table)'),
|
||||
'appid' => new external_value(PARAM_NOTAGS, 'The app id, 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'),
|
||||
'enable' => new external_value(PARAM_INT, 'Whether the device is enabled or not'),
|
||||
'timecreated' => new external_value(PARAM_INT, 'Time created'),
|
||||
'timemodified' => new external_value(PARAM_INT, 'Time modified'),
|
||||
)
|
||||
),
|
||||
'List of devices'
|
||||
),
|
||||
'warnings' => new external_warnings()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function enable_device_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'deviceid' => new external_value(PARAM_INT, 'The device id'),
|
||||
'enable' => new external_value(PARAM_BOOL, 'True for enable the device, false otherwise')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables a registered user device so it can receive Push notifications
|
||||
*
|
||||
* @param integer $deviceid the device id
|
||||
* @param bool $enable whether to enable the device
|
||||
* @return array warnings and success status
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function enable_device($deviceid, $enable) {
|
||||
global $USER;
|
||||
|
||||
$params = self::validate_parameters(
|
||||
self::enable_device_parameters(),
|
||||
array(
|
||||
'deviceid' => $deviceid,
|
||||
'enable' => $enable,
|
||||
)
|
||||
);
|
||||
|
||||
$context = context_system::instance();
|
||||
self::validate_context($context);
|
||||
require_capability('message/airnotifier:managedevice', $context);
|
||||
|
||||
if (!message_airnotifier_manager::enable_device($params['deviceid'], $params['enable'])) {
|
||||
throw new moodle_exception('unknowndevice', 'message_airnotifier');
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => true,
|
||||
'warnings' => array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
public static function enable_device_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'success' => new external_value(PARAM_BOOL, 'True if success'),
|
||||
'warnings' => new external_warnings()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -46,14 +46,10 @@ echo $OUTPUT->header();
|
||||
// Response class to be converted to json string.
|
||||
$response = new stdClass();
|
||||
|
||||
$device = $DB->get_record('message_airnotifier_devices', array('id' => $id), '*', MUST_EXIST);
|
||||
if (!message_airnotifier_manager::enable_device($id, $enable)) {
|
||||
throw new moodle_exception('unknowndevice', 'message_airnotifier');
|
||||
}
|
||||
|
||||
// Check that the device belongs to the current user.
|
||||
$userdevice = $DB->get_record('user_devices', array('id' => $device->userdeviceid, 'userid' => $USER->id), '*', MUST_EXIST);
|
||||
|
||||
$device->enable = required_param('enable', PARAM_BOOL);
|
||||
$DB->update_record('message_airnotifier_devices', $device);
|
||||
$response->success = true;
|
||||
|
||||
echo json_encode($response);
|
||||
die;
|
@ -135,4 +135,93 @@ class message_airnotifier_external_testcase extends externallib_advanced_testcas
|
||||
$this->assertEquals($expected, $preferences['users']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_devices
|
||||
*/
|
||||
public function test_get_user_devices() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/user/externallib.php');
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
|
||||
// System not configured.
|
||||
$devices = message_airnotifier_external::get_user_devices('');
|
||||
$devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
|
||||
$this->assertCount(1, $devices['warnings']);
|
||||
$this->assertEquals('systemnotconfigured', $devices['warnings'][0]['warningcode']);
|
||||
|
||||
// Fake configuration.
|
||||
set_config('airnotifieraccesskey', random_string());
|
||||
// Enable the plugin.
|
||||
$DB->set_field('message_processors', 'enabled', 1, array('name' => 'airnotifier'));
|
||||
|
||||
// Get devices.
|
||||
$devices = message_airnotifier_external::get_user_devices('');
|
||||
$devices = external_api::clean_returnvalue(message_airnotifier_external::get_user_devices_returns(), $devices);
|
||||
$this->assertCount(0, $devices['warnings']);
|
||||
// No devices, unfortunatelly we cannot create devices (we can't mock airnotifier server).
|
||||
$this->assertCount(0, $devices['devices']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_devices permissions
|
||||
*/
|
||||
public function test_get_user_devices_permissions() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/user/externallib.php');
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$otheruser = self::getDataGenerator()->create_user();
|
||||
self::setUser($user);
|
||||
|
||||
// No permission to get other users devices.
|
||||
$this->expectException('required_capability_exception');
|
||||
$devices = message_airnotifier_external::get_user_devices('', $otheruser->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enable_device
|
||||
*/
|
||||
public function test_enable_device() {
|
||||
global $USER, $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
|
||||
// Add fake core device.
|
||||
$device = array(
|
||||
'appid' => 'com.moodle.moodlemobile',
|
||||
'name' => 'occam',
|
||||
'model' => 'Nexus 4',
|
||||
'platform' => 'Android',
|
||||
'version' => '4.2.2',
|
||||
'pushid' => 'apushdkasdfj4835',
|
||||
'uuid' => 'asdnfl348qlksfaasef859',
|
||||
'userid' => $USER->id,
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
);
|
||||
$coredeviceid = $DB->insert_record('user_devices', (object) $device);
|
||||
|
||||
$airnotifierdev = array(
|
||||
'userdeviceid' => $coredeviceid,
|
||||
'enable' => 1
|
||||
);
|
||||
$airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev);
|
||||
|
||||
// Disable and enable.
|
||||
$result = message_airnotifier_external::enable_device($airnotifierdevid, false);
|
||||
$result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
|
||||
$this->assertCount(0, $result['warnings']);
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertEquals(0, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
|
||||
|
||||
$result = message_airnotifier_external::enable_device($airnotifierdevid, true);
|
||||
$result = external_api::clean_returnvalue(message_airnotifier_external::enable_device_returns(), $result);
|
||||
$this->assertCount(0, $result['warnings']);
|
||||
$this->assertTrue($result['success']);
|
||||
$this->assertEquals(1, $DB->get_field('message_airnotifier_devices', 'enable', array('id' => $airnotifierdevid)));
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,6 @@
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2016052300; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->version = 2016052303; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2016051900; // Requires this Moodle version.
|
||||
$plugin->component = 'message_airnotifier'; // Full name of the plugin (used for diagnostics).
|
||||
|
Loading…
x
Reference in New Issue
Block a user