Skip to content

Commit c706e66

Browse files
committed
Calendar: remove future absence when deleting participant or editing the date
1 parent 023f529 commit c706e66

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ v31.0.00
4343
Attendance: student attendance history now ordered by timetable period
4444
Attendance: fixed the Consecutive Absences check to only count the final status for each day
4545
Calendar: switched default sorting of Manage Events to newest first
46+
Calendar: remove future absence when deleting participant or editing the date
4647
Formal Assessment: added the feature for teachers to view unpublished internal assessments
4748
Library: updated automatic shelf lists so that lists are queried dynamically in Browse Library
4849
Library: added the option to edit the category and sub-category for an automatic shelf

modules/Calendar/calendar_event_edit.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
if (empty($gibbonCalendarEventID) && isset($_GET['editID'])) {
4141
$page->return->setEditLink($session->get('absoluteURL').'/index.php?q=/modules/Calendar/calendar_event_edit.php&gibbonCalendarEventID='.$_GET['editID']);
4242
}
43+
44+
$page->return->addReturns([
45+
'warning9' => __('Your request was completed successfully, but previous future absences for students in this event have been removed because the event date or time changed. Please set up future absences again.'),
46+
]);
4347

4448
$calendarEventGateway = $container->get(CalendarEventGateway::class);
4549
$calendarEventPersonGateway = $container->get(CalendarEventPersonGateway::class);

modules/Calendar/calendar_event_editProcess.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
use Gibbon\Data\Validator;
21+
use Gibbon\Domain\Attendance\AttendanceLogPersonGateway;
2122
use Gibbon\Domain\Calendar\CalendarEventGateway;
2223
use Gibbon\Domain\Calendar\CalendarEventPersonGateway;
2324
use Gibbon\Support\Facades\Access;
@@ -40,6 +41,7 @@
4041

4142
$calendarEventGateway = $container->get(CalendarEventGateway::class);
4243
$calendarEventPersonGateway = $container->get(CalendarEventPersonGateway::class);
44+
$attendanceLogPersonGateway = $container->get(AttendanceLogPersonGateway::class);
4345

4446
// Get event details
4547
$event = $calendarEventGateway->getEventDetailsByID($gibbonCalendarEventID, $session->get('gibbonPersonID'));
@@ -92,23 +94,26 @@
9294
header("Location: {$URL}");
9395
}
9496

97+
// Check if the date or time has changed compared to the existing event
98+
$dateTimeChanged = $data['dateStart'] !== $event['dateStart'] || $data['dateEnd'] !== $event['dateEnd'] || $data['timeStart'] !== $event['timeStart'] || $data['timeEnd'] !== $event['timeEnd'];
99+
95100
// Update the record
96101
if (!$calendarEventGateway->update($gibbonCalendarEventID, $data)) {
97102
$URL .= '&return=error2';
98103
header("Location: {$URL}");
99104
}
100105

101-
$organiser = $calendarEventPersonGateway->selectBy(['gibbonCalendarEventID' => $gibbonCalendarEventID, 'role' => 'Organiser', 'gibbonPersonID' => $gibbonPersonIDOrganiser])->fetch();
106+
$organiser = $calendarEventPersonGateway->selectBy(['gibbonCalendarEventID' => $gibbonCalendarEventID, 'role' => 'Organiser', 'gibbonPersonID' => $gibbonPersonIDOrganiser])->fetch();
102107

103108
if (empty($organiser)) {
104109
$organiserData = [
105-
'gibbonCalendarEventID' => $gibbonCalendarEventID,
106-
'gibbonPersonID' => $gibbonPersonIDOrganiser,
107-
'role' => 'Organiser',
108-
'gibbonPersonIDModified' => $session->get('gibbonPersonID') ?? '',
109-
'timestampModified' => date('Y-m-d H:i:s'),
110-
'timestampCreated' => date('Y-m-d H:i:s'),
111-
'gibbonPersonIDCreated' => $session->get('gibbonPersonID') ?? '',
110+
'gibbonCalendarEventID' => $gibbonCalendarEventID,
111+
'gibbonPersonID' => $gibbonPersonIDOrganiser,
112+
'role' => 'Organiser',
113+
'gibbonPersonIDModified' => $session->get('gibbonPersonID') ?? '',
114+
'timestampModified' => date('Y-m-d H:i:s'),
115+
'timestampCreated' => date('Y-m-d H:i:s'),
116+
'gibbonPersonIDCreated' => $session->get('gibbonPersonID') ?? '',
112117
];
113118

114119
$inserted = $calendarEventPersonGateway->insertAndUpdate($organiserData, $organiserData);
@@ -135,9 +140,40 @@
135140
$inserted = $calendarEventPersonGateway->insertAndUpdate($personData, $personData);
136141
$partialFail &= !$inserted;
137142
}
138-
139-
$URL .= $partialFail
140-
? "&return=warning1"
141-
: "&return=success0&editID=$gibbonCalendarEventID";
142-
header("Location: {$URL}");
143+
144+
// If event date or time changed, remove future absences for all student participants using the OLD dates
145+
$absencesRemoved = false;
146+
if ($dateTimeChanged) {
147+
$criteria = $calendarEventPersonGateway->newQueryCriteria()
148+
->sortBy(['roleCategory', 'surname', 'preferredName']);
149+
150+
$participants = $calendarEventPersonGateway->queryEventAttendees($criteria, $gibbonCalendarEventID);
151+
152+
$studentPersonIDs = array_reduce($participants->toArray(), function ($group, $item) {
153+
if ($item['roleCategory'] == 'Student') $group[] = $item['gibbonPersonID'];
154+
return $group;
155+
}, []);
156+
157+
if (!empty($studentPersonIDs)) {
158+
// Query using the OLD event dates/times before the update
159+
$futureAbsences = $event['allDay'] == 'Y' ? $attendanceLogPersonGateway->selectFutureAttendanceLogsByDate($event['dateStart'], $event['dateEnd'])->fetchAll() : $attendanceLogPersonGateway->selectFutureAttendanceLogsByDateAndTime($event['dateStart'], $event['dateEnd'], $event['timeStart'], $event['timeEnd'])->fetchAll();
160+
161+
foreach ($futureAbsences as $absence) {
162+
if (in_array($absence['groupBy'], $studentPersonIDs)) {
163+
$futureAbsenceDeleted = $attendanceLogPersonGateway->delete($absence['gibbonAttendanceLogPersonID']);
164+
$absencesRemoved = true;
165+
}
166+
}
167+
}
168+
}
169+
170+
if ($absencesRemoved) {
171+
$URL .= "&return=warning9&editID=$gibbonCalendarEventID";
172+
} elseif ($partialFail) {
173+
$URL .= "&return=warning1";
174+
} else {
175+
$URL .= "&return=success0&editID=$gibbonCalendarEventID";
176+
}
177+
178+
header("Location: {$URL}");
143179
}

0 commit comments

Comments
 (0)