project_ajax.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. usort($addresses, function($a, $b) use ($location) {
  90. $lat_lng = $location->getGeolocation();
  91. return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
  92. });
  93. $output = [];
  94. foreach($addresses as $address) {
  95. $user = $address->getUser();
  96. $is_current_tester = $user->id == $test->tester_id;
  97. $is_deactivated = $user->level == User::LEVEL_DEACTIVATED;
  98. $is_registered = $user->level == User::LEVEL_REGISTERED;
  99. $is_unverified = $user->level == User::LEVEL_UNVERIFIED;
  100. if($is_deactivated) continue;
  101. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  102. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  103. $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
  104. return ($user->id == $test->tester_id);
  105. });
  106. $pending_test_count = count($user_pending_tests);
  107. $age = $user->getAge();
  108. // Age check
  109. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  110. continue;
  111. }
  112. else if($project->min_age && $age < $project->min_age) {
  113. continue;
  114. }
  115. else if($project->max_age && $age > $project->max_age) {
  116. continue;
  117. }
  118. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  119. $output[] = [
  120. "id" => (int) $user->id,
  121. "addressId" => (int) $address->id,
  122. "name" => $user->getDisplayName(),
  123. "age" => $age,
  124. "phone" => $user->phone,
  125. "score" => (int) $user->score,
  126. "distanceMiles" => $distance_miles,
  127. "distanceKilometers" => $distance_kilometers,
  128. "maxRadiusMiles" => (float) $address->max_radius_miles,
  129. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  130. "maxRadiusExceeded" => $max_radius_exceeded,
  131. "latitude" => (float) $address->latitude,
  132. "longitude" => (float) $address->longitude,
  133. "pendingTestCount" => $pending_test_count,
  134. "isCurrentTester" => $is_current_tester,
  135. "isRegistered" => $is_registered,
  136. "isUnverified" => $is_unverified,
  137. "levelText" => $user->getLevelText(),
  138. "levelClass" => $user->getLevelClass(),
  139. ];
  140. }
  141. JSON::success([
  142. "testers" => $output
  143. ]);
  144. }
  145. // Assigns a tester to a test
  146. case "assign-tester": {
  147. $location_id = http::value("location_id", "int");
  148. $location = Location::get($location_id);
  149. if(!$location) throw new Exception("Location not found.");
  150. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  151. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  152. $nvp = [
  153. "location_id" => $location_id,
  154. "project_id" => $project->id,
  155. "status" => $status_constraint,
  156. ];
  157. $test = Test::queryOne($nvp);
  158. if(!$test) throw new Exception("A test does not exist for this location.");
  159. $address_id = http::value("address_id", "int");
  160. $address = UserAddress::get($address_id);
  161. if(!$address) throw new Exception("User address cannot be found.");
  162. $user = $address->getUser();
  163. if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
  164. if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
  165. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  166. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  167. if($user->level >= User::LEVEL_VENUE) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  168. $test->status = Test::STATUS_ASSIGNED;
  169. $test->tester_id = $user->id;
  170. $test->dts_assigned = Util::getDateTime();
  171. $success = $test->commit();
  172. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  173. // TODO: send user email
  174. JSON::success();
  175. break;
  176. }
  177. default:
  178. throw new Exception("Invalid request.");
  179. }
  180. }
  181. catch(Exception $e) {
  182. JSON::fail($e->getMessage());
  183. }
  184. }
  185. JSON::fail("Invalid request.");
  186. ?>