Quellcode durchsuchen

Merge branch 'develop'

Alan Lam vor 8 Jahren
Ursprung
Commit
2c595ecaa6

+ 16 - 13
controllers/account.php

@@ -2,6 +2,8 @@
 
 
 $page_title = "Account";
 $page_title = "Account";
 
 
+$user = Core::$user;
+
 Core::addBreadcrumb("/account", $page_title);
 Core::addBreadcrumb("/account", $page_title);
 
 
 $users_pending_verification = 0;
 $users_pending_verification = 0;
@@ -10,28 +12,29 @@ $tests_remaining = 0;
 
 
 $icons = [];
 $icons = [];
 
 
-if(Core::$user->level >= User::LEVEL_ADMIN) {
+if($user->level >= User::LEVEL_ADMIN) {
 
 
 	$icons[] = new AccountIcon("Customers", "/account/customers", "warehouse");
 	$icons[] = new AccountIcon("Customers", "/account/customers", "warehouse");
 	$icons[] = new AccountIcon("Map Overview", "/account/map-overview", "map-marked");
 	$icons[] = new AccountIcon("Map Overview", "/account/map-overview", "map-marked");
-	$icons[] = new AccountIcon((Core::$user->level == User::LEVEL_SUPERADMIN ? "Users" : "Testers"), "/account/users", "users");
+	$icons[] = new AccountIcon(($user->level == User::LEVEL_SUPERADMIN ? "Users" : "Testers"), "/account/users", "users");
 
 
 	$users = User::query(["level" => User::LEVEL_UNVERIFIED]);
 	$users = User::query(["level" => User::LEVEL_UNVERIFIED]);
 	$tests = Test::query(["status" => Test::STATUS_PENDING_VERIFICATION]);
 	$tests = Test::query(["status" => Test::STATUS_PENDING_VERIFICATION]);
 
 
 	$users_pending_verification = count($users);
 	$users_pending_verification = count($users);
 	$tests_pending_verification = count($tests);
 	$tests_pending_verification = count($tests);
-}
 
 
-if(Core::$user->level >= User::LEVEL_CUSTOMER) {
-	$icons[] = new AccountIcon("Reports", "/account/reports", "chart-pie");
 	$icons[] = new AccountIcon("Projects", "/account/projects", "list");
 	$icons[] = new AccountIcon("Projects", "/account/projects", "list");
 	$icons[] = new AccountIcon("Locations", "/account/locations", "map-marker");
 	$icons[] = new AccountIcon("Locations", "/account/locations", "map-marker");
 	$icons[] = new AccountIcon("Templates", "/account/templates", "file-alt");
 	$icons[] = new AccountIcon("Templates", "/account/templates", "file-alt");
 }
 }
 
 
-if(Core::$user->level >= User::LEVEL_TESTER) {
-	$tests = Core::$user->getPendingTests();
+if($user->level >= User::LEVEL_CUSTOMER) {
+	$icons[] = new AccountIcon("Reports", "/account/reports", "chart-pie");
+}
+
+if($user->isTester() || $user->isAdmin()) {
+	$tests = $user->getPendingTests();
 	$tests_remaining = count($tests);
 	$tests_remaining = count($tests);
 
 
 	$tests_icon = new AccountIcon("Tests", "/account/tests", "clipboard-check");
 	$tests_icon = new AccountIcon("Tests", "/account/tests", "clipboard-check");
@@ -41,16 +44,16 @@ if(Core::$user->level >= User::LEVEL_TESTER) {
 	$icons[] = new AccountIcon("Practice", "/account/practice", "chalkboard-teacher");
 	$icons[] = new AccountIcon("Practice", "/account/practice", "chalkboard-teacher");
 }
 }
 
 
-if(Core::$user->level == User::LEVEL_TESTER) {
+if($user->level == User::LEVEL_TESTER) {
 	$icons[] = new AccountIcon("Addresses", "/account/addresses", "address-book");
 	$icons[] = new AccountIcon("Addresses", "/account/addresses", "address-book");
 	$icons[] = new AccountIcon("Tests (Completed)", "/account/tests/completed", "check");
 	$icons[] = new AccountIcon("Tests (Completed)", "/account/tests/completed", "check");
 }
 }
 
 
-if(Core::$user->level == User::LEVEL_REGISTERED) {
+if($user->level == User::LEVEL_REGISTERED) {
 	$icons[] = new AccountIcon("Submit ID", "/account/submit-id", "id-card");
 	$icons[] = new AccountIcon("Submit ID", "/account/submit-id", "id-card");
 }
 }
 
 
-if(!Core::$user->terms_accepted) {
+if(!$user->terms_accepted) {
 	$icons = [];
 	$icons = [];
 }
 }
 
 
@@ -62,10 +65,10 @@ usort($icons, function($a, $b) {
 
 
 // Actions
 // Actions
 $actions = [];
 $actions = [];
-if(Core::$user->level == User::LEVEL_DEACTIVATED) {
+if($user->level == User::LEVEL_DEACTIVATED) {
 	$actions[] = new AccountIcon("Reactivate Account", "/account/reactivate", "user-check");
 	$actions[] = new AccountIcon("Reactivate Account", "/account/reactivate", "user-check");
 }
 }
-else if(Core::$user->level == User::LEVEL_TESTER) {
+else if($user->level == User::LEVEL_TESTER) {
 	$actions[] = new AccountIcon("Deactivate Account...", "/account/deactivate", "user-times");
 	$actions[] = new AccountIcon("Deactivate Account...", "/account/deactivate", "user-times");
 }
 }
 
 
@@ -85,7 +88,7 @@ if($tests_pending_verification) {
 	$to_do[] = $icon;
 	$to_do[] = $icon;
 }
 }
 
 
-$referrals = Core::$user->getReferrals();
+$referrals = $user->getReferrals();
 
 
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
 View::include("account", compact("icons", "actions", "to_do", "referrals"));
 View::include("account", compact("icons", "actions", "to_do", "referrals"));

+ 55 - 547
controllers/reports.php

@@ -29,6 +29,13 @@ $all_projects = Project::query([
 	"customer_id" => $customer->id
 	"customer_id" => $customer->id
 ]);
 ]);
 
 
+if(Core::$user->isCustomer()) {
+	// Filter to only projects that have past the deadline
+	$all_projects = array_filter($all_projects, function($project) {
+		return $project->isPastDeadline();
+	});
+}
+
 $data = [];
 $data = [];
 $region_data = [];
 $region_data = [];
 $area_manager_data = [];
 $area_manager_data = [];
@@ -67,24 +74,56 @@ if($submit) {
 		Core::validateCSRFToken($submit);
 		Core::validateCSRFToken($submit);
 
 
 		$form_exception = new FormException;
 		$form_exception = new FormException;
-		if($form_exception->hasErrors()) throw $form_exception;
 
 
-		// Get all tests
+		// Get all projects
 		$projects = Project::query([
 		$projects = Project::query([
 			"customer_id" => $customer->id,
 			"customer_id" => $customer->id,
 			"id" => $project_ids
 			"id" => $project_ids
 		]);
 		]);
 
 
+		// Enforce unique template ID in selection
+		$template_ids = array_unique(array_column($projects, "template_id"));
+		if(count($template_ids) > 1) $form_exception->addError("Projects with different templates have been selected.");
+
+		// Check template exists
+		$template_id = array_shift($template_ids);
+		$template = Template::get($template_id);
+		if(!$template) $form_exception->addError("Template cannot be found.");
+
+		if($form_exception->hasErrors()) throw $form_exception;
+
+		// Get all completed tests
 		$tests = Test::query([
 		$tests = Test::query([
 			"project_id" => $project_ids,
 			"project_id" => $project_ids,
 			"status" => Test::STATUS_COMPLETED
 			"status" => Test::STATUS_COMPLETED
 		]);
 		]);
 		$test_count = count($tests);
 		$test_count = count($tests);
 
 
+		// Get test values
+		$test_ids = array_column($tests, "id");
+		$test_values = TestValue::query([
+			"test_id" => $test_ids,
+		]);
+
+		$test_values_map = [];
+		foreach($test_values as $test_value) {
+			$test_id = $test_value->test_id;
+			$name = $test_value->name;
+
+			if(!isset($test_values_map[$test_id])) {
+				$test_values_map[$test_id] = new stdClass;
+			}
+
+			$test_values_map[$test_id]->{$name} = $test_value->value;
+		}
+
+		// Load users
 		User::load(array_column($tests, "tester_id"));
 		User::load(array_column($tests, "tester_id"));
-		$locations = Location::load(array_column($tests, "location_id"));
 
 
+		// Load locations
+		$locations = Location::load(array_column($tests, "location_id"));
 
 
+		// Get region and area manager IDs
 		$regions = array_filter(array_column($locations, "region"));
 		$regions = array_filter(array_column($locations, "region"));
 		$area_managers = array_filter(array_column($locations, "area_manager"));
 		$area_managers = array_filter(array_column($locations, "area_manager"));
 
 
@@ -107,6 +146,7 @@ if($submit) {
 			if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
 			if($has_area_managers && !$location->area_manager) $locations_missing_area_manager[] = $location;
 		}
 		}
 
 
+		// Calculate date ranges
 		$min_year = date("Y");
 		$min_year = date("Y");
 		$max_year = date("Y");
 		$max_year = date("Y");
 
 
@@ -167,6 +207,7 @@ if($submit) {
 				break;
 				break;
 		}
 		}
 
 
+		// Check if date range is same year
 		$same_year = ($min_year == $max_year);
 		$same_year = ($min_year == $max_year);
 
 
 		// Filter tests based on date range
 		// Filter tests based on date range
@@ -189,59 +230,10 @@ if($submit) {
 			return true;
 			return true;
 		});
 		});
 
 
