| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- JSON::header();
- $project = Project::get($id);
- if(!$project) throw new Exception("Project cannot be found.");
- if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- switch($action) {
- // Gets all locations for a project
- case "get-locations": {
- $customer = $project->getCustomer();
- $tests = $project->getTests();
- $locations = Location::load(array_column($tests, "location_id"));
- $location_tests = [];
- foreach($tests as $test) {
- if($test->status == Test::STATUS_CANCELLED) continue;
- $location_tests[$test->location_id] = $test;
- }
- $output = [];
- foreach($locations as $location) {
- $test = $location_tests[$location->id] ?? false;
- $test_id = null;
- $status = 0;
- $status_text = "";
- $class = "secondary";
- if($test) {
- $test_id = $test->id;
- $class = $test->getStatusClass();
- $status = $test->status;
- $status_text = $test->getStatusText();
- }
- $output[] = (object) [
- "id" => (int) $location->id,
- "testId" => (int) $test_id,
- "name" => $location->name,
- "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
- "status" => $status,
- "statusText" => $status_text,
- "class" => $class,
- "latitude" => (float) $location->latitude,
- "longitude" => (float) $location->longitude
- ];
- }
- usort($output, function($a, $b) {
- return $a->status <=> $b->status;
- });
- JSON::success([
- "locations" => $output
- ]);
- break;
- }
- // Gets testers near a project
- case "get-testers": {
- $location_id = http::value("location_id", "int");
- $location = Location::get($location_id);
- if(!$location) throw new Exception("Location not found.");
- $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
- $status_constraint->setValues(Test::STATUS_UNASSIGNED);
- $nvp = [
- "location_id" => $location_id,
- "project_id" => $project->id,
- "status" => $status_constraint,
- ];
- $test = Test::queryOne($nvp);
- if(!$test) throw new Exception("A test does not exist for this project.");
- /*
- $max_distance = http::value("max_distance", "int");
- if($max_distance < 2.5) $max_distance = 2.5;
- if($max_distance > 25) $max_distance = 25;
- */
- $max_distance = 25;
- $bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
- $lat_constraint = new Constraint("BETWEEN");
- $lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
- $lng_constraint = new Constraint("BETWEEN");
- $lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
- $nvp = [
- "latitude" => $lat_constraint,
- "longitude" => $lng_constraint
- ];
- $addresses = UserAddress::query($nvp);
- $users = User::load(array_column($addresses, "user_id"));
- $pending_tests = Test::query([
- "tester_id" => array_column($users, "id"),
- "status" => Test::STATUS_ASSIGNED,
- ]);
- $age_constraint = false;
- usort($addresses, function($a, $b) use ($location) {
- $lat_lng = $location->getGeolocation();
- return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
- });
- $output = [];
- foreach($addresses as $address) {
- $user = $address->getUser();
- $is_current_tester = $user->id == $test->tester_id;
- $distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
- $distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
- $user_pending_tests = array_filter($pending_tests, function($test) use ($user) {
- return ($user->id == $test->tester_id);
- });
- $pending_test_count = count($user_pending_tests);
- $age = $user->getAge();
- // Age check
- if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
- continue;
- }
- else if($project->min_age && $age < $project->min_age) {
- continue;
- }
- else if($project->max_age && $age > $project->max_age) {
- continue;
- }
- $max_radius_exceeded = ($distance_miles > $address->max_radius_miles);
- $output[] = [
- "id" => (int) $user->id,
- "addressId" => (int) $address->id,
- "name" => $user->getDisplayName(),
- "age" => $age,
- "phone" => $user->phone,
- "score" => (int) $user->score,
- "distanceMiles" => $distance_miles,
- "distanceKilometers" => $distance_kilometers,
- "maxRadiusMiles" => (float) $address->max_radius_miles,
- "maxRadiusKilometers" => $address->max_radius_miles * Geolocation::KMS_IN_MILE,
- "maxRadiusExceeded" => $max_radius_exceeded,
- "latitude" => (float) $address->latitude,
- "longitude" => (float) $address->longitude,
- "pendingTestCount" => $pending_test_count,
- "isCurrentTester" => $is_current_tester
- ];
- }
- JSON::success([
- "testers" => $output
- ]);
- }
- // Assigns a tester to a test
- case "assign-tester": {
- $location_id = http::value("location_id", "int");
- $location = Location::get($location_id);
- if(!$location) throw new Exception("Location not found.");
- $status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
- $status_constraint->setValues(Test::STATUS_UNASSIGNED);
- $nvp = [
- "location_id" => $location_id,
- "project_id" => $project->id,
- "status" => $status_constraint,
- ];
- $test = Test::queryOne($nvp);
- if(!$test) throw new Exception("A test does not exist for this location.");
- $address_id = http::value("address_id", "int");
- $address = UserAddress::get($address_id);
- if(!$address) throw new Exception("User address cannot be found.");
- $user = $address->getUser();
- if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
- if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
- if($user->level >= User::LEVEL_VENUE) throw new Exception("A test cannot be assigned to a user that's not a tester.");
- $test->status = Test::STATUS_ASSIGNED;
- $test->tester_id = $user->id;
- $test->dts_assigned = Util::getDateTime();
- $success = $test->commit();
- if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
- // TODO: send user email
- JSON::success();
- break;
- }
- default:
- throw new Exception("Invalid request.");
- }
- }
- catch(Exception $e) {
- JSON::fail($e->getMessage());
- }
- }
- JSON::fail("Invalid request.");
- ?>
|