Kaynağa Gözat

Add support for batch moving tests to another project

Alan Lam 2 yıl önce
ebeveyn
işleme
627e479ab2
4 değiştirilmiş dosya ile 166 ekleme ve 32 silme
  1. 76 13
      controllers/project.php
  2. 7 2
      engine/customer.php
  3. 13 0
      public/js/app-project.js
  4. 70 17
      views/project.php

+ 76 - 13
controllers/project.php

@@ -1,8 +1,16 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $id
+ */
 
 $project = Project::get($id);
-if(!$project) throw new Exception("Project not found.");
-if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this page.");
+if(!$project) {
+	throw new RuntimeException("Project not found.");
+}
+if(!Core::$user->hasPermission($project)) {
+	throw new RuntimeException("You do not have permission to access this page.");
+}
 
 $page_title = $project->name;
 
@@ -16,25 +24,82 @@ Core::addBreadcrumb("/account/projects/$project->id", $page_title);
 Core::addScript("public/js/app-project.js");
 Core::addScript("public/js/markerclusterer.js");
 
+$action = http::value("action", "string");
+$target_project_id = http::value("target_project_id");
+$location_ids_json = http::value("location_ids_json", "string");
+
+if($action) {
+	switch($action) {
+		case "move-tests":
+
+			$location_ids = json_decode($location_ids_json);
+			if(!is_array($location_ids)) {
+				$location_ids = [];
+			}
+
+			$target_project = Project::get($target_project_id);
+			if(!$target_project) {
+				throw new RuntimeException("Target project not found.");
+			}
+
+			if($target_project->template_id !== $project->template_id) {
+				throw new RuntimeException("Target project template is not the same as this project.");
+			}
+
+			$source_tests = Test::query(["location_id" => $location_ids, "project_id" => $id]);
+
+			$source_test_count = count($source_tests);
+			$expected_test_count = count($location_ids);
+
+			if($source_test_count !== $expected_test_count) {
+				throw new RuntimeException("Tests do not match number of IDs submitted. $source_test_count $expected_test_count");
+			}
+
+			$conflicting_tests = Test::query(["location_id" => $location_ids, "project_id" => $target_project_id]);
+			$conflicting_test_count = count($conflicting_tests);
+			if($conflicting_test_count > 0) {
+				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));
+			}
+
+			// OK at this point
+
+			foreach($source_tests as $source_test) {
+				$source_test->project_id = $target_project_id;
+				$source_test->commit();
+			}
+
+			Core::addAlert("success", __("Tests moved successfully."));
+			break;
+
+		default:
+			throw new RuntimeException(__("Invalid action."));
+	}
+}
+
+
+
+
 $completed_percentage = $project->getCompletedPercentage();
 
 $locations = $customer->getLocations();
 
 $tests = $project->getTests();
