project.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. */
  5. $project = Project::get($id);
  6. if(!$project) {
  7. throw new RuntimeException("Project not found.");
  8. }
  9. if(!Core::$user->hasPermission($project)) {
  10. throw new RuntimeException("You do not have permission to access this page.");
  11. }
  12. $page_title = $project->name;
  13. $customer = $project->getCustomer();
  14. Core::addBreadcrumb("/account", "Account");
  15. Core::addBreadcrumb("/account/projects", "Projects");
  16. Core::addBreadcrumb("/account/projects?customer_id=$customer->id", "$customer->name Projects");
  17. Core::addBreadcrumb("/account/projects/$project->id", $page_title);
  18. Core::addScript("public/js/app-project.js");
  19. Core::addScript("public/js/markerclusterer.js");
  20. $action = http::value("action", "string");
  21. $target_project_id = http::value("target_project_id");
  22. $test_ids_json = http::value("test_ids_json", "string");
  23. if($action) {
  24. switch($action) {
  25. case "move-tests":
  26. $test_ids = json_decode($test_ids_json);
  27. if(!is_array($test_ids)) {
  28. $test_ids = [];
  29. }
  30. $target_project = Project::get($target_project_id);
  31. if(!$target_project) {
  32. throw new RuntimeException("Target project not found.");
  33. }
  34. if($target_project->template_id !== $project->template_id) {
  35. throw new RuntimeException("Target project template is not the same as this project.");
  36. }
  37. $source_tests = Test::query(["id" => $test_ids, "project_id" => $id, "status" => Constraint::notEquals(Test::STATUS_CANCELLED)]);
  38. $source_test_count = count($source_tests);
  39. $expected_test_count = count($test_ids);
  40. if($source_test_count !== $expected_test_count) {
  41. throw new RuntimeException("Tests do not match number of IDs submitted. $source_test_count $expected_test_count");
  42. }
  43. $location_ids = [];
  44. foreach($source_tests as $source_test) {
  45. $location_ids[] = $source_test->location_id;
  46. }
  47. $conflicting_tests = Test::query(["location_id" => $location_ids, "project_id" => $target_project_id, "status" => Constraint::notEquals(Test::STATUS_CANCELLED)]);
  48. $conflicting_test_count = count($conflicting_tests);
  49. if($conflicting_test_count > 0) {
  50. $location_names = [];
  51. foreach($conflicting_tests as $conflicting_test) {
  52. $location_names[] = $conflicting_test->getLocation()->name;
  53. }
  54. throw new RuntimeException(__("%d conflicting test(s) were found in the target project. Locations: %s", $conflicting_test_count, implode(", ", $location_names)));
  55. }
  56. // OK at this point
  57. foreach($source_tests as $source_test) {
  58. $source_test->project_id = $target_project_id;
  59. $source_test->commit();
  60. }
  61. Core::addAlert("success", __("Tests moved successfully."));
  62. break;
  63. default:
  64. throw new RuntimeException(__("Invalid action."));
  65. }
  66. }
  67. $completed_percentage = $project->getCompletedPercentage();
  68. $locations = $customer->getLocations();
  69. $tests = $project->getTests();
  70. usort($tests, static function(Test $a, Test $b) {
  71. return $a->status <=> $b->status;
  72. });
  73. $created_test_count = count($tests);
  74. $completed_test_count = count(array_filter($tests, static function(Test $test) {
  75. return $test->isCompleted();
  76. }));
  77. $pass_count = count(array_filter($tests, static function(Test $test) {
  78. return $test->isPass();
  79. }));
  80. $fail_count = count(array_filter($tests, static function(Test $test) {
  81. return $test->isFail();
  82. }));
  83. $pass_fail_count = $pass_count + $fail_count;
  84. $pass_percentage = 0;
  85. $fail_percentage = 0;
  86. if($pass_fail_count) {
  87. $pass_percentage = number_format(($pass_count / $pass_fail_count) * 100, 1);
  88. $fail_percentage = 100 - $pass_percentage;
  89. }
  90. $min_lat = $min_lng = 180;
  91. $max_lat = $max_lng = -180;
  92. foreach($locations as $location) {
  93. if($location->latitude < $min_lat) $min_lat = $location->latitude;
  94. if($location->latitude > $max_lat) $max_lat = $location->latitude;
  95. if($location->longitude < $min_lng) $min_lng = $location->longitude;
  96. if($location->longitude > $max_lng) $max_lng = $location->longitude;
  97. }
  98. $latitude = ($min_lat + $max_lat) / 2;
  99. $longitude = ($min_lng + $max_lng) / 2;
  100. $latitude = 51.3555177;
  101. $longitude = -1.1743197;
  102. $deadline_date = explode(" ", $project->dts_deadline)[0];
  103. View::includeOnce("header", compact("page_title"));
  104. View::include("project", compact("project", "tests", "latitude", "longitude", "completed_percentage", "pass_count", "fail_count", "pass_fail_count", "pass_percentage", "fail_percentage", "customer", "locations", "deadline_date", "created_test_count", "completed_test_count"));
  105. View::include("modal_assign_tester");
  106. View::includeOnce("footer");