Skip to content

Commit

Permalink
Improvement: Check for existing answers if they allow overlapping and…
Browse files Browse the repository at this point in the history
… fix check for sessions overlapping (#651)
  • Loading branch information
eynimeni committed Dec 5, 2024
1 parent 974b028 commit 30bc1d7
Show file tree
Hide file tree
Showing 6 changed files with 564 additions and 30 deletions.
21 changes: 11 additions & 10 deletions classes/bo_availability/bo_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -1283,26 +1283,27 @@ private static function add_back_button(
}

/**
* Returns part of SQL-Query according to DB Family for a specified column and key.
* Returns part of an SQL query to extract a JSON key from a column based on the DB Family.
*
* @param string $dbcolumn
* @param string $jsonkey
*
* @return string
* @param string $dbcolumn The name of the column containing JSON data.
* @param string $jsonkey The key to extract from the JSON object.
*
* @return string SQL snippet for extracting the JSON key.
*/
public static function check_for_sqljson_key(string $dbcolumn, string $jsonkey): string {
public static function check_for_sqljson_key_in_array(string $dbcolumn, string $jsonkey, int $index = 0): string {
global $DB;

$databasetype = $DB->get_dbfamily();
// The $key param is the name of the param in json.

switch ($databasetype) {
case 'postgres':
return " ($dbcolumn->>'$jsonkey')";
// PostgreSQL: Extract key from JSON array element at specified index.
return "(CAST($dbcolumn AS JSONB)->$index->>'" . addslashes($jsonkey) . "')";
case 'mysql':
return " JSON_EXTRACT($dbcolumn, '$jsonkey')";
// MySQL: Extract key from JSON array element at specified index.
return "JSON_UNQUOTE(JSON_EXTRACT($dbcolumn, '$[$index]." . addslashes($jsonkey) . "'))";
default:
return '';
throw new \moodle_exception('Unsupported database type for JSON key extraction.');
}
}

Expand Down
6 changes: 4 additions & 2 deletions classes/bo_availability/conditions/nooverlapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ public function is_available(booking_option_settings $settings, int $userid, boo
// This is the return value. Not available to begin with.
$isavailable = false;

// Check if this bookingoption forbids the overlapping.
$forbidden = true;
if (empty($this->return_handling_from_settings($settings))) {
return true;
$forbidden = false;
}

// Get the booking answers for this instance.
Expand All @@ -158,7 +160,7 @@ public function is_available(booking_option_settings $settings, int $userid, boo
isset($bookinginformation['iambooked'])
|| isset($bookinginformation['onwaitinglist'])
)
|| empty($this->overlappinganswers = $bookinganswer->is_overlapping($userid))
|| empty($this->overlappinganswers = $bookinganswer->is_overlapping($userid, $forbidden))
) {
$isavailable = true;
}
Expand Down
Loading

0 comments on commit 30bc1d7

Please sign in to comment.