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, ]); 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; $is_deactivated = $user->level == User::LEVEL_DEACTIVATED; $is_registered = $user->level == User::LEVEL_REGISTERED; $is_unverified = $user->level == User::LEVEL_UNVERIFIED; if($is_deactivated) continue; $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, "isRegistered" => $is_registered, "isUnverified" => $is_unverified, "levelText" => $user->getLevelText(), "levelClass" => $user->getLevelClass(), ]; } 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_BANNED) throw new Exception("A test cannot be assigned to an banned user."); if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user."); 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."); ?>