MDL-56045 message_airnotifier: Move enable device code to manager class

This commit is contained in:
Juan Leyva 2016-09-21 10:44:56 +01:00
parent 618cba3494
commit 95f53a652a
2 changed files with 27 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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;