Forráskód Böngészése

Add project customer CSV export

Alan Lam 8 éve
szülő
commit
3c09711153
3 módosított fájl, 120 hozzáadás és 0 törlés
  1. 118 0
      controllers/project_customer_csv.php
  2. 1 0
      routes.php
  3. 1 0
      views/project.php

+ 118 - 0
controllers/project_customer_csv.php

@@ -0,0 +1,118 @@
+<?php
+
+$project = Project::get($id);
+if(!$project) throw new Exception("Project cannot be found.");
+if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
+
+header("Content-Type: text/csv");
+header("Content-Disposition: attachment; filename=\"export.csv\"");
+header("Cache-Control: private, max-age=0, must-revalidate");
+header("Pragma: public");
+
+$template = $project->getTemplate();
+
+$object = json_decode($template->json);
+
+$fields_1 = $object->beforeTest;
+$fields_2 = $object->duringTest;
+
+$fields = array_merge($fields_1, $fields_2);
+$fields = array_filter($fields, function($field) {
+	if(!isset($field->name)) return false;
+	if($field->type == "file") return false;
+	return true;
+});
+
+$tests = Test::query([
+	"project_id" => $id,
+	"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+]);
+
+$locations = Location::load(array_column($tests, "location_id"));
+$testers = User::load(array_column($tests, "tester_id"));
+
+$handle = fopen("php://output", "w");
+
+$field_names = [
+	"Project Name",
+	"Test Status", "Test Result",
+	"Location Name", "Address", "Region", "Internal ID", "Area Manager",
+
+	"Date",
+];
+
+foreach($fields as $field) {
+	$field_names[] = $field->label;
+}
+
+fputcsv($handle, $field_names);
+
+foreach($tests as $test) {
+
+	$location = $test->getLocation();
+	$tester = $test->getTester();
+	$values = $test->getValues(true);
+
+	$test_date = "";
+	$test_start = "";
+	$test_end = "";
+	$test_duration = "";
+
+	$result = "Pending";
+
+	$age = "";
+	$gender = "";
+	$travel_costs = "";
+
+	$output = [];
+	$output[] = $project->name;
+	$output[] = $test->getStatusText();
+
+	if(!is_null($test->pass)) {
+
+		$result = $test->pass ? "Pass" : "Fail";
+
+		$test_start_dt = new DateTime($test->dts_start);
+		$test_end_dt = new DateTime($test->dts_end);
+		$test_duration_diff = $test_start_dt->diff($test_end_dt, true);
+
+		$test_date = $test_start_dt->format("d/m/Y");
+		$test_start = $test_start_dt->format("H:i:s");
+		$test_end = $test_end_dt->format("H:i:s");
+		$test_duration = $test_duration_diff->h . ":" . $test_duration_diff->i . ":" . $test_duration_diff->s;
+
+		if($tester) {
+			$tester_dob_dt = new DateTime($tester->date_of_birth);
+			$dob_diff = $test_start_dt->diff($tester_dob_dt, true);
+			$age = $dob_diff->y;
+			$gender = $tester->gender;
+			$travel_costs = $test->test_travel_costs;
+		}
+	}
+
+	$output[] = $result;
+	$output[] = $location->name;
+	$output[] = $location->getDisplayAddress();
+	$output[] = $location->region;
+	$output[] = $location->internal_id;
+	$output[] = $location->area_manager;
+	$output[] = $test_date;
+
+	foreach($fields as $field) {
+		$field_name = $field->name;
+		$value = isset($test->$field_name) ? $test->$field_name : ($values[$field->name] ?? "");
+
+		if(!empty($field->displayStrings)) {
+			$index = array_search($value, $field->values);
+			$value = $field->displayStrings[$index];
+		}
+
+		$output[] = $value;
+	}
+
+	fputcsv($handle, $output);
+}
+
+fclose($handle);
+
+?>

+ 1 - 0
routes.php

@@ -64,6 +64,7 @@ $routes = [
 	"/^account\/projects\/$id_pattern$/" => ["controller" => "project", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/$id_pattern\/edit$/" => ["controller" => "project_edit", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-csv$/" => ["controller" => "project_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/export-customer-csv$/" => ["controller" => "project_customer_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 
 	// Tests

+ 1 - 0
views/project.php

@@ -20,6 +20,7 @@ var projectID = <?=$project->id?>;
 				<div class="dropdown-menu dropdown-menu-right" aria-labelledby="actions-button">
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/edit">Edit</a>
 					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-csv">Export CSV</a>
+					<a class="dropdown-item" href="/account/projects/<?=$project->id?>/export-customer-csv">Export Customer CSV</a>
 				</div>
 			</div>
 			<?php