project_ajax.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. if(Core::$user->level == User::LEVEL_SUPERADMIN) {
  75. $max_distance = 1000;
  76. }
  77. $bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
  78. $lat_constraint = new Constraint("BETWEEN");
  79. $lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
  80. $lng_constraint = new Constraint("BETWEEN");
  81. $lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
  82. $nvp = [
  83. "latitude" => $lat_constraint,
  84. "longitude" => $lng_constraint
  85. ];
  86. $addresses = UserAddress::query($nvp);
  87. $users = User::load(array_column($addresses, "user_id"));
  88. $pending_tests = Test::query([
  89. "tester_id" => array_column($users, "id"),
  90. "status" => Test::STATUS_ASSIGNED,
  91. ]);
  92. usort($addresses, function($a, $b) use ($location) {
  93. $lat_lng = $location->getGeolocation();
  94. return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
  95. });
  96. $output = [];
  97. foreach($addresses as $address) {
  98. $user = $address->getUser();
  99. $is_current_tester = $user->id == $test->tester_id;
  100. $is_deactivated = $user->level == User::LEVEL_DEACTIVATED;
  101. $is_registered = $user->level == User::LEVEL_REGISTERED;
  102. $is_unverified = $user->level == User::LEVEL_UNVERIFIED;
  103. if($is_deactivated) continue;
  104. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  105. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  106. $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
  107. return ($user->id == $test->tester_id);
  108. });
  109. $pending_test_count = count($user_pending_tests);
  110. $age = $user->getAge();
  111. // Age check
  112. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  113. continue;
  114. }
  115. else if($project->min_age && $age < $project->min_age) {
  116. continue;
  117. }
  118. else if($project->max_age && $age > $project->max_age) {
  119. continue;
  120. }
  121. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  122. $output[] = [
  123. "id" => (int) $user->id,
  124. "addressId" => (int) $address->id,
  125. "name" => $user->getDisplayName(),
  126. "age" => $age,
  127. "phone" => $user->phone,
  128. "score" => (int) $user->score,
  129. "distanceMiles" => $distance_miles,
  130. "distanceKilometers" => $distance_kilometers,
  131. "maxRadiusMiles" => (float) $address->max_radius_miles,
  132. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  133. "maxRadiusExceeded" => $max_radius_exceeded,
  134. "latitude" => (float) $address->latitude,
  135. "longitude" => (float) $address->longitude,
  136. "pendingTestCount" => $pending_test_count,
  137. "isCurrentTester" => $is_current_tester,
  138. "isRegistered" => $is_registered,
  139. "isUnverified" => $is_unverified,
  140. "levelText" => $user->getLevelText(),
  141. "levelClass" => $user->getLevelClass(),
  142. ];
  143. }
  144. JSON::success([
  145. "testers" => $output
  146. ]);
  147. }
  148. // Assigns a tester to a test
  149. case "assign-tester": {
  150. $year = http::value("year", "int");
  151. $month = http::value("month", "int");
  152. $day = http::value("day", "int");
  153. $unix_due = 0;
  154. if($year && $month && $day) {
  155. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  156. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  157. $unix_due = strtotime("$year-$month-$day");
  158. }
  159. $message = http::value("message");
  160. $location_id = http::value("location_id", "int");
  161. $location = Location::get($location_id);
  162. if(!$location) throw new Exception("Location not found.");
  163. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  164. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  165. $nvp = [
  166. "location_id" => $location_id,
  167. "project_id" => $project->id,
  168. "status" => $status_constraint,
  169. ];
  170. $test = Test::queryOne($nvp);
  171. if(!$test) throw new Exception("A test does not exist for this location.");
  172. $address_id = http::value("address_id", "int");
  173. $address = UserAddress::get($address_id);
  174. if(!$address) throw new Exception("User address cannot be found.");
  175. $user = $address->getUser();
  176. if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
  177. if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
  178. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  179. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  180. if($user->level != User::LEVEL_TESTER) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  181. $test->status = Test::STATUS_ASSIGNED;
  182. $test->tester_id = $user->id;
  183. $test->dts_assigned = Util::getDateTime();
  184. if($unix_due) {
  185. $test->dts_due = "$year-$month-$day 23:59:59";
  186. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
  187. if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
  188. }
  189. $success = $test->commit();
  190. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  191. // TODO: send user email
  192. JSON::success();
  193. break;
  194. }
  195. default:
  196. throw new Exception("Invalid request.");
  197. }
  198. }
  199. catch(Exception $e) {
  200. JSON::fail($e->getMessage());
  201. }
  202. }
  203. JSON::fail("Invalid request.");
  204. ?>