+		// Empty data objects hook
+		require $template->getProcessingFilePath("reports_create_empty_objects.php");
 
 
-		$empty_data = (object) [
-			"label" => "",
-			"testCount" => 0,
-			"passCount" => 0,
-			"failCount" => 0,
-
-			"maleTesterCount" => 0,
-			"femaleTesterCount" => 0,
-
-			"maleChallengerCount" => 0,
-			"femaleChallengerCount" => 0,
-			"unspecifiedChallengerCount" => 0,
-
-			"maleChallengeMaleCount" => 0,
-			"maleChallengeMalePassCount" => 0,
-
-			"maleChallengeFemaleCount" => 0,
-			"maleChallengeFemalePassCount" => 0,
-
-			"femaleChallengeMaleCount" => 0,
-			"femaleChallengeMalePassCount" => 0,
-
-			"femaleChallengeFemaleCount" => 0,
-			"femaleChallengeFemalePassCount" => 0,
-
-			"stage1Count" => 0,
-			"stage2Count" => 0,
-			"stage3Count" => 0,
-			"stage4Count" => 0,
-			"stage5Count" => 0,
-		];
-
-		$total_data = (object) [
-			"maleTesterCount" => 0,
-			"femaleTesterCount" => 0,
-
-			"maleChallengerCount" => 0,
-			"femaleChallengerCount" => 0,
-			"unspecifiedChallengerCount" => 0,
-
-			"testCount" => 0,
-			"passCount" => 0,
-			"failCount" => 0,
-
-			"stage1Count" => 0,
-			"stage2Count" => 0,
-			"stage3Count" => 0,
-			"stage4Count" => 0,
-			"stage5Count" => 0,
-		];
-
-		$resolution_name = "";
+		$has_pass_fail = isset($empty_data->passCount);
 
 
 		// Initialise date-array based on date range and resolution
 		// Initialise date-array based on date range and resolution
 		$keys = [];
 		$keys = [];
@@ -313,13 +305,13 @@ if($submit) {
 
 
 		$max_unix = 0;
 		$max_unix = 0;
 
 
-		// Increment counts based on test results
+		// Process test results
 		foreach($tests as $test) {
 		foreach($tests as $test) {
 
 
 			$tester = $test->getTester();
 			$tester = $test->getTester();
 			$location = $test->getLocation();
 			$location = $test->getLocation();
-
 			$project = $test->getProject();
 			$project = $test->getProject();
+			$test_values = $test_values_map[$test->id] ?? new stdClass;
 
 
 			$region = $location->region;
 			$region = $location->region;
 			$area_manager = $location->area_manager;
 			$area_manager = $location->area_manager;
@@ -353,23 +345,6 @@ if($submit) {
 			if($region) $region_data[$region][$key]->testCount++;
 			if($region) $region_data[$region][$key]->testCount++;
 			if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
 			if($area_manager) $area_manager_data[$area_manager][$key]->testCount++;
 
 
-			$stage_key = "stage" . $test->challenge_stage . "Count";
-			$data[$key]->$stage_key++;
-			$total_data->{$stage_key}++;
-
-			if($test->pass) {
-				$data[$key]->passCount++;
-				$total_data->passCount++;
-				if($region) $region_data[$region][$key]->passCount++;
-				if($area_manager) $area_manager_data[$area_manager][$key]->passCount++;
-			}
-			else {
-				$data[$key]->failCount++;
-				$total_data->failCount++;
-				if($region) $region_data[$region][$key]->failCount++;
-				if($area_manager) $area_manager_data[$area_manager][$key]->failCount++;
-			}
-
 			if($tester->gender == "M") {
 			if($tester->gender == "M") {
 				$data[$key]->maleTesterCount++;
 				$data[$key]->maleTesterCount++;
 				$total_data->maleTesterCount++;
 				$total_data->maleTesterCount++;
@@ -379,41 +354,8 @@ if($submit) {
 				$total_data->femaleTesterCount++;
 				$total_data->femaleTesterCount++;
 			}
 			}
 
 
-			if($test->challenger_gender == "M") {
-				$data[$key]->maleChallengerCount++;
-				$total_data->maleChallengerCount++;
-			}
-			else if($test->challenger_gender == "F") {
-				$data[$key]->femaleChallengerCount++;
-				$total_data->femaleChallengerCount++;
-			}
-			else {
-				$data[$key]->unspecifiedChallengerCount++;
-				$total_data->unspecifiedChallengerCount++;
-			}
-
-
-			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++;
-				}
-			}
+			// Process test hook
+			require $template->getProcessingFilePath("reports_process_test.php");
 		}
 		}
 
 
 		$current_month = date("m", $max_unix);
 		$current_month = date("m", $max_unix);
@@ -423,437 +365,8 @@ if($submit) {
 			return $d->label;
 			return $d->label;
 		}, $data));
 		}, $data));
 
 
