test_validate.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. $challenge_stage = (int) $test->challenge_stage;
  16. // Calculate minimum duration
  17. $min_duration = 0;
  18. if($test->challenge_stage >= 2) {
  19. if($line_of_sight) {
  20. $min_duration += 1;
  21. }
  22. else {
  23. $min_duration += 2;
  24. }
  25. }
  26. $min_duration += $play_minutes;
  27. if(in_array($challenge_stage, [4, 5]) && $play_minutes < 3) {
  28. $form_exception->addError("Please confirm that you played for at least 3 minutes because you have selected After Play / No Challenge.");
  29. }
  30. if($duration < $min_duration) {
  31. $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).");
  32. }
  33. if((int) $test->challenge_stage === 5) {
  34. $bar_time = $values["bar_time"] ?? null;
  35. if(!$bar_time) {
  36. $form_exception->addError("Bar arrival time must be filled in if you were not challenged.");
  37. }
  38. else {
  39. $dt_bar_min = $dt_start->addMinutes($min_duration);
  40. $dt_bar = CarbonImmutable::instance($dt_start)->setTimeFromTimeString($bar_time);
  41. $start_time = $dt_start->format("H:i");
  42. $end_time = $dt_end->format("H:i");
  43. $min_bar_time = $dt_bar_min->format("H:i");
  44. if($dt_bar < $dt_bar_min) {
  45. $form_exception->addError("Bar arrival time needs to be $min_bar_time or after.");
  46. }
  47. if($dt_bar < $dt_bar_min || $dt_bar > $dt_end) {
  48. $form_exception->addError("Bar arrival time must be on or after $min_bar_time.");
  49. }
  50. }
  51. }
  52. }