project_ajax.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. $is_tester = $user->isTester();
  135. if($is_deactivated) continue;
  136. if(!$include_unverified && $is_unverified) continue;
  137. if(!$is_tester) continue;
  138. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  139. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  140. $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
  141. return ($user->id == $test->tester_id);
  142. });
  143. $pending_test_count = count($user_pending_tests);
  144. $age = $user->getAge();
  145. // Age check
  146. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  147. continue;
  148. }
  149. else if($project->min_age && $age < $project->min_age) {
  150. continue;
  151. }
  152. else if($project->max_age && $age > $project->max_age) {
  153. continue;
  154. }
  155. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  156. $time_last_test = $user_last_test_times[$user->id] ?? false;
  157. $output[] = [
  158. "id" => (int) $user->id,
  159. "addressId" => (int) $address->id,
  160. "name" => $user->getDisplayName(),
  161. "age" => $age,
  162. "phone" => $user->phone,
  163. "score" => (int) $user->score,
  164. "distanceMiles" => $distance_miles,
  165. "distanceKilometers" => $distance_kilometers,
  166. "maxRadiusMiles" => (float) $address->max_radius_miles,
  167. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  168. "maxRadiusExceeded" => $max_radius_exceeded,
  169. "latitude" => (float) $address->latitude,
  170. "longitude" => (float) $address->longitude,
  171. "pendingTestCount" => $pending_test_count,
  172. "isCurrentTester" => $is_current_tester,
  173. "isRegistered" => $is_registered,
  174. "isUnverified" => $is_unverified,
  175. "levelText" => $user->getLevelText(),
  176. "levelClass" => $user->getLevelClass(),
  177. "termsAccepted" => boolval($user->terms_accepted),
  178. "reported" => (int) $user->reported,
  179. "reportedComment" => $user->reported_comment,
  180. "timeRegistered" => strtotime($user->dts_registered . " UTC"),
  181. "timeLastTest" => $time_last_test,
  182. ];
  183. }
  184. JSON::success([
  185. "testers" => $output
  186. ]);
  187. }
  188. // Assigns a tester to a test
  189. case "assign-tester": {
  190. $year = http::value("year", "int");
  191. $month = http::value("month", "int");
  192. $day = http::value("day", "int");
  193. $unix_due = 0;
  194. if($year && $month && $day) {
  195. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  196. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  197. $unix_due = strtotime("$year-$month-$day");
  198. }
  199. $message = http::value("message");
  200. $location_id = http::value("location_id", "int");
  201. $location = Location::get($location_id);
  202. if(!$location) throw new Exception("Location not found.");
  203. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  204. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  205. $nvp = [
  206. "location_id" => $location_id,
  207. "project_id" => $project->id,
  208. "status" => $status_constraint,
  209. ];
  210. $test = Test::queryOne($nvp);
  211. if(!$test) throw new Exception("A test does not exist for this location.");
  212. $address_id = http::value("address_id", "int");
  213. $address = UserAddress::get($address_id);
  214. if(!$address) throw new Exception("User address cannot be found.");
  215. $user = $address->getUser();
  216. if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
  217. if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
  218. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  219. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  220. if($user->level != User::LEVEL_TESTER) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  221. $test->status = Test::STATUS_ASSIGNED;
  222. $test->tester_id = $user->id;
  223. $test->dts_assigned = Util::getDateTime();
  224. $test->dts_updated = Util::getDateTime();
  225. if($unix_due) {
  226. $test->dts_due = "$year-$month-$day 23:59:59";
  227. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
  228. if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
  229. }
  230. $success = $test->commit();
  231. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  232. $test->sendAssignEmail($message);
  233. JSON::success();
  234. break;
  235. }
  236. default:
  237. throw new Exception("Invalid request.");
  238. }
  239. }
  240. catch(Exception $e) {
  241. JSON::fail($e->getMessage());
  242. }
  243. }
  244. JSON::fail("Invalid request.");
  245. ?>