-		// Current Period
-		if($date_resolution == "monthly") {
-			$key = date("Y") . "-" . $current_month;
-			$current_period_name = date("M Y", $max_unix);
-		}
-		if($date_resolution == "quarterly") {
-			$key = date("Y") . "-" . $current_quarter;
-			$current_period_name = "Q" . $current_quarter . " " . date("Y", $max_unix);
-		}
-
-		// Tests by Stage
-
-		$chart = new Chart;
-		$chart->setType("doughnut");
-		$chart->setTitle("Tests by Stage $date_range_label");
-		$chart->setLabels(["1 - On Entry", "2 - While Browsing", "3 - During Play", "4 - On Exit", "5 - No Challenge"]);
-		$chart->addDataset("Tests", [
-			$total_data->stage1Count,
-			$total_data->stage2Count,
-			$total_data->stage3Count,
-			$total_data->stage4Count,
-			$total_data->stage5Count,
-		], [
-			"background_colours" => [
-				"#4CAF50",
-				"#FFEB3B",
-				"#FBC02D",
-				"#FFAB91",
-				"#F44336",
-			],
-			"datalabels" => Chart::DATALABELS_BOX,
-		]);
-		$chart->setPrintSuffixes("");
-		$chart->print = true;
-		$chart->printPercentages = true;
-		$chart->setPrintLabels(["Stage 1", "Stage 2", "Stage 3", "Stage 4", "Stage 5"]);
-
-		$charts[] = $chart;
-
-
-		// Challenger Gender
-		$chart = new Chart;
-		$chart->setType("pie");
-		$chart->setTitle("Staff Gender $date_range_label");
-		$chart->setLabels(["Male", "Female", "Unspecified"]);
-		$chart->addDataset("Counts", [
-			$total_data->maleChallengerCount,
-			$total_data->femaleChallengerCount,
-			$total_data->unspecifiedChallengerCount,
-		], [
-			"background_colours" => [
-				"#2196F3",
-				"#E91E63",
-				"#9E9E9E",
-			],
-			"datalabels" => Chart::DATALABELS_BOX,
-		]);
-		$charts[] = $chart;
-
-		// Tester Gender
-		$chart = new Chart;
-		$chart->setType("pie");
-		$chart->setTitle("Tester Gender $date_range_label");
-		$chart->setLabels(["Male", "Female"]);
-		$chart->addDataset("Counts", [
-			$total_data->maleTesterCount,
-			$total_data->femaleTesterCount,
-		], [
-			"background_colours" => [
-				"#2196F3",
-				"#E91E63",
-			],
-			"datalabels" => Chart::DATALABELS_BOX,
-		]);
-		$charts[] = $chart;
-
-		// Pass / Fail
-		$chart = new Chart;
-		$chart->setType("pie");
-		$chart->setTitle("Pass / Fail $date_range_label");
-		$chart->setLabels(["Pass", "Fail"]);
-		$chart->addDataset("Counts", [
-			$total_data->passCount,
-			$total_data->failCount,
-		], [
-			"background_colours" => [
-				"#4CAF50",
-				"#F44336",
-			],
-			"datalabels" => Chart::DATALABELS_BOX,
-		]);
-		$charts[] = $chart;
-
-		// Pass Rate Period
-		$pass_rates = array_values(array_map(function($d) {
-			if($d->testCount == 0) return 0;
-			return round(($d->passCount / $d->testCount) * 100);
-		}, $data));
-
-		$fail_rates = array_values(array_map(function($d) {
-			if($d->testCount == 0) return 0;
-			$pass_rate = round(($d->passCount / $d->testCount) * 100);
-			return 100 - $pass_rate;
-		}, $data));
-
-		$chart = new Chart;
-		$chart->addXAxis([
-			"type" => "category",
-			"stacked" => true,
-		]);
-		$chart->addYAxis([
-			"id" => "y-axis-1",
-			"tick_config" => ["min" => 0, "max" => 100],
-			"stacked" => true,
-		]);
-
-		$chart->setType("bar");
-		$chart->setTitle("Pass / Fail by $resolution_name");
-		$chart->setLabels($x_data_labels);
-		$chart->addDataset("Pass Rate (%)", $pass_rates, [
-			"background_colours" => "#4CAF50"
-		]);
-		$chart->addDataset("Fail Rate (%)", $fail_rates, [
-			"background_colours" => "#F44336"
-		]);
-		$chart->setDatalabelSuffixes("%", "%");
-		$charts[] = $chart;
-
-		// Totals by Period
-
-		$test_counts = array_column($data, "testCount");
-		$pass_counts = array_column($data, "passCount");
-		$fail_counts = array_column($data, "failCount");
-
-		$chart = new Chart;
-		$chart->setType("bar");
-		$chart->setTitle("Totals by $resolution_name");
-		$chart->setLabels($x_data_labels);
-		$chart->addYAxis([
-			"id" => "y-axis-1",
-			"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
-		]);
-		$chart->addYAxis([
-			"position" => "right",
-			"id" => "y-axis-2",
-			"grid_lines" => false,
-			"tick_config" => ["min" => 0, "max" => 100]
-		]);
-
-		$chart->addDataset("Pass Rate (%)", $pass_rates, [
-			"border_colours" => "#FFEB3B",
-			"background_colours" => "transparent",
-			"type" => "line",
-			"y_axis_id" => "y-axis-2",
-			"datalabels" => [
-				"display" => false
-			],
-			"line_tension" => 0,
-		]);
-		$chart->addDataset("Test Count", $test_counts, [
-			"background_colours" => "#2196F3",
-			"type" => "bar",
-			"y_axis_id" => "y-axis-1",
-			"datalabels" => [
-				"display" => false
-			],
-		]);
-		$chart->addDataset("Pass", $pass_counts, [
-			"background_colours" => "#4CAF50",
-			"type" => "bar",
-			"y_axis_id" => "y-axis-1",
-			"datalabels" => [
-				"display" => false
-			],
-		]);
-		$chart->addDataset("Fail", $fail_counts, [
-			"background_colours" => "#F44336",
-			"type" => "bar",
-			"y_axis_id" => "y-axis-1",
-			"datalabels" => [
-				"display" => false
-			],
-		]);
-		$chart->setDatalabelSuffixes("%", "", "", "");
-		$chart->print = true;
-		$charts[] = $chart;
-
-		// Point of Challenge (Each Period)
-
-		foreach($keys as $index => $key) {
-			$key_label = $key_labels[$index];
-
-			$period_data = $data[$key];
-
-			if($period_data->testCount == 0) continue;
-
-			$chart = new Chart;
-			$chart->setType("pie");
-			$chart->setTitle("$key_label Point of Challenge");
-			$chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
-			$chart->addDataset("Counts", [
-				$period_data->stage1Count,
-				$period_data->stage2Count,
-				$period_data->stage3Count,
-				$period_data->stage4Count,
-				$period_data->stage5Count,
-			], [
-				"background_colours" => [
-					"#4CAF50",
-					"#FFEB3B",
-					"#FBC02D",
-					"#FFAB91",
-					"#F44336",
-				],
-				"datalabels" => Chart::DATALABELS_BOX,
-			]);
-			$charts[] = $chart;
-		}
-
-		// Tester Genders by Period
-		$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->addYAxis([
-			"id" => "y-axis-1",
-			"tick_config" => ["min" => 0, "max" => ceil(max(max($male_counts), max($female_counts)) / 10) * 10]
-		]);
-
-		$chart->setType("bar");
-		$chart->setTitle("Tester Gender by $resolution_name");
-		$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 by Period
-		$test_counts = array_values(array_map(function($d) {
-			return $d->testCount;
-		}, $data));
-
-		$chart = new Chart;
-		$chart->addYAxis([
-			"id" => "y-axis-1",
-			"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
-		]);
-		$chart->setType("bar");
-		$chart->setTitle("Tests by $resolution_name");
-		$chart->setLabels($x_data_labels);
-		$chart->addDataset("Tests", $test_counts, [
-			"background_colours" => "#2196F3"
-		]);
-		$charts[] = $chart;
-
-		$pass_counts = array_column($data, "passCount");
-		$fail_counts = array_column($data, "failCount");
-
-		if($has_regions) {
-
-			ksort($region_data, SORT_NATURAL);
-
-			$region_keys = array_keys($region_data);
-
-			$x_data_labels = array_map(function($key) {
-				return "Region $key";
-			}, $region_keys);
-
-			// Region Pass Rate (Selected Date Range)
-			$data_values = [];
-
-			$test_counts = [];
-			$pass_counts = [];
-			$fail_counts = [];
-
-			foreach($region_data as $region => $r_data) {
-				$total_region_test_count = array_sum(array_column($r_data, "testCount"));
-				$total_region_pass_count = array_sum(array_column($r_data, "passCount"));
-
-				$test_counts[] = $total_region_test_count;
-				$pass_counts[] = $total_region_pass_count;
-				$fail_counts[] = $total_region_test_count - $total_region_pass_count;
-
-				$data_values[] = ($total_region_test_count == 0) ? 0 : round(($total_region_pass_count * 100) / $total_region_test_count);
-			}
-
-			$chart = new Chart;
-			$chart->addYAxis([
-				"id" => "y-axis-1",
-				"tick_config" => ["min" => 0, "max" => 100]
-			]);
-			$chart->setType("bar");
-			$chart->setTitle("Region Pass Rate $date_range_label");
-			$chart->setLabels($x_data_labels);
-			$chart->addDataset("Pass %", $data_values , [
-				"background_colours" => "#2196F3"
-			]);
-			$chart->addPrintDataset("Tests", $test_counts);
-			$chart->addPrintDataset("Pass", $pass_counts);
-			$chart->addPrintDataset("Fail", $fail_counts);
-
-			$chart->setDatalabelSuffixes("%");
-			$chart->print = true;
-			$chart->printTotalColumn = true;
-			$charts[] = $chart;
-
-			// Region Pass Rate Period
-			$chart = new Chart;
-			$chart->addYAxis([
-				"id" => "y-axis-1",
-				"tick_config" => ["min" => 0, "max" => 100]
-			]);
-			$chart->setType("bar");
-			$chart->setTitle("Region Pass Rate by $resolution_name");
-			$chart->setLabels($x_data_labels);
-
-			foreach($keys as $index => $key) {
-				$data_values = [];
-				foreach($region_keys as $region) {
-					$r_data = $region_data[$region][$key];
-					$data_values[] = ($r_data->testCount == 0) ? 0 : round(($r_data->passCount * 100) / $r_data->testCount);
-				}
-				$chart->addDataset($key_labels[$index], $data_values, ["background_colours" => $colours[$index]]);
-			}
-			$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
-
-			$charts[] = $chart;
-
-		}
-
-		if($has_area_managers) {
-
-			ksort($area_manager_data, SORT_NATURAL);
-
-			$area_manager_keys = array_keys($area_manager_data);
-
-			$x_data_labels = array_map(function($key) {
-				return "$key";
-			}, $area_manager_keys);
-
-			// Area Pass Rate (Selected Date Range)
-			$data_values = [];
-
-			$test_counts = [];
-			$pass_counts = [];
-			$fail_counts = [];
-
-			foreach($area_manager_data as $area_manager => $a_data) {
-				$total_area_manager_test_count = array_sum(array_column($a_data, "testCount"));
-				$total_area_manager_pass_count = array_sum(array_column($a_data, "passCount"));
-				$total_area_manager_fail_count = array_sum(array_column($a_data, "failCount"));
-
-				$test_counts[] = $total_area_manager_test_count;
-				$pass_counts[] = $total_area_manager_pass_count;
-				$fail_counts[] = $total_area_manager_fail_count;
-
-				$data_values[] = ($total_region_test_count == 0) ? 0 : round(($total_area_manager_pass_count * 100) / $total_area_manager_test_count);
-			}
-
-			$chart = new Chart;
-			$chart->addXAxis([
-				"type" => "category",
-				"scale_label_config" => [
-					"display" => true,
-					"labelString" => "Area Manager",
-				]
-			]);
-			$chart->addYAxis([
-				"id" => "y-axis-1",
-				"tick_config" => ["min" => 0, "max" => 100]
-			]);
-			$chart->setType("bar");
-			$chart->setTitle("Area Manager Pass Rate $date_range_label");
-			$chart->setLabels($x_data_labels);
-			$chart->addDataset("Pass %", $data_values , [
-				"background_colours" => "#2196F3"
-			]);
-			$chart->addPrintDataset("Tests", $test_counts);
-			$chart->addPrintDataset("Pass", $pass_counts);
-			$chart->addPrintDataset("Fail", $fail_counts);
-			$chart->setDatalabelSuffixes("%");
-			$chart->print = true;
-			$chart->printTotalColumn = true;
-			$charts[] = $chart;
-
-			// Area Pass Rate Period
-			$chart = new Chart;
-			$chart->addXAxis([
-				"type" => "category",
-				"scale_label_config" => [
-					"display" => true,
-					"labelString" => "Area Manager",
-				]
-			]);
-			$chart->addYAxis([
-				"id" => "y-axis-1",
-				"tick_config" => ["min" => 0, "max" => 100]
-			]);
-			$chart->setType("bar");
-			$chart->setTitle("Area Manager Pass Rate by $resolution_name");
-			$chart->setLabels($x_data_labels);
-
-			foreach($keys as $index => $key) {
-				$data_values = [];
-				foreach($area_manager_keys as $area_manager) {
-					$a_data = $area_manager_data[$area_manager][$key];
-					$data_values[] = ($a_data->testCount == 0) ? 0 : round(($a_data->passCount * 100) / $a_data->testCount);
-				}
-				$chart->addDataset($key_labels[$index], $data_values, [
-					"background_colours" => $colours[$index],
-					"datalabels" => [
-						"display" => false,
-					],
-				]);
-			}
-			$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
-
-			$charts[] = $chart;
-
-		}
-
-
-
+		// Build charts hook
+		require $template->getProcessingFilePath("reports_build_charts.php");
 	}
 	}
 	catch(FormException $e) {
 	catch(FormException $e) {
 		$e->generateAlerts();
 		$e->generateAlerts();
@@ -862,26 +375,21 @@ if($submit) {
 		Core::addAlert("danger", $e->getMessage());
 		Core::addAlert("danger", $e->getMessage());
 	}
 	}
 
 
-
 	// Export hook
 	// Export hook
 	if(!empty($action)) {
 	if(!empty($action)) {
 		switch($action) {
 		switch($action) {
 			case "export-pptx":
 			case "export-pptx":
 			case "export-odp":
 			case "export-odp":
-
 				require "reports_presentation.php";
 				require "reports_presentation.php";
-
 				break;
 				break;
 			default:
 			default:
 				break;
 				break;
 		}
 		}
 	}
 	}
-
 }
 }
 
 
