Quellcode durchsuchen

Add alcohol and combined stat slides

Alan Lam vor 2 Jahren
Ursprung
Commit
7b13a84761

+ 118 - 56
controllers/reports_presentation.php

@@ -1,9 +1,19 @@
 <?php /** @noinspection PhpUnhandledExceptionInspection */
-/** @noinspection PhpUndefinedMethodInspection */
 
 /**
  * @var string $date_range_label
  * @var string[] $key_labels
+ * @var ?bool $retests
+ * @var Customer $customer
+ * @var Template $template
+ * @var object $template_config
+ * @var bool $has_logo
+ * @var string $logo_file_path
+ * @var array $total_data
+ * @var array $charts
+ * @var array $region_data
+ * @var array $area_manager_data
+ * @var string $action
  */
 
 use PhpOffice\PhpPresentation\PhpPresentation;
@@ -35,7 +45,6 @@ if($retests !== null) {
 $title = sprintf("%s %s %s Tests (%s)", $customer->name, $template->name, date("F Y"), $retests_title);
 
 if($has_logo) {
-
 	$shape = $slide->createDrawingShape();
 	$shape->setName("$customer->name logo")
 		->setDescription("$customer->name logo")
@@ -49,10 +58,6 @@ if($has_logo) {
 		->setWidth(940)
 		->setOffsetX(120)
 		->setOffsetY(120);
-	$run = $shape->createTextRun($title);
-	$run->getFont()->setBold(true)->setSize(16);
-
-
 }
 else {
 	// Title
@@ -62,60 +67,114 @@ else {
 		  ->setOffsetX(10)
 		  ->setOffsetY(315);
 	$shape->getActiveParagraph()->getAlignment()->setHorizontal( Alignment::HORIZONTAL_CENTER );
-	$run = $shape->createTextRun($title);
-	$run->getFont()->setBold(true)
-					   ->setSize(16);
 }
 
-// Create summary slide
-$slide = $presentation->createSlide();
-
-// Title
-$shape = $slide->createRichTextShape()
-	->setHeight(60)->setWidth(900)
-	->setOffsetX(30)->setOffsetY(10);
-$run = $shape->createTextRun("Summary");
-$run->getFont()->setBold(true)->setSize(24);
-
-// Date range
-$shape = $slide->createRichTextShape()
-	->setHeight(60)->setWidth(900)
-	->setOffsetX(30)->setOffsetY(70);
-$run = $shape->createTextRun($date_range_label);
-$run->getFont()->setBold(true)->setSize(24);
-
-// Test Count
-$count_text = "Test Count: {$total_data->testCount}";
-
-// Pass/Fail count
-if(isset($total_data->passCount)) {
-	$pass_percentage = 0;
-	$fail_percentage = 0;
-	if($total_data->testCount > 0) {
-		$pass_percentage = number_format(($total_data->passCount * 100) / $total_data->testCount, 2);
-		$fail_percentage = 100 - $pass_percentage;
+$run = $shape->createTextRun($title);
+$run->getFont()->setBold(true)->setSize(16);
+
+$create_summary_slide = static function(array $options) use ($presentation, $charts, $date_range_label, $total_data) {
+
+	$title = $options["title"] ?? "Summary";
+	$array_key = $options["array_key"] ?? null;
+	$chart_prefix = $options["chart_prefix"] ?? "Totals by";
+
+	if($array_key) {
+		$pass_count = $total_data->$array_key["pass"];
+		$fail_count = $total_data->$array_key["fail"];
+		$test_count = $pass_count + $fail_count;
+
+		$has_stages = false;
 	}
+	else {
+		$test_count = $total_data->testCount;
+		$pass_count = $total_data->passCount;
+		$fail_count = $total_data->failCount;
 
-	$count_text .= "\nPass Count: {$total_data->passCount} ({$pass_percentage}%)";
-	$count_text .= "\nFail Count: {$total_data->failCount}  ({$fail_percentage}%)";
-}
+		$has_stages = true;
+	}
 
-$shape = $slide->createRichTextShape()
-	->setHeight(60)->setWidth(900)
-	->setOffsetX(30)->setOffsetY(130);
-$run = $shape->createTextRun($count_text);
-$run->getFont()->setBold(true)->setSize(16);
+	// Create summary slide
+	$slide = $presentation->createSlide();
+
+	// Title
+	$shape = $slide->createRichTextShape()
+		->setHeight(60)->setWidth(900)
+		->setOffsetX(30)->setOffsetY(10);
+	$run = $shape->createTextRun($title);
+	$run->getFont()->setBold(true)->setSize(24);
 
-// Tests by stage
-foreach($charts as $chart) {
-	if(stripos($chart->getTitle(), "Tests by Stage") === false) continue;
-	$chart->addDataTable($slide, 30, 260);
+	// Date range
+	$shape = $slide->createRichTextShape()
+		->setHeight(60)->setWidth(900)
+		->setOffsetX(30)->setOffsetY(70);
+	$run = $shape->createTextRun($date_range_label);
+	$run->getFont()->setBold(true)->setSize(24);
+
+	// Test Count
+	$count_text = "Test Count: {$test_count}";
+
+	// Pass/Fail count
+	if(isset($pass_count)) {
+		$pass_percentage = 0;
+		$fail_percentage = 0;
+		if($test_count > 0) {
+			$pass_percentage = number_format(($pass_count * 100) / $test_count, 2);
+			$fail_percentage = 100 - $pass_percentage;
+		}
+
+		$count_text .= "\nPass Count: {$pass_count} ({$pass_percentage}%)";
+		$count_text .= "\nFail Count: {$fail_count}  ({$fail_percentage}%)";
+	}
+
+	$shape = $slide->createRichTextShape()
+		->setHeight(60)->setWidth(900)
+		->setOffsetX(30)->setOffsetY(130);
+	$run = $shape->createTextRun($count_text);
+	$run->getFont()->setBold(true)->setSize(16);
+
+	if($has_stages) {
+		// Tests by stage
+
+		/** @var Chart $chart */
+		foreach($charts as $chart) {
+			if(stripos($chart->getTitle(), "Tests by Stage") === false) {
+				continue;
+			}
+			$chart->addDataTable($slide, 30, 260);
+		}
+	}
+
+
+	// Totals by period
+
+	/** @var Chart $chart */
+	foreach($charts as $chart) {
+		if(stripos($chart->getTitle(), $chart_prefix) !== false) {
+			$chart->addDataTable($slide, 30, 360);
+			break;
+		}
+	}
+};
+
+$create_summary_slide([
+	"title" => $template->reports->gamingStatsPage->title ?? null,
+	"chart_prefix" => $template->reports->gamingStatsPage->chartPrefix ?? null
+]);
+
+if(isset($template_config->reports->barStatsPage)) {
+	$create_summary_slide([
+		"title" => $template_config->reports->barStatsPage->title ?? null,
+		"array_key" => "barCounts",
+		"chart_prefix" => $template_config->reports->barStatsPage->chartPrefix ?? null
+	]);
 }
 
-// Totals by month/quarter
-foreach($charts as $chart) {
-	if(stripos($chart->getTitle(), "Totals by") === false) continue;
-	$chart->addDataTable($slide, 30, 360);
+if(isset($template_config->reports->combinedStatsPage)) {
+	$create_summary_slide([
+		"title" => $template_config->reports->combinedStatsPage->title ?? null,
+		"array_key" => "combinedCounts",
+		"chart_prefix" => $template_config->reports->combinedStatsPage->chartPrefix ?? null
+	]);
 }
 
 $names = [];
@@ -123,7 +182,10 @@ $names = [];
 $charts_b64 = http::value("charts_b64");
 foreach($charts_b64 as $index => $b64) {
 
-	$chart = $charts[$index];
+	$chart = $charts[$index] ?? null;
+	if(!$chart) {
+		continue;
+	}
 
 	$slide = $presentation->createSlide();
 
@@ -133,7 +195,7 @@ foreach($charts_b64 as $index => $b64) {
 	$name = tempnam("/tmp", "ppt");
 	$name .= ".png";
 
-	$handle = fopen($name, "w");
+	$handle = fopen($name, "wb");
 	fwrite($handle, base64_decode($png_b64));
 
 	fclose($handle);
@@ -229,14 +291,14 @@ foreach([0, 1] as $mode) {
 	}
 }
 
-if($action == "export-pptx") {
+if($action === "export-pptx") {
 	header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
 	http::setDownloadHeaders("report.pptx");
 	$pptx_writer = IOFactory::createWriter($presentation, "PowerPoint2007");
 	$pptx_writer->save("php://output");
 }
 
-if($action == "export-odp") {
+if($action === "export-odp") {
 	header("Content-Type: application/vnd.oasis.opendocument.presentation");
 	http::setDownloadHeaders("report.odp");
 	$odp_writer = IOFactory::createWriter($presentation, "ODPresentation");

+ 40 - 15
engine/chart_generator.php

@@ -480,7 +480,7 @@ class ChartGenerator {
 			"stacked" => true,
 		]);
 		$chart->setType("bar");
-		$chart->setTitle($title);
+		$chart->setTitle("$title by {$this->resolutionName}");
 		$chart->setLabels($this->xDataLabels);
 		$chart->addDataset("Pass (%)", $pass_rates, ["background_colours" => self::COLOUR_GREEN]);
 		$chart->addDataset("Fail (%)", $fail_rates, ["background_colours" => self::COLOUR_RED]);
@@ -519,12 +519,37 @@ class ChartGenerator {
 		);
 	}
 
-	public function getPeriodicTotalsChart(string $title = "Totals"): Chart {
+	public function getPeriodicTotalsChart(array $options = []): Chart {
+
+		$title = $options["title"] ?? "Totals";
+
+		// Test counts are in "pass" and "fail" keys
+		$array_key = $options["array_key"] ?? null;
+
+		if($array_key) {
+			$test_counts = [];
+			$pass_rates = [];
+			$pass_counts = [];
+			$fail_counts = [];
+
+			foreach($this->periodicData as $index => $period) {
+				$pass_count = $period->$array_key["pass"];
+				$fail_count = $period->$array_key["fail"];
+				$test_count = $pass_count + $fail_count;
+
+				$test_counts[] = $test_count;
+				$pass_rates[] = $test_count > 0 ? round(($pass_count / $test_count) * 100) : 0;
+				$pass_counts[] = $pass_count;
+				$fail_counts[] = $fail_count;
+			}
+		}
+		else {
+			$test_counts = $this->getPeriodicTestCounts();
+			$pass_rates = $this->getPeriodicPassRates();
+			$pass_counts = $this->getPeriodicPassCounts();
+			$fail_counts = $this->getPeriodicFailCounts();
+		}
 
-		$test_counts = $this->getPeriodicTestCounts();
-		$pass_rates = $this->getPeriodicPassRates();
-		$pass_counts = $this->getPeriodicPassCounts();
-		$fail_counts = $this->getPeriodicFailCounts();
 
 		$chart = new Chart();
 		$chart->setType("bar");
@@ -663,9 +688,6 @@ class ChartGenerator {
 		$chart->setType("bar");
 		$chart->setTitle("{$title} by {$this->resolutionName}");
 		$chart->setLabels($this->xDataLabels);
-		$chart->addDataset("Tests", $test_counts, [
-			"background_colours" => self::COLOUR_BLUE
-		]);
 		$chart->addDataset("Pass Rate", $pass_rates, [
 			"type" => "line",
 			"y_axis_id" => "y-axis-2",
@@ -686,6 +708,9 @@ class ChartGenerator {
 			],
 			"point_background_colour" => self::COLOUR_RED,
 		]);
+		$chart->addDataset("Tests", $test_counts, [
+			"background_colours" => self::COLOUR_BLUE
+		]);
 		$chart->setDatalabelSuffixes("", "%");
 		return $chart;
 	}
@@ -1141,16 +1166,16 @@ class ChartGenerator {
 	// Private methods
 
 	private function getPeriodicPassRates(): array {
-		return array_values(array_map(static function($d) {
-			if($d->testCount === 0) return 0;
-			return round(($d->passCount / $d->testCount) * 100);
+		return array_values(array_map(static function($period) {
+			if($period->testCount === 0) return 0;
+			return round(($period->passCount / $period->testCount) * 100);
 		}, $this->periodicData));
 	}
 
 	private function getPeriodicFailRates(): array {
-		return array_values(array_map(static function($d) {
-			if($d->testCount === 0) return 0;
-			$pass_rate = round(($d->passCount / $d->testCount) * 100);
+		return array_values(array_map(static function($period) {
+			if($period->testCount === 0) return 0;
+			$pass_rate = round(($period->passCount / $period->testCount) * 100);
 			return 100 - $pass_rate;
 		}, $this->periodicData));
 	}

+ 13 - 1
json/age_verification_greene_king.json

@@ -9,7 +9,19 @@
 		"excludeCategories": [
 			"Gender",
 			"Eye Contact"
-		]
+		],
+		"gamingStatsPage": {
+			"title": "Gaming",
+			"chartPrefix": "Gaming Totals"
+		},
+		"barStatsPage": {
+			"title": "Alcohol",
+			"chartPrefix": "Alcohol Totals"
+		},
+		"combinedStatsPage": {
+			"title": "Combined",
+			"chartPrefix": "Combined Totals"
+		}
 	},
 	"customerInvoiceFields": [
 		{"name": "play_spent", "label": "Money Spent by Testers"}

+ 18 - 2
template_processing/age_verification_greene_king/reports_build_charts.php

@@ -72,14 +72,30 @@ $periodic_alcohol_age_verification_rate_chart = $generator->getPeriodicPassRateC
 $periodic_alcohol_age_verification_rate_chart->setCategories(["All", "Alcohol"]);
 $charts[] = $periodic_alcohol_age_verification_rate_chart;
 
-$periodic_alcohol_pass_rate_chart = $generator->getPeriodicPassRateChartByKey(__("Alcohol Pass Rate"), "barCounts");
+$periodic_alcohol_pass_rate_chart = $generator->getPeriodicPassRateChartByKey(__("Alcohol Pass / Fail"), "barCounts");
 $periodic_alcohol_pass_rate_chart->setCategories(["All", "Alcohol"]);
 $charts[] = $periodic_alcohol_pass_rate_chart;
 
-$periodic_gaming_totals_chart = $generator->getPeriodicTotalsChart(__("Gaming Totals"));
+$periodic_gaming_totals_chart = $generator->getPeriodicTotalsChart([
+	"title" => __("Gaming Totals")
+]);
 $periodic_gaming_totals_chart->setCategories(["All", "Gaming"]);
 $charts[] = $periodic_gaming_totals_chart;
 
+$periodic_alcohol_totals_chart = $generator->getPeriodicTotalsChart([
+	"title" => __("Alcohol Totals"),
+	"array_key" => "barCounts"
+]);
+$periodic_alcohol_totals_chart->setCategories(["All", "Alcohol"]);
+$charts[] = $periodic_alcohol_totals_chart;
+
+$periodic_combined_totals_chart = $generator->getPeriodicTotalsChart([
+	"title" => __("Combined Totals"),
+	"array_key" => "combinedCounts"
+]);
+$periodic_combined_totals_chart->setCategories(["All", "Gaming", "Alcohol"]);
+$charts[] = $periodic_combined_totals_chart;
+
 foreach($generator->getPointOfChallengeCharts("Gaming %s Point of Challenge", ["On Entry", "While Browsing", "During Play", "After Play", "No Challenge"]) as $gaming_point_of_challenge_chart) {
 	$gaming_point_of_challenge_chart->setCategories(["All", "Gaming"]);
 	$charts[] = $gaming_point_of_challenge_chart;

+ 4 - 0
template_processing/age_verification_greene_king/reports_create_empty_objects.php

@@ -45,6 +45,8 @@ $empty_data = (object) [
 	"barCounts" => ["pass" => 0, "fail" => 0],
 	"barAgeVerificationCounts" => ["pass" => 0, "fail" => 0],
 
+	"combinedCounts" => ["pass" => 0, "fail" => 0],
+
 	"stage1Count" => 0,
 	"stage2Count" => 0,
 	"stage3Count" => 0,
@@ -75,4 +77,6 @@ $total_data = (object) [
 
 	"barCounts" => ["pass" => 0, "fail" => 0],
 	"barAgeVerificationCounts" => ["pass" => 0, "fail" => 0],
+
+	"combinedCounts" => ["pass" => 0, "fail" => 0],
 ];

+ 10 - 3
template_processing/age_verification_greene_king/reports_process_test.php

@@ -21,7 +21,7 @@
  * @param $prefix
  * @return void
  */
-$booleanPassFieldIncrementor = static function($value, $pass, $prefix) use (&$data, $key) {
+$increment_periodic_pass_fail = static function($value, $pass, $prefix) use (&$data, $key) {
 
 	if($pass) {
 		$field = $prefix . "PassCounts";
@@ -66,8 +66,12 @@ else {
 	if($brand) $brand_data[$brand][$key]->failCount++;
 }
 
-$booleanPassFieldIncrementor($eye_contact, $pass, "eyeContact");
-$booleanPassFieldIncrementor($line_of_sight, $pass, "lineOfSight");
+$pass_key = $pass ? "pass" : "fail";
+$data[$key]->combinedCounts[$pass_key]++;
+$total_data->combinedCounts[$pass_key]++;
+
+$increment_periodic_pass_fail($eye_contact, $pass, "eyeContact");
+$increment_periodic_pass_fail($line_of_sight, $pass, "lineOfSight");
 
 if($test->challenger_gender === "M") {
 	$data[$key]->maleChallengerCount++;
@@ -128,4 +132,7 @@ if($test->challenge_stage === 5) {
 
 	$data[$key]->barCounts[$alcohol_key]++;
 	$total_data->barCounts[$alcohol_key]++;
+
+	$data[$key]->combinedCounts[$alcohol_key]++;
+	$total_data->combinedCounts[$alcohol_key]++;
 }