diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index a85790b..44cb94e 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -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 diff --git a/lib.php b/lib.php index 85efa33..4ee3b81 100644 --- a/lib.php +++ b/lib.php @@ -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 * diff --git a/tests/driprelease_test.php b/tests/driprelease_test.php index b49f1de..522427b 100644 --- a/tests/driprelease_test.php +++ b/tests/driprelease_test.php @@ -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); } @@ -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); }