-
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
-View::include("reports", compact("customer", "date_range", "from_quarter", "to_quarter", "date_resolution", "all_projects", "project_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels"));
+View::include("reports", compact("customer", "date_range", "from_quarter", "to_quarter", "date_resolution", "all_projects", "project_ids", "charts", "data", "total_data", "locations_missing_region", "locations_missing_area_manager", "region_data", "area_manager_data", "key_labels", "has_pass_fail"));
 View::includeOnce("footer");
 View::includeOnce("footer");
 
 
-?>
+?>

+ 19 - 1
controllers/user_edit.php

@@ -8,6 +8,11 @@ if($user->level > Core::$user->level) $form_exception->addError("You do not have
 
 
 $page_title = "Edit User";
 $page_title = "Edit User";
 
 
+$user_customer_link = UserCustomer::queryOne(["user_id" => $user->id]);
+if(!$user_customer_link) $user_customer_link = new UserCustomer;
+
+$customers = Customer::query([], ["sort" => "name"]);
+
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/users", "Users");
 Core::addBreadcrumb("/account/users", "Users");
 Core::addBreadcrumb("/account/users/$id", $user->getDisplayName());
 Core::addBreadcrumb("/account/users/$id", $user->getDisplayName());
@@ -42,6 +47,19 @@ if($submit) {
 		$user->activated = http::value("activated", "int");
 		$user->activated = http::value("activated", "int");
 		$user->can_claim = http::value("can_claim", "int");
 		$user->can_claim = http::value("can_claim", "int");
 
 
+		$user_customer_link->delete();
+
+		$customer_id = http::value("customer_id", "int");
+		if($customer_id) {
+			$customer = Customer::get($customer_id);
+			if(!$customer) $form_exception->addError("Customer is invalid.");
+
+			$user_customer_link = new UserCustomer;
+			$user_customer_link->user_id = $user->id;
+			$user_customer_link->customer_id = $customer->id;
+			$user_customer_link->commit();
+		}
+
 		if($password && !User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
 		if($password && !User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
 		if(!isset(User::LEVELS[$user->level])) $form_exception->addError("User level is invalid.");
 		if(!isset(User::LEVELS[$user->level])) $form_exception->addError("User level is invalid.");
 		if($user->level > Core::$user->level) $form_exception->addError("You cannot elevate this user's level higher than your own.");
 		if($user->level > Core::$user->level) $form_exception->addError("You cannot elevate this user's level higher than your own.");
@@ -69,7 +87,7 @@ if($submit) {
 }
 }
 
 
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
-View::include("user_edit_form", compact("user"));
+View::include("user_edit_form", compact("user", "customers", "user_customer_link"));
 View::includeOnce("footer");
 View::includeOnce("footer");
 
 
 ?>
 ?>

+ 5 - 0
engine/project.php

@@ -78,6 +78,11 @@ class Project extends Model {
 		}
 		}
 	}
 	}
 
 
+	public function isPastDeadline() {
+		$deadline_unix = Util::getUnixTimeFromUTC($this->dts_deadline);
+		return time() > $deadline_unix;
+	}
+
 }
 }
 
 
 ?>
 ?>

+ 1 - 1
engine/template.php

@@ -18,7 +18,7 @@ class Template extends Model {
 		if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
 		if(!file_exists($folder_path)) throw new Exception("Template processing folder does not exist.");
 
 
 		$file_path = $folder_path . "/" . $file;
 		$file_path = $folder_path . "/" . $file;
-		if(!file_exists($file_path)) throw new Exception("Template processing file does not exist.");
+		if(!file_exists($file_path)) throw new Exception("Template processing file \"$file\" does not exist.");
 
 
 		return $file_path;
 		return $file_path;
 	}
 	}

+ 20 - 3
engine/test.php

@@ -149,9 +149,12 @@ class Test extends Model {
 
 
 	public function sendResultEmail($to_address) {
 	public function sendResultEmail($to_address) {
 
 
-		// Subject Format: {pass-text} - {stage-number} - Check Policy visit to: {internal-id} : {name} : {city} ({dd/mm/yyyy} {hh:mm})
-
 		$location = $this->getLocation();
 		$location = $this->getLocation();
+		$project = $this->getProject();
+		$template = $project->getTemplate();
+
+		$subject_format_file_path = $template->getProcessingFilePath("result_email_subject_format.txt");
+		$subject_format = file_get_contents($subject_format_file_path);
 
 
 		// Content
 		// Content
 		$pass_text = strtoupper($this->getPassText());
 		$pass_text = strtoupper($this->getPassText());
@@ -163,6 +166,20 @@ class Test extends Model {
 		$unix_start = strtotime($this->dts_start . " UTC");
 		$unix_start = strtotime($this->dts_start . " UTC");
 		$date_time = date("d/m/Y H:i", $unix_start);
 		$date_time = date("d/m/Y H:i", $unix_start);
 
 
+		// Subject
+		$search_replace = [
+			"{pass}" => $pass_text,
+			"{stage}" => $stage,
+			"{location}" => $location_string,
+			"{date-time}" => $date_time,
+		];
+
+		$subject = str_replace(
+			array_keys($search_replace),
+			array_values($search_replace),
+			$subject_format
+		);
+
 		// Attachments
 		// Attachments
 		$pdf_path = $this->getDirectory() . "/pdf.pdf";
 		$pdf_path = $this->getDirectory() . "/pdf.pdf";
 		$pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
 		$pdf_name = implode(" - ", array_filter([$location->internal_id, date("jS M Y")])) . ".pdf";
@@ -180,7 +197,7 @@ class Test extends Model {
 		$mail->addAddress($to_address);
 		$mail->addAddress($to_address);
 		$mail->addAttachment($pdf_path, $pdf_name);
 		$mail->addAttachment($pdf_path, $pdf_name);
 
 
-		$mail->setSubject("$pass_text - $stage - Check Policy visit to: $location_string $date_time");
+		$mail->setSubject($subject);
 
 
 		$mail->send();
 		$mail->send();
 	}
 	}

+ 12 - 0
engine/user.php

@@ -258,6 +258,18 @@ class User extends Model {
 		}
 		}
 	}
 	}
 
 
