location_helper.php 4.4 KB

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