Răsfoiți Sursa

Add support for conditional templates

Alan Lam 2 ani în urmă
părinte
comite
9503df6209

+ 25 - 7
controllers/practice.php

@@ -5,10 +5,15 @@
  */
  */
 
 
 Core::addScript("public/js/app-practice-test.js");
 Core::addScript("public/js/app-practice-test.js");
+Core::addScript("public/js/app-test-requirements.js");
 
 
 $template = Template::get($id);
 $template = Template::get($id);
-if(!$template) throw new Exception(__("Practice form not found."));
-if(!$template->practice) throw new Exception(__("This practice form is no longer available."));
+if(!$template) {
+	throw new RuntimeException(__("Practice form not found."));
+}
+if(!$template->practice) {
+	throw new RuntimeException(__("This practice form is no longer available."));
+}
 
 
 $page_title = __("Practice");
 $page_title = __("Practice");
 
 
@@ -42,6 +47,7 @@ $is_confirmed = $test->getStatus() === Test::STATUS_CONFIRMED;
 $is_started = $test->getStatus() === Test::STATUS_STARTED;
 $is_started = $test->getStatus() === Test::STATUS_STARTED;
 $is_unverified = $test->getStatus() === Test::STATUS_PENDING_VERIFICATION;
 $is_unverified = $test->getStatus() === Test::STATUS_PENDING_VERIFICATION;
 
 
+$elements = [];
 $fields = [];
 $fields = [];
 
 
 $submit = http::value("submit");
 $submit = http::value("submit");
