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