project.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $location_ids_json = http::value("location_ids_json", "string");
  23. if($action) {
  24. switch($action) {
  25. case "move-tests":
  26. $location_ids = json_decode($location_ids_json);
  27. if(!is_array($location_ids)) {
  28. $location_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(["location_id" => $location_ids, "project_id" => $id]);
  38. $source_test_count = count($source_tests);
  39. $expected_test_count = count($location_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. $conflicting_tests = Test::query(["location_id" => $location_ids, "project_id" => $target_project_id]);
  44. $conflicting_test_count = count($conflicting_tests);
  45. if($conflicting_test_count > 0) {
  46. throw new RuntimeException(__("%d conflicting test(s) for a location was found in the target project. Please remove from the target project and try again.", $conflicting_test_count));
  47. }
  48. // OK at this point
  49. foreach($source_tests as $source_test) {
  50. $source_test->project_id = $target_project_id;
  51. $source_test->commit();
  52. }
  53. Core::addAlert("success", __("Tests moved successfully."));
  54. break;
  55. default:
  56. throw new RuntimeException(__("Invalid action."));
  57. }
  58. }
  59. $completed_percentage = $project->getCompletedPercentage();
  60. $locations = $customer->getLocations();
  61. $tests = $project->getTests();
  62. usort($tests, static function(Test $a, Test $b) {
  63. return $a->status <=> $b->status;
  64. });
  65. $created_test_count = count($tests);
  66. $completed_test_count = count(array_filter($tests, static function(Test $test) {
  67. return $test->isCompleted();
  68. }));
  69. $pass_count = count(array_filter($tests, static function(Test $test) {
  70. return $test->isPass();
  71. }));
  72. $fail_count = count(array_filter($tests, static function(Test $test) {
  73. return $test->isFail();
  74. }));
  75. $pass_fail_count = $pass_count + $fail_count;
  76. $pass_percentage = 0;
  77. $fail_percentage = 0;
  78. if($pass_fail_count) {
  79. $pass_percentage = number_format(($pass_count / $pass_fail_count) * 100, 1);
  80. $fail_percentage = 100 - $pass_percentage;
  81. }
  82. $min_lat = $min_lng = 180;
  83. $max_lat = $max_lng = -180;
  84. foreach($locations as $location) {
  85. if($location->latitude < $min_lat) $min_lat = $location->latitude;
  86. if($location->latitude > $max_lat) $max_lat = $location->latitude;
  87. if($location->longitude < $min_lng) $min_lng = $location->longitude;
  88. if($location->longitude > $max_lng) $max_lng = $location->longitude;
  89. }
  90. $latitude = ($min_lat + $max_lat) / 2;
  91. $longitude = ($min_lng + $max_lng) / 2;
  92. $latitude = 51.3555177;
  93. $longitude = -1.1743197;
  94. $deadline_date = explode(" ", $project->dts_deadline)[0];
  95. View::includeOnce("header", compact("page_title"));
  96. 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"));
  97. View::include("modal_assign_tester");
  98. View::includeOnce("footer");