@@ -59,13 +65,13 @@ if($submit) {
 
 
 		if($is_confirmed) {
 		if($is_confirmed) {
 			// Parse before the test form
 			// Parse before the test form
-			$fields = $json->beforeTest;
+			$elements = $json->beforeTest;
 
 
 		}
 		}
 
 
 		if($is_started || $is_unverified) {
 		if($is_started || $is_unverified) {
 			// Parse after the test form
 			// Parse after the test form
-			$fields = $json->duringTest;
+			$elements = $json->duringTest;
 
 
 			// Start/End times
 			// Start/End times
 			if($is_started) {
 			if($is_started) {
@@ -92,7 +98,15 @@ if($submit) {
 			}
 			}
 		}
 		}
 
 
-		if(empty($fields)) throw new LogicException(__("No fields can be shown."));
+		foreach($elements as $element) {
+			if(isset($element->name)) {
+				$fields[] = $element;
+			}
+		}
+
+		if(empty($fields)) {
+			throw new LogicException(__("No fields can be shown."));
+		}
 
 
 		foreach($fields as $field) {
 		foreach($fields as $field) {
 
 
@@ -160,8 +174,12 @@ if($submit) {
 			else {
 			else {
 				$value = http::value($name, "trim");
 				$value = http::value($name, "trim");
 
 
-				if($required && $value === null) $form_exception->addError(__("%s is required.", $label));
-				if($values && !in_array($value, $values, false)) $form_exception->addError(__("Value for %s is invalid.", $label));
+				if($required && $value === null) {
+					$form_exception->addError(__("%s is required.", $label));
+				}
+				if($values && !in_array($value, $values, false)) {
+					$form_exception->addError(__("Value for %s is invalid.", $label));
+				}
 
 
 				if(in_array($name, Test::TEST_FIELDS, true)) {
 				if(in_array($name, Test::TEST_FIELDS, true)) {
 					$test->$name = $value;
 					$test->$name = $value;

+ 6 - 1
controllers/template.php

@@ -1,6 +1,11 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/**
+ * @var int $id
+ */
 
 
 Core::addScript("/public/js/app-test.js");
 Core::addScript("/public/js/app-test.js");
+Core::addScript("public/js/app-test-requirements.js");
 
 
 $template = Template::get($id);
 $template = Template::get($id);
 
 

+ 5 - 2
controllers/test.php

@@ -7,7 +7,9 @@
 Core::addScript("public/js/app-test.js");
 Core::addScript("public/js/app-test.js");
 
 
 $test = Test::get($id);
 $test = Test::get($id);
-if(!$test) throw new Exception(__("Test cannot be found."));
+if(!$test) {
+	throw new RuntimeException(__("Test cannot be found."));
+}
 
 
 $location = $test->getLocation();
 $location = $test->getLocation();
 $project = $test->getProject();
 $project = $test->getProject();
@@ -28,6 +30,7 @@ Core::addBreadcrumb("/account/tests", "Tests");
 Core::addBreadcrumb("/account/tests/$id", $page_title);
 Core::addBreadcrumb("/account/tests/$id", $page_title);
 
 
 if($user->isTester()) {
 if($user->isTester()) {
+	Core::addScript("public/js/app-test-requirements.js");
 	require ROOT_DIR . "/controllers/test_tester_form.php";
 	require ROOT_DIR . "/controllers/test_tester_form.php";
 }
 }
 else if($user->canVerifyTests()) {
 else if($user->canVerifyTests()) {
@@ -37,5 +40,5 @@ else if($user->canAccessTestResult()) {
 	HTTP::redirect("/account/tests/$id/result");
 	HTTP::redirect("/account/tests/$id/result");
 }
 }
 else {
 else {
-	throw new Exception(__("Invalid request."));
+	throw new RuntimeException(__("Invalid request."));
 }
 }

+ 42 - 47
engine/form.php

@@ -169,15 +169,23 @@ class Form {
             case "time":
             case "time":
                 self::renderTimeField($options);
                 self::renderTimeField($options);
                 break;
                 break;
-
 		}
 		}
-
 	}
 	}
 
 
 	private static function renderHeader($options): void {
 	private static function renderHeader($options): void {
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
 		?>
 		?>
-		<h3 <?=$hidden?>><?=htmlspecialchars($options->content)?></h3>
+		<h3 <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>"><?=htmlspecialchars($options->content)?></h3>
+		<?php
+	}
+
+	private static function renderRequiredBadge(bool $shown): void {
+		$hidden = $shown ? "" : "hidden";
+		?>
+		<span class="badge badge-danger badge-required" <?=$hidden?>>
+			<?=htmlspecialchars(__("Required"))?>
+		</span>
 		<?php
 		<?php
 	}
 	}
 
 
@@ -192,16 +200,14 @@ class Form {
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 		?>
 		?>
-		<div class="form-group" <?=$hidden?>>
+		<div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
 			<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
@@ -231,17 +237,15 @@ class Form {
 		$step = $options->step ?? 1;
 		$step = $options->step ?? 1;
 		$prepend_text = $options->prependText ?? "";
 		$prepend_text = $options->prependText ?? "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 
 
 		?>
 		?>
-		<div class="form-group" <?=$hidden?>>
+		<div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<div class="input-group">
 			<div class="input-group">
@@ -280,17 +284,15 @@ class Form {
 
 
 		$required = ($options->required ?? false) ? "required" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 
 
 		?>
 		?>
-		<div class="form-group">
+		<div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
 			<select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
@@ -322,16 +324,14 @@ class Form {
 		$id = str_replace("_", "-", $options->name);
 		$id = str_replace("_", "-", $options->name);
 		$required = ($options->required ?? false) ? "required" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 		?>
 		?>
-		<div class="form-group">
+		<div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
 			<input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
@@ -356,16 +356,14 @@ class Form {
 		$required = ($options->required ?? false) ? "required" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 		?>
 		?>
-		<div class="form-group">
+		<div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
 			<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="Enter text" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
@@ -382,8 +380,9 @@ class Form {
 
 
 	private static function renderHorizontalBar(object $options): void {
 	private static function renderHorizontalBar(object $options): void {
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
 	    ?>
 	    ?>
-        <hr <?=$hidden?> />
+        <hr <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" />
         <?php
         <?php
     }
     }
 
 
@@ -392,8 +391,9 @@ class Form {
 		$checked = !empty($options->value) ? "checked" : "";
 		$checked = !empty($options->value) ? "checked" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
+		$requirements = $options->requirements ?? [];
 		?>
 		?>
-		<div class="form-group">
+		<div class="form-group" data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
 			<div class="custom-control custom-switch">
 			<div class="custom-control custom-switch">
 				<input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1"  <?=$checked?> <?=$required?> />
 				<input type="checkbox" class="custom-control-input" id="<?=htmlspecialchars($id)?>" name="<?=htmlspecialchars($options->name)?>" value="1"  <?=$checked?> <?=$required?> />
 				<label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
 				<label for="<?=htmlspecialchars($id)?>" class="custom-control-label">
@@ -427,17 +427,14 @@ class Form {
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
 		$inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
 		$inline = ($options->type === "inline-radio") ? "custom-control-inline" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
 
 
 		?>
 		?>
-		<div class="form-group" <?=$hidden?>>
+		<div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>">
 			<label>
 			<label>
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				if($help_text) {
 				if($help_text) {
 					?>
 					?>
 					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
 					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
@@ -480,16 +477,14 @@ class Form {
 		$required = ($options->required ?? false) ? "required" : "";
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		$help_text = $options->helpText ?? false;
 		$hidden = !empty($options->hidden) ? "hidden" : "";
 		$hidden = !empty($options->hidden) ? "hidden" : "";
+		$requirements = $options->requirements ?? [];
+		$required_if_valid = !empty($options->requiredIfValid) ? "true" : "false";
 		?>
 		?>
-		<div class="form-group" <?=$hidden?>>
+		<div class="form-group" <?=$hidden?> data-requirements="<?=htmlspecialchars(json_encode($requirements))?>" data-required-if-valid="<?=$required_if_valid?>">
 			<label for="<?=$id?>">
 			<label for="<?=$id?>">
 				<?=htmlspecialchars($options->label)?>
 				<?=htmlspecialchars($options->label)?>
 				<?php
 				<?php
-				if($required) {
-					?>
-					<span class="badge badge-danger"><?=htmlspecialchars(__("Required"))?></span>
-					<?php
-				}
+				self::renderRequiredBadge($required);
 				?>
 				?>
 			</label>
 			</label>
 			<input class="form-control" type="time" name="<?=$options->name?>" id="<?=$id?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />
 			<input class="form-control" type="time" name="<?=$options->name?>" id="<?=$id?>" value="<?=htmlspecialchars($value)?>" <?=$required?> />

+ 3 - 2
engine/template.php

@@ -136,8 +136,9 @@ class Template extends Model {
 		$all_fields = array_merge($before_fields, $during_fields);
 		$all_fields = array_merge($before_fields, $during_fields);
 
 
 		foreach($all_fields as $field) {
 		foreach($all_fields as $field) {
-			if(empty($field->name)) continue;
-			$fields[$field->name] = $field;
+			if(isset($field->name)) {
+				$fields[$field->name] = $field;
+			}
 		}
 		}
 
 
 		return $fields;
 		return $fields;

+ 95 - 37
json/age_verification_greene_king.json

@@ -65,7 +65,13 @@
 			"name": "security_staff_challenge",
 			"name": "security_staff_challenge",
 			"label": "Did security staff ask for ID?",
 			"label": "Did security staff ask for ID?",
 			"values": ["0", "1"],
 			"values": ["0", "1"],
-			"displayStrings": ["No", "Yes"]
+			"displayStrings": ["No", "Yes"],
+			"requirements": [
+				{
+					"fieldName": "security_staff_present",
+					"validValues": ["1"]
+				}
+			]
 		},
 		},
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
@@ -74,7 +80,6 @@
 			"values": ["0", "1"],
 			"values": ["0", "1"],
 			"displayStrings": ["No", "Yes"]
 			"displayStrings": ["No", "Yes"]
 		},
 		},
-
 		{
 		{
 			"type": "hr"
 			"type": "hr"
 		},
 		},
@@ -98,7 +103,6 @@
 			"maxLength": 400,
 			"maxLength": 400,
 			"helpText": "Describe how the challenge took place. For example: \"While entering I was approached politely and asked for ID.\""
 			"helpText": "Describe how the challenge took place. For example: \"While entering I was approached politely and asked for ID.\""
 		},
 		},
-
 		{
 		{
 			"type": "hr"
 			"type": "hr"
 		},
 		},
@@ -110,9 +114,15 @@
 		{
 		{
 			"type": "text",
 			"type": "text",
 			"name": "challenger_name",
 			"name": "challenger_name",
-			"label": "What was the name of the staff member that challenged you? (If applicable)",
+			"label": "What was the name of the staff member that challenged you?",
 			"maxLength": 32,
 			"maxLength": 32,
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [
+				{
+					"fieldName": "challenge_stage",
+					"validValues": ["1", "2", "3", "4"]
+				}
+			]
 		},
 		},
 		{
 		{
 			"type": "textarea",
 			"type": "textarea",
@@ -125,10 +135,16 @@
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
 			"name": "challenger_gender",
 			"name": "challenger_gender",
-			"label": "What was the the staff member's gender? (If applicable)",
+			"label": "What was the the staff member's gender?",
 			"values": ["", "M", "F"],
 			"values": ["", "M", "F"],
 			"displayStrings": ["No challenge", "Male", "Female"],
 			"displayStrings": ["No challenge", "Male", "Female"],
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [
+				{
+					"fieldName": "challenge_stage",
+					"validValues": ["1", "2", "3", "4"]
+				}
+			]
 		},
 		},
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
@@ -136,23 +152,31 @@
 			"label": "Was there eye contact with the staff member?",
 			"label": "Was there eye contact with the staff member?",
 			"values": ["", "0", "1"],
 			"values": ["", "0", "1"],
 			"displayStrings": ["No challenge", "No", "Yes"],
 			"displayStrings": ["No challenge", "No", "Yes"],
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [
+				{
+					"fieldName": "challenge_stage",
+					"validValues": ["1", "2", "3", "4"]
+				}
+			]
 		},
 		},
-
 		{
 		{
 			"type": "hr"
 			"type": "hr"
 		},
 		},
 
 
 		{
 		{
 			"type": "header",
 			"type": "header",
-			"content": "Play Details"
+			"content": "Play Details",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}]
 		},
 		},
 		{
 		{
 			"type": "text",
 			"type": "text",
 			"name": "play_machine",
 			"name": "play_machine",
 			"label": "What was the name of the machine that you played?",
 			"label": "What was the name of the machine that you played?",
 			"maxLength": 20,
 			"maxLength": 20,
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
 			"type": "text",
 			"type": "text",
@@ -160,30 +184,38 @@
 			"label": "Where was the machine located in the venue?",
 			"label": "Where was the machine located in the venue?",
 			"maxLength": 100,
 			"maxLength": 100,
 			"customerExport": false,
 			"customerExport": false,
-			"helpText": "For example: Opposite the bar."
+			"helpText": "For example: Opposite the bar.",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
-			"type": "radio",
+			"type": "select",
 			"name": "play_jackpot",
 			"name": "play_jackpot",
 			"label": "What was the machine's jackpot value?",
 			"label": "What was the machine's jackpot value?",
 			"values": ["", 5, 10, 15, 20, 25, 35, 50, 100, 500],
 			"values": ["", 5, 10, 15, 20, 25, 35, 50, 100, 500],
-			"displayStrings": ["Did not play", "£5", "£10", "£15", "£20", "£25", "£35", "£50", "£100", "£500"],
-			"customerExport": false
+			"displayStrings": ["Please select", "£5", "£10", "£15", "£20", "£25", "£35", "£50", "£100", "£500"],
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
-			"type": "radio",
+			"type": "select",
 			"name": "play_machine_stake",
 			"name": "play_machine_stake",
 			"label": "What was the stake played?",
 			"label": "What was the stake played?",
 			"values": ["", 0.1, 0.2, 0.25, 0.3, 0.5, 1, 2],
 			"values": ["", 0.1, 0.2, 0.25, 0.3, 0.5, 1, 2],
-			"displayStrings": ["Did not play", "10p", "20p", "25p", "30p", "50p", "£1", "£2"],
-			"helpText": "This is how much the machine charges you per spin."
+			"displayStrings": ["Please select", "10p", "20p", "25p", "30p", "50p", "£1", "£2"],
+			"helpText": "This is how much the machine charges you per spin.",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
-			"type": "radio",
+			"type": "select",
 			"name": "play_minutes",
 			"name": "play_minutes",
 			"label": "How long did you play? (Minutes)",
 			"label": "How long did you play? (Minutes)",
 			"values": ["", "1", "2", "3", "4", "5"],
 			"values": ["", "1", "2", "3", "4", "5"],
-			"displayStrings": ["Did not play", "1 minute", "2 minutes", "3 minutes", "4 minutes", "5 minutes or more"]
+			"displayStrings": ["Please select", "1 minute", "2 minutes", "3 minutes", "4 minutes", "5 minutes or more"],
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
 			"type": "number",
 			"type": "number",
@@ -194,24 +226,27 @@
 			"max": 2,
 			"max": 2,
 			"step": 0.01,
 			"step": 0.01,
 			"prependText": "£",
 			"prependText": "£",
-			"helpText": "For example: Enter 1.50 if you have spent £1.50."
+			"helpText": "For example: Enter 1.50 if you have spent £1.50.",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}],
+			"requiredIfValid": true
 		},
 		},
-
 		{
 		{
-			"type": "hr"
+			"type": "hr",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["3", "4"]}]
 		},
 		},
 
 
 		{
 		{
 			"type": "header",
 			"type": "header",
-			"content": "Bar Details"
+			"content": "Bar Details",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
-
 		{
 		{
 			"type": "time",
 			"type": "time",
 			"name": "bar_time",
 			"name": "bar_time",
 			"label": "What time did you arrive at the bar?",
 			"label": "What time did you arrive at the bar?",
 			"maxLength": 32,
 			"maxLength": 32,
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 
 
 		{
 		{
@@ -219,14 +254,16 @@
 			"name": "bar_staff_name",
 			"name": "bar_staff_name",
 			"label": "What is the bartender's name?",
 			"label": "What is the bartender's name?",
 			"maxLength": 32,
 			"maxLength": 32,
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 		{
 		{
 			"type": "textarea",
 			"type": "textarea",
 			"name": "bar_staff_description",
 			"name": "bar_staff_description",
 			"label": "Description of the bartender and their interactions",
 			"label": "Description of the bartender and their interactions",
 			"maxLength": 250,
 			"maxLength": 250,
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
@@ -234,7 +271,8 @@
 			"label": "What gender was the bartender?",
 			"label": "What gender was the bartender?",
 			"values": ["M", "F", "O"],
 			"values": ["M", "F", "O"],
 			"displayStrings": ["Male", "Female", "Other"],
 			"displayStrings": ["Male", "Female", "Other"],
-			"customerExport": false
+			"customerExport": false,
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 
 
 		{
 		{
@@ -242,20 +280,30 @@
 			"name": "bar_age_verification",
 			"name": "bar_age_verification",
 			"label": "Were you asked for ID?",
 			"label": "Were you asked for ID?",
 			"values": ["0", "1"],
 			"values": ["0", "1"],
-			"displayStrings": ["No", "Yes"]
+			"displayStrings": ["No", "Yes"],
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
 			"name": "alcohol_purchased",
 			"name": "alcohol_purchased",
 			"label": "Were you served alcohol?",
 			"label": "Were you served alcohol?",
 			"values": ["0", "1"],
 			"values": ["0", "1"],
-			"displayStrings": ["No", "Yes"]
+			"displayStrings": ["No", "Yes"],
+			"requirements": [
+				{"fieldName": "challenge_stage", "validValues": ["5"]}
+			],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
 			"type": "text",
 			"type": "text",
 			"name": "bar_purchase_item",
 			"name": "bar_purchase_item",
 			"label": "What were you served?",
 			"label": "What were you served?",
-			"maxLength": 20
+			"maxLength": 20,
+			"requirements": [
+				{"fieldName": "challenge_stage", "validValues": ["5"]},
+				{"fieldName": "alcohol_purchased", "validValues": ["1"]}
+			],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
 			"type": "number",
 			"type": "number",
@@ -266,22 +314,33 @@
 			"max": 20,
 			"max": 20,
 			"step": 0.01,
 			"step": 0.01,
 			"prependText": "£",
 			"prependText": "£",
-			"helpText": "For example: Enter 1.50 if you have spent £1.50."
+			"helpText": "For example: Enter 1.50 if you have spent £1.50.",
+			"requirements": [
+				{"fieldName": "challenge_stage", "validValues": ["5"]},
+				{"fieldName": "alcohol_purchased", "validValues": ["1"]}
+			],
+			"requiredIfValid": true
 		},
 		},
 		{
 		{
 			"type": "file",
 			"type": "file",
 			"label": "Photo of receipt",
 			"label": "Photo of receipt",
-			"name": "bar_receipt"
+			"name": "bar_receipt",
+			"requirements": [
+				{"fieldName": "challenge_stage", "validValues": ["5"]},
+				{"fieldName": "alcohol_purchased", "validValues": ["1"]}
+			],
+			"requiredIfValid": true
 		},
 		},
-
 		{
 		{
-			"type": "hr"
+			"type": "hr",
+			"requirements": [{"fieldName": "challenge_stage", "validValues": ["5"]}]
 		},
 		},
 
 
 		{
 		{
 			"type": "header",
 			"type": "header",
 			"content": "Signage Details"
 			"content": "Signage Details"
 		},
 		},
+
 		{
 		{
 			"type": "inline-radio",
 			"type": "inline-radio",
 			"name": "citizen_card_brochure_displayed",
 			"name": "citizen_card_brochure_displayed",
@@ -320,7 +379,6 @@
 			"maxLength": 200,
 			"maxLength": 200,
 			"helpText": "Describe the location of the poster that was displayed."
 			"helpText": "Describe the location of the poster that was displayed."
 		},
 		},
-
 		{
 		{
 			"type": "hr"
 			"type": "hr"
 		},
 		},

+ 68 - 0
public/js/app-test-requirements.js

@@ -0,0 +1,68 @@
+class AppTestRequirements {
+	constructor() {
+		this.$form = $("#test-form");
+		this.$form.on("change", () => this.onChange())
+
+		this.$elements = this.$form.find("[data-requirements]");
+
+		this.$form.trigger("change");
+	}
+
+	onChange() {
+		this.$elements.each((index, element) => {
+			const $el = $(element);
+			const { requirements, requiredIfValid } = $el.data();
+
+			const $field = $el.find("[name]");
+
+			if(requirements && requirements.length > 0) {
+				let requirementsMet = true;
+				let value;
+
+				requirements.forEach(requirement => {
+					const { fieldName, validValues } = requirement;
+
+					const isCheckboxRadio = this.$form.find(`input[name=${fieldName}][type=checkbox], input[name=${fieldName}][type=radio]`).length > 0;
+					let selector;
+
+					if(isCheckboxRadio) {
+						selector = `input[name=${fieldName}]:checked`;
+					}
+					else {
+						selector = `[name=${fieldName}]`;
+					}
+
+					value = this.$form.find(selector).val();
+
+					if(validValues.indexOf(value) === -1) {
+						requirementsMet = false;
+					}
+
+					console.log(`${fieldName} ${selector} is ${value}`);
+				});
+
+				if(!requirementsMet) {
+					$el.slideUp("fast");
+					$field.prop("required", false);
+					$el.find(".badge-required").prop("hidden", true);
+				}
+				else {
+					$el.slideDown("fast");
+					if(requiredIfValid) {
+						$field.prop("required", true);
+						$el.find(".badge-required").prop("hidden", false);
+					}
+					else {
+						$el.find(".badge-required").prop("hidden", true);
+					}
+				}
+
+			}
+
+		})
+	}
+}
+
+$(() => {
+	new AppTestRequirements();
+});

+ 0 - 7
views/practice_form.php

@@ -29,18 +29,11 @@ else {
 
 
 	// View submitted data
 	// View submitted data
 	foreach($fields as $field) {
 	foreach($fields as $field) {
-
 		$name = $field->name ?? false;
 		$name = $field->name ?? false;
 		if($name) {
 		if($name) {
 			$field->value = in_array($name, Test::TEST_FIELDS) ? $test->$name : ($values[$name] ?? null);
 			$field->value = in_array($name, Test::TEST_FIELDS) ? $test->$name : ($values[$name] ?? null);
 		}
 		}
 
 
-		if($field->type === "header") {
-			?>
-			<hr />
-			<?php
-		}
-
 		if($field->type === "file") {
 		if($field->type === "file") {
 			?>
 			?>
 			<div class="row mb-1">
 			<div class="row mb-1">

+ 0 - 1
views/test_form.php

@@ -32,7 +32,6 @@ else if($test->isPendingVerification()) {
 	<form method="post" id="test-form">
 	<form method="post" id="test-form">
 	<?php
 	<?php
 	foreach($fields as $field) {
 	foreach($fields as $field) {
-
 		$name = $field->name ?? false;
 		$name = $field->name ?? false;
 		if($name) {
 		if($name) {
 			if(isset($test->$name)) $field->value = $test->$name;
 			if(isset($test->$name)) $field->value = $test->$name;