1
0

tests_available_ajax.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_rejected = null;
  28. $test->dts_updated = Util::getDateTime();
  29. if($unix_due) {
  30. $test->dts_due = "$date_due 23:59:59";
  31. if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception(__("Too early."));
  32. }
  33. $success = $test->commit();
  34. if(!$success) throw new Exception(__("Test cannot be claimed, please try again later."));
  35. $test->sendClaimedEmail();
  36. }
  37. catch(Exception $e) {
  38. JSON::fail($e->getMessage());
  39. }
  40. JSON::success();
  41. break;
  42. case "get-tests":
  43. $output = [];
  44. foreach($tests as $test) {
  45. $location = $test->getLocation();
  46. $output[] = (object) [
  47. "id" => (int) $test->id,
  48. "location" => (object) [
  49. "id" => (int) $location->id,
  50. "name" => $location->name,
  51. "address" => $location->getDisplayAddress(),
  52. "geolocation" => $location->getGeolocation(),
  53. ],
  54. ];
  55. }
  56. $response["tests"] = $output;
  57. break;
  58. default:
  59. throw new Exception(__("Invalid action."));
  60. }
  61. JSON::success($response);
  62. ?>