+	public function isAdmin() {
+		return ($this->level == User::LEVEL_ADMIN || $this->level == User::LEVEL_SUPERADMIN);
+	}
+
+	public function isCustomer() {
+		return $this->level == User::LEVEL_CUSTOMER;
+	}
+
+	public function isTester() {
+		return $this->level == User::LEVEL_TESTER;
+	}
+
 	public function getGenderText() {
 	public function getGenderText() {
 
 
 		if($this->gender == "M") return "Male";
 		if($this->gender == "M") return "Male";

+ 558 - 0
template_processing/age_verification/reports_build_charts.php

@@ -0,0 +1,558 @@
+<?php
+
+// Tests by Stage
+
+$chart = new Chart;
+$chart->setType("doughnut");
+$chart->setTitle("Tests by Stage $date_range_label");
+$chart->setLabels(["1 - On Entry", "2 - While Browsing", "3 - During Play", "4 - On Exit", "5 - No Challenge"]);
+$chart->addDataset("Tests", [
+	$total_data->stage1Count,
+	$total_data->stage2Count,
+	$total_data->stage3Count,
+	$total_data->stage4Count,
+	$total_data->stage5Count,
+], [
+	"background_colours" => [
+		"#4CAF50",
+		"#FFEB3B",
+		"#FBC02D",
+		"#FFAB91",
+		"#F44336",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$chart->setPrintSuffixes("");
+$chart->print = true;
+$chart->printPercentages = true;
+$chart->setPrintLabels(["Stage 1", "Stage 2", "Stage 3", "Stage 4", "Stage 5"]);
+
+$charts[] = $chart;
+
+
+// Challenger Gender
+$chart = new Chart;
+$chart->setType("pie");
+$chart->setTitle("Staff Gender $date_range_label");
+$chart->setLabels(["Male", "Female", "Unspecified"]);
+$chart->addDataset("Counts", [
+	$total_data->maleChallengerCount,
+	$total_data->femaleChallengerCount,
+	$total_data->unspecifiedChallengerCount,
+], [
+	"background_colours" => [
+		"#2196F3",
+		"#E91E63",
+		"#9E9E9E",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$charts[] = $chart;
+
+// Tester Gender
+$chart = new Chart;
+$chart->setType("pie");
+$chart->setTitle("Tester Gender $date_range_label");
+$chart->setLabels(["Male", "Female"]);
+$chart->addDataset("Counts", [
+	$total_data->maleTesterCount,
+	$total_data->femaleTesterCount,
+], [
+	"background_colours" => [
+		"#2196F3",
+		"#E91E63",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$charts[] = $chart;
+
+// Pass / Fail
+$chart = new Chart;
+$chart->setType("pie");
+$chart->setTitle("Pass / Fail $date_range_label");
+$chart->setLabels(["Pass", "Fail"]);
+$chart->addDataset("Counts", [
+	$total_data->passCount,
+	$total_data->failCount,
+], [
+	"background_colours" => [
+		"#4CAF50",
+		"#F44336",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$charts[] = $chart;
+
+// Pass Rate Period
+$pass_rates = array_values(array_map(function($d) {
+	if($d->testCount == 0) return 0;
+	return round(($d->passCount / $d->testCount) * 100);
+}, $data));
+
+$fail_rates = array_values(array_map(function($d) {
+	if($d->testCount == 0) return 0;
+	$pass_rate = round(($d->passCount / $d->testCount) * 100);
+	return 100 - $pass_rate;
+}, $data));
+
+$chart = new Chart;
+$chart->addXAxis([
+	"type" => "category",
+	"stacked" => true,
+]);
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => 100],
+	"stacked" => true,
+]);
+
+$chart->setType("bar");
+$chart->setTitle("Pass / Fail by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("Pass Rate (%)", $pass_rates, [
+	"background_colours" => "#4CAF50"
+]);
+$chart->addDataset("Fail Rate (%)", $fail_rates, [
+	"background_colours" => "#F44336"
+]);
+$chart->setDatalabelSuffixes("%", "%");
+$charts[] = $chart;
+
+// Totals by Period
+
+$test_counts = array_column($data, "testCount");
+$pass_counts = array_column($data, "passCount");
+$fail_counts = array_column($data, "failCount");
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Totals by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
+]);
+$chart->addYAxis([
+	"position" => "right",
+	"id" => "y-axis-2",
+	"grid_lines" => false,
+	"tick_config" => ["min" => 0, "max" => 100]
+]);
+
+$chart->addDataset("Pass Rate (%)", $pass_rates, [
+	"border_colours" => "#FFEB3B",
+	"background_colours" => "transparent",
+	"type" => "line",
+	"y_axis_id" => "y-axis-2",
+	"datalabels" => [
+		"display" => false
+	],
+	"line_tension" => 0,
+]);
+$chart->addDataset("Test Count", $test_counts, [
+	"background_colours" => "#2196F3",
+	"type" => "bar",
+	"y_axis_id" => "y-axis-1",
+	"datalabels" => [
+		"display" => false
+	],
+]);
+$chart->addDataset("Pass", $pass_counts, [
+	"background_colours" => "#4CAF50",
+	"type" => "bar",
+	"y_axis_id" => "y-axis-1",
+	"datalabels" => [
+		"display" => false
+	],
+]);
+$chart->addDataset("Fail", $fail_counts, [
+	"background_colours" => "#F44336",
+	"type" => "bar",
+	"y_axis_id" => "y-axis-1",
+	"datalabels" => [
+		"display" => false
+	],
+]);
+$chart->setDatalabelSuffixes("%", "", "", "");
+$chart->print = true;
+$charts[] = $chart;
+
+// Point of Challenge (Each Period)
+
+foreach($keys as $index => $key) {
+	$key_label = $key_labels[$index];
+
+	$period_data = $data[$key];
+
+	if($period_data->testCount == 0) continue;
+
+	$chart = new Chart;
+	$chart->setType("pie");
+	$chart->setTitle("$key_label Point of Challenge");
+	$chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
+	$chart->addDataset("Counts", [
+		$period_data->stage1Count,
+		$period_data->stage2Count,
+		$period_data->stage3Count,
+		$period_data->stage4Count,
+		$period_data->stage5Count,
+	], [
+		"background_colours" => [
+			"#4CAF50",
+			"#FFEB3B",
+			"#FBC02D",
+			"#FFAB91",
+			"#F44336",
+		],
+		"datalabels" => Chart::DATALABELS_BOX,
+	]);
+	$charts[] = $chart;
+}
+
+// Tester Genders by Period
+$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->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max(max($male_counts), max($female_counts)) / 10) * 10]
+]);
+
+$chart->setType("bar");
+$chart->setTitle("Tester Gender by $resolution_name");
+$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 by Period
+$test_counts = array_values(array_map(function($d) {
+	return $d->testCount;
+}, $data));
+
+$chart = new Chart;
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
+]);
+$chart->setType("bar");
+$chart->setTitle("Tests by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("Tests", $test_counts, [
+	"background_colours" => "#2196F3"
+]);
+$charts[] = $chart;
+
+// Gender Graphs
+$gender_keys = [
+	"maleChallengeMale" => [
+		"title" => "Male Challenge Male",
+		"label" => "M Cha. M",
+	],
+	"maleChallengeFemale" => [
+		"title" => "Male Challenge Female",
+		"label" => "M Cha. F",
+	],
+	"femaleChallengeMale" => [
+		"title" => "Female Challenge Male",
+		"label" => "F Cha. M",
+	],
+	"femaleChallengeFemale" => [
+		"title" => "Female Challenge Female",
+		"label" => "F Cha. F",
+	],
+	"unknownChallenge" => [
+		"title" => "Gender Not Specified",
+		"label" => "Gender Not Specified",
+	],
+];
+
+$gender_test_counts = [];
+foreach($gender_keys as $gk => $gv) {
+	$gender_test_counts[] = array_sum(array_column($data, "{$gk}Count"));
+}
+
+// Gender Comparison by Date Range
+$chart = new Chart;
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max($gender_test_counts) / 10) * 10]
+]);
+$chart->setType("bar");
+$chart->setTitle("Gender Comparison $date_range_label");
+$chart->setLabels(array_column($gender_keys, "label"));
+$chart->addDataset("Tests", $gender_test_counts, [
+	"background_colours" => "#2196F3"
+]);
+$charts[] = $chart;
+
+// Gender Comparison by Resolution
+$mcm_counts = array_column($data, "maleChallengeMaleCount");
+$mcf_counts = array_column($data, "maleChallengeFemaleCount");
+$fcm_counts = array_column($data, "femaleChallengeMaleCount");
+$fcf_counts = array_column($data, "femaleChallengeFemaleCount");
+$ns_counts = array_column($data, "unknownChallengeCount");
+
+$chart = new Chart;
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max(max($mcm_counts), max($mcf_counts), max($fcm_counts), max($fcf_counts)) / 10) * 10]
+]);
+$chart->setType("bar");
+$chart->setTitle("Gender Comparison by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("M Cha. M", $mcm_counts, ["background_colours" => "#2196F3"]);
+$chart->addDataset("M Cha. F", $mcf_counts, ["background_colours" => "#3F51B5"]);
+$chart->addDataset("F Cha. M", $fcm_counts, ["background_colours" => "#9C27B0"]);
+$chart->addDataset("F Cha. F", $fcf_counts, ["background_colours" => "#E91E63"]);
+$chart->addDataset("Not Specified", $ns_counts, ["background_colours" => "#9E9E9E"]);
+$chart->print = true;
+$charts[] = $chart;
+
+// Gender Comparison Pass Rate by Resolution
+$mcm_pass_counts = array_column($data, "maleChallengeMalePassCount");
+$mcf_pass_counts = array_column($data, "maleChallengeFemalePassCount");
+$fcm_pass_counts = array_column($data, "femaleChallengeMalePassCount");
+$fcf_pass_counts = array_column($data, "femaleChallengeFemalePassCount");
+$ns_pass_counts = array_column($data, "unknownChallengePassCount");
+
+foreach(["mcm", "mcf", "fcm", "fcf", "ns"] as $prefix) {
+	$pass_rates = [];
+	$pass_counts = ${$prefix . "_pass_counts"};
+	$test_counts = ${$prefix . "_counts"};
+
+	foreach($test_counts as $index => $test_count) {
+		if($test_count == 0) {
+			$pass_rates[] = 0;
+			continue;
+		}
+		$pass_rates[] = number_format(($pass_counts[$index] * 100) / $test_count, 2);
+	}
+
+	${$prefix . "_pass_rates"} = $pass_rates;
+}
+
+$chart = new Chart;
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => 100],
+]);
+$chart->setType("bar");
+$chart->setTitle("Gender Comparison Pass Rate by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("M Cha. M", $mcm_pass_rates, ["background_colours" => "#2196F3", "datalabels" => ["display" => false],  "line_tension" => 0]);
+$chart->addDataset("M Cha. F", $mcf_pass_rates, ["background_colours" => "#3F51B5", "datalabels" => ["display" => false], "line_tension" => 0]);
+$chart->addDataset("F Cha. M", $fcm_pass_rates, ["background_colours" => "#9C27B0", "datalabels" => ["display" => false], "line_tension" => 0]);
+$chart->addDataset("F Cha. F", $fcf_pass_rates, ["background_colours" => "#E91E63", "datalabels" => ["display" => false], "line_tension" => 0]);
+$chart->addDataset("Not Specified", $ns_pass_rates, ["background_colours" => "#9E9E9E", "datalabels" => ["display" => false], "line_tension" => 0]);
+$chart->print = true;
+$chart->setDatalabelSuffixes("%", "%", "%", "%", "%");
+$charts[] = $chart;
+
+// Gender Comparison Point of Challenge
+foreach($keys as $index => $key) {
+	$key_label = $key_labels[$index];
+	$period_data = $data[$key];
+
+	foreach($gender_keys as $prefix => $gv) {
+
+		$title = $gv["label"];
+		$test_count = $period_data->{$prefix . "Count"};
+		$stage_data = $period_data->{$prefix . "Stages"};
+
+		if($test_count == 0) continue;
+
+		$chart = new Chart;
+		$chart->setType("pie");
+		$chart->setTitle("$key_label $title Point of Challenge");
+		$chart->setLabels(["On Entry", "While Browsing", "During Play", "On Exit", "No Challenge"]);
+		$chart->addDataset("Counts", $stage_data, [
+			"background_colours" => [
+				"#4CAF50",
+				"#FFEB3B",
+				"#FBC02D",
+				"#FFAB91",
+				"#F44336",
+			],
+			"datalabels" => Chart::DATALABELS_BOX,
+		]);
+		$charts[] = $chart;
+	}
+}
+
+// Region Graphs
+if($has_regions) {
+
+	ksort($region_data, SORT_NATURAL);
+
+	$region_keys = array_keys($region_data);
+
+	$x_data_labels = array_map(function($key) {
+		return "Region $key";
+	}, $region_keys);
+
+	// Region Pass Rate (Selected Date Range)
+	$data_values = [];
+
+	$test_counts = [];
+	$pass_counts = [];
+	$fail_counts = [];
+
+	foreach($region_data as $region => $r_data) {
+		$total_region_test_count = array_sum(array_column($r_data, "testCount"));
+		$total_region_pass_count = array_sum(array_column($r_data, "passCount"));
+
+		$test_counts[] = $total_region_test_count;
+		$pass_counts[] = $total_region_pass_count;
+		$fail_counts[] = $total_region_test_count - $total_region_pass_count;
+
+		$data_values[] = ($total_region_test_count == 0) ? 0 : round(($total_region_pass_count * 100) / $total_region_test_count);
+	}
+
+	$chart = new Chart;
+	$chart->addYAxis([
+		"id" => "y-axis-1",
+		"tick_config" => ["min" => 0, "max" => 100]
+	]);
+	$chart->setType("bar");
+	$chart->setTitle("Region Pass Rate $date_range_label");
+	$chart->setLabels($x_data_labels);
+	$chart->addDataset("Pass %", $data_values , [
+		"background_colours" => "#2196F3"
+	]);
+	$chart->addPrintDataset("Tests", $test_counts);
+	$chart->addPrintDataset("Pass", $pass_counts);
+	$chart->addPrintDataset("Fail", $fail_counts);
+
+	$chart->setDatalabelSuffixes("%");
+	$chart->print = true;
+	$chart->printTotalColumn = true;
+	$charts[] = $chart;
+
+	// Region Pass Rate Period
+	$chart = new Chart;
+	$chart->addYAxis([
+		"id" => "y-axis-1",
+		"tick_config" => ["min" => 0, "max" => 100]
+	]);
+	$chart->setType("bar");
+	$chart->setTitle("Region Pass Rate by $resolution_name");
+	$chart->setLabels($x_data_labels);
+
+	foreach($keys as $index => $key) {
+		$data_values = [];
+		foreach($region_keys as $region) {
+			$r_data = $region_data[$region][$key];
+			$data_values[] = ($r_data->testCount == 0) ? 0 : round(($r_data->passCount * 100) / $r_data->testCount);
+		}
+		$chart->addDataset($key_labels[$index], $data_values, ["background_colours" => $colours[$index]]);
+	}
+	$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
+
+	$charts[] = $chart;
+
+}
+
+// Area Manager Graphs
+if($has_area_managers) {
+
+	ksort($area_manager_data, SORT_NATURAL);
+
+	$area_manager_keys = array_keys($area_manager_data);
+
+	$x_data_labels = array_map(function($key) {
+		return "$key";
+	}, $area_manager_keys);
+
+	// Area Pass Rate (Selected Date Range)
+	$data_values = [];
+
+	$test_counts = [];
+	$pass_counts = [];
+	$fail_counts = [];
+
+	foreach($area_manager_data as $area_manager => $a_data) {
+		$total_area_manager_test_count = array_sum(array_column($a_data, "testCount"));
+		$total_area_manager_pass_count = array_sum(array_column($a_data, "passCount"));
+		$total_area_manager_fail_count = array_sum(array_column($a_data, "failCount"));
+
+		$test_counts[] = $total_area_manager_test_count;
+		$pass_counts[] = $total_area_manager_pass_count;
+		$fail_counts[] = $total_area_manager_fail_count;
+
+		$data_values[] = ($total_region_test_count == 0) ? 0 : round(($total_area_manager_pass_count * 100) / $total_area_manager_test_count);
+	}
+
+	$chart = new Chart;
+	$chart->addXAxis([
+		"type" => "category",
+		"scale_label_config" => [
+			"display" => true,
+			"labelString" => "Area Manager",
+		]
+	]);
+	$chart->addYAxis([
+		"id" => "y-axis-1",
+		"tick_config" => ["min" => 0, "max" => 100]
+	]);
+	$chart->setType("bar");
+	$chart->setTitle("Area Manager Pass Rate $date_range_label");
+	$chart->setLabels($x_data_labels);
+	$chart->addDataset("Pass %", $data_values , [
+		"background_colours" => "#2196F3"
+	]);
+	$chart->addPrintDataset("Tests", $test_counts);
+	$chart->addPrintDataset("Pass", $pass_counts);
+	$chart->addPrintDataset("Fail", $fail_counts);
+	$chart->setDatalabelSuffixes("%");
+	$chart->print = true;
+	$chart->printTotalColumn = true;
+	$charts[] = $chart;
+
+	// Area Pass Rate Period
+	$chart = new Chart;
+	$chart->addXAxis([
+		"type" => "category",
+		"scale_label_config" => [
+			"display" => true,
+			"labelString" => "Area Manager",
+		]
+	]);
+	$chart->addYAxis([
+		"id" => "y-axis-1",
+		"tick_config" => ["min" => 0, "max" => 100]
+	]);
+	$chart->setType("bar");
+	$chart->setTitle("Area Manager Pass Rate by $resolution_name");
+	$chart->setLabels($x_data_labels);
+
+	foreach($keys as $index => $key) {
+		$data_values = [];
+		foreach($area_manager_keys as $area_manager) {
+			$a_data = $area_manager_data[$area_manager][$key];
+			$data_values[] = ($a_data->testCount == 0) ? 0 : round(($a_data->passCount * 100) / $a_data->testCount);
+		}
+		$chart->addDataset($key_labels[$index], $data_values, [
+			"background_colours" => $colours[$index],
+			"datalabels" => [
+				"display" => false,
+			],
+		]);
+	}
+	$chart->setDatalabelSuffixes(...array_fill(0, count($keys), "%"));
+
+	$charts[] = $chart;
+
+}
+
+?>

+ 62 - 0
template_processing/age_verification/reports_create_empty_objects.php

@@ -0,0 +1,62 @@
+<?php
+
+$empty_data = (object) [
+	"label" => "",
+	"testCount" => 0,
+	"passCount" => 0,
+	"failCount" => 0,
+
+	"maleTesterCount" => 0,
+	"femaleTesterCount" => 0,
+
+	"maleChallengerCount" => 0,
+	"femaleChallengerCount" => 0,
+	"unspecifiedChallengerCount" => 0,
+
+	"maleChallengeMaleCount" => 0,
+	"maleChallengeMalePassCount" => 0,
+	"maleChallengeMaleStages" => [0, 0, 0, 0, 0],
+
+	"maleChallengeFemaleCount" => 0,
+	"maleChallengeFemalePassCount" => 0,
+	"maleChallengeFemaleStages" => [0, 0, 0, 0, 0],
+
+	"femaleChallengeMaleCount" => 0,
+	"femaleChallengeMalePassCount" => 0,
+	"femaleChallengeMaleStages" => [0, 0, 0, 0, 0],
+
+	"femaleChallengeFemaleCount" => 0,
+	"femaleChallengeFemalePassCount" => 0,
+	"femaleChallengeFemaleStages" => [0, 0, 0, 0, 0],
+
+	"unknownChallengeCount" => 0,
+	"unknownChallengePassCount" => 0,
+	"unknownChallengeStages" => [0, 0, 0, 0, 0],
+
+	"stage1Count" => 0,
+	"stage2Count" => 0,
+	"stage3Count" => 0,
+	"stage4Count" => 0,
+	"stage5Count" => 0,
+];
+
+$total_data = (object) [
+	"maleTesterCount" => 0,
+	"femaleTesterCount" => 0,
+
+	"maleChallengerCount" => 0,
+	"femaleChallengerCount" => 0,
+	"unspecifiedChallengerCount" => 0,
+
+	"testCount" => 0,
+	"passCount" => 0,
+	"failCount" => 0,
+
+	"stage1Count" => 0,
+	"stage2Count" => 0,
+	"stage3Count" => 0,
+	"stage4Count" => 0,
+	"stage5Count" => 0,
+];
+
+?>

+ 70 - 0
template_processing/age_verification/reports_process_test.php

@@ -0,0 +1,70 @@
+<?php
+
+$stage_key = "stage" . $test->challenge_stage . "Count";
+$data[$key]->$stage_key++;
+$total_data->{$stage_key}++;
+
+if($test->pass) {
+	$data[$key]->passCount++;
+	$total_data->passCount++;
+	if($region) $region_data[$region][$key]->passCount++;
+	if($area_manager) $area_manager_data[$area_manager][$key]->passCount++;
+}
+else {
+	$data[$key]->failCount++;
+	$total_data->failCount++;
+	if($region) $region_data[$region][$key]->failCount++;
+	if($area_manager) $area_manager_data[$area_manager][$key]->failCount++;
+}
+
+if($test->challenger_gender == "M") {
+	$data[$key]->maleChallengerCount++;
+	$total_data->maleChallengerCount++;
+}
+else if($test->challenger_gender == "F") {
+	$data[$key]->femaleChallengerCount++;
+	$total_data->femaleChallengerCount++;
+}
+else {
+	$data[$key]->unspecifiedChallengerCount++;
+	$total_data->unspecifiedChallengerCount++;
+}
+
+// Genders Comparison
+if($tester->gender == "M") {
+	if($test->challenger_gender == "M") {
+		$data[$key]->maleChallengeMaleCount++;
+		$data[$key]->maleChallengeMaleStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->maleChallengeMalePassCount++;
+	}
+	else if($test->challenger_gender == "F") {
+		$data[$key]->femaleChallengeMaleCount++;
+		$data[$key]->femaleChallengeMaleStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->femaleChallengeMalePassCount++;
+	}
+	else {
+		$data[$key]->unknownChallengeCount++;
+		$data[$key]->unknownChallengeStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->unknownChallengePassCount++;
+	}
+}
+
+if($tester->gender == "F") {
+	if($test->challenger_gender == "M") {
+		$data[$key]->maleChallengeFemaleCount++;
+		$data[$key]->maleChallengeFemaleStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->maleChallengeFemalePassCount++;
+	}
+	else if($test->challenger_gender == "F") {
+		$data[$key]->femaleChallengeFemaleCount++;
+		$data[$key]->femaleChallengeFemaleStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->femaleChallengeFemalePassCount++;
+	}
+	else {
+		$data[$key]->unknownChallengeCount++;
+		$data[$key]->unknownChallengeStages[$test->challenge_stage - 1]++;
+		if($test->pass) $data[$key]->unknownChallengePassCount++;
+	}
+}
+
+?>

+ 1 - 0
template_processing/age_verification/result_email_subject_format.txt

@@ -0,0 +1 @@
+{pass} - {stage} - Check Policy visit to: {location} {date-time}

+ 250 - 0
template_processing/mystery_shop/reports_build_charts.php

@@ -0,0 +1,250 @@
+<?php
+
+// Tests by Cleanliness
+
+$chart = new Chart;
+$chart->setType("doughnut");
+$chart->setTitle("Tests by Cleanliness $date_range_label");
+$chart->setLabels(["Very dirty", "Dirty", "Adequate", "Clean", "Very Clean"]);
+$chart->addDataset("Tests", [
+	$total_data->cleanlinessCounts[0],
+	$total_data->cleanlinessCounts[1],
+	$total_data->cleanlinessCounts[2],
+	$total_data->cleanlinessCounts[3],
+	$total_data->cleanlinessCounts[4],
+], [
+	"background_colours" => [
+		"#F44336",
+		"#FFAB91",
+		"#FBC02D",
+		"#FFEB3B",
+		"#4CAF50",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$chart->setPrintSuffixes("");
+$chart->print = true;
+$chart->printPercentages = true;
+
+$charts[] = $chart;
+
+// Cleanliness (Each Period)
+foreach($keys as $index => $key) {
+	$key_label = $key_labels[$index];
+
+	$period_data = $data[$key];
+
+	if($period_data->testCount == 0) continue;
+
+	$chart = new Chart;
+	$chart->setType("pie");
+	$chart->setTitle("$key_label Cleanliness");
+	$chart->setLabels(["Very dirty", "Dirty", "Adequate", "Clean", "Very Clean"]);
+	$chart->addDataset("Counts", [
+		$period_data->cleanlinessCounts[0],
+		$period_data->cleanlinessCounts[1],
+		$period_data->cleanlinessCounts[2],
+		$period_data->cleanlinessCounts[3],
+		$period_data->cleanlinessCounts[4],
+	], [
+		"background_colours" => [
+			"#F44336",
+			"#FFAB91",
+			"#FBC02D",
+			"#FFEB3B",
+			"#4CAF50",
+		],
+		"datalabels" => Chart::DATALABELS_BOX,
+	]);
+	$charts[] = $chart;
+}
+
+// Tester Gender
+$chart = new Chart;
+$chart->setType("pie");
+$chart->setTitle("Tester Gender $date_range_label");
+$chart->setLabels(["Male", "Female"]);
+$chart->addDataset("Counts", [
+	$total_data->maleTesterCount,
+	$total_data->femaleTesterCount,
+], [
+	"background_colours" => [
+		"#2196F3",
+		"#E91E63",
+	],
+	"datalabels" => Chart::DATALABELS_BOX,
+]);
+$charts[] = $chart;
+
+// Staff Badge Worn
+$staff_badge_worn_counts = array_column($data, "staffBadgeWornCounts");
+
+$unknown_counts = array_column($staff_badge_worn_counts, 0);
+$no_counts = array_column($staff_badge_worn_counts, 1);
+$yes_counts = array_column($staff_badge_worn_counts, 2);
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Staff Badge Worn by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("Don't know", $unknown_counts, [
+	"background_colours" => "#CCCCCC"
+]);
+$chart->addDataset("No", $no_counts, [
+	"background_colours" => "#F44336"
+]);
+$chart->addDataset("Yes", $yes_counts, [
+	"background_colours" => "#4CAF50"
+]);
+$charts[] = $chart;
+
+// Staff Offered Drink
+$staff_offered_drink_counts = array_column($data, "staffOfferedDrinkCounts");
+
+$no_counts = array_column($staff_offered_drink_counts, 0);
+$yes_counts = array_column($staff_offered_drink_counts, 1);
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Staff Offered Drink by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("No", $no_counts, [
+	"background_colours" => "#F44336"
+]);
+$chart->addDataset("Yes", $yes_counts, [
+	"background_colours" => "#4CAF50"
+]);
+$charts[] = $chart;
+
+// Staff Offered Promotion
+$staff_offered_promotion_counts = array_column($data, "staffOfferedPromotionCounts");
+
+$no_counts = array_column($staff_offered_promotion_counts, 0);
+$yes_counts = array_column($staff_offered_promotion_counts, 1);
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Staff Offered Promotion by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("No", $no_counts, [
+	"background_colours" => "#F44336"
+]);
+$chart->addDataset("Yes", $yes_counts, [
+	"background_colours" => "#4CAF50"
+]);
+$charts[] = $chart;
+
+// Staff Offered Help
+$staff_offered_help_counts = array_column($data, "staffOfferedHelpCounts");
+
+$no_counts = array_column($staff_offered_help_counts, 0);
+$yes_counts = array_column($staff_offered_help_counts, 1);
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Staff Offered Help by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("No", $no_counts, [
+	"background_colours" => "#F44336"
+]);
+$chart->addDataset("Yes", $yes_counts, [
+	"background_colours" => "#4CAF50"
+]);
+$charts[] = $chart;
+
+// Staff Behaviour (Each Period)
+foreach($keys as $index => $key) {
+	$key_label = $key_labels[$index];
+
+	$period_data = $data[$key];
+
+	if($period_data->testCount == 0) continue;
+
+	$chart = new Chart;
+	$chart->setType("pie");
+	$chart->setTitle("$key_label Staff Behaviour");
+	$chart->setLabels(["N/A", "Rude", "Slightly rude", "Neither rude or polite", "Polite", "Very polite"]);
+	$chart->addDataset("Counts", [
+		$period_data->staffBehaviourCounts[0],
+		$period_data->staffBehaviourCounts[1],
+		$period_data->staffBehaviourCounts[2],
+		$period_data->staffBehaviourCounts[3],
+		$period_data->staffBehaviourCounts[4],
+		$period_data->staffBehaviourCounts[5],
+	], [
+		"background_colours" => [
+			"#CCCCCC",
+			"#F44336",
+			"#FFAB91",
+			"#FBC02D",
+			"#FFEB3B",
+			"#4CAF50",
+		],
+		"datalabels" => Chart::DATALABELS_BOX,
+	]);
+	$charts[] = $chart;
+}
+
+// Staff Uniform
+$staff_uniform_counts = array_column($data, "staffUniformCounts");
+
+$no_counts = array_column($staff_uniform_counts, 0);
+$yes_counts = array_column($staff_uniform_counts, 1);
+
+$chart = new Chart;
+$chart->setType("bar");
+$chart->setTitle("Staff Uniform by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("No", $no_counts, [
+	"background_colours" => "#F44336"
+]);
+$chart->addDataset("Yes", $yes_counts, [
+	"background_colours" => "#4CAF50"
+]);
+$charts[] = $chart;
+
+// Tester Genders by Period
+$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->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max(max($male_counts), max($female_counts)) / 10) * 10]
+]);
+
+$chart->setType("bar");
+$chart->setTitle("Tester Gender by $resolution_name");
+$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 by Period
+$test_counts = array_values(array_map(function($d) {
+	return $d->testCount;
+}, $data));
+
+$chart = new Chart;
+$chart->addYAxis([
+	"id" => "y-axis-1",
+	"tick_config" => ["min" => 0, "max" => ceil(max($test_counts) / 10) * 10]
+]);
+$chart->setType("bar");
+$chart->setTitle("Tests by $resolution_name");
+$chart->setLabels($x_data_labels);
+$chart->addDataset("Tests", $test_counts, [
+	"background_colours" => "#2196F3"
+]);
+$charts[] = $chart;
+
+?>

