location_helper.php 4.8 KB

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