project.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. $test_ids = json_decode($test_ids_json);
  25. if(!is_array($test_ids)) {
  26. $test_ids = [];
  27. }
  28. switch($action) {
  29. case "move-tests":
  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. case "unassign":
  64. $tests = Test::query(["id" => $test_ids, "project_id" => $id, "status" => Test::STATUS_ASSIGNED]);
  65. foreach($tests as $test) {
  66. $test->unassign();
  67. }
  68. Core::addAlert("success", __("Tests unassigned successfully."));
  69. break;
  70. default:
  71. throw new RuntimeException(__("Invalid action."));
  72. }
  73. }
  74. $completed_percentage = $project->getCompletedPercentage();
  75. $locations = $customer->getLocations();
  76. $tests = $project->getTests();
  77. usort($tests, static function(Test $a, Test $b) {
  78. return $a->status <=> $b->status;
  79. });
  80. $created_test_count = count($tests);
  81. $completed_test_count = count(array_filter($tests, static function(Test $test) {
  82. return $test->isCompleted();
  83. }));
  84. $pass_count = count(array_filter($tests, static function(Test $test) {
  85. return $test->isPass();
  86. }));
  87. $fail_count = count(array_filter($tests, static function(Test $test) {
  88. return $test->isFail();
  89. }));
  90. $pass_fail_count = $pass_count + $fail_count;
  91. $pass_percentage = 0;
  92. $fail_percentage = 0;
  93. if($pass_fail_count) {
  94. $pass_percentage = number_format(($pass_count / $pass_fail_count) * 100, 1);
  95. $fail_percentage = 100 - $pass_percentage;
  96. }
  97. $min_lat = $min_lng = 180;
  98. $max_lat = $max_lng = -180;
  99. foreach($locations as $location) {
  100. if($location->latitude < $min_lat) $min_lat = $location->latitude;
  101. if($location->latitude > $max_lat) $max_lat = $location->latitude;
  102. if($location->longitude < $min_lng) $min_lng = $location->longitude;
  103. if($location->longitude > $max_lng) $max_lng = $location->longitude;
  104. }
  105. $latitude = ($min_lat + $max_lat) / 2;
  106. $longitude = ($min_lng + $max_lng) / 2;
  107. $latitude = 51.3555177;
  108. $longitude = -1.1743197;
  109. $deadline_date = explode(" ", $project->dts_deadline)[0];
  110. View::includeOnce("header", compact("page_title"));
  111. 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"));
  112. View::include("modal_assign_tester");
  113. View::includeOnce("footer");