+ 28 - 0
template_processing/mystery_shop/reports_create_empty_objects.php

@@ -0,0 +1,28 @@
+<?php
+
+$empty_data = (object) [
+	"label" => "",
+	"testCount" => 0,
+
+	"cleanlinessCounts" => [0, 0, 0, 0, 0],
+	"staffBadgeWornCounts" => [0, 0, 0],
+	"staffOfferedDrinkCounts" => [0, 0],
+	"staffOfferedPromotionCounts" => [0, 0],
+	"staffOfferedHelpCounts" => [0, 0],
+	"staffBehaviourCounts" => [0, 0, 0, 0, 0, 0],
+	"staffUniformCounts" => [0, 0],
+
+	"maleTesterCount" => 0,
+	"femaleTesterCount" => 0,
+];
+
+$total_data = (object) [
+	"cleanlinessCounts" => [0, 0, 0, 0, 0],
+
+	"maleTesterCount" => 0,
+	"femaleTesterCount" => 0,
+
+	"testCount" => 0,
+];
+
+?>

+ 27 - 0
template_processing/mystery_shop/reports_process_test.php

@@ -0,0 +1,27 @@
+<?php
+
+$cleanliness = $test_values->cleanliness;
+$data[$key]->cleanlinessCounts[$cleanliness - 1]++;
+$total_data->cleanlinessCounts[$cleanliness - 1]++;
+
+$staff_badge_worn = $test_values->staff_badge_worn ?? null;
+if(is_null($staff_badge_worn)) $data[$key]->staffBadgeWornCounts[0]++;
+else $data[$key]->staffBadgeWornCounts[$staff_badge_worn + 1]++;
+
+$staff_offered_drink = $test_values->staff_offered_drink ?? 0;
+$data[$key]->staffOfferedDrinkCounts[$staff_offered_drink]++;
+
+$staff_offered_promotion = $test_values->staff_offered_promotion ?? 0;
+$data[$key]->staffOfferedPromotionCounts[$staff_offered_promotion]++;
+
+$staff_help = $test_values->staff_help ?? 0;
+$data[$key]->staffOfferedHelpCounts[$staff_help]++;
+
+$staff_behaviour = $test_values->staff_behaviour ?? null;
+if(is_null($staff_behaviour)) $data[$key]->staffBehaviourCounts[0]++;
+else $data[$key]->staffBehaviourCounts[$staff_behaviour]++;
+
+$staff_uniform = $test_values->staff_uniform ?? 0;
+$data[$key]->staffUniformCounts[$staff_uniform]++;
+
+?>

