project_ajax.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $pass = 0;
  28. $pass_class = "";
  29. $pass_text = "";
  30. $pass_icon = "";
  31. $class = "secondary";
  32. if($test) {
  33. $test_id = $test->id;
  34. $class = $test->getStatusClass();
  35. $status = $test->status;
  36. $status_text = $test->getStatusText();
  37. $pass = $test->pass;
  38. $pass_class = $test->getPassClass();
  39. $pass_text = $test->getPassText();
  40. $pass_icon = $test->getPassIcon();
  41. }
  42. $output[] = (object) [
  43. "id" => (int) $location->id,
  44. "testId" => (int) $test_id,
  45. "name" => $location->name,
  46. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  47. "status" => $status,
  48. "statusText" => $status_text,
  49. "pass" => (int) $pass,
  50. "passClass" => $pass_class,
  51. "passText" => $pass_text,
  52. "passIcon" => $pass_icon,
  53. "class" => $class,
  54. "latitude" => (float) $location->latitude,
  55. "longitude" => (float) $location->longitude
  56. ];
  57. }
  58. usort($output, function($a, $b) {
  59. return $a->status <=> $b->status;
  60. });
  61. JSON::success([
  62. "locations" => $output
  63. ]);
  64. break;
  65. }
  66. // Gets testers near a project
  67. case "get-testers": {
  68. $location_id = http::value("location_id", "int");
  69. $location = Location::get($location_id);
  70. if(!$location) throw new Exception("Location not found.");
  71. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  72. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  73. $nvp = [
  74. "location_id" => $location_id,
  75. "project_id" => $project->id,
  76. "status" => $status_constraint,
  77. ];
  78. $test = Test::queryOne($nvp);
  79. if(!$test) throw new Exception("A test does not exist for this project.");
  80. /*
  81. $max_distance = http::value("max_distance", "int");
  82. if($max_distance < 2.5) $max_distance = 2.5;
  83. if($max_distance > 25) $max_distance = 25;
  84. */
  85. $max_distance = 25;
  86. if(Core::$user->level == User::LEVEL_SUPERADMIN) {
  87. $max_distance = 1000;
  88. }
  89. $bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
  90. $lat_constraint = new Constraint("BETWEEN");
  91. $lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
  92. $lng_constraint = new Constraint("BETWEEN");
  93. $lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
  94. $nvp = [
  95. "latitude" => $lat_constraint,
  96. "longitude" => $lng_constraint
  97. ];
  98. $addresses = UserAddress::query($nvp);
  99. $users = User::load(array_column($addresses, "user_id"));
  100. $pending_tests = Test::query([
  101. "tester_id" => array_column($users, "id"),
  102. "status" => Test::STATUS_ASSIGNED,
  103. ]);
  104. usort($addresses, function($a, $b) use ($location) {
  105. $lat_lng = $location->getGeolocation();
  106. return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
  107. });
  108. $output = [];
  109. foreach($addresses as $address) {
  110. $user = $address->getUser();
  111. $is_current_tester = $user->id == $test->tester_id;
  112. $is_deactivated = $user->level == User::LEVEL_DEACTIVATED;
  113. $is_registered = $user->level == User::LEVEL_REGISTERED;
  114. $is_unverified = $user->level == User::LEVEL_UNVERIFIED;
  115. if($is_deactivated) continue;
  116. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  117. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  118. $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
  119. return ($user->id == $test->tester_id);
  120. });
  121. $pending_test_count = count($user_pending_tests);
  122. $age = $user->getAge();
  123. // Age check
  124. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  125. continue;
  126. }
  127. else if($project->min_age && $age < $project->min_age) {
  128. continue;
  129. }
  130. else if($project->max_age && $age > $project->max_age) {
  131. continue;
  132. }
  133. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  134. $output[] = [
  135. "id" => (int) $user->id,
  136. "addressId" => (int) $address->id,
  137. "name" => $user->getDisplayName(),
  138. "age" => $age,
  139. "phone" => $user->phone,
  140. "score" => (int) $user->score,
  141. "distanceMiles" => $distance_miles,
  142. "distanceKilometers" => $distance_kilometers,
  143. "maxRadiusMiles" => (float) $address->max_radius_miles,
  144. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  145. "maxRadiusExceeded" => $max_radius_exceeded,
  146. "latitude" => (float) $address->latitude,
  147. "longitude" => (float) $address->longitude,
  148. "pendingTestCount" => $pending_test_count,
  149. "isCurrentTester" => $is_current_tester,
  150. "isRegistered" => $is_registered,
  151. "isUnverified" => $is_unverified,
  152. "levelText" => $user->getLevelText(),
  153. "levelClass" => $user->getLevelClass(),
  154. ];
  155. }
  156. JSON::success([
  157. "testers" => $output
  158. ]);
  159. }
  160. // Assigns a tester to a test
  161. case "assign-tester": {
  162. $year = http::value("year", "int");
  163. $month = http::value("month", "int");
  164. $day = http::value("day", "int");
  165. $unix_due = 0;
  166. if($year && $month && $day) {
  167. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  168. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  169. $unix_due = strtotime("$year-$month-$day");
  170. }
  171. $message = http::value("message");
  172. $location_id = http::value("location_id", "int");
  173. $location = Location::get($location_id);
  174. if(!$location) throw new Exception("Location not found.");
  175. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  176. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  177. $nvp = [
  178. "location_id" => $location_id,
  179. "project_id" => $project->id,
  180. "status" => $status_constraint,
  181. ];
  182. $test = Test::queryOne($nvp);
  183. if(!$test) throw new Exception("A test does not exist for this location.");
  184. $address_id = http::value("address_id", "int");
  185. $address = UserAddress::get($address_id);
  186. if(!$address) throw new Exception("User address cannot be found.");
  187. $user = $address->getUser();
  188. if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
  189. if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
  190. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  191. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  192. if($user->level != User::LEVEL_TESTER) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  193. $test->status = Test::STATUS_ASSIGNED;
  194. $test->tester_id = $user->id;
  195. $test->dts_assigned = Util::getDateTime();
  196. $test->dts_updated = Util::getDateTime();
  197. if($unix_due) {
  198. $test->dts_due = "$year-$month-$day 23:59:59";
  199. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
  200. if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
  201. }
  202. $success = $test->commit();
  203. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  204. // TODO: send user email
  205. JSON::success();
  206. break;
  207. }
  208. default:
  209. throw new Exception("Invalid request.");
  210. }
  211. }
  212. catch(Exception $e) {
  213. JSON::fail($e->getMessage());
  214. }
  215. }
  216. JSON::fail("Invalid request.");
  217. ?>