1
0

tests_available.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. Core::addScript("public/js/app-tests-available.js");
  3. $page_title = "Find Tests";
  4. Core::addBreadcrumb("/account", "Account");
  5. Core::addBreadcrumb("/account/tests", "Tests");
  6. Core::addBreadcrumb("/account/tests/available", $page_title);
  7. $user = Core::$user;
  8. if(!$user->can_claim) throw new Exception("You do not have permission to perform this action.");
  9. $dts = date("Y-m-d H:i:s");
  10. // Get active projects
  11. $projects = Project::query([
  12. "tester_can_claim" => 1,
  13. "dts_start" => (new Constraint("LESS_THAN_OR_EQUAL"))->setValues($dts),
  14. ]);
  15. $project_ids = array_column($projects, "id");
  16. $tests = Test::query([
  17. "project_id" => $project_ids,
  18. "tester_id" => (new Constraint("IS_NULL")),
  19. "status" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues(1),
  20. ]);
  21. $locations = Location::load(array_column($tests, "location_id"));
  22. $addresses = $user->getAddresses();
  23. if(!$addresses) {
  24. $latitude = 51.509865;
  25. $longitude = -0.118092;
  26. }
  27. else {
  28. $address = $addresses[0];
  29. list($latitude, $longitude) = $address->getGeolocation();
  30. }
  31. // AJAX
  32. if(HTTP::isAJAX()) {
  33. require "tests_available_ajax.php";
  34. }
  35. usort($tests, function($a, $b) use ($address) {
  36. $distance_a = Geolocation::getDistance($a->getLocation()->getGeolocation(), $address->getGeolocation());
  37. $distance_b = Geolocation::getDistance($b->getLocation()->getGeolocation(), $address->getGeolocation());
  38. return $distance_a <=> $distance_b;
  39. });
  40. View::includeOnce("header", compact("page_title"));
  41. View::include("tests_available", compact("tests", "address", "latitude", "longitude"));
  42. View::include("modal_test_claim");
  43. View::includeOnce("footer");
  44. ?>