project_ajax.php 9.8 KB

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