+ 1 - 0
template_processing/mystery_shop/result_email_subject_format.txt

@@ -0,0 +1 @@
+Mystery Shop - Check Policy visit to: {location} {date-time}

+ 157 - 146
views/reports.php

@@ -173,63 +173,63 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 	</form>
 	</form>
 
 
 	<?php
 	<?php
-	if($locations_missing_region) {
-		?>
-		<div class="alert alert-warning d-print-none">
-			There are some locations that are missing "region" information:
-			<ul class="mb-0">
-			<?php
-			foreach($locations_missing_region as $location) {
-				$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
-				?>
-				<li>
-					<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
-					<?php
-					if($location->internal_id) {
-						?>
-						<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
-						<?php
-					}
-					?>
-				</li>
-				<?php
-			}
+	if(Core::$user->isAdmin()) {
+		if($locations_missing_region) {
 			?>
 			?>
-			</ul>
-		</div>
-		<?php
-	}
-	?>
-
-	<?php
-	if($locations_missing_area_manager) {
-		?>
-		<div class="alert alert-warning d-print-none">
-			There are some locations that are missing "area manager" information:
-			<ul class="mb-0">
-			<?php
-			foreach($locations_missing_area_manager as $location) {
-				$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
-				?>
-				<li>
-					<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
-					<?php
-					if($location->internal_id) {
-						?>
-						<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
-						<?php
-					}
-					?>
-				</li>
+			<div class="alert alert-warning d-print-none">
+				There are some locations that are missing "region" information:
+				<ul class="mb-0">
 				<?php
 				<?php
-			}
+				foreach($locations_missing_region as $location) {
+					$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
+					?>
+					<li>
+						<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
+						<?php
+						if($location->internal_id) {
+							?>
+							<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
+							<?php
+						}
+						?>
+					</li>
+					<?php
+				}
+				?>
+				</ul>
+			</div>
+			<?php
+		}
+		if($locations_missing_area_manager) {
 			?>
 			?>
-			</ul>
-		</div>
-		<?php
+			<div class="alert alert-warning d-print-none">
+				There are some locations that are missing "area manager" information:
+				<ul class="mb-0">
+				<?php
+				foreach($locations_missing_area_manager as $location) {
+					$url = (Core::$user->canAdmin()) ? "/account/locations/$location->id/edit" : "#";
+					?>
+					<li>
+						<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($location->name)?></a>
+						<?php
+						if($location->internal_id) {
+							?>
+							<small class="text-info">(Internal ID: <?=htmlspecialchars($location->internal_id)?>)</small>
+							<?php
+						}
+						?>
+					</li>
+					<?php
+				}
+				?>
+				</ul>
+			</div>
+			<?php
+		}
 	}
 	}
 	?>
 	?>
 
 
