project.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $dt_deadline = $project->getDateTimeDeadline();
  76. $dt_default_due = CarbonImmutable::now(TIMEZONE)->addDays(7);
  77. /** @var CarbonImmutable $dt_due */
  78. $dt_due = min($dt_deadline, $dt_default_due);
  79. $tests = Test::query(["id" => $test_ids, "project_id" => $id, "status" => [
  80. Test::STATUS_UNASSIGNED,
  81. Test::STATUS_ASSIGNED,
  82. ]]);
  83. foreach($tests as $test) {
  84. $test->assignTester($tester, $dt_due);
  85. }
  86. break;
  87. case "unassign":
  88. $tests = Test::query(["id" => $test_ids, "project_id" => $id, "status" => Test::STATUS_ASSIGNED]);
  89. foreach($tests as $test) {
  90. $test->unassignTester();
  91. }
  92. Core::addAlert("success", __("Tests unassigned successfully."));
  93. break;
  94. default:
  95. throw new RuntimeException(__("Invalid action."));
  96. }
  97. }
  98. $completed_percentage = $project->getCompletedPercentage();
  99. $locations = $customer->getLocations();
  100. $tests = $project->getTests();
  101. usort($tests, static function(Test $a, Test $b) {
  102. return $a->status <=> $b->status;
  103. });
  104. $created_test_count = count($tests);
  105. $completed_test_count = count(array_filter($tests, static function(Test $test) {
  106. return $test->isCompleted();
  107. }));
  108. $pass_count = count(array_filter($tests, static function(Test $test) {
  109. return $test->isPass();
  110. }));
  111. $fail_count = count(array_filter($tests, static function(Test $test) {
  112. return $test->isFail();
  113. }));
  114. $pass_fail_count = $pass_count + $fail_count;
  115. $pass_percentage = 0;
  116. $fail_percentage = 0;
  117. if($pass_fail_count) {
  118. $pass_percentage = number_format(($pass_count / $pass_fail_count) * 100, 1);
  119. $fail_percentage = 100 - $pass_percentage;
  120. }
  121. $min_lat = $min_lng = 180;
  122. $max_lat = $max_lng = -180;
  123. foreach($locations as $location) {
  124. if($location->latitude < $min_lat) $min_lat = $location->latitude;
  125. if($location->latitude > $max_lat) $max_lat = $location->latitude;
  126. if($location->longitude < $min_lng) $min_lng = $location->longitude;
  127. if($location->longitude > $max_lng) $max_lng = $location->longitude;
  128. }
  129. $latitude = ($min_lat + $max_lat) / 2;
  130. $longitude = ($min_lng + $max_lng) / 2;
  131. $latitude = 51.3555177;
  132. $longitude = -1.1743197;
  133. $deadline_date = explode(" ", $project->dts_deadline)[0];
  134. View::includeOnce("header", compact("page_title"));
  135. 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"));
  136. View::include("modal_assign_tester");
  137. View::includeOnce("footer");