project_ajax.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * @var int $id
  4. * @var string $action
  5. */
  6. JSON::header();
  7. $project = Project::get($id);
  8. if(!$project) throw new Exception("Project cannot be found.");
  9. if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
  10. $submit = http::value("submit");
  11. if($submit) {
  12. try {
  13. Core::validateCSRFToken($submit);
  14. switch($action) {
  15. // Gets all locations for a project
  16. case "get-locations": {
  17. $customer = $project->getCustomer();
  18. $tests = $project->getTests();
  19. $locations = Location::load(array_column($tests, "location_id"));
  20. $location_tests = [];
  21. foreach($tests as $test) {
  22. if($test->status == Test::STATUS_CANCELLED) continue;
  23. $location_tests[$test->location_id] = $test;
  24. }
  25. $output = [];
  26. foreach($locations as $location) {
  27. $test = $location_tests[$location->id] ?? false;
  28. $test_id = null;
  29. $tester_id = null;
  30. $status = 0;
  31. $status_text = "";
  32. $pass = 0;
  33. $pass_class = "";
  34. $pass_text = "";
  35. $pass_icon = "";
  36. $retest = false;
  37. $class = "secondary";
  38. if($test) {
  39. $test_id = (int) $test->id;
  40. $tester_id = (int) $test->tester_id;
  41. $class = $test->getStatusClass();
  42. $status = $test->status;
  43. $status_text = $test->getStatusText();
  44. $pass = (int) $test->pass;
  45. $pass_class = $test->getPassClass();
  46. $pass_text = $test->getPassText();
  47. $pass_icon = $test->getPassIcon();
  48. $retest = (bool) $test->retest;
  49. }
  50. $output[] = (object) [
  51. "id" => (int) $location->id,
  52. "internalId" => $location->internal_id,
  53. "testId" => $test_id,
  54. "testerId" => $tester_id,
  55. "name" => $location->name,
  56. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  57. "address1" => $location->address_1,
  58. "status" => $status,
  59. "statusText" => $status_text,
  60. "pass" => $pass,
  61. "passClass" => $pass_class,
  62. "passText" => $pass_text,
  63. "passIcon" => $pass_icon,
  64. "class" => $class,
  65. "latitude" => (float) $location->latitude,
  66. "longitude" => (float) $location->longitude,
  67. "retest" => $retest,
  68. ];
  69. }
  70. usort($output, function($a, $b) {
  71. return $a->status <=> $b->status;
  72. });
  73. JSON::success([
  74. "locations" => $output
  75. ]);
  76. break;
  77. }
  78. // Gets testers near a project
  79. case "get-testers": {
  80. $location_id = http::value("location_id", "int");
  81. $include_unverified = http::value("include_unverified", "int");
  82. $minimum_score = http::value("minimum_score", "int");
  83. $location = Location::get($location_id);
  84. if(!$location) throw new Exception("Location not found.");
  85. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  86. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  87. $test = Test::queryOne([
  88. "location_id" => $location_id,
  89. "project_id" => $project->id,
  90. "status" => $status_constraint,
  91. ]);
  92. if(!$test) {
  93. throw new RuntimeException("A test does not exist for this project.");
  94. }
  95. /*
  96. $max_distance = http::value("max_distance", "int");
  97. if($max_distance < 2.5) $max_distance = 2.5;
  98. if($max_distance > 25) $max_distance = 25;
  99. */
  100. $max_distance = 25;
  101. if(Core::$user->canAccessProjects()) {
  102. $max_distance = 1000;
  103. }
  104. $bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
  105. $lat_constraint = new Constraint("BETWEEN");
  106. $lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
  107. $lng_constraint = new Constraint("BETWEEN");
  108. $lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
  109. $addresses = UserAddress::query([
  110. "latitude" => $lat_constraint,
  111. "longitude" => $lng_constraint
  112. ]);
  113. $users = User::load(array_column($addresses, "user_id"));
  114. $user_last_tests = Test::query([
  115. "tester_id" => array_column($users, "id"),
  116. "dts_start" => new Constraint("IS_NOT_NULL"),
  117. ]);
  118. $user_last_test_times = [];
  119. foreach($user_last_tests as $user_last_test) {
  120. $unix_start = strtotime($user_last_test->dts_start . " UTC");
  121. $current_start = $user_last_test_times[$user_last_test->tester_id] ?? 0;
  122. if($unix_start > $current_start) {
  123. $user_last_test_times[$user_last_test->tester_id] = $unix_start;
  124. }
  125. }
  126. $pending_tests = Test::query([
  127. "tester_id" => array_column($users, "id"),
  128. "status" => Test::STATUS_ASSIGNED,
  129. ]);
  130. // Sort by distance to location
  131. usort($addresses, static function($a, $b) use ($location) {
  132. $lat_lng = $location->getGeolocation();
  133. return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
  134. });
  135. $output = [];
  136. foreach($addresses as $address) {
  137. $user = $address->getUser();
  138. $is_current_tester = $user->id == $test->tester_id;
  139. $is_unverified = $user->level <= User::LEVEL_UNVERIFIED;
  140. if($user->isArchived()) {
  141. continue;
  142. }
  143. if($user->isDeactivated()) {
  144. continue;
  145. }
  146. if(!$include_unverified && $user->isUnverified()) {
  147. continue;
  148. }
  149. if(!$user->isTester()) {
  150. continue;
  151. }
  152. if($user->score < $minimum_score) {
  153. continue;
  154. }
  155. $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
  156. $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
  157. $user_pending_tests = array_filter($pending_tests, static function($test) use ($user) {
  158. return ((int) $user->id === (int) $test->tester_id);
  159. });
  160. $pending_test_count = count($user_pending_tests);
  161. $age = $user->getAge();
  162. // Age check
  163. if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
  164. continue;
  165. }
  166. else if($project->min_age && $age < $project->min_age) {
  167. continue;
  168. }
  169. else if($project->max_age && $age > $project->max_age) {
  170. continue;
  171. }
  172. $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
  173. $time_last_test = $user_last_test_times[$user->id] ?? false;
  174. $output[] = [
  175. "id" => (int) $user->id,
  176. "addressId" => (int) $address->id,
  177. "name" => $user->getDisplayName(),
  178. "age" => $age,
  179. "phone" => $user->phone,
  180. "score" => (int) $user->score,
  181. "distanceMiles" => $distance_miles,
  182. "distanceKilometers" => $distance_kilometers,
  183. "maxRadiusMiles" => (float) $address->max_radius_miles,
  184. "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
  185. "maxRadiusExceeded" => $max_radius_exceeded,
  186. "latitude" => (float) $address->latitude,
  187. "longitude" => (float) $address->longitude,
  188. "pendingTestCount" => $pending_test_count,
  189. "isCurrentTester" => $is_current_tester,
  190. "isRegistered" => $user->isRegistered(),
  191. "isUnverified" => $is_unverified,
  192. "levelText" => $user->getLevelText(),
  193. "levelClass" => $user->getLevelClass(),
  194. "termsAccepted" => boolval($user->terms_accepted),
  195. "reported" => (int) $user->reported,
  196. "reportedComment" => $user->reported_comment,
  197. "timeRegistered" => strtotime($user->dts_registered . " UTC"),
  198. "timeLastTest" => $time_last_test,
  199. ];
  200. }
  201. JSON::success([
  202. "testers" => $output
  203. ]);
  204. }
  205. // Assigns a tester to a test
  206. case "assign-tester": {
  207. $year = http::value("year", "int");
  208. $month = http::value("month", "int");
  209. $day = http::value("day", "int");
  210. $unix_due = 0;
  211. if($year && $month && $day) {
  212. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  213. $day = str_pad($day, 2, "0", STR_PAD_LEFT);
  214. $unix_due = strtotime("$year-$month-$day");
  215. }
  216. $message = http::value("message");
  217. $location_id = http::value("location_id", "int");
  218. $location = Location::get($location_id);
  219. if(!$location) throw new Exception("Location not found.");
  220. $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
  221. $status_constraint->setValues(Test::STATUS_UNASSIGNED);
  222. $nvp = [
  223. "location_id" => $location_id,
  224. "project_id" => $project->id,
  225. "status" => $status_constraint,
  226. ];
  227. $test = Test::queryOne($nvp);
  228. if(!$test) throw new Exception("A test does not exist for this location.");
  229. $address_id = http::value("address_id", "int");
  230. $address = UserAddress::get($address_id);
  231. if(!$address) throw new Exception("User address cannot be found.");
  232. $user = $address->getUser();
  233. if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
  234. if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
  235. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
  236. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
  237. if($user->level != User::LEVEL_TESTER) throw new Exception("A test cannot be assigned to a user that's not a tester.");
  238. $test->status = Test::STATUS_ASSIGNED;
  239. $test->tester_id = $user->id;
  240. $test->dts_assigned = Util::getDateTime();
  241. $test->dts_updated = Util::getDateTime();
  242. if($unix_due) {
  243. $test->dts_due = "$year-$month-$day 23:59:59";
  244. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
  245. if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
  246. }
  247. $success = $test->commit();
  248. if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
  249. $test->sendAssignEmail($message);
  250. JSON::success();
  251. break;
  252. }
  253. case "add-locations": {
  254. $location_ids = http::value("location_ids", "int");
  255. $customer = $project->getCustomer();
  256. $locations = Location::query([
  257. "customer_id" => $customer->id,
  258. "id" => $location_ids
  259. ]);
  260. $dupe_locations = [];
  261. foreach($locations as $location) {
  262. $test = $project->getActiveTestForLocationId($location->id);
  263. if($test) {
  264. $dupe_locations[] = $location;
  265. }
  266. else {
  267. $project->createTestForLocationId($location->id);
  268. }
  269. }
  270. $dupe_location_objects = [];
  271. foreach($dupe_locations as $location) {
  272. $dupe_location_objects[] = [
  273. "id" => $location->id,
  274. "name" => $location->name
  275. ];
  276. }
  277. JSON::success([
  278. "dupeLocations" => $dupe_location_objects
  279. ]);
  280. break;
  281. }
  282. default:
  283. throw new Exception("Invalid request.");
  284. }
  285. }
  286. catch(Exception $e) {
  287. JSON::fail($e->getMessage());
  288. }
  289. }
  290. JSON::fail("Invalid request.");
  291. ?>