Просмотр исходного кода

Add "Tests by Stage" chart to account page for customers

Alan Lam 5 лет назад
Родитель
Сommit
8c17d43c5e
7 измененных файлов с 89 добавлено и 42 удалено
  1. 28 5
      controllers/account.php
  2. 0 2
      controllers/reports.php
  3. 7 0
      engine/chart.php
  4. 4 1
      engine/core.php
  5. 0 27
      public/js/app-reports.js
  6. 27 1
      public/js/app.js
  7. 23 6
      views/account.php

+ 28 - 5
controllers/account.php

@@ -9,6 +9,7 @@ $pass_count = 0;
 $pass_fail_test_count = 0;
 $completed_tests = null;
 $year_cycle_start_text = null;
+$stage_chart = null;
 
 $pass_rate = null;
 $logo_path = null;
@@ -24,9 +25,7 @@ if($user->isCustomer()) {
 		$logo_path = $customer->getLogoPath();
 
 		$page_title = __("%s Dashboard", $customer->name);
-		$year_cycle = $customer->getYearCycleDates();
-
-		list($start, $end) = $year_cycle;
+		[$start, $end] = $customer->getYearCycleDates();
 
 		$dt = new DateTime($start);
 		$year_cycle_start_text = $dt->format("jS M");
@@ -43,9 +42,33 @@ if($user->isCustomer()) {
 
 		$tests = $test_result_set->filterByDateRange($start, $end);
 
+
 		$test_count = count($tests);
 
-		$pass_fail_tests = array_filter($tests, function(Test $test) {
+		if($test_count > 0) {
+			$total_data = (object) [
+				"stage1Count" => 0,
+				"stage2Count" => 0,
+				"stage3Count" => 0,
+				"stage4Count" => 0,
+				"stage5Count" => 0,
+			];
+
+			foreach($tests as $test) {
+				if($test->challenge_stage <= 0) continue;
+
+				$key = sprintf("stage%dCount", $test->challenge_stage);
+
+				++$total_data->$key;
+			}
+
+			$chart_generator = new ChartGenerator($total_data, []);
+			$chart_generator->setDateRangeLabel(__("YTD"));
+
+			$stage_chart = $chart_generator->getTestsByStageChart();
+		}
+
+		$pass_fail_tests = array_filter($tests, static function(Test $test) {
 			$project = $test->getProject();
 			$template = $project->getTemplate();
 			return $template->hasPassFail();
@@ -205,6 +228,6 @@ if($tests_pending_verification) {
 $referrals = $user->getReferrals();
 
 View::includeOnce("header", compact("page_title"));
-View::include("account", compact("page_title", "icons", "actions", "to_do", "referrals", "customer", "logo_path", "year_cycle_start_text", "pass_rate", "pass_count", "pass_fail_test_count", "test_count"));
+View::include("account", compact("page_title", "icons", "actions", "to_do", "referrals", "customer", "logo_path", "year_cycle_start_text", "pass_rate", "pass_count", "pass_fail_test_count", "test_count", "stage_chart"));
 View::includeOnce("footer");
 

+ 0 - 2
controllers/reports.php

@@ -1,8 +1,6 @@
 <?php /** @noinspection PhpIncludeInspection */
 /** @noinspection PhpUnhandledExceptionInspection */
 
-Core::addScript("public/js/Chart.bundle.min.js");
-Core::addScript("public/js/chartjs-plugin-datalabels.min.js");
 Core::addScript("public/js/app-reports.js");
 
 Core::addBreadcrumb("/account", "Account");

+ 7 - 0
engine/chart.php

@@ -282,6 +282,13 @@ class Chart {
 
 	}
 
+	public function renderCanvasHtml() {
+		?>
+		<canvas id="<?=$this->getID()?>" data-chart="<?=htmlspecialchars($this->getJSON())?>" width="400" height="300">
+		</canvas>
+		<?php
+	}
+
 	public function printDataTable() {
 		?>
 		<table class="mt-3 w-100 table table-sm small table-bordered">

+ 4 - 1
engine/core.php

@@ -24,7 +24,10 @@ class Core {
 
 	public static $cookie_alert_shown = false;
 
-	public static $scripts = [];
+	public static $scripts = [
+		"public/js/Chart.bundle.min.js",
+		"public/js/chartjs-plugin-datalabels.min.js",
+	];
 	public static $stylesheets = [
 		"public/css/bootstrap.min.css",
 		"public/css/daterangepicker.css",

+ 0 - 27
public/js/app-reports.js

@@ -59,33 +59,6 @@ $(function() {
 		dataTable.columns.adjust();
 	});
 
-	$("canvas[data-chart]").each(function() {
-		var object = $(this).data("chart");
-
-		object.options.plugins.datalabels.display = function(context) {
-			var dataset = context.dataset;
-			return dataset.data[context.dataIndex];
-		};
-
-		if(object.datalabelFormatterCallback) {
-			object.options.plugins.datalabels.formatter = window[object.datalabelFormatterCallback];
-		}
-
-		if(object.options.datalabelSuffixes) {
-			object.options.plugins.datalabels.formatter = function(value, context) {
-				let suffix = object.options.datalabelSuffixes[context.datasetIndex];
-
-				if(suffix.length > 2 && parseInt(value) !== 1) {
-					suffix += "s";
-				}
-
-				return value + suffix;
-			}
-		}
-
-		new Chart(this, object);
-	});
-
 	$("input[data-date-range-radio-input]").change(function() {
 		let value = $(this).val();
 		if(value === "custom") {

+ 27 - 1
public/js/app.js

@@ -56,6 +56,32 @@ $(function() {
 		initialiseTooltips();
 	}
 
+	$("canvas[data-chart]").each(function() {
+		var object = $(this).data("chart");
+
+		object.options.plugins.datalabels.display = function(context) {
+			var dataset = context.dataset;
+			return dataset.data[context.dataIndex];
+		};
+
+		if(object.datalabelFormatterCallback) {
+			object.options.plugins.datalabels.formatter = window[object.datalabelFormatterCallback];
+		}
+
+		if(object.options.datalabelSuffixes) {
+			object.options.plugins.datalabels.formatter = function(value, context) {
+				let suffix = object.options.datalabelSuffixes[context.datasetIndex];
+
+				if(suffix.length > 2 && parseInt(value) !== 1) {
+					suffix += "s";
+				}
+
+				return value + suffix;
+			}
+		}
+
+		new Chart(this, object);
+	});
 
 });
 
@@ -161,4 +187,4 @@ function initialiseMaps() {
 
 	});
 
-}
+}

+ 23 - 6
views/account.php

@@ -1,10 +1,22 @@
 <?php
 
 /**
+ * @var string $page_title
+ * @var Customer $customer
  * @var string $logo_path
  * @var AccountIcon[] $icons
+ * @var int $test_count
+ * @var int $pass_fail_test_count
+ * @var int $pass_rate
+ * @var int $pass_count
+ * @var int $year_cycle_start_text
+ * @var array $to_do
+ * @var array $referrals
+ * @var Chart $stage_chart
  */
 
+$user = Core::$user;
+
 ?>
 <div class="container mt-3 py-4">
 
@@ -21,7 +33,7 @@
 
 	<?php
 
-	switch(Core::$user->level) {
+	switch($user->level) {
 		case User::LEVEL_BANNED:
 			?>
 			<div class="alert alert-danger">
@@ -183,13 +195,13 @@
 				<?php
 			}
 
-			if(Core::$user->level != User::LEVEL_DEACTIVATED && !Core::$user->terms_accepted) {
+			if(!$user->terms_accepted && !$user->isDeactivated()) {
 				?>
 				<div class="alert alert-danger">
 					<p><i class="fas fa-info-circle"></i> <strong><?=htmlspecialchars(__("Important"))?></strong></p>
 
 					<?php
-					if(is_null(Core::$user->terms_accepted)) {
+					if(is_null($user->terms_accepted)) {
 						?>
 						<p>
 							<a href="/terms-and-conditions">Terms and Conditions</a> have been published.
@@ -218,7 +230,12 @@
 			?>
 
 			<?php
-			if(Core::$user->isCustomer() == false) {
+			if($user->isCustomer()) {
+				if($stage_chart !== null) {
+					$stage_chart->renderCanvasHtml();
+				}
+			}
+			else {
 				?>
 				<h3><?=htmlspecialchars(__("Useful Documents"))?></h3>
 
@@ -247,7 +264,7 @@
 				<?php
 			}
 
-			if(Core::$user->isTester()) {
+			if($user->isTester()) {
 			    ?>
                 <h3><?=htmlspecialchars(__("Referrals"))?></h3>
 
@@ -266,7 +283,7 @@
 					</span>
 
                         <span>
-						<a href="#" data-clipboard-text="<?=PROTOCOL?>://<?=DOMAIN?>/register?r=<?=Core::$user->id?>" data-toggle="tooltip" data-title="<?=htmlspecialchars(__("Copy to clipboard"))?>">
+						<a href="#" data-clipboard-text="<?=PROTOCOL?>://<?=DOMAIN?>/register?r=<?=$user->id?>" data-toggle="tooltip" data-title="<?=htmlspecialchars(__("Copy to clipboard"))?>">
 							<i class="fal fa-fw fa-clipboard"></i>
 							<?=htmlspecialchars(__("Copy to clipboard"))?>
 						</a>