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
5 changes: 2 additions & 3 deletions lbplanner/classes/enums/WEEKDAY.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
use local_lbplanner\polyfill\Enum;

/**
* All the days of the week.
* All seven of them.
* Yup.
* ISO 8601 numeric representation of the day of the week.
* Same as `(int)DateTime::format('N');`
*/
class WEEKDAY extends Enum {
/**
Expand Down
39 changes: 39 additions & 0 deletions lbplanner/classes/helpers/slot_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,42 @@ public static function get_all_slots(): array {
return $slotsobj;
}

/**
* Returns a list of all slots relevant for a vintage and range of weekdays.
*
* @param string $vintage the vintage to filter for
* @param int $today the starting day to filter for
* @param int $range the range in days
* @return slot[] An array of the slots.
*/
public static function get_vintage_time_slots(string $vintage, int $today, int $range): array {
global $DB;

if ($range < 7) {
$valid = [];
for ($i = $today; $i < ($today + $range); $i++) {
array_push($valid, (($i - 1) % 7) + 1);
}
$insert = " AND slot.weekday IN (" . implode(',', $valid) . ")";
} else {
$insert = '';
}

$slots = $DB->get_records_sql(
'SELECT slot.* FROM {' . self::TABLE_SLOTS . '} as slot ' .
'INNER JOIN {' . self::TABLE_SLOT_FILTERS . '} as filter ON slot.id=filter.slotid ' .
'WHERE (filter.vintage=? OR filter.vintage=NULL)' . $insert,
[$vintage]
);

$slotsobj = [];
foreach ($slots as $slot) {
array_push($slotsobj, slot::from_db($slot));
}

return $slotsobj;
}

/**
* Returns a list of all slots belonging to a supervisor.
* @param int $supervisorid userid of the supervisor in question
Expand Down Expand Up @@ -306,6 +342,9 @@ public static function filter_slots_for_user(array $allslots, \stdClass $user):
* @return slot[] the filtered slot array
*/
public static function filter_slots_for_time(array $allslots, int $range): array {
if ($range === 7) {
return $allslots;
}
$utctz = new DateTimeZone('UTC');
$now = new DateTimeImmutable('now', $utctz);
$slots = [];
Expand Down
12 changes: 8 additions & 4 deletions lbplanner/services/slots/get_my_slots.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace local_lbplanner_services;

use core_external\{external_api, external_function_parameters, external_multiple_structure};
use DateTimeImmutable;
use local_lbplanner\helpers\{config_helper, slot_helper};
use local_lbplanner\model\slot;

Expand All @@ -43,13 +44,16 @@ public static function get_my_slots_parameters(): external_function_parameters {
public static function get_my_slots(): array {
global $USER;

$allslots = slot_helper::get_all_slots();
$dayofweek = (int)(new DateTimeImmutable('today'))->format('N');
$allslots = slot_helper::get_vintage_time_slots(
$USER->address,
$dayofweek,
config_helper::get_slot_futuresight()
);

$myslots = slot_helper::filter_slots_for_user($allslots, $USER);

$returnslots = slot_helper::filter_slots_for_time($myslots, config_helper::get_slot_futuresight());

return array_map(fn(slot $slot) => $slot->prepare_for_api(), $returnslots);
return array_map(fn(slot $slot) => $slot->prepare_for_api(), $myslots);
}

/**
Expand Down
Loading