1
0

location_helper.php 5.0 KB

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