diff --git a/message/output/airnotifier/classes/manager.php b/message/output/airnotifier/classes/manager.php index 6af514fb8ee..6b3aefc1922 100644 --- a/message/output/airnotifier/classes/manager.php +++ b/message/output/airnotifier/classes/manager.php @@ -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); + } + } diff --git a/message/output/airnotifier/rest.php b/message/output/airnotifier/rest.php index 018b3bfcb6c..ee8b7a268e6 100644 --- a/message/output/airnotifier/rest.php +++ b/message/output/airnotifier/rest.php @@ -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; \ No newline at end of file