From 530e73e2a4b83f288e5be7d25dbcb8129ae8fbb0 Mon Sep 17 00:00:00 2001 From: Riedler Date: Sat, 18 Jan 2025 03:51:19 +0100 Subject: [PATCH] feat: added slots_remove_slot_supervisor API call --- lbplanner/db/services.php | 10 +++ .../services/slots/remove_slot_supervisor.php | 86 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 lbplanner/services/slots/remove_slot_supervisor.php diff --git a/lbplanner/db/services.php b/lbplanner/db/services.php index 8044688b..deddca49 100644 --- a/lbplanner/db/services.php +++ b/lbplanner/db/services.php @@ -341,6 +341,15 @@ 'capabilities' => 'local/lb_planner:teacher', 'ajax' => true, ], + 'local_lbplanner_slots_remove_slot_supervisor' => [ + 'classname' => 'local_lbplanner_services\slots_remove_slot_supervisor', + 'methodname' => 'remove_slot_supervisor', + 'classpath' => 'local/lbplanner/services/slots/remove_slot_supervisor.php', + 'description' => 'Removes supervisor from a slot', + 'type' => 'write', + 'capabilities' => 'local/lb_planner:teacher', + 'ajax' => true, + ], 'local_lbplanner_slots_add_slot_filter' => [ 'classname' => 'local_lbplanner_services\slots_add_slot_filter', 'methodname' => 'add_slot_filter', @@ -408,6 +417,7 @@ 'local_lbplanner_slots_get_slot_filters', 'local_lbplanner_slots_get_student_slots', 'local_lbplanner_slots_get_supervisor_slots', + 'local_lbplanner_slots_remove_slot_supervisor', ], 'restrictedusers' => 0, 'enabled' => 1, diff --git a/lbplanner/services/slots/remove_slot_supervisor.php b/lbplanner/services/slots/remove_slot_supervisor.php new file mode 100644 index 00000000..91e0caa5 --- /dev/null +++ b/lbplanner/services/slots/remove_slot_supervisor.php @@ -0,0 +1,86 @@ +. + +namespace local_lbplanner_services; + +use core_external\{external_api, external_function_parameters, external_value}; + +use local_lbplanner\helpers\slot_helper; + +/** + * Removes a supervisor from a slot + * + * @package local_lbplanner + * @subpackage services_slots + * @copyright 2025 necodeIT + * @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC-BY-NC-SA 4.0 International or later + */ +class slots_remove_slot_supervisor extends external_api { + /** + * Parameters for remove_slot_supervisor. + * @return external_function_parameters + */ + public static function remove_slot_supervisor_parameters(): external_function_parameters { + return new external_function_parameters([ + 'userid' => new external_value( + PARAM_INT, + 'ID of the user to be removed as a supervisor', + VALUE_REQUIRED, + null, + NULL_NOT_ALLOWED + ), + 'slotid' => new external_value( + PARAM_INT, + 'ID of the slot', + VALUE_REQUIRED, + null, + NULL_NOT_ALLOWED + ), + ]); + } + + /** + * Removes a supervisor from a slot + * @param int $userid ID of the user to be demoted + * @param int $slotid ID of the slot + */ + public static function remove_slot_supervisor(int $userid, int $slotid): void { + global $USER, $DB; + self::validate_parameters( + self::remove_slot_supervisor_parameters(), + [ + 'userid' => $userid, + 'slotid' => $slotid, + ] + ); + + // Check if current user is supervisor for this slot, throw error if not. + slot_helper::assert_slot_supervisor($USER->id, $slotid); + + $DB->delete_records( + slot_helper::TABLE_SUPERVISORS, + ['id' => $userid, 'slotid' => $slotid] + ); + } + + /** + * Return structure of remove_slot_supervisor + * @return null + */ + public static function remove_slot_supervisor_returns() { + return null; + } +}