Alan Lam пре 7 година
родитељ
комит
b2135a9158

+ 2 - 1
controllers/account.php

@@ -40,6 +40,7 @@ if($user->level >= User::LEVEL_ADMIN) {
 
 if($user->level >= User::LEVEL_CUSTOMER) {
 	$icons[] = new AccountIcon(__("Reports"), "/account/reports", "chart-pie");
+	$icons[] = new AccountIcon(__("Test Results"), "/account/test-results", "clipboard-list-check");
 }
 
 if($user->isTester() || $user->isAdmin()) {
@@ -104,4 +105,4 @@ View::includeOnce("header", compact("page_title"));
 View::include("account", compact("icons", "actions", "to_do", "referrals"));
 View::includeOnce("footer");
 
-?>
+?>

+ 7 - 5
controllers/test.php

@@ -191,7 +191,11 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 					}
 				}
 
-				$test->pass = ($test->challenge_stage <= $template->pass_stage) ? 1 : 0; // Pass = Challenged during browsing or before
+				$test->pass = 1;
+				if($template->hasPassFail()) {
+					$test->pass = ($test->challenge_stage <= $template->getPassStage()) ? 1 : 0; // Pass = Challenged during browsing or before
+				}
+
 				$test->dts_updated = Util::getDateTime();
 
 				$success = $test->commit();
@@ -245,8 +249,6 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 
 	$directory = $test->getDirectory();
 
-	$has_pdf = file_exists($directory . "/pdf.pdf");
-
 	$submit = http::value("submit");
 	$tester_message = http::value("tester_message", "trim");
 	if($submit) {
@@ -451,7 +453,7 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 	});
 
 	View::includeOnce("header", compact("page_title"));
-	View::include("test_verify_form", compact("test", "has_pdf", "tester", "location", "project", "future_projects", "customer", "template", "fields", "values", "tester_message"));
+	View::include("test_verify_form", compact("test", "tester", "location", "project", "future_projects", "customer", "template", "fields", "values", "tester_message"));
 	View::includeOnce("footer");
 	return;
 }
@@ -464,4 +466,4 @@ else {
 	throw new Exception(__("Invalid request."));
 }
 
-?>
+?>

+ 16 - 4
controllers/test_file.php

@@ -3,10 +3,24 @@
 $test = Test::get($id);
 if(!$test) throw new Exception(__("Test not found."));
 
-if(Core::$user->level < User::LEVEL_ADMIN) {
-	if(Core::$user->id != $test->tester_id) throw new Exception(__("You do not have permission to access this resource."));
+$is_pdf = $name == "pdf";
+
+if($is_pdf) {
+	$project = $test->getProject();
+
+	$customers = Core::$user->getCustomers();
+	$customer_ids = array_column($customers, "id");
+
+	if(!in_array($project->customer_id, $customer_ids)) throw new Exception(__("You do not have permission to access this resource."));
 }
 
+if(!$is_pdf) {
+	if(Core::$user->level < User::LEVEL_ADMIN) {
+		if(Core::$user->id != $test->tester_id) throw new Exception(__("You do not have permission to access this resource."));
+	}
+}
+
+
 $directory = $test->getDirectory();
 $path_jpg = $directory . "/" . $name . ".jpg";
 $path_pdf = $directory . "/" . $name . ".pdf";
@@ -23,5 +37,3 @@ else {
 	header("Content-Type: image/png");
 	readfile("public/img/blank.png");
 }
-
-?>

+ 16 - 0
controllers/test_results_customer_list.php

@@ -0,0 +1,16 @@
+<?php
+
+$customers = Core::$user->getCustomers();
+if(count($customers) == 1) {
+	$customer = $customers[0];
+	http::redirect($customer->getTestResultsUrl());
+}
+
+$page_title = __("Test Results Customer List");
+
+Core::addBreadcrumb("/account", __("Account"));
+Core::addBreadcrumb("/account/test-results", $page_title);
+
+View::includeOnce("header", compact("page_title"));
+View::include("test_results_customer_list", compact("customers"));
+View::includeOnce("footer");

+ 26 - 0
controllers/test_results_project_list.php

@@ -0,0 +1,26 @@
+<?php
+
+$customers = Core::$user->getCustomers();
+if(!$customers) throw new Exception(__("An unexpected error has occurred. Please try again later."));
+
+$customer_ids = array_column($customers, "id");
+if(!in_array($customer_id, $customer_ids)) throw new Exception(__("You do not have permission to access this page."));
+
+$customer = Customer::get($customer_id);
+$projects = $customer->getProjects();
+
+$projects = array_filter($projects, function($project) {
+    return $project->isFinished();
+});
+
+$test_results_url = $customer->getTestResultsUrl();
+
+$page_title = __("Test Results %s Project List", $customer->name);
+
+Core::addBreadcrumb("/account", __("Account"));
+Core::addBreadcrumb("/account/test-results", __("Test Results"));
+Core::addBreadcrumb($test_results_url, $customer->name);
+
+View::includeOnce("header", compact("page_title"));
+View::include("test_results_project_list", compact("projects"));
+View::includeOnce("footer");

+ 47 - 0
controllers/test_results_test_list.php

@@ -0,0 +1,47 @@
+<?php
+
+$customers = Core::$user->getCustomers();
+if(!$customers) throw new Exception(__("An unexpected error has occurred. Please try again later."));
+
+$project = Project::get($project_id);
+if(!$project) throw new Exception(__("Project not found."));
+
+$template = $project->getTemplate();
+$has_pass_fail = $template->hasPassFail();
+
+$customer_ids = array_column($customers, "id");
+if(!in_array($project->customer_id, $customer_ids)) throw new Exception(__("You do not have permission to access this page."));
+
+$customer = $project->getCustomer();
+$tests = $project->getTests();
+
+$location_ids = array_column($tests, "location_id");
+Location::load($location_ids);
+
+usort($tests, function($a, $b) {
+    $a_location = $a->getLocation();
+    $b_location = $b->getLocation();
+
+    return strnatcasecmp($a_location->name, $b_location->name);
+});
+
+$test_count = $project->getTestCount();
+$pass_count = $project->getPassCount();
+$fail_count = $project->getFailCount();
+
+$pass_percentage = number_format($pass_count / $test_count, 4) * 100;
+$fail_percentage = 100 - $pass_percentage;
+
+$project_list_url = $customer->getTestResultsUrl();
+$test_list_url = $project->getTestResultsUrl();
+
+$page_title = __("Test Results %s Test List", $project->name);
+
+Core::addBreadcrumb("/account", __("Account"));
+Core::addBreadcrumb("/account/test-results", __("Test Results"));
+Core::addBreadcrumb($project_list_url, $customer->name);
+Core::addBreadcrumb($test_list_url, $project->name);
+
+View::includeOnce("header", compact("page_title"));
+View::include("test_results_test_list", compact("project", "has_pass_fail", "tests", "test_count", "pass_count", "fail_count", "pass_percentage", "fail_percentage"));
+View::includeOnce("footer");

+ 6 - 0
engine/customer.php

@@ -83,4 +83,10 @@ class Customer extends Model {
 		return Template::query(["customer_id" => $this->id], ["sort" => "name"]);
 	}
 
+	public function getTestResultsUrl(): string {
+		$slug = Util::slugWithId($this->name, $this->id);
+		$url = "/account/test-results/{$slug}";
+		return $url;
+	}
+
 }

+ 1 - 1
engine/model.php

@@ -451,4 +451,4 @@ abstract class Model {
 
 }
 
-?>
+?>

+ 55 - 3
engine/project.php

@@ -30,8 +30,10 @@ class Project extends Model {
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
 
-	protected static $field_map = [
-	];
+	/**
+	 * @var Test[] $tests
+	 */
+	protected $tests = null;
 
 	// Object methods
 	/**
@@ -46,11 +48,50 @@ class Project extends Model {
 	 */
 	public function getTests() {
 
+		if(!is_null($this->tests)) return $this->tests;
+
 		$nvp = [];
 		$nvp["project_id"] = $this->id;
 		$nvp["status"] = (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED);
 
-		return Test::query($nvp);
+		$this->tests = Test::query($nvp);
+
+		return $this->tests;
+	}
+
+	public function getTestCount() {
+		if(is_null($this->tests)) $this->getTests();
+		return count($this->tests);
+	}
+
+	public function getPassCount() {
+		$count = 0;
+		$tests = $this->getTests();
+		foreach($tests as $test) {
+			if(!$test->isPass()) continue;
+			++$count;
+		}
+		return $count;
+	}
+
+	public function getFailCount() {
+		$count = 0;
+		$tests = $this->getTests();
+		foreach($tests as $test) {
+			if(!$test->isFail()) continue;
+			++$count;
+		}
+		return $count;
+	}
+
+	public function getPendingCount() {
+		$count = 0;
+		$tests = $this->getTests();
+		foreach($tests as $test) {
+			if(!$test->isPending()) continue;
+			++$count;
+		}
+		return $count;
 	}
 
 	public function getLocations() {
@@ -148,6 +189,10 @@ class Project extends Model {
 		return time() > $deadline_unix;
 	}
 
+	public function isFinished() {
+		return $this->dts_finished === null;
+	}
+
 	public function markFinished() {
 
 		if(!Core::$user) throw new Exception("Trying to mark project as finished while not logged in.");
@@ -191,4 +236,11 @@ class Project extends Model {
 		];
 	}
 
+	public function getTestResultsUrl(): string {
+		$customer = $this->getCustomer();
+		$customer_slug = Util::slugWithId($customer->name, $customer->id);
+		$project_slug = Util::slugWithId($this->name, $this->id);
+		return "/account/test-results/{$customer_slug}/{$project_slug}";
+	}
+
 }

+ 6 - 4
engine/template.php

@@ -21,8 +21,6 @@ class Template extends Model {
 	// Object methods
 	public function exportTestsCSV($processing_file, $tests = []) {
 
-		$template = $this;
-
 		// Preload
 		Project::load(array_column($tests, "project_id"));
 		Location::load(array_column($tests, "location_id"));
@@ -303,8 +301,12 @@ class Template extends Model {
 		<?php
 	}
 
+	public function getPassStage(): int {
+		return (int) $this->pass_stage;
+	}
 
+	public function hasPassFail() {
+		return !is_null($this->pass_stage);
+	}
 
 }
-
-?>

+ 27 - 0
engine/test.php

@@ -482,10 +482,37 @@ class Test extends Model {
 		return $success;
 	}
 
+	public function isPass(): bool {
+		if(is_null($this->pass)) return false;
+		return !empty($this->pass);
+	}
+
+	public function isPending(): bool {
+		return is_null($this->pass);
+	}
+
+	public function isFail(): bool {
+		if(is_null($this->pass)) return false;
+		return empty($this->pass);
+	}
+
+	public function isCompleted() {
+		return $this->status == Test::STATUS_COMPLETED;
+	}
+
 	public function isPendingVerification() {
 		return $this->status == Test::STATUS_PENDING_VERIFICATION;
 	}
 
+	public function hasPDF() {
+		$directory = $this->getDirectory();
+		return file_exists($directory . "/pdf.pdf");
+	}
+
+	public function getPDFUrl() {
+		return "/account/tests/{$this->id}/files/pdf";
+	}
+
 	public function hasTesterInvoicePDF() {
 		$directory = $this->getDirectory();
 		return file_exists($directory . "/tester_invoice.pdf");

+ 6 - 0
routes.php

@@ -71,6 +71,11 @@ $routes = [
 	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "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],
 
+	// Test Results
+	"/^account\/test-results$/" => ["controller" => "test_results_customer_list"],
+	"/^account\/test-results\/{$slug_id_pattern}$/" => ["controller" => "test_results_project_list", "params" => ["customer_id"]],
+	"/^account\/test-results\/{$slug_id_pattern}\/{$slug_id_pattern}$/" => ["controller" => "test_results_test_list", "params" => ["customer_id", "project_id"]],
+
 	// Tests
 	"/^account\/tests$/" => ["controller" => "tests", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/postal\-codes$/" => ["controller" => "tests_tester_postal_codes", "require_user_level" => User::LEVEL_TESTER],
@@ -79,6 +84,7 @@ $routes = [
 	"/^account\/tests\/completed$/" => ["controller" => "tests_completed", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern$/" => ["controller" => "test", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern\/(selfie)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_TESTER],
+	"/^account\/tests\/$id_pattern\/files\/(pdf)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/tests\/$id_pattern\/files\/([a-z_]+)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/tests\/$id_pattern\/result$/" => ["controller" => "test_result", "params" => ["id"], "require_user_level" => User::LEVEL_VENUE],
 	"/^account\/tests\/$id_pattern\/pdf$/" => ["controller" => "test_pdf", "params" => ["id"], "require_user_level" => User::LEVEL_VENUE],

+ 35 - 0
views/test_results_customer_list.php

@@ -0,0 +1,35 @@
+<div class="container mt-3 py-4">
+
+    <h2><?=htmlspecialchars(__("Test Results"))?></h2>
+
+    <h3><?=htmlspecialchars(__("Select Customer"))?></h3>
+
+    <?php
+
+    if(empty($customers)) {
+        ?>
+        <div class="alert alert-info">
+        <?php
+            echo __("No customers to display.");
+        ?>
+        </div>
+        <?php
+    }
+    else {
+        ?>
+        <div class="list-group">
+        <?php
+        foreach($customers as $customer) {
+            $url = $customer->getTestResultsUrl();
+            ?>
+            <a class="list-group-item list-group-item-action" href="<?=htmlspecialchars($url)?>">
+                <?=htmlspecialchars($customer->name)?>
+            </a>
+            <?php
+        }
+        ?>
+        </div>
+        <?php
+    }
+    ?>
+</div>

+ 42 - 0
views/test_results_project_list.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * @var Project[] $projects
+ */
+?>
+<div class="container mt-3 py-4">
+
+    <h2><?=htmlspecialchars(__("Test Results"))?></h2>
+
+    <h3><?=htmlspecialchars(__("Select Project"))?></h3>
+
+    <?php
+
+    if(empty($projects)) {
+        ?>
+        <div class="alert alert-info">
+        <?php
+            echo __("No projects to display.");
+        ?>
+        </div>
+        <?php
+    }
+    else {
+        ?>
+        <div class="list-group">
+        <?php
+        foreach($projects as $project) {
+
+            $url = $project->getTestResultsUrl();
+
+            ?>
+            <a class="list-group-item list-group-item-action" href="<?=htmlspecialchars($url)?>">
+                <?=htmlspecialchars($project->name)?>
+            </a>
+            <?php
+        }
+        ?>
+        </div>
+        <?php
+    }
+    ?>
+</div>

+ 142 - 0
views/test_results_test_list.php

@@ -0,0 +1,142 @@
+<?php
+/**
+ * @var Test[] $tests
+ */
+?>
+<div class="container mt-3 py-4">
+
+    <h2><?=htmlspecialchars(__("%s Test Results", $project->name))?></h2>
+
+    <?php
+
+    if(empty($tests)) {
+        ?>
+        <div class="alert alert-info">
+        <?php
+            echo __("No tests to display.");
+        ?>
+        </div>
+        <?php
+    }
+    else {
+
+        ?>
+        <div class="row">
+            <div class="col-3">
+                <div class="card mb-3">
+                    <div class="card-body">
+                        Test Count
+                        <div class="stat">
+                            <?=$test_count?>
+                        </div>
+                    </div>
+                </div>
+            </div>
+            <div class="col">
+            <?php
+
+            if($has_pass_fail) {
+                ?>
+                <div class="card mb-3">
+                    <div class="card-body">
+                        Pass Rate
+                        <div class="stat mb-3">
+                            <?=number_format($pass_count)?> Pass / <?=number_format($fail_count)?> Fail (<?=intval($pass_percentage)?>%)
+                        </div>
+                        <div class="progress">
+                            <div class="progress-bar bg-success" role="progressbar" style="width: <?=$pass_percentage?>%"></div>
+                            <div class="progress-bar bg-danger" role="progressbar" style="width: <?=$fail_percentage?>%"></div>
+                        </div>
+                    </div>
+                </div>
+                <?php
+            }
+
+            ?>
+            </div>
+        </div>
+
+        <h3><?=__("Tests")?></h3>
+
+        <table class="table table-bordered table-hover">
+            <thead>
+                <tr>
+                    <th><?=htmlspecialchars(__("Location"))?></th>
+                    <?php
+                    if($has_pass_fail) {
+                        ?>
+                        <th><?=htmlspecialchars(__("Result"))?></th>
+                        <th><?=htmlspecialchars(__("Challenge Stage"))?></th>
+                        <?php
+                    }
+                    ?>
+                    <th><?=htmlspecialchars(__("PDF"))?></th>
+                </tr>
+            </thead>
+            <tbody>
+            <?php
+            foreach($tests as $test) {
+
+                $has_pdf = $test->hasPDF();
+
+                $location = $test->getLocation();
+
+                $pass_text = $test->getPassText();
+                $pass_class = $test->getPassClass();
+                $pass_icon = $test->getPassIcon();
+
+                $challenge_stage = $test->challenge_stage;
+                $challenge_stage_text = $test->getChallengeStageText();
+
+                ?>
+                <tr>
+                    <td>
+                        <?=htmlspecialchars($location->name)?>
+                    </td>
+                    <?php
+                    if($has_pass_fail) {
+                        ?>
+                        <td class="text-center">
+                            <strong class="text-<?=$pass_class?>"><?=htmlspecialchars($pass_text)?></strong>
+                        </td>
+                        <td>
+                        <?php
+                        if($test->isPending()) {
+                            ?>
+                            <?=htmlspecialchars(__("Pending"))?>
+                            <?php
+                        }
+                        else {
+                            ?>
+                            <strong class="text-<?=$pass_class?>"><?=htmlspecialchars(__("Stage %d", $challenge_stage))?></strong> -
+                            <?=htmlspecialchars($challenge_stage_text)?>
+                            <?php
+                        }
+                        ?>
+                        </td>
+                        <?php
+                    }
+                    ?>
+                    <td>
+                    <?php
+                    if($has_pdf) {
+                        $pdf_url = $test->getPDFUrl();
+                        ?>
+                        <a href="<?=htmlspecialchars($pdf_url)?>" target="_blank">View PDF</a>
+                        <?php
+                    }
+                    else {
+                        echo htmlspecialchars(__("No PDF available"));
+                    }
+                    ?>
+                    </td>
+                </tr>
+                <?php
+            }
+            ?>
+            </tbody>
+        </table>
+        <?php
+    }
+    ?>
+</div>

+ 4 - 3
views/test_verify_form.php

@@ -97,9 +97,10 @@ function render_card_list_group($details) {
 					<div class="dropdown-divider"></div>
 					<a class="dropdown-item" href="/account/tests/<?=$test->id?>/pdf" target="_blank">Generate New PDF</a>
 					<?php
-					if($has_pdf) {
+					if($test->hasPDF()) {
+						$pdf_url = $test->getPDFUrl();
 						?>
-						<a class="dropdown-item" href="/account/tests/<?=$test->id?>/files/pdf" target="_blank">View Sent PDF</a>
+						<a class="dropdown-item" href="<?=$pdf_url?>" target="_blank">View Sent PDF</a>
 						<?php
 					}
 					if($test->hasTesterInvoicePDF()) {
@@ -468,4 +469,4 @@ function render_card_list_group($details) {
 			</div>
 		</div>
 	</form>
-</div>
+</div>