project.php 5.4 KB

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