-usort($tests, function($a, $b) {
+usort($tests, static function(Test $a, Test $b) {
 	return $a->status <=> $b->status;
 });
 
 $created_test_count = count($tests);
 
-$pending_verification_tests = array_filter($tests, function($test) { return $test->status == Test::STATUS_PENDING_VERIFICATION; });
-$pending_verification_test_count = count($pending_verification_tests);
+$completed_test_count = count(array_filter($tests, static function(Test $test) {
+	return $test->isCompleted();
+}));
 
-$completed_tests = array_filter($tests, function($test) { return $test->status == Test::STATUS_COMPLETED; });
-$completed_test_count = count($completed_tests);
-
-$pass_count = count(array_filter($tests, function($test) { return $test->isPass(); }));
-$fail_count = count(array_filter($tests, function($test) { return $test->isFail(); }));
+$pass_count = count(array_filter($tests, static function(Test $test) {
+	return $test->isPass();
+}));
+$fail_count = count(array_filter($tests, static function(Test $test) {
+	return $test->isFail();
+}));
 $pass_fail_count = $pass_count + $fail_count;
 
 $pass_percentage = 0;
@@ -66,5 +131,3 @@ View::includeOnce("header", compact("page_title"));
 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"));
 View::include("modal_assign_tester");
 View::includeOnce("footer");
-
-?>

+ 7 - 2
engine/customer.php

@@ -94,8 +94,13 @@ class Customer extends Model {
 	 * @return Project[]
 	 * @throws Exception
 	 */
-	public function getProjects() {
-		return Project::query(["customer_id" => $this->id], ["sort" => "dts_deadline", "order" => "desc"]);
+	public function getProjects(array $nvp = []): array {
+		$nvp = array_merge($nvp, ["customer_id" => $this->id]);
+
+		return Project::query($nvp, [
+			"sort" => "dts_deadline",
+			"order" => "desc"
+		]);
 	}
 
 	public function getFinishedProjects() {

+ 13 - 0
public/js/app-project.js

@@ -85,6 +85,16 @@ $(function() {
 			}
 		}, 1000);
 	}
+
+	$(document).on("change", ".location-checkbox", event => {
+		const locationIds = $(document).find(".location-checkbox:checked").map((index, element) => {
+			return element.value;
+		}).get();
+
+		$("input[name=location_ids_json]").val(JSON.stringify(locationIds));
+
+	});
+
 });
 
 function clearMarkers() {
@@ -175,6 +185,9 @@ function initialiseLocations() {
 			// Populate table
 			var tr = $("<tr data-location-id=\"" + location.id + "\" data-internal-id=\"" + location.internalId + "\" data-tester-id=\"" + location.testerId + "\" data-latitude=\"" + location.latitude + "\" data-longitude=\"" + location.longitude + "\" data-filter-value=\"" + filterValue + "\"></tr>");
 
+			const $checkboxCell = $(`<td class="text-center"><input class="location-checkbox" type="checkbox" value="${location.id}" /></td>`);
+			tr.append($checkboxCell);
+
 			var locationIdTd = $("<td class=\"text-right\"></td>");
 			locationIdTd.append(location.id);
 			tr.append(locationIdTd);

+ 70 - 17
views/project.php

@@ -222,39 +222,92 @@ if($auto_select_location_id) {
 
 
 	<div class="row">
-		<div class="col-lg-6 mb-3">
+		<div class="col-lg-6">
 
-			<h3>Tests</h3>
+			<section class="mb-4">
+				<h3>Tests</h3>
 
-			<div class="input-group mb-3">
-				<div class="input-group-prepend">
-					<label for="filter" class="input-group-text">
-						Filter
-					</label>
+				<div class="input-group mb-3">
+					<div class="input-group-prepend">
+						<label for="filter" class="input-group-text">
+							Filter
+						</label>
+					</div>
+					<input class="form-control" id="filter" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#tests-tbody" />
 				</div>
-				<input class="form-control" id="filter" type="text" placeholder="Enter name, address, ID or internal ID" data-filter-target="#tests-tbody" />
-			</div>
 
-			<div class="limit-height-2">
-				<table class="table table-hover table-sm table-sm-text">
-					<thead>
+				<div class="limit-height-2">
+					<table class="table table-hover table-sm table-sm-text">
+						<thead>
 						<tr>
+							<th></th>
 							<th class="text-right">ID</th>
 							<th>Name</th>
 							<th>Status</th>
 							<th>Result</th>
 							<th></th>
 						</tr>
-					</thead>
-					<tbody id="tests-tbody">
+						</thead>
+						<tbody id="tests-tbody">
 						<tr>
 							<td colspan="5" class="text-center">
 								No tests to show
 							</td>
 						</tr>
-					</tbody>
-				</table>
-			</div>
+						</tbody>
+					</table>
+				</div>
+			</section>
+
+			<?php
+			$customer = $project->getCustomer();
+
+			$target_projects = $customer->getProjects([
+				"id" => Constraint::notEquals($project->id),
+				"template_id" => $project->template_id
+			]);
+			?>
+
+			<section class="mb-4">
+				<form method="post">
+					<div class="card">
+						<div class="card-body bg-light">
+							<h3><?=htmlspecialchars(__("Batch Actions"))?></h3>
+
+							<div class="form-group">
+								<label for="batch-action"><?=htmlspecialchars(__("Action"))?></label>
+								<select class="form-control" name="action" id="batch-action" required>
+									<option value=""><?=htmlspecialchars(__("Select Action"))?></option>
+									<option value="move-tests">Move Selected Tests To Another Project</option>
+								</select>
+							</div>
+
+							<div class="form-group">
+								<label for="target-project-id"><?=htmlspecialchars(__("Target Project"))?></label>
+								<select class="form-control" name="target_project_id" id="target-project-id" required>
+									<option value=""><?=htmlspecialchars(__("Select Project"))?></option>
+									<?php
+									foreach($target_projects as $target_project) {
+										?>
+										<option value="<?=$target_project->id?>">
+											<?=htmlspecialchars($target_project->name)?>
+										</option>
+										<?php
+									}
+									?>
+								</select>
+							</div>
+
+							<input type="hidden" name="location_ids_json" value="" />
+
+							<button type="submit" class="btn btn-primary">
+								<?=htmlspecialchars(__("Submit"))?>
+							</button>
+						</div>
+					</div>
+				</form>
+			</section>
+
 		</div>
 		<div class="col-lg-6 mb-3">