Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lbplanner/classes/helpers/invite_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion lbplanner/classes/helpers/notifications_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lbplanner/classes/helpers/plan_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
23 changes: 8 additions & 15 deletions lbplanner/services/plan/accept_invite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 7 additions & 15 deletions lbplanner/services/plan/decline_invite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/**
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lbplanner/services/plan/invite_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
Loading