+
 	<?php
 	<?php
 	if($charts) {
 	if($charts) {
 		?>
 		?>
@@ -246,13 +246,19 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 				</div>
 				</div>
 			</div>
 			</div>
 			<div class="col-md-6">
 			<div class="col-md-6">
-				<div class="card card-body mb-3">
-					Pass/Fail Count
-					<div class="stat">
-						<?=$total_data->passCount?> /
-						<?=$total_data->failCount?>
+				<?php
+				if(isset($total_data->passCount)) {
+					?>
+					<div class="card card-body mb-3">
+						Pass/Fail Count
+						<div class="stat">
+							<?=$total_data->passCount?> /
+							<?=$total_data->failCount?>
+						</div>
 					</div>
 					</div>
-				</div>
+					<?php
+				}
+				?>
 			</div>
 			</div>
 		</div>
 		</div>
 
 
@@ -324,111 +330,116 @@ $date_range_custom_style = ($date_range == "custom") ? "" : "display: none";
 			<?php
 			<?php
 		}
 		}
 
 
-		?>
-		<div class="d-print-none">
-			<h2>Extra Information</h2>
+		if($has_pass_fail) {
+			?>
+			<div class="d-print-none">
+				<h2>Extra Information</h2>
 
 
-			<h3>Region Data</h3>
+				<h3>Region Data</h3>
 
 
-			<?php
+				<?php
 
 
-			foreach($region_data as $region => $rows) {
+				foreach($region_data as $region => $rows) {
 
 
-				?>
-				<h4>Region: <?=htmlspecialchars($region)?></h4>
-
-				<table class="table">
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<thead>
-						<tr>
-							<th>Period</th>
-							<th>Tests</th>
-							<th>Passes</th>
-							<th>Fails</th>
-							<th>Rate</th>
-						</tr>
-					</thead>
-					<tbody>
-					<?php
+					?>
+					<h4>Region: <?=htmlspecialchars($region)?></h4>
+
+					<table class="table">
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<thead>
+							<tr>
+								<th>Period</th>
+								<th>Tests</th>
+								<th>Passes</th>
+								<th>Fails</th>
+								<th>Rate</th>
+							</tr>
+						</thead>
+						<tbody>
+						<?php
 
 
-					$index = 0;
+						$index = 0;
 
 
-					foreach($rows as $key => $data) {
-						$rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
-						$label = $key_labels[$index];
+						foreach($rows as $key => $data) {
+							$rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
+							$label = $key_labels[$index];
+							?>
+							<tr>
+								<td><?=htmlspecialchars($label)?></td>
+								<td><?=number_format($data->testCount)?></td>
+								<td><?=number_format($data->passCount)?></td>
+								<td><?=number_format($data->failCount)?></td>
+								<td><?=$rate?>%</td>
+							</tr>
+							<?php
+							$index++;
+						}
 						?>
 						?>
-						<tr>
-							<td><?=htmlspecialchars($label)?></td>
-							<td><?=number_format($data->testCount)?></td>
-							<td><?=number_format($data->passCount)?></td>
-							<td><?=number_format($data->failCount)?></td>
-							<td><?=$rate?>%</td>
-						</tr>
-						<?php
-						$index++;
-					}
-					?>
-					</tbody>
-				</table>
+						</tbody>
+					</table>
 
 
-				<?php
+					<?php
 
 
-			}
+				}
 
 
-			?>
-			<h3>Area Manager Data</h3>
-			<?php
+				?>
+				<h3>Area Manager Data</h3>
+				<?php
 
 
-			foreach($area_manager_data as $area_manager => $rows) {
+				foreach($area_manager_data as $area_manager => $rows) {
 
 
-				?>
-				<h4>Area Manager: <?=htmlspecialchars($area_manager)?></h4>
-
-				<table class="table">
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<col style="width: 20%" />
-					<thead>
-						<tr>
-							<th>Period</th>
-							<th>Tests</th>
-							<th>Passes</th>
-							<th>Fails</th>
-							<th>Rate</th>
-						</tr>
-					</thead>
-					<tbody>
-					<?php
-					$index = 0;
-					foreach($rows as $key => $data) {
-						$rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
-						$label = $key_labels[$index];
-						?>
-						<tr>
-							<td><?=htmlspecialchars($label)?></td>
-							<td><?=number_format($data->testCount)?></td>
-							<td><?=number_format($data->passCount)?></td>
-							<td><?=number_format($data->failCount)?></td>
-							<td><?=$rate?>%</td>
-						</tr>
-						<?php
-						$index++;
-					}
 					?>
 					?>
-					</tbody>
-				</table>
+					<h4>Area Manager: <?=htmlspecialchars($area_manager)?></h4>
+
+					<table class="table">
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<col style="width: 20%" />
+						<thead>
+							<tr>
+								<th>Period</th>
+								<th>Tests</th>
+								<th>Passes</th>
+								<th>Fails</th>
+								<th>Rate</th>
+							</tr>
+						</thead>
+						<tbody>
+						<?php
+						$index = 0;
+						foreach($rows as $key => $data) {
+							$rate = ($data->testCount == 0) ? "0.00" : number_format(($data->passCount * 100) / $data->testCount, 2);
+							$label = $key_labels[$index];
+							?>
+							<tr>
+								<td><?=htmlspecialchars($label)?></td>
+								<td><?=number_format($data->testCount)?></td>
+								<td><?=number_format($data->passCount)?></td>
+								<td><?=number_format($data->failCount)?></td>
+								<td><?=$rate?>%</td>
+							</tr>
+							<?php
+							$index++;
+						}
+						?>
+						</tbody>
+					</table>
 
 
-				<?php
+					<?php
+
+				}
+			?>
+			</div>
+			<?php
+		}
 
 
-			}
 		?>
 		?>
-		</div>
 		<?php
 		<?php
 
 
 	}
 	}

+ 27 - 0
views/user_edit_form.php

@@ -34,6 +34,32 @@ var userId = <?=$user->id?>;
 						</select>
 						</select>
 					</div>
 					</div>
 				</div>
 				</div>
+
+				<?php
+
+				if($user->isCustomer()) {
+					?>
+					<div class="row mb-1">
+						<label class="col-sm-4 col-form-label"  for="customer-id">Customer</label>
+						<div class="col-sm">
+							<select class="form-control" name="customer_id" id="customer-id" />
+								<option value="">Unassigned</option>
+								<?php
+								foreach($customers as $customer) {
+									$selected = ($customer->id == $user_customer_link->customer_id) ? "selected" : "";
+									?>
+									<option value="<?=htmlspecialchars($customer->id)?>" <?=$selected?>><?=htmlspecialchars($customer->name)?></option>
+									<?php
+								}
+								?>
+							</select>
+						</div>
+					</div>
+					<?php
+				}
+
+				?>
+
 				<div class="row mb-1">
 				<div class="row mb-1">
 					<div class="col-sm offset-sm-4">
 					<div class="col-sm offset-sm-4">
 						<div class="form-check">
 						<div class="form-check">
@@ -207,6 +233,7 @@ var userId = <?=$user->id?>;
 					<?php
 					<?php
 				}
 				}
 				?>
 				?>
+
 			</div>
 			</div>
 		</div>
 		</div>
 		<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Submit</button>
 		<button class="btn btn-primary" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">Submit</button>