project_ajax.php 11 KB

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