Skip to content

Commit

Permalink
migrate new entity logic (not yet finished)
Browse files Browse the repository at this point in the history
# Conflicts:
#	classes/entitiesrelation_handler.php
  • Loading branch information
bernhard-wunderbyte authored and georgmaisser committed Dec 13, 2023
1 parent 9c81bd2 commit 9565ea4
Showing 1 changed file with 58 additions and 18 deletions.
76 changes: 58 additions & 18 deletions classes/entitiesrelation_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,77 @@ public function instance_form_definition_elements(
public function instance_form_validation(array $data, array &$errors) {

// First, see if an entitiyid is set. If not, we can proceed right away.
if (!preg_grep('/^local_entities/', array_keys($data))) {
if (!$entityidkeys = preg_grep('/^local_entities_entityid/', array_keys($data))) {
// For performance.
return;
}

// Validation needs to be made for all possible indexes.
foreach ($entityidkeys as $entityidkey) {

if (!$data['local_entities_entityid']) {
return;
}
if (empty($data[$entityidkey])) {
// If there is no entityid value found, we don't need to validate.
continue;
}

// Now determine if there is a conflict.
// Now determine if there are conflicts.
$conflicts = entities::return_conflicts($data[$entityidkey],
$data['datestobook'] ?? [],
$data['optionid'] ?? 0,
'optiondate');

$conflicts = entities::return_conflicts($data['local_entities_entityid'],
$data['datestobook'] ?? [],
$data['optionid'] ?? 0,
'optiondate');
if (!empty($conflicts['conflicts'])) {

if (!empty($conflicts['conflicts'])) {
$errors[$entityidkey] = get_string('errorwiththefollowingdates', 'local_entities');

$errors['local_entities_entityid'] = get_string('errorwiththefollowingdates', 'local_entities');
foreach ($conflicts['conflicts'] as $conflict) {
$link = $conflict->link->out();
$errors[$entityidkey] .= "<br><a href='$link'>$conflict->name (" .
dates::prettify_dates_start_end($conflict->starttime, $conflict->endtime, current_language()) . ")</a>";
}
}
if (!empty($conflicts['openinghours'])) {
$errors[$entityidkey] .= get_string('notwithinopeninghours', 'local_entities');
}
}

foreach ($conflicts['conflicts'] as $conflict) {
$link = $conflict->link->out();
$errors['local_entities_entityid'] .= "<br><a href='$link'>$conflict->name (" .
dates::prettify_dates_start_end($conflict->starttime, $conflict->endtime, current_language()) . ")</a>";
// Validation for entities in combination with mod_booking.
if ($this->component = 'mod_booking' && $this->area = 'option') {
$optionid = $this->instanceid;

// In validation we need to check, if there are optiondates that have "outlier" entities.
// If so, the outliers must be changed to the main entity before all relations can be saved.
if (!empty($data['er_saverelationsforoptiondates']) &&
self::option_has_dates_with_entity_outliers($optionid) &&
empty($data['confirm:er_saverelationsforoptiondates'])) {
$errors['confirm:er_saverelationsforoptiondates'] =
get_string('error:er_saverelationsforoptiondates', 'mod_booking');
}
}
if (!empty($conflicts['openinghours'])) {
$errors['local_entities_entityid'] .= get_string('notwithinopeninghours', 'local_entities');
}

/**
* Helper function to check if there are dates with "entity outliers"
* (e.g. if all dates have set "Classroom" but there is a date that is
* happening outside and has set "Park").
* @param int $optionid
* @return bool true if there are outliers, false if not
*/
public static function option_has_dates_with_entity_outliers(int $optionid): bool {
global $DB;
// If we have "outliers" (deviating entities), we show a confirm box...
// ...so a user does not overwrite them accidentally.
$sql = "SELECT COUNT(DISTINCT er.entityid) numberofentities
FROM {local_entities_relations} er
JOIN {booking_optiondates} bod
ON bod.id = er.instanceid
WHERE er.area = 'optiondate'
AND bod.optionid = :optionid";
$params = ['optionid' => $optionid];
$numberofentities = $DB->get_field_sql($sql, $params);
if (!empty($numberofentities) && $numberofentities > 1) {
return true;
}
return false;
}

/**
Expand Down

0 comments on commit 9565ea4

Please sign in to comment.