| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?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 = "";
- $pass = 0;
- $pass_class = "";
- $pass_text = "";
- $pass_icon = "";
- $class = "secondary";
- if($test) {
- $test_id = $test->id;
- $class = $test->getStatusClass();
- $status = $test->status;
- $status_text = $test->getStatusText();
- $pass = $test->pass;
- $pass_class = $test->getPassClass();
- $pass_text = $test->getPassText();
- $pass_icon = $test->getPassIcon();
- }
- $output[] = (object) [
- "id" => (int) $location->id,
- "internalId" => $location->internal_id,
- "testId" => (int) $test_id,
- "name" => $location->name,
- "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
- "status" => $status,
- "statusText" => $status_text,
- "pass" => (int) $pass,
- "passClass" => $pass_class,
- "passText" => $pass_text,
- "passIcon" => $pass_icon,
- "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;
- if(Core::$user->level == User::LEVEL_SUPERADMIN) {
- $max_distance = 1000;
- }
- $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": {
- $year = http::value("year", "int");
- $month = http::value("month", "int");
- $day = http::value("day", "int");
- $unix_due = 0;
- if($year && $month && $day) {
- $month = str_pad($month, 2, "0", STR_PAD_LEFT);
- $day = str_pad($day, 2, "0", STR_PAD_LEFT);
- $unix_due = strtotime("$year-$month-$day");
- }
- $message = http::value("message");
- $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_TESTER) 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();
- $test->dts_updated = Util::getDateTime();
- if($unix_due) {
- $test->dts_due = "$year-$month-$day 23:59:59";
- if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
- if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
- }
- $success = $test->commit();
- if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
- $test->sendAssignEmail($message);
- JSON::success();
- break;
- }
- default:
- throw new Exception("Invalid request.");
- }
- }
- catch(Exception $e) {
- JSON::fail($e->getMessage());
- }
- }
- JSON::fail("Invalid request.");
- ?>
|