project_ajax.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. JSON::header();
  3. $project = Project::get($id);
  4. if(!$project) throw new Exception("Project cannot be found.");
  5. if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
  6. $submit = http::value("submit");
  7. if($submit) {
  8. try {
  9. Core::validateCSRFToken($submit);
  10. switch($action) {
  11. // Gets all locations for a project
  12. case "get-locations": {
  13. $customer = $project->getCustomer();
  14. $tests = $project->getTests();
  15. $locations = Location::load(array_column($tests, "location_id"));
  16. $location_tests = [];
  17. foreach($tests as $test) {
  18. if($test->status == Test::STATUS_CANCELLED) continue;
  19. $location_tests[$test->location_id] = $test;
  20. }
  21. $output = [];
  22. foreach($locations as $location) {
  23. $test = $location_tests[$location->id] ?? false;
  24. $test_id = null;
  25. $status = 0;
  26. $status_text = "";
  27. $class = "secondary";
  28. if($test) {
  29. $test_id = $test->id;
  30. $class = $test->getStatusClass();
  31. $status = $test->status;
  32. $status_text = $test->getStatusText();
  33. }
  34. $output[] = (object) [
  35. "id" => (int) $location->id,
  36. "testId" => (int) $test_id,
  37. "name" => $location->name,
  38. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  39. "status" => $status,
  40. "statusText" => $status_text,
  41. "class" => $class,
  42. "latitude" => (float) $location->latitude,
  43. "longitude" => (float) $location->longitude
  44. ];
  45. }
  46. usort($output, function($a, $b) {
  47. return $a->status <=> $b->status;
  48. });
  49. JSON::success([
  50. "locations" => $output
  51. ]);
  52. break;
  53. }
  54. // Gets testers near a project
  55. case "get-testers": {
  56. $location_id = http::value("location_id", "int");
  57. $location = Location::get($location_id);
  58. if(!$location) throw new Exception("Location not found.");
  59. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  60. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  61. $nvp = [
  62. "location_id" => $location_id,
  63. "project_id" => $project->id,
  64. "status" => $status_constraint,
  65. ];
  66. $test = Test::queryOne($nvp);
  67. if(!$test) throw new Exception("A test does not exist for this project.");
  68. /*
  69. $max_distance = http::value("max_distance", "int");
  70. if($max_distance < 2.5) $max_distance = 2.5;
  71. if($max_distance > 25) $max_distance = 25;
  72. */
  73. $max_distance = 25;
  74. $bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
  75. $lat_constraint = new Constraint("BETWEEN");
  76. $lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
  77. $lng_constraint = new Constraint("BETWEEN");
  78. $lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
  79. $nvp = [
  80. "latitude" => $lat_constraint,
  81. "longitude" => $lng_constraint
  82. ];
  83. $addresses = UserAddress::query($nvp);
  84. $users = User::load(array_column($addresses, "user_id"));
  85. $pending_tests = Test::query([
  86. "tester_id" => array_column($users, "id"),
  87. "status" => Test::STATUS_ASSIGNED,
  88. ]);
  89. $age_constraint = false;
  90. usort($addresses, function($a, $b) use ($location) {
  91. $lat_lng = $location->getGeolocation();
  92. return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
  93. });
  94. $output = [];
  95. foreach($addresses as $address) {
  96. $user = $address->getUser();
  97. $is_current_tester = $user->id == $test->tester_id;
  98. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  99. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  100. $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
  101. return ($user->id == $test->tester_id);
  102. });
  103. $pending_test_count = count($user_pending_tests);
  104. $age = $user->getAge();
  105. // Age check
  106. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  107. continue;
  108. }
  109. else if($project->min_age && $age < $project->min_age) {
  110. continue;
  111. }
  112. else if($project->max_age && $age > $project->max_age) {
  113. continue;
  114. }
  115. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  116. $output[] = [
  117. "id" => (int) $user->id,
  118. "addressId" => (int) $address->id,
  119. "name" => $user->getDisplayName(),
  120. "age" => $age,
  121. "phone" => $user->phone,
  122. "score" => (int) $user->score,
  123. "distanceMiles" => $distance_miles,
  124. "distanceKilometers" => $distance_kilometers,
  125. "maxRadiusMiles" => (float) $address->max_radius_miles,
  126. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  127. "maxRadiusExceeded" => $max_radius_exceeded,
  128. "latitude" => (float) $address->latitude,
  129. "longitude" => (float) $address->longitude,
  130. "pendingTestCount" => $pending_test_count,
  131. "isCurrentTester" => $is_current_tester
  132. ];
  133. }
  134. JSON::success([
  135. "testers" => $output
  136. ]);
  137. }
  138. // Assigns a tester to a test
  139. case "assign-tester": {
  140. $location_id = http::value("location_id", "int");
  141. $location = Location::get($location_id);
  142. if(!$location) throw new Exception("Location not found.");
  143. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  144. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  145. $nvp = [
  146. "location_id" => $location_id,
  147. "project_id" => $project->id,
  148. "status" => $status_constraint,
  149. ];
  150. $test = Test::queryOne($nvp);
  151. if(!$test) throw new Exception("A test does not exist for this location.");
  152. $address_id = http::value("address_id", "int");
  153. $address = UserAddress::get($address_id);
  154. if(!$address) throw new Exception("User address cannot be found.");
  155. $user = $address->getUser();
  156. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  157. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  158. if($user->level >= User::LEVEL_VENUE) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  159. $test->status = Test::STATUS_ASSIGNED;
  160. $test->tester_id = $user->id;
  161. $test->dts_assigned = Util::getDateTime();
  162. $success = $test->commit();
  163. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  164. // TODO: send user email
  165. JSON::success();
  166. break;
  167. }
  168. default:
  169. throw new Exception("Invalid request.");
  170. }
  171. }
  172. catch(Exception $e) {
  173. JSON::fail($e->getMessage());
  174. }
  175. }
  176. JSON::fail("Invalid request.");
  177. ?>