1
0

tests_available_ajax.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. JSON::header();
  3. $action = http::value("action");
  4. $response = [];
  5. switch($action) {
  6. case "claim-test":
  7. $id = http::value("id", "int");
  8. $test = Test::queryOne([
  9. "id" => $id,
  10. "status" => Test::STATUS_UNASSIGNED,
  11. ]);
  12. try {
  13. if(!$test) throw new Exception("Test no longer available.");
  14. $project = $test->getProject();
  15. // Set due date
  16. $date_due = date("Y-m-d", strtotime("+7 DAYS"));
  17. $unix_due = strtotime("$date_due 23:59:59");
  18. $user = Core::$user;
  19. if($user->level == User::LEVEL_BANNED) throw new Exception("You are a banned tester.");
  20. if($user->level == User::LEVEL_REGISTERED) throw new Exception("Your account is not verified.");
  21. if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("Your account is not verified.");
  22. if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("Your account is deactivated.");
  23. if($user->level != User::LEVEL_TESTER) throw new Exception("You are not a tester.");
  24. $test->status = Test::STATUS_CONFIRMED;
  25. $test->tester_id = $user->id;
  26. $test->dts_assigned = Util::getDateTime();
  27. $test->dts_updated = Util::getDateTime();
  28. if($unix_due) {
  29. $test->dts_due = "$date_due 23:59:59";
  30. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("Too early.");
  31. }
  32. $success = $test->commit();
  33. if(!$success) throw new Exception("Test cannot be claimed, please try again later.");
  34. $test->sendClaimedEmail();
  35. }
  36. catch(Exception $e) {
  37. JSON::fail($e->getMessage());
  38. }
  39. JSON::success();
  40. break;
  41. case "get-tests":
  42. $output = [];
  43. foreach($tests as $test) {
  44. $location = $test->getLocation();
  45. $output[] = (object) [
  46. "id" => (int) $test->id,
  47. "location" => (object) [
  48. "id" => (int) $location->id,
  49. "name" => $location->name,
  50. "address" => $location->getDisplayAddress(),
  51. "geolocation" => $location->getGeolocation(),
  52. ],
  53. ];
  54. }
  55. $response["tests"] = $output;
  56. break;
  57. default:
  58. throw new Exception("Invalid action.");
  59. }
  60. JSON::success($response);
  61. ?>