Alan Lam 8 лет назад
Родитель
Сommit
f59defd3f9

+ 1 - 0
controllers/account.php

@@ -22,6 +22,7 @@ if(Core::$user->level >= User::LEVEL_ADMIN) {
 }
 
 if(Core::$user->level >= User::LEVEL_CUSTOMER) {
+	$links["/account/performance"] = "Performance";
 	$links["/account/projects"] = "Projects";
 	$links["/account/locations"] = "Locations";
 	$links["/account/templates"] = "Templates";

+ 320 - 0
controllers/performance.php

@@ -0,0 +1,320 @@
+<?php
+
+Core::addScript("public/js/Chart.bundle.min.js");
+Core::addScript("public/js/app-performance.js");
+
+Core::addBreadcrumb("/account", "Account");
+Core::addBreadcrumb("/account/performance", "Performance");
+
+$customer = Customer::getForAdmin();
+if(!$customer) throw new Exception("You do not have permission to access this page.");
+
+$page_title = "$customer->name Performance";
+
+Core::addBreadcrumb("/account/performance?customer_id=$customer->id", $page_title);
+
+$date_range = http::value("date_range");
+$date_resolution = http::value("date_resolution");
+$project_ids = http::value("project_ids", "int");
+if(!is_array($project_ids)) $project_ids = [];
+
+$all_projects = Project::query([
+	"customer_id" => $customer->id
+]);
+
+$data = [];
+
+$charts = [];
+
+$submit = http::value("submit");
+if($submit) {
+	try {
+		Core::validateCSRFToken($submit);
+
+		$form_exception = new FormException;
+		if($form_exception->hasErrors()) throw $form_exception;
+
+
+		// Get all tests
+		$projects = Project::query([
+			"customer_id" => $customer->id,
+			"id" => $project_ids,
+		]);
+
+		$tests = Test::query([
+			"project_id" => $project_ids,
+			"status" => Test::STATUS_COMPLETED
+		]);
+
+		User::load(array_column($tests, "tester_id"));
+
+		$min_year = date("Y");
+		$max_year = date("Y");
+
+		switch($date_range) {
+
+
+			case "ytd":
+				// Nothing to do
+				break;
+
+			case "unbound":
+				// Work out year range
+				foreach($tests as $test) {
+					$dt = new DateTime($test->dts_start . " UTC");
+					$dt->setTimezone(new DateTimeZone(TIMEZONE));
+
+					$year = $dt->format("Y");
+
+					if($year < $min_year) $min_year = $year;
+					if($year > $max_year) $max_year = $year;
+				}
+				break;
+
+			case "custom":
+				// TODO
+				break;
+		}
+
+		$empty_data = (object) [
+			"label" => "",
+			"testCount" => 0,
+			"passCount" => 0,
+			"failCount" => 0,
+
+			"maleTesterCount" => 0,
+			"femaleTesterCount" => 0,
+			"maleChallengerCount" => 0,
+			"femaleChallengerCount" => 0,
+
+			"maleChallengeMaleCount" => 0,
+			"maleChallengeMalePassCount" => 0,
+
+			"maleChallengeFemaleCount" => 0,
+			"maleChallengeFemalePassCount" => 0,
+
+			"femaleChallengeMaleCount" => 0,
+			"femaleChallengeMalePassCount" => 0,
+
+			"femaleChallengeFemaleCount" => 0,
+			"femaleChallengeFemalePassCount" => 0,
+		];
+
+		$total_data = (object) [
+			"maleTesterCount" => 0,
+			"femaleTesterCount" => 0,
+			"maleChallengerCount" => 0,
+			"femaleChallengerCount" => 0,
+			"passCount" => 0,
+			"failCount" => 0,
+		];
+
+
+		// Initialise date-array based on date range and resolution
+		for($year = $min_year; $year <= $max_year; $year++) {
+			switch($date_resolution) {
+				case "monthly":
+
+					foreach(range(1, 12) as $month) {
+						$month = str_pad($month, 2, "0", STR_PAD_LEFT);
+						$key = "$year-$month";
+						$data[$key] = clone $empty_data;
+						$data[$key]->label = date("M Y", mktime(0, 0, 0, $month, 1, date("Y")));
+					}
+
+					break;
+
+				case "quarterly":
+				default:
+
+					foreach(range(1, 4) as $quarter) {
+						$key = "$year-$quarter";
+						$data[$key] = clone $empty_data;
+						$data[$key]->label = "Q$quarter $year";
+					}
+
+					break;
+			}
+
+		}
+
+		// Increment counts based on test results
+		foreach($tests as $test) {
+
+			$tester = $test->getTester();
+
+			$dt = new DateTime($test->dts_start . " UTC");
+			$dt->setTimezone(new DateTimeZone(TIMEZONE));
+
+			$year = $dt->format("Y");
+			$month = $dt->format("m");
+			$quarter = ceil($month / 3);
+
+			switch($date_resolution) {
+				case "monthly":
+					$key = "$year-$month";
+					break;
+				case "quarterly":
+				default:
+					$key = "$year-$quarter";
+					break;
+			}
+
+			$data[$key]->testCount++;
+
+			if($test->pass) {
+				$data[$key]->passCount++;
+				$total_data->passCount++;
+			}
+			else {
+				$data[$key]->failCount++;
+				$total_data->failCount++;
+			}
+
+			if($tester->gender == "M") {
+				$data[$key]->maleTesterCount++;
+				$total_data->maleTesterCount++;
+			}
+			if($tester->gender == "F") {
+				$data[$key]->femaleTesterCount++;
+				$total_data->femaleTesterCount++;
+			}
+
+			if($test->challenger_gender == "M") {
+				$data[$key]->maleChallengerCount++;
+				$total_data->maleChallengerCount++;
+			}
+			if($test->challenger_gender == "F") {
+				$data[$key]->femaleChallengerCount++;
+				$total_data->femaleChallengerCount++;
+			}
+
+			if($tester->gender == "M") {
+				if($test->challenger_gender == "M") {
+					$data[$key]->maleChallengeMaleCount++;
+					if($test->pass) $data[$key]->maleChallengeMalePassCount++;
+				}
+				if($test->challenger_gender == "F") {
+					$data[$key]->femaleChallengeMaleCount++;
+					if($test->pass) $data[$key]->femaleChallengeMalePassCount++;
+				}
+			}
+
+			if($tester->gender == "F") {
+				if($test->challenger_gender == "M") {
+					$data[$key]->maleChallengeFemaleCount++;
+					if($test->pass) $data[$key]->maleChallengeFemalePassCount++;
+				}
+				if($test->challenger_gender == "F") {
+					$data[$key]->femaleChallengeFemaleCount++;
+					if($test->pass) $data[$key]->femaleChallengeFemalePassCount++;
+				}
+			}
+		}
+
+
+		$x_data_labels = array_values(array_map(function($d) {
+			return $d->label;
+		}, $data));
+
+		// Challenger Gender
+		$chart = new Chart;
+		$chart->setType("pie");
+		$chart->setTitle("Staff Gender");
+		$chart->setLabels(["Male", "Female"]);
+		$chart->addDataset("Counts", [
+			$total_data->maleChallengerCount,
+			$total_data->femaleChallengerCount,
+		], [
+			"background_colours" => [
+				"#2196F3",
+				"#E91E63",
+			]
+		]);
+		$charts[] = $chart;
+
+		// Tester Gender
+		$chart = new Chart;
+		$chart->setType("pie");
+		$chart->setTitle("Tester Gender");
+		$chart->setLabels(["Male", "Female"]);
+		$chart->addDataset("Counts", [
+			$total_data->maleTesterCount,
+			$total_data->femaleTesterCount,
+		], [
+			"background_colours" => [
+				"#2196F3",
+				"#E91E63",
+			]
+		]);
+		$charts[] = $chart;
+
+		// Pass / Fail
+		$chart = new Chart;
+		$chart->setType("pie");
+		$chart->setTitle("Pass / Fail");
+		$chart->setLabels(["Pass", "Fail"]);
+		$chart->addDataset("Counts", [
+			$total_data->passCount,
+			$total_data->failCount,
+		], [
+			"background_colours" => [
+				"#4CAF50",
+				"#F44336",
+			]
+		]);
+		$charts[] = $chart;
+
+		// Tester Genders by Month
+		$male_counts = array_values(array_map(function($d) {
+			return $d->maleTesterCount;
+		}, $data));
+
+		$female_counts = array_values(array_map(function($d) {
+			return $d->femaleTesterCount;
+		}, $data));
+
+		$chart = new Chart;
+		$chart->setType("bar");
+		$chart->setTitle("Tester Gender by Month");
+		$chart->setLabels($x_data_labels);
+		$chart->addDataset("Male", $male_counts, [
+			"background_colours" => "#2196F3"
+		]);
+		$chart->addDataset("Female", $female_counts, [
+			"background_colours" => "#E91E63"
+		]);
+		$charts[] = $chart;
+
+		// Test Count
+		$test_counts = array_values(array_map(function($d) {
+			return $d->testCount;
+		}, $data));
+
+		$chart = new Chart;
+		$chart->setType("bar");
+		$chart->setTitle("Tests by Month");
+		$chart->setLabels($x_data_labels);
+		$chart->addDataset("Tests", $test_counts, [
+			"background_colours" => "#2196F3"
+		]);
+		$charts[] = $chart;
+
+
+
+	}
+	catch(FormException $e) {
+		$e->generateAlerts();
+	}
+	catch(Exception $e) {
+		Core::addAlert("danger", $e->getMessage());
+	}
+
+}
+
+
+View::includeOnce("header", compact("page_title"));
+View::include("performance", compact("customer", "date_range", "date_resolution", "all_projects", "project_ids", "charts", "data", "total_data"));
+View::includeOnce("footer");
+
+?>

+ 80 - 0
engine/chart.php

@@ -0,0 +1,80 @@
+<?php
+
+class Chart {
+
+	private $id = "";
+	private $title = "Untitled";
+	private $type = "bar";
+	private $datasets = [];
+	private $labels = [];
+
+	public function __construct() {
+		$this->id = bin2hex(random_bytes(8));
+	}
+
+	public function getID() {
+		return $this->id;
+	}
+
+	public function getTitle() {
+		return $this->title;
+	}
+
+	public function setTitle($title) {
+		$this->title = $title;
+	}
+
+	public function setType($type) {
+		$this->type = $type;
+	}
+
+	public function addDataset($label, $data, $options = []) {
+
+		$background_colours = $options["background_colours"] ?? false;
+		$border_colours = $options["border_colours"] ?? false;
+		$border_width = $options["border_width"] ?? false;
+
+		$dataset = [
+			"label" => $label,
+			"data" => $data,
+		];
+
+		if($background_colours) $dataset["backgroundColor"] = $background_colours;
+		if($border_colours) $dataset["borderColor"] = $border_colours;
+		if($border_width) $dataset["borderWidth"] = (int) $border_width;
+
+		$this->datasets[] = $dataset;
+
+	}
+
+	public function setLabels($labels) {
+		$this->labels = $labels;
+	}
+
+	public function getJSON() {
+
+		return json_encode([
+			"type" => $this->type,
+			"data" => [
+				"labels" => $this->labels,
+				"datasets" => $this->datasets
+			],
+			"options" => [
+				"title" => [
+					"display" => true,
+					"fontSize" => 32,
+					"fontFamily" => "Roboto",
+					"fontStyle" => "300",
+					"text" => $this->title
+				],
+				"legend" => [
+					"position" => "top",
+				],
+			],
+		]);
+
+	}
+
+}
+
+?>

+ 1 - 0
package.json

@@ -10,6 +10,7 @@
   "license": "ISC",
   "dependencies": {
     "bootstrap": "^4.0.0",
+    "chart.js": "^2.7.2",
     "clipboard": "^2.0.0",
     "holderjs": "^2.9.4",
     "jquery": "^3.3.1",

Разница между файлами не показана из-за своего большого размера
+ 9 - 0
public/js/Chart.bundle.min.js


+ 8 - 0
public/js/app-performance.js

@@ -0,0 +1,8 @@
+$(function() {
+
+	$("canvas[data-chart]").each(function() {
+		var object = $(this).data("chart");
+		var chart = new Chart(this, object);
+	});
+
+});

+ 3 - 0
routes.php

@@ -108,6 +108,9 @@ $routes = [
 	// Submit ID
 	"/^account\/submit-id$/" => ["controller" => "submit_id", "require_user_level" => User::LEVEL_REGISTERED],
 
+	// Performance
+	"/^account\/performance$/" => ["controller" => "performance", "require_user_level" => User::LEVEL_VENUE],
+
 
 ];
 

+ 110 - 0
views/performance.php

@@ -0,0 +1,110 @@
+<?php
+
+$date_resolutions = [
+	"quarterly" => "Quarterly",
+	"monthly" => "Monthly",
+];
+
+$date_ranges = [
+	"ytd" => "Year to Date",
+	"unbound" => "Unbound",
+	"custom" => "Custom",
+];
+
+?>
+<div class="container">
+	<div class="row">
+		<div class="col-auto">
+			<h2><?=htmlspecialchars($customer->name)?> Performance</h2>
+		</div>
+		<div class="col">
+		</div>
+	</div>
+
+	<form class="mb-4" method="post">
+
+		<input type="hidden" name="customer_id" value="<?=htmlspecialchars($customer->id)?>" />
+
+		<div class="row">
+			<div class="col-md-6">
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label">
+						Date Range
+					</label>
+					<div class="col-sm">
+					<?php
+					foreach($date_ranges as $value => $display) {
+						$checked = ($value == $date_range) ? "checked" : "";
+						?>
+						<div class="form-check form-check-inline">
+							<input class="form-check-input" type="radio" name="date_range" id="date-range-<?=htmlspecialchars($value)?>" value="<?=htmlspecialchars($value)?>" <?=$checked?> />
+							<label class="form-check-label" for="date-range-<?=htmlspecialchars($value)?>"><?=htmlspecialchars($display)?></label>
+						</div>
+						<?php
+					}
+					?>
+					</div>
+				</div>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="date-resolution">
+						Date Resolution
+					</label>
+					<div class="col-sm">
+						<select class="form-control" name="date_resolution" id="date-resolution">
+						<?php
+						foreach($date_resolutions as $value => $display) {
+							$selected = ($value == $date_resolution) ? "selected" : "";
+							?>
+							<option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
+							<?php
+						}
+						?>
+						</select>
+					</div>
+				</div>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="project-ids">
+						Projects
+					</label>
+					<div class="col-sm">
+						<select class="form-control" name="project_ids[]" id="project-ids" multiple size="8">
+						<?php
+						foreach($all_projects as $project) {
+							$selected = in_array($project->id, $project_ids) ? "selected" : "";
+							?>
+							<option value="<?=$project->id?>" <?=$selected?>><?=htmlspecialchars($project->name)?></option>
+							<?php
+						}
+						?>
+						</select>
+						<small class="form-text text-muted">Hint: Ctrl+Click / Cmd+Click to select multiple projects.</small>
+					</div>
+				</div>
+				<div class="row">
+					<div class="col-sm offset-sm-4">
+						<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
+							Submit
+						</button>
+					</div>
+				</div>
+			</div>
+		</div>
+	</form>
+
+	<div class="row">
+	<?php
+	foreach($charts as $chart) {
+		?>
+		<div class="col col-md-6 my-4">
+			<canvas id="<?=$chart->getID()?>" data-chart="<?=htmlspecialchars($chart->getJSON())?>" width="400" height="400">
+			</canvas>
+		</div>
+		<?php
+	}
+	?>
+	</div>
+
+	<pre><?=print_r($total_data, true)?></pre>
+	<pre><?=print_r($data, true)?></pre>
+
+</div>

Некоторые файлы не были показаны из-за большого количества измененных файлов