project_ajax.php 9.6 KB

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