location_helper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Helpers;
  3. use Constraint;
  4. use Exception;
  5. use Location;
  6. use RuntimeException;
  7. use Test;
  8. use User;
  9. use Util;
  10. class LocationHelper {
  11. public const DATE_FORMAT = "/^2\d{3}-[01]\d-[0-3]\d$/";
  12. /**
  13. * @param Location[] $locations
  14. * @param string|null $min_date
  15. * @param string|null $max_date
  16. * @return array
  17. * @throws Exception
  18. */
  19. public static function getMarkerDataForLocations(array $locations, ?string $min_date, ?string $max_date): array {
  20. if(empty($locations)) {
  21. return [];
  22. }
  23. $customer = $locations[0]->getCustomer();
  24. $init_result = static function(Location $location): object {
  25. return (object) [
  26. "location" => $location,
  27. "latestTest" => null,
  28. "testerId" => null,
  29. "testCount" => 0
  30. ];
  31. };
  32. $results = [];
  33. foreach($locations as $location) {
  34. $results[$location->id] = $init_result($location);
  35. }
  36. // Get tests
  37. $nvp = [
  38. "status" => Constraint::notEquals(Test::STATUS_CANCELLED),
  39. "location_id" => array_column($locations, "id")
  40. ];
  41. if($min_date && $max_date) {
  42. if(preg_match(self::DATE_FORMAT, $min_date) && preg_match(self::DATE_FORMAT, $max_date)) {
  43. $min_dts = $min_date;
  44. $max_dts = $max_date;
  45. $nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
  46. }
  47. else {
  48. throw new RuntimeException("Invalid dates submitted.");
  49. }
  50. }
  51. else {
  52. [$min_dts, $max_dts] = $customer->getYearCycleDates();
  53. $nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
  54. }
  55. $tests = Test::query($nvp);
  56. foreach($tests as $test) {
  57. $result = $results[$test->location_id];
  58. $result->testCount++;
  59. $assign_test = false;
  60. if($result->latestTest === null) {
  61. $assign_test = true;
  62. }
  63. else {
  64. /** @var Test $existing */
  65. $existing = $result->latestTest;
  66. $unix_start_existing = ($existing->dts_start === null) ? 0 : strtotime($existing->dts_start . " UTC");
  67. $unix_start_current = ($test->dts_start === null) ? 0 : strtotime($test->dts_start . " UTC");
  68. if($unix_start_current > $unix_start_existing) {
  69. $assign_test = true;
  70. }
  71. }
  72. if($assign_test) {
  73. $result->latestTest = $test;
  74. $result->testerId = $test->tester_id;
  75. }
  76. }
  77. $tester_ids = array_column($results, "testerId");
  78. User::load($tester_ids);
  79. $output = [];
  80. foreach($results as $result) {
  81. /** @var Location $location */
  82. $location = $result->location;
  83. /** @var Test $test */
  84. $test = $result->latestTest;
  85. $customer_slug = Util::slug($customer->name, "_");
  86. /** @var User $tester */
  87. $tester = null;
  88. $formatted_start_dts = null;
  89. $formatted_due_dts = null;
  90. $icon = null;
  91. $icon_file = "{$customer_slug}_none.png";
  92. $past_due = false;
  93. if($test) {
  94. $tester = $test->getTester();
  95. if($test->dts_start !== null) {
  96. $unix_start = strtotime($test->dts_start);
  97. $formatted_start_dts = date("d/m/Y H:i", $unix_start);
  98. }
  99. if($test->isPending()) {
  100. $icon_file = "{$customer_slug}_pending.png";
  101. $unix_due = null;
  102. if($test->dts_due !== null) {
  103. $unix_due = strtotime($test->dts_due);
  104. $formatted_due_dts = date("d/m/Y H:i", $unix_due);
  105. }
  106. if($tester && $unix_due && time() > $unix_due) {
  107. $icon_file = "{$customer_slug}_past_due.png";
  108. $past_due = true;
  109. }
  110. }
  111. else if($test->pass) {
  112. $icon_file = "{$customer_slug}_pass.png";
  113. }
  114. else {
  115. $icon_file = "{$customer_slug}_fail.png";
  116. }
  117. }
  118. if($location->do_not_test) {
  119. $icon_file = "do_not_test.png";
  120. }
  121. if($location->closed) {
  122. $icon_file = "closed.png";
  123. }
  124. if(file_exists("public/img/markers/$icon_file")) {
  125. $icon = $icon_file;
  126. }
  127. $data = [
  128. "id" => (int) $location->id,
  129. "internalId" => $location->internal_id,
  130. "name" => $location->name,
  131. "latitude" => (float) $location->latitude,
  132. "longitude" => (float) $location->longitude,
  133. "icon" => $icon,
  134. "test" => null,
  135. "doNotTest" => (bool) $location->do_not_test,
  136. "closed" => (bool) $location->closed,
  137. "archived" => $location->isArchived(),
  138. "testCount" => $result->testCount
  139. ];
  140. if($test) {
  141. $data["test"] = [
  142. "id" => $test->id,
  143. "formattedStartDts" => $formatted_start_dts,
  144. "formattedDueDts" => $formatted_due_dts,
  145. "tester" => null,
  146. "pastDue" => $past_due,
  147. ];
  148. if($tester) {
  149. $data["test"]["tester"] = [
  150. "id" => $tester->id,
  151. "name" => $tester->getDisplayName(),
  152. ];
  153. }
  154. }
  155. $output[] = $data;
  156. }
  157. return $output;
  158. }
  159. }