project_ajax.php 8.5 KB

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