Skip to content

Commit

Permalink
Slight refactor and additional phpdoc for get_availability
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed Aug 31, 2024
1 parent 3c392c3 commit 3bbab9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ jobs:
fail-fast: false
matrix:
include:
- php: '8.1'
moodle-branch: 'MOODLE_404_STABLE'
database: 'mariadb'
# - php: '8.1'
# moodle-branch: 'master'
# database: 'pgsql'
# moodle-branch: 'MOODLE_404_STABLE'
# database: 'mariadb'
- php: '8.1'
moodle-branch: 'master'
database: 'pgsql'
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
46 changes: 29 additions & 17 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,33 +311,45 @@ function update_availability(array $tabledata, \stdClass $driprelease) {


/**
* Get the date related availability for an activity
* Take an optional JSON string as input and return an array containing
* the date-related availability information.
* *
* If the input JSON is empty, the function returns an empty array.
*
* @param string $json
* @return array
* Loop through each restriction in the JSON object and checks
* if the restriction type is "date". If true, it extracts the operator and
* timestamp from the restriction object.
*
* Based on the operator, the function updates the availability array with
* the formatted date string using userdate() function.
*
* The function returns the availability array containing the 'from' and 'to'
* dates in a human-readable format.
* @param string $json The optional JSON input string containing the availability restrictions
* @return array The availability array containing the 'from' and 'to' dates
*/
function get_availability(?string $json): array {
global $USER;
$availability = [];

if ($json > "" && $json <> '{}') {
$decoded = json_decode($json);
foreach ($decoded->c as $restriction) {
if (property_exists($restriction, 'type') && $restriction->type == "date") {
$operator = $restriction->d;
if ($operator == ">=") {
$datetime = $restriction->t;
$availability['from'] = userdate($datetime, '%a %d %b %Y %H:%M');
} else {
$datetime = $restriction->t;
$availability['to'] = userdate($datetime, '%a %d %b %Y %H:%M');
}
if (empty($json)) {
return $availability;
}
$decoded = json_decode($json);
foreach ($decoded->c as $restriction) {
if (property_exists($restriction, 'type') && $restriction->type == "date") {
$operator = $restriction->d;
if ($operator == ">=") {
$datetime = $restriction->t;
$availability['from'] = userdate($datetime, '%a %d %b %Y %H:%M');
} else {
$datetime = $restriction->t;
$availability['to'] = userdate($datetime, '%a %d %b %Y %H:%M');
}
}
}
return $availability;
}


/**
* Calculate the dripping out of availability and format the dates for the session labels
*
Expand Down
6 changes: 3 additions & 3 deletions tests/driprelease_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ public function test_get_availability_with_dates(): void {
public function test_get_availability_with_no_dates(): void {
$this->resetAfterTest();

$json = '{}';
$json = "";
$availability = get_availability($json);

// Assert the output
// Assert the output.
$this->assertSame([], $availability);
}

Expand All @@ -307,7 +307,7 @@ public function test_get_availability_with_null(): void {

$availability = get_availability(null);

// Assert the output
// Assert the output.
$this->assertSame([], $availability);
}

Expand Down

0 comments on commit 3bbab9d

Please sign in to comment.