test_validate.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * @var Test $test
  4. * @var FormException $form_exception
  5. */
  6. use Carbon\CarbonImmutable;
  7. if($test->dts_start && $test->dts_end) {
  8. $dt_start = $test->getDateTimeStart();
  9. $dt_end = $test->getDateTimeEnd();
  10. $duration = $dt_end->diffInMinutes($dt_start);
  11. /** @var string[] $values */
  12. $values = $test->getValues(true);
  13. $line_of_sight = (bool) ($values["play_machine_line_of_sight"] ?? false);
  14. $play_minutes = (int) ($values["play_minutes"] ?? 0);
  15. // Calculate minimum duration
  16. $min_duration = 0;
  17. if($test->challenge_stage >= 2) {
  18. if($line_of_sight) {
  19. $min_duration += 1;
  20. }
  21. else {
  22. $min_duration += 2;
  23. }
  24. }
  25. $min_duration += $play_minutes;
  26. if($duration < $min_duration) {
  27. $form_exception->addError("Please check the entry and exit times. Based on the information you have given, the minimum test duration is $min_duration minute(s).");
  28. }
  29. if((int) $test->challenge_stage === 5) {
  30. $bar_time = $values["bar_time"] ?? null;
  31. if(!$bar_time) {
  32. $form_exception->addError("Bar arrival time must be filled in if you were not challenged.");
  33. }
  34. else {
  35. $dt_bar = CarbonImmutable::instance($dt_start)->setTimeFromTimeString($bar_time);
  36. $start_time = $dt_start->format("H:i");
  37. $end_time = $dt_end->format("H:i");
  38. if($dt_bar < $dt_start || $dt_bar > $dt_end) {
  39. $form_exception->addError("Bar arrival time must be between $start_time and $end_time.");
  40. }
  41. }
  42. }
  43. }