1
0

tests_available.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. $projects = Project::load($project_ids);
  23. $templates = Template::load(array_column($projects, "template_id"));
  24. $addresses = $user->getAddresses();
  25. if(!$addresses) {
  26. $latitude = 51.509865;
  27. $longitude = -0.118092;
  28. }
  29. else {
  30. $address = $addresses[0];
  31. list($latitude, $longitude) = $address->getGeolocation();
  32. }
  33. // AJAX
  34. if(HTTP::isAJAX()) {
  35. require "tests_available_ajax.php";
  36. }
  37. usort($tests, function($a, $b) use ($address) {
  38. $distance_a = Geolocation::getDistance($a->getLocation()->getGeolocation(), $address->getGeolocation());
  39. $distance_b = Geolocation::getDistance($b->getLocation()->getGeolocation(), $address->getGeolocation());
  40. return $distance_a <=> $distance_b;
  41. });
  42. View::includeOnce("header", compact("page_title"));
  43. View::include("tests_available", compact("tests", "address", "latitude", "longitude"));
  44. View::include("modal_test_claim");
  45. View::includeOnce("footer");
  46. ?>