diff --git a/lbplanner/classes/helpers/invite_helper.php b/lbplanner/classes/helpers/invite_helper.php index 4cb1921b..abb50b80 100644 --- a/lbplanner/classes/helpers/invite_helper.php +++ b/lbplanner/classes/helpers/invite_helper.php @@ -17,6 +17,7 @@ namespace local_lbplanner\helpers; use core_external\{external_single_structure, external_value}; +use local_lbplanner\enums\PLAN_INVITE_STATE; /** * Helper class for plan invites @@ -45,4 +46,16 @@ public static function structure(): external_single_structure { ] ); } + + /** + * Throw error if the invite status isn't pending + * @param int $status the invite status + * @throws moodle_exception + */ + public static function assert_invite_pending(int $status) { + if ($status !== PLAN_INVITE_STATE::PENDING) { + $name = PLAN_INVITE_STATE::name_from($status); + throw new \moodle_exception('Invite already '.$name); + } + } } diff --git a/lbplanner/classes/helpers/notifications_helper.php b/lbplanner/classes/helpers/notifications_helper.php index 3487c4c9..ac768511 100644 --- a/lbplanner/classes/helpers/notifications_helper.php +++ b/lbplanner/classes/helpers/notifications_helper.php @@ -59,7 +59,7 @@ public static function structure(): external_single_structure { * Notifies the given user about the given event, with the given info. * * @param int $userid The user to notify. - * @param int $info Additional information as stringified json. + * @param int $info Additional information as an integer. * @param int $type The type of notification. * * @return integer The id of the notification. diff --git a/lbplanner/classes/helpers/plan_helper.php b/lbplanner/classes/helpers/plan_helper.php index 7f3966f4..976cf7bf 100644 --- a/lbplanner/classes/helpers/plan_helper.php +++ b/lbplanner/classes/helpers/plan_helper.php @@ -82,7 +82,7 @@ public static function get_owner(int $planid): int { 'userid', ['planid' => $planid, 'accesstype' => PLAN_ACCESS_TYPE::OWNER] ); - return $owner; + return intval($owner); } /** diff --git a/lbplanner/services/plan/accept_invite.php b/lbplanner/services/plan/accept_invite.php index 0b389772..2e28c200 100644 --- a/lbplanner/services/plan/accept_invite.php +++ b/lbplanner/services/plan/accept_invite.php @@ -20,6 +20,7 @@ use local_lbplanner\helpers\plan_helper; use local_lbplanner\helpers\notifications_helper; use local_lbplanner\enums\{PLAN_ACCESS_TYPE, PLAN_INVITE_STATE, NOTIF_TRIGGER}; +use local_lbplanner\helpers\invite_helper; /** * Accept an invite to the plan. @@ -54,23 +55,15 @@ public static function accept_invite(int $inviteid) { 'inviteid' => $inviteid, ]); - if (!$DB->record_exists(plan_helper::INVITES_TABLE, ['id' => $inviteid, 'inviteeid' => $USER->id])) { + $invite = $DB->get_record( + plan_helper::INVITES_TABLE, + ['id' => $inviteid], + ); + + if ($invite === false) { throw new \moodle_exception('Invite not found'); } - if (!$DB->record_exists(plan_helper::INVITES_TABLE, - [ 'id' => $inviteid, 'inviteeid' => $USER->id, 'status' => PLAN_INVITE_STATE::PENDING])) { - throw new \moodle_exception('Invite already accepted or declined'); - } - - $invite = $DB->get_record(plan_helper::INVITES_TABLE, - [ - 'id' => $inviteid, - 'inviteeid' => $USER->id, - 'status' => PLAN_INVITE_STATE::PENDING, - ], - '*', - MUST_EXIST - ); + invite_helper::assert_invite_pending($invite->status); // Notify the user that invite has been accepted. notifications_helper::notify_user( diff --git a/lbplanner/services/plan/decline_invite.php b/lbplanner/services/plan/decline_invite.php index 1e3712ef..608b0b8b 100644 --- a/lbplanner/services/plan/decline_invite.php +++ b/lbplanner/services/plan/decline_invite.php @@ -17,8 +17,7 @@ namespace local_lbplanner_services; use core_external\{external_api, external_function_parameters, external_value}; -use local_lbplanner\helpers\plan_helper; -use local_lbplanner\helpers\notifications_helper; +use local_lbplanner\helpers\{plan_helper, notifications_helper, invite_helper}; use local_lbplanner\enums\{NOTIF_TRIGGER, PLAN_INVITE_STATE}; /** @@ -54,22 +53,15 @@ public static function decline_invite(int $inviteid) { 'inviteid' => $inviteid, ]); - if (!$DB->record_exists(plan_helper::INVITES_TABLE, ['id' => $inviteid, 'inviteeid' => $USER->id])) { - throw new \moodle_exception('Invite not found'); - } - - $invite = $DB->get_record(plan_helper::INVITES_TABLE, - [ - 'id' => $inviteid, - 'inviteeid' => $USER->id, - ], - '*', - MUST_EXIST + $invite = $DB->get_record( + plan_helper::INVITES_TABLE, + ['id' => $inviteid], ); - if ($invite->status !== PLAN_INVITE_STATE::PENDING) { - throw new \moodle_exception('Invite already accepted or declined'); + if ($invite === false) { + throw new \moodle_exception('Invite not found'); } + invite_helper::assert_invite_pending($invite->status); // Notify the user that invite has been declined. notifications_helper::notify_user( diff --git a/lbplanner/services/plan/invite_user.php b/lbplanner/services/plan/invite_user.php index 9dcae19a..53714f6e 100644 --- a/lbplanner/services/plan/invite_user.php +++ b/lbplanner/services/plan/invite_user.php @@ -63,7 +63,7 @@ public static function invite_user(int $inviteeid): mixed { $planid = plan_helper::get_plan_id($USER->id); - if (plan_helper::get_owner($planid) !== $USER->id) { + if (plan_helper::get_owner($planid) !== intval($USER->id)) { throw new \moodle_exception('Access denied'); }