Jelajahi Sumber

Add support for toggleable fields in a template (template keys)

Alan Lam 6 tahun lalu
induk
melakukan
1afacad9ad

+ 0 - 2
controllers/practice.php

@@ -198,5 +198,3 @@ if($is_unverified) {
 View::includeOnce("header", compact("page_title"));
 View::include("practice_form", compact("test", "template", "fields"));
 View::includeOnce("footer");
-
-?>

+ 1 - 0
controllers/project_add.php

@@ -33,6 +33,7 @@ if($submit) {
 		Core::validateCSRFToken($submit);
 
 		$project->template_id = http::value("template_id", "int");
+		$project->setTemplateKeys(HTTP::value("template_keys"));
 		$project->name = http::value("name", "trim");
 		$project->test_count = http::value("test_count", "int");
 		$project->min_age = http::value("min_age", "int");

+ 1 - 0
controllers/project_edit.php

@@ -34,6 +34,7 @@ if($submit) {
 		$location_ids_to_delete = array_diff($location_ids, $new_location_ids);
 
 		$project->template_id = http::value("template_id", "int");
+		$project->setTemplateKeys(HTTP::value("template_keys"));
 		$project->name = http::value("name", "trim");
 		$project->test_count = http::value("test_count", "int");
 		$project->min_age = http::value("min_age", "int");

+ 10 - 3
controllers/template.php

@@ -4,6 +4,15 @@ Core::addScript("/public/js/app-test.js");
 
 $template = Template::get($id);
 
+$keys = HTTP::value("keys");
+
+if(is_array($keys)) {
+	$template->setKeys($keys);
+}
+else {
+	$keys = [];
+}
+
 if(!Core::$user->hasPermission($template)) throw new Exception("You do not have permission to access this page.");
 
 $page_title = $template->name;
@@ -19,7 +28,5 @@ if(HTTP::isAJAX()) {
 }
 
 View::includeOnce("header", compact("page_title"));
-View::include("template", compact("template"));
+View::include("template", compact("template", "keys"));
 View::includeOnce("footer");
-
-?>

+ 3 - 3
controllers/test.php

@@ -20,9 +20,9 @@ $tester = $test->getTester();
 $template = $project->getTemplate();
 
 $template_overrides = $project->getTemplateOverrides();
-if($template_overrides) {
-	$template->setOverrides($template_overrides);
-}
+if($template_overrides) $template->setOverrides($template_overrides);
+
+$template->setKeys($project->getTemplateKeys());
 
 $page_title = $location->name;
 

+ 9 - 0
controllers/test_pdf.php

@@ -11,6 +11,7 @@ $project = $test->getProject();
 $customer = $project->getCustomer();
 
 $template = $project->getTemplate();
+$template->setKeys($project->getTemplateKeys());
 $template->setOverrides($project->getTemplateOverrides());
 
 $template_parts = json_decode($template->getJSON());
@@ -86,6 +87,7 @@ ob_start()
 			body {
 				font-family: sans-serif;
 				font-size: 0.75rem;
+				line-height: 1.5;
 			}
 
 			a {
@@ -96,6 +98,13 @@ ob_start()
 				border-bottom: 1px solid #eee;
 			}
 
+			hr {
+				color: #eee;
+				border-color: #eee;
+				background: transparent;
+				margin: 4px 0;
+			}
+
 			.section {
 				background: #e2f0d9;
 				color: #385723;

+ 11 - 0
db/migrations/20200606124116_add_template_keys_field_to_project_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddTemplateKeysFieldToProjectTable extends AbstractMigration {
+	public function change() {
+		$this->table("project")
+			->addColumn("template_keys", "string", ["after" => "template_id", "null" => true])
+			->update();
+	}
+}

+ 147 - 69
engine/form.php

@@ -157,17 +157,36 @@ class Form {
 	 */
 	public static function renderField(object $options) {
 
+		$required_key = $options->requiredKey ?? null;
+
+		/** @var Template $template */
+		$template = $options->template ?? null;
+
+		if($required_key && $template) {
+			$keys = $template->getKeys();
+			if(!in_array($required_key, $keys)) return;
+		}
+
 		switch($options->type) {
 
 			case "header":
 				self::renderHeader($options);
 				break;
 
+			case "checkbox":
+				self::renderCheckboxField($options);
+				break;
+
 			case "dropdown":
 			case "select":
 				self::renderSelectField($options);
 				break;
 
+			case "radio":
+			case "inline-radio":
+				self::renderRadioField($options);
+				break;
+
 			case "text":
 				self::renderTextField($options);
 				break;
@@ -210,18 +229,16 @@ class Form {
 		$help_text = $options->helpText ?? false;
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		?>
-		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
-			<div class="col-sm">
-				<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
-				<?php
-				if($help_text) {
-					?>
-					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
-					<?php
-				}
+		<div class="form-group">
+			<label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<input class="form-control" type="text" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" <?=$required?> <?=$max_length?> />
+			<?php
+			if($help_text) {
 				?>
-			</div>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
 		</div>
 		<?php
 	}
@@ -243,29 +260,27 @@ class Form {
 		$prepend_text = $options->prependText ?? "";
 
 		?>
-		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
-			<div class="col-sm">
-				<div class="input-group">
-					<?php
-					if(!empty($prepend_text)) {
-						?>
-						<div class="input-group-prepend">
-							<span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
-						</div>
-						<?php
-					}
-					?>
-					<input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
-				</div>
+		<div class="form-group">
+			<label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<div class="input-group">
 				<?php
-				if($help_text) {
+				if(!empty($prepend_text)) {
 					?>
-					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<div class="input-group-prepend">
+						<span class="input-group-text"><?=htmlspecialchars($prepend_text)?></span>
+					</div>
 					<?php
 				}
 				?>
+				<input class="form-control" type="number" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" value="<?=htmlspecialchars($value)?>" step="<?=$step?>" <?=$required?> <?=$min?> <?=$max?> <?=$max_length?> />
 			</div>
+			<?php
+			if($help_text) {
+				?>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
 		</div>
 		<?php
 	}
@@ -281,28 +296,26 @@ class Form {
 		$help_text = $options->helpText ?? false;
 
 		?>
-		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
-			<div class="col-sm">
-				<select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
-				<?php
-				foreach($options->values as $index => $value) {
-					$display = $options->displayStrings[$index];
-					$selected = ($selected_value === strval($value)) ? "selected" : "";
-					?>
-					<option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
-					<?php
-				}
+		<div class="form-group">
+			<label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<select class="form-control" name="<?=$options->name?>" id="<?=$id?>" <?=$required?>>
+			<?php
+			foreach($options->values as $index => $value) {
+				$display = $options->displayStrings[$index];
+				$selected = ($selected_value === strval($value)) ? "selected" : "";
 				?>
-				</select>
+				<option value="<?=htmlspecialchars($value)?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
 				<?php
-				if($help_text) {
-					?>
-					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
-					<?php
-				}
+			}
+			?>
+			</select>
+			<?php
+			if($help_text) {
 				?>
-			</div>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
 		</div>
 		<?php
 	}
@@ -315,18 +328,16 @@ class Form {
 		$required = ($options->required ?? false) ? "required" : "";
 		$help_text = $options->helpText ?? false;
 		?>
-		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
-			<div class="col-sm">
-				<input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
-				<?php
-				if($help_text) {
-					?>
-					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
-					<?php
-				}
+		<div class="form-group">
+			<label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<input class="form-control-file" type="file" name="<?=$options->name?>" id="<?=$id?>" accept="image/*" <?=$required?> />
+			<?php
+			if($help_text) {
 				?>
-			</div>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
 		</div>
 		<?php
 	}
@@ -343,18 +354,16 @@ class Form {
 		$help_text = $options->helpText ?? false;
 		$max_length = ($options->maxLength ?? false) ? "maxlength=\"$options->maxLength\"" : "";
 		?>
-		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label" for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
-			<div class="col-sm">
-				<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
-				<?php
-				if($help_text) {
-					?>
-					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
-					<?php
-				}
+		<div class="form-group">
+			<label for="<?=$id?>"><?=htmlspecialchars($options->label)?></label>
+			<textarea class="form-control" name="<?=$options->name?>" id="<?=$id?>" placeholder="<?=htmlspecialchars($placeholder)?>" rows="4" <?=$required?> <?=$max_length?>><?=htmlspecialchars($value)?></textarea>
+			<?php
+			if($help_text) {
 				?>
-			</div>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
 		</div>
 		<?php
 	}
@@ -365,6 +374,75 @@ class Form {
         <?php
     }
 
+	private static function renderCheckboxField(object $options) {
+		$id = str_replace("_", "-", $options->name);
+		$checked = !empty($options->value) ? "checked" : "";
+		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
+		?>
+		<div class="form-group">
+			<div class="custom-control custom-switch">
+				<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">
+					<?=htmlspecialchars($options->label)?>
+				</label>
+			</div>
+			<?php
+			if($help_text) {
+				?>
+				<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+				<?php
+			}
+			?>
+		</div>
+		<?php
+
+	}
+
+	private static function renderRadioField(object $options) {
+		$id = str_replace("_", "-", $options->name);
+		$current_value = $options->value ?? http::value($options->name);
+		$required = ($options->required ?? false) ? "required" : "";
+		$help_text = $options->helpText ?? false;
+		$inline = ($options->type == "inline-radio") ? "custom-control-inline" : "";
+
+		?>
+		<div class="form-group">
+			<label>
+				<?=htmlspecialchars($options->label)?>
+				<?php
+				if($help_text) {
+					?>
+					<small class="form-text text-muted"><?=htmlspecialchars($help_text)?></small>
+					<?php
+				}
+				?>
+			</label>
+			<br />
+			<?php
+			foreach($options->values as $index => $value) {
+				$display = $options->displayStrings[$index];
+				$checked = ($current_value === strval($value)) ? "checked" : "";
+				$radio_id = sprintf("%s-%s", $id, $value);
+
+				if($index === 0 && $current_value === null) {
+					$checked = "checked";
+				}
+
+				?>
+				<div class="custom-control custom-radio <?=$inline?>">
+					<input id="<?=$radio_id?>" name="<?=$options->name?>" type="radio" class="custom-control-input" value="<?=htmlspecialchars($value)?>" <?=$checked?> <?=$required?> />
+					<label for="<?=$radio_id?>" class="custom-control-label">
+						<?=htmlspecialchars($display)?>
+					</label>
+				</div>
+				<?php
+			}
+			?>
+		</div>
+		<?php
+	}
+
 }
 
 ?>

+ 3 - 3
engine/http.php

@@ -141,20 +141,20 @@ class HTTP {
 				break;
 		}
 
-		if(!isset($array[$key])) return false;
+		if(!isset($array[$key])) return null;
 
 		$value = $array[$key];
 
 		switch($validation) {
 
 			case "int": {
-				if(!is_array($value) && strlen($value)===0) return false;
+				if(!is_array($value) && strlen($value)===0) return null;
 				return is_array($value) ? array_map("intval", $value) : intval($value);
 			}
 
 			case "double":
 			case "float": {
-				if(strlen($value)===0) return false;
+				if(strlen($value)===0) return null;
 				return floatval($value);
 			}
 

+ 12 - 1
engine/project.php

@@ -4,6 +4,7 @@
  * @property int $id
  * @property int $customer_id
  * @property int $template_id
+ * @property string $template_keys
  * @property string $name
  * @property int $test_count
  * @property int $min_age
@@ -30,7 +31,7 @@ class Project extends Model {
 
 	protected static $table = "project";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
+	protected static $fillable_fields = ["customer_id", "template_id", "template_keys", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
 
 	/**
 	 * @var Test[] $tests
@@ -500,4 +501,14 @@ class Project extends Model {
 		return $items;
 	}
 
+	public function setTemplateKeys($keys) {
+		if(!is_array($keys)) $keys = [];
+		$this->template_keys = json_encode($keys);
+	}
+
+	public function getTemplateKeys(): array {
+		$keys = json_decode($this->template_keys);
+		return $keys ?: [];
+	}
+
 }

+ 105 - 88
engine/template.php

@@ -19,7 +19,26 @@ class Template extends Model {
 
 	private $overrides = [];
 
+	/** @var string[] Render fields with these keys otherwise skip */
+	private $keys = [];
+
 	// Object methods
+	public function setKeys($keys) {
+		$this->keys = $keys;
+	}
+
+	public function getKeys(): array {
+		return $this->keys;
+	}
+
+	/**
+	 * Returns possible template keys this template has
+	 * @return string[]
+	 */
+	public function getTemplateKeys(): array {
+		$fields = $this->getFields();
+		return array_unique(array_column($fields, "requiredKey"));
+	}
 
 	/**
 	 * @param string $processing_file
@@ -203,6 +222,7 @@ class Template extends Model {
 			<?php
 			foreach($fields as $field) {
 				$field = $this->applyOverridesToField($field);
+				$field->template = $this;
 				Form::renderField($field);
 			}
 			?>
@@ -239,6 +259,7 @@ class Template extends Model {
 				<?php
 				foreach($fields as $field) {
 					$field = $this->applyOverridesToField($field);
+					$field->template = $this;
 					Form::renderField($field);
 				}
 				?>
@@ -249,98 +270,94 @@ class Template extends Model {
 
 				<p>Please confirm your entry and exit times below. Please try to be as accurate as possible.</p>
 
-				<div class="row mb-1">
-					<label class="col-sm-4 col-form-label" for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
-					<div class="col-sm">
-						<div class="row">
-							<div class="col-lg">
-								<select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
-									<option value="">HH</option>
-									<?php
-									Form::renderHourOptions();
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
-									<option value="">MM</option>
-									<?php
-									Form::renderMinuteOptions();
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="start_override_second" id="start-override-second">
-									<option value="">SS</option>
-									<?php
-									Form::renderSecondOptions(0);
-									?>
-								</select>
-							</div>
-							<div class="col-lg">
-								<select class="form-control w-auto d-inline-block" name="start_override_day" id="start-override-day">
-									<option value="">Day</option>
-									<?php
-									Form::renderDayOptions(date("j"));
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="start_override_month" id="start-override-month">
-									<option value="">Month</option>
-									<?php
-									Form::renderMonthOptions(date("n"));
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
-									<option value="">Year</option>
-									<?php
-									Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
-									?>
-								</select>
-							</div>
+				<div class="form-group">
+					<label for="start-override-hour">Entry Time <span class="badge badge-danger">Required</span></label>
+					<div class="form-row">
+						<div class="col-lg-auto">
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
+								<option value="">HH</option>
+								<?php
+								Form::renderHourOptions();
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_minute" id="start-override-minute">
+								<option value="">MM</option>
+								<?php
+								Form::renderMinuteOptions();
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_second" id="start-override-second">
+								<option value="">SS</option>
+								<?php
+								Form::renderSecondOptions(0);
+								?>
+							</select>
+						</div>
+						<div class="col-lg-auto">
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_day" id="start-override-day">
+								<option value="">Day</option>
+								<?php
+								Form::renderDayOptions(date("j"));
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_month" id="start-override-month">
+								<option value="">Month</option>
+								<?php
+								Form::renderMonthOptions(date("n"));
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="start_override_year" id="start-override-year">
+								<option value="">Year</option>
+								<?php
+								Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
+								?>
+							</select>
 						</div>
 					</div>
 				</div>
 
-				<div class="row mb-1">
-					<label class="col-sm-4 col-form-label" for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
-					<div class="col-sm">
-						<div class="row">
-							<div class="col-lg">
-								<select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
-									<option value="">HH</option>
-									<?php
-									Form::renderHourOptions();
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
-									<option value="">MM</option>
-									<?php
-									Form::renderMinuteOptions();
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="end_override_second" id="end-override-second">
-									<option value="">SS</option>
-									<?php
-									Form::renderSecondOptions(0);
-									?>
-								</select>
-							</div>
-							<div class="col-lg">
-								<select class="form-control w-auto d-inline-block" name="end_override_day" id="end-override-day">
-									<option value="">Day</option>
-									<?php
-									Form::renderDayOptions(date("j"));
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="end_override_month" id="end-override-month">
-									<option value="">Month</option>
-									<?php
-									Form::renderMonthOptions(date("n"));
-									?>
-								</select>
-								<select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
-									<option value="">Year</option>
-									<?php
-									Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
-									?>
-								</select>
-							</div>
+				<div class="form-group">
+					<label for="end-override-hour">Exit Time <span class="badge badge-danger">Required</span></label>
+					<div class="form-row">
+						<div class="col-lg-auto">
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
+								<option value="">HH</option>
+								<?php
+								Form::renderHourOptions();
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_minute" id="end-override-minute">
+								<option value="">MM</option>
+								<?php
+								Form::renderMinuteOptions();
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_second" id="end-override-second">
+								<option value="">SS</option>
+								<?php
+								Form::renderSecondOptions(0);
+								?>
+							</select>
+						</div>
+						<div class="col-lg-auto">
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_day" id="end-override-day">
+								<option value="">Day</option>
+								<?php
+								Form::renderDayOptions(date("j"));
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_month" id="end-override-month">
+								<option value="">Month</option>
+								<?php
+								Form::renderMonthOptions(date("n"));
+								?>
+							</select>
+							<select class="form-control form-control-sm w-auto d-inline-block" name="end_override_year" id="end-override-year">
+								<option value="">Year</option>
+								<?php
+								Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
+								?>
+							</select>
 						</div>
 					</div>
 				</div>

+ 9 - 1
engine/test_pdf.php

@@ -8,9 +8,17 @@ class TestPDF {
 	 */
 	public static function getValueHtml(?string $value): string {
 		if(empty($value)) {
-			return "<span class=\"text-muted\">N/A</span>";
+			return "<span class=\"text-muted\">None</span>";
 		}
 		return htmlspecialchars($value);
 	}
 
+	public static function getBooleanHtml($value): string {
+		return $value ? "Yes" : "No";
+	}
+
+	public static function getNumericHtml($value): string {
+		return number_format($value);
+	}
+
 }

+ 90 - 14
json/mystery_shop.json

@@ -50,21 +50,56 @@
 			"content": "Venue Details"
 		},
 		{
-			"type": "dropdown",
+			"type": "radio",
 			"name": "cleanliness",
 			"label": "Cleanliness",
-			"values": ["", 1, 2, 3, 4, 5],
-			"displayStrings": ["Select", "Very dirty", "Dirty", "Adequate", "Clean", "Very clean"],
+			"values": [1, 2, 3, 4, 5],
+			"displayStrings": ["Very dirty", "Dirty", "Adequate", "Clean", "Very clean"],
 			"required": true
 		},
 
+		{
+			"type": "inline-radio",
+			"name": "entrance_hand_sanitiser_count",
+			"label": "Hand Sanitisers at Entrance",
+			"values": [0, 1, 2, 3],
+			"displayStrings": ["0", "1", "2", "3+"],
+			"required": true,
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "face_mask_offered",
+			"label": "Face Mask Offered",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "gloves_offered",
+			"label": "Gloves Offered",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "social_distancing_signage",
+			"label": "Signage for Social Distancing",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+
+
 		{
 			"type": "header",
 			"content": "Staff Details"
 		},
 
 		{
-			"type": "dropdown",
+			"type": "inline-radio",
 			"name": "staff_badge_worn",
 			"label": "Staff Badge Worn",
 			"values": ["", "0", "1"],
@@ -79,10 +114,10 @@
 		},
 
 		{
-			"type": "dropdown",
+			"type": "inline-radio",
 			"name": "staff_offered_drink",
 			"label": "Drink Offered",
-			"values": ["0", "1"],
+			"values": [0, 1],
 			"displayStrings": ["No", "Yes"]
 		},
 
@@ -95,10 +130,10 @@
 		},
 
 		{
-			"type": "dropdown",
+			"type": "inline-radio",
 			"name": "staff_offered_promotion",
 			"label": "Promotion Offered",
-			"values": ["0", "1"],
+			"values": [0, 1],
 			"displayStrings": ["No", "Yes"]
 		},
 
@@ -111,26 +146,26 @@
 		},
 
 		{
-			"type": "dropdown",
+			"type": "inline-radio",
 			"name": "staff_help",
 			"label": "Machine Explanation",
-			"values": ["0", "1"],
+			"values": [0, 1],
 			"displayStrings": ["No", "Yes"],
 			"helpText": "Did staff explain how the machines work and how to play them?"
 		},
 		{
-			"type": "dropdown",
+			"type": "radio",
 			"name": "staff_behaviour",
 			"label": "Staff Behaviour",
 			"values": ["", 1, 2, 3, 4, 5],
-			"displayStrings": ["N/A", "Rude", "Slightly rude", "Neither rude or polite", "Polite and friendly","Very polite and friendly"]
+			"displayStrings": ["No interaction", "Rude", "Slightly rude", "Neither rude or polite", "Polite and friendly","Very polite and friendly"]
 		},
 
 		{
-			"type": "dropdown",
+			"type": "inline-radio",
 			"name": "staff_uniform",
 			"label": "Staff Uniform",
-			"values": ["0", "1"],
+			"values": [0, 1],
 			"displayStrings": ["No", "Yes"]
 		},
 
@@ -142,6 +177,47 @@
 			"helpText": "For example: \"They were in uniform but they were wearing trainers.\""
 		},
 
+		{
+			"type": "inline-radio",
+			"name": "staff_wearing_gloves",
+			"label": "Staff Wearing Gloves",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "staff_wearing_face_mask",
+			"label": "Staff Wearing Face Mask",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "staff_wearing_face_protection",
+			"label": "Staff Wearing Face Protection",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "inline-radio",
+			"name": "staff_offered_cleaning",
+			"label": "Staff Offered Cleaning Before Playing",
+			"values": [0, 1],
+			"displayStrings": ["No", "Yes"],
+			"requiredKey": "COVID-19"
+		},
+		{
+			"type": "textarea",
+			"name": "social_distancing_notes",
+			"label": "Additional Notes on Social Distancing",
+			"maxLength": 400,
+			"requiredKey": "COVID-19"
+		},
+
+
 		{
 			"type": "header",
 			"content": "Play Details"

File diff ditekan karena terlalu besar
+ 3 - 3
public/css/bootstrap.min.css


+ 39 - 3
public/js/app-project-form.js

@@ -46,8 +46,44 @@ $(function() {
 
 	initialiseTooltips();
 
+	const $templateDropdown = $("#template-id");
+
 	$("#template-id, #time-of-day, #date-start-month, #date-start-year").on("change", () => autogenProjectName());
-	autogenProjectName();
+	$templateDropdown.on("change", () => {
+		const keys = $templateDropdown.find("option:selected").data("keys");
+		const keyCount = keys.length;
+
+		const $extraFieldsRow = $("#template-extra-fields-row");
+		const $extraFieldsContainer = $("#template-extra-fields-container");
+
+		if(keyCount === 0) {
+			$extraFieldsRow.collapse("hide");
+			$extraFieldsContainer.empty();
+		}
+		else {
+			const initialTemplateKeys = JSON.parse($("#initial-template-keys").val());
+
+			$.each(keys, (index, value) => {
+				const id = `template-key-${index}`;
+
+				const $element = $(`<div class="custom-control custom-switch">
+	<input type="checkbox" class="custom-control-input" id="${id}" name="template_keys[]" />
+	<label class="custom-control-label" for="${id}">Enable ${value} fields</label>
+</div>`);
+				const checked = initialTemplateKeys.indexOf(value) !== -1;
+
+				$element.find("input").attr("value", value).prop("checked", checked);
+
+				$extraFieldsContainer.append($element);
+			});
+			$extraFieldsRow.collapse("show");
+		}
+
+
+
+	});
+
+	$("#template-id").trigger("change");
 
 	$("#autogen-name").on("change", event => {
 		const checked = event.currentTarget.checked;
@@ -148,11 +184,11 @@ function refreshLocationAlerts() {
 
 function autogenProjectName() {
 
-	const autogenChecked = $("#autogen-name").is(":checked");
+	const autogenChecked = $("#autogen-name").prop("checked");
 	if(!autogenChecked) return;
 
 	const customerName = $("#customer-name").text();
-	const templateName = $("#template-id").find("option:selected").text();
+	const templateName = $.trim($("#template-id").find("option:selected").text());
 	const dateStartMonth = $("#date-start-month").find("option:selected").text();
 	const dateStartYear = $("#date-start-year").find("option:selected").text();
 	const timeOfDay = $("#time-of-day").find("option:selected").text();

+ 8 - 0
scss/_main.scss

@@ -138,3 +138,11 @@ input, select, textarea {
 .table-pointer {
 	cursor: pointer;
 }
+
+label {
+	font-weight: 600;
+}
+
+.custom-control-label {
+	font-weight: 400;
+}

+ 71 - 7
template_processing/mystery_shop/test_pdf.php

@@ -1,5 +1,9 @@
 <?php
 
+/**
+ * @var Template $template
+ */
+
 // Values
 $cleanliness = $values["cleanliness"];
 switch($cleanliness) {
@@ -36,6 +40,9 @@ switch($staff_behaviour) {
 $staff_uniform = $values["staff_uniform"] ?? "";
 $staff_uniform = $staff_uniform ? "Yes" : "No";
 
+// COVID-19
+$covid_19 = in_array("COVID-19", $template->getKeys());
+
 // Location Photo
 $location_photo_path = $test->getDirectory() . "/location_photo.jpg";
 if(!file_exists($location_photo_path)) {
@@ -146,11 +153,35 @@ $mpdf->imageVars["location_photo"] = file_get_contents($location_photo_path);
 		<strong>Venue</strong>
 	</div>
 
-	<div>
+	<div class="wide-field-names">
 		<div class="field">Cleanliness:</div>
 		<div class="value"><?=TestPDF::getValueHtml($cleanliness)?></div>
 	</div>
 
+	<?php
+	if($covid_19) {
+		?>
+		<hr />
+		<div class="wide-field-names">
+			<div class="field"><?=htmlspecialchars($field_name_label_map["entrance_hand_sanitiser_count"])?>:</div>
+			<div class="value"><?=TestPDF::getNumericHtml($values["entrance_hand_sanitiser_count"] ?? 0)?></div>
+		</div>
+		<div class="wide-field-names">
+			<div class="field"><?=htmlspecialchars($field_name_label_map["face_mask_offered"])?>:</div>
+			<div class="value"><?=TestPDF::getBooleanHtml($values["face_mask_offered"] ?? 0)?></div>
+		</div>
+		<div class="wide-field-names">
+			<div class="field"><?=htmlspecialchars($field_name_label_map["gloves_offered"])?>:</div>
+			<div class="value"><?=TestPDF::getBooleanHtml($values["gloves_offered"] ?? 0)?></div>
+		</div>
+		<div class="wide-field-names">
+			<div class="field"><?=htmlspecialchars($field_name_label_map["social_distancing_signage"])?>:</div>
+			<div class="value"><?=TestPDF::getBooleanHtml($values["social_distancing_signage"] ?? 0)?></div>
+		</div>
+		<?php
+	}
+	?>
+
 	<br />
 
 	<div class="subheader">
@@ -184,7 +215,7 @@ $mpdf->imageVars["location_photo"] = file_get_contents($location_photo_path);
 			<div class="value"><?=TestPDF::getValueHtml($values["staff_name"] ?? "")?></div>
 		</div>
 
-		<br />
+		<hr />
 
 		<div>
 			<div class="field"><?=htmlspecialchars($field_name_label_map["staff_offered_drink"])?>:</div>
@@ -196,7 +227,7 @@ $mpdf->imageVars["location_photo"] = file_get_contents($location_photo_path);
 			<div class="value"><?=TestPDF::getValueHtml($values["staff_offered_drink_name"] ?? "")?></div>
 		</div>
 
-		<br />
+		<hr />
 
 		<div>
 			<div class="field"><?=htmlspecialchars($field_name_label_map["staff_offered_promotion"])?>:</div>
@@ -208,7 +239,7 @@ $mpdf->imageVars["location_photo"] = file_get_contents($location_photo_path);
 			<div class="value"><?=TestPDF::getValueHtml($values["staff_offered_promotion_name"] ?? "")?></div>
 		</div>
 
-		<br />
+		<hr />
 
 		<div>
 			<div class="field"><?=htmlspecialchars($field_name_label_map["staff_help"])?>:</div>
@@ -220,15 +251,48 @@ $mpdf->imageVars["location_photo"] = file_get_contents($location_photo_path);
 			<div class="value"><?=TestPDF::getValueHtml($staff_behaviour)?></div>
 		</div>
 
-		<br />
+		<hr />
 
 		<div>
 			<div class="field"><?=htmlspecialchars($field_name_label_map["staff_uniform"])?>:</div>
 			<div class="value"><?=TestPDF::getValueHtml($staff_uniform)?></div>
 		</div>
+
 	</div>
 
-	<strong><?=htmlspecialchars($field_name_label_map["staff_uniform_description"])?>:</strong><br />
-	<?=TestPDF::getValueHtml($values["staff_uniform_description"] ?? "")?>
+	<div>
+		<strong><?=htmlspecialchars($field_name_label_map["staff_uniform_description"])?>:</strong><br />
+		<?=TestPDF::getValueHtml($values["staff_uniform_description"] ?? "")?>
+	</div>
+
+	<?php
+	if($covid_19) {
+		?>
+		<hr />
+		<div class="wide-field-names">
+			<div>
+				<div class="field"><?=htmlspecialchars($field_name_label_map["staff_wearing_gloves"])?>:</div>
+				<div class="value"><?=TestPDF::getBooleanHtml($values["staff_wearing_gloves"] ?? 0)?></div>
+			</div>
+			<div>
+				<div class="field"><?=htmlspecialchars($field_name_label_map["staff_wearing_face_mask"])?>:</div>
+				<div class="value"><?=TestPDF::getBooleanHtml($values["staff_wearing_face_mask"] ?? 0)?></div>
+			</div>
+			<div>
+				<div class="field"><?=htmlspecialchars($field_name_label_map["staff_wearing_face_protection"])?>:</div>
+				<div class="value"><?=TestPDF::getBooleanHtml($values["staff_wearing_face_protection"] ?? 0)?></div>
+			</div>
+			<div>
+				<div class="field"><?=htmlspecialchars($field_name_label_map["staff_offered_cleaning"])?>:</div>
+				<div class="value"><?=TestPDF::getBooleanHtml($values["staff_offered_cleaning"] ?? 0)?></div>
+			</div>
+			<div>
+				<strong><?=htmlspecialchars($field_name_label_map["social_distancing_notes"])?>:</strong><br />
+				<?=TestPDF::getValueHtml($values["social_distancing_notes"] ?? "")?>
+			</div>
+		</div>
+		<?php
+	}
+	?>
 
 </div>

+ 11 - 1
views/practice_form.php

@@ -1,3 +1,12 @@
+<?php
+/**
+ * @controller practice.php
+ *
+ * @var Test $test
+ * @var Template $template
+ * @var array $fields
+ */
+?>
 <div class="container mt-3 py-4">
 <?php
 if($test->status == Test::STATUS_CONFIRMED) {
@@ -44,6 +53,7 @@ else {
 			<?php
 		}
 		else {
+			$field->template = $template;
 			Form::renderField($field);
 		}
 
@@ -72,4 +82,4 @@ else {
 	<?php
 }
 ?>
-</div>
+</div>

+ 15 - 2
views/project.php

@@ -3,6 +3,8 @@
 $time_of_day_icon = $project->getTimeOfDayIcon();
 $time_of_day_text = $project->getTimeOfDayText();
 
+$template_keys = $project->getTemplateKeys();
+
 ?>
 
 <script>
@@ -14,10 +16,21 @@ var projectID = <?=$project->id?>;
 <div class="container pt-3 my-4">
 	<div class="row">
 		<div class="col-auto">
-			<h2>
+			<h2 class="mb-0">
 				<?=htmlspecialchars($project->name)?>
-				<i class="fal fa-<?=$time_of_day_icon?>" data-toggle="tooltip" data-title="<?=$time_of_day_text?> Tests"></i>
+				<i class="fa fa-<?=$time_of_day_icon?>" data-toggle="tooltip" data-title="<?=$time_of_day_text?> Tests"></i>
 			</h2>
+			<div class="mb-3">
+				<?php
+				foreach($template_keys as $key) {
+					?>
+					<span class="badge badge-primary">
+						<?=htmlspecialchars($key)?>
+					</span>
+					<?php
+				}
+				?>
+			</div>
 		</div>
 		<div class="col text-right">
 		<?php

+ 14 - 1
views/project_form.php

@@ -34,6 +34,8 @@ $autogen_checked = $project->id ? "" : "checked";
 
 	<input type="hidden" id="project-id" value="<?=$project->id?>" />
 
+	<input type="hidden" id="initial-template-keys" value="<?=htmlspecialchars(json_encode($project->getTemplateKeys()))?>" />
+
 	<form method="post" id="project-form">
 
 		<h3>Project Details</h3>
@@ -59,8 +61,12 @@ $autogen_checked = $project->id ? "" : "checked";
 						<?php
 						foreach($templates as $template) {
 							$selected = ($template->id == $project->template_id) ? "selected" : "";
+							$keys = $template->getTemplateKeys();
+							$keys_json = json_encode($keys);
 							?>
-							<option value="<?=$template->id?>" <?=$selected?>><?=htmlspecialchars($template->name)?></option>
+							<option value="<?=$template->id?>" data-keys="<?=htmlspecialchars($keys_json)?>" <?=$selected?>>
+								<?=htmlspecialchars($template->name)?>
+							</option>
 							<?php
 						}
 						?>
@@ -68,6 +74,13 @@ $autogen_checked = $project->id ? "" : "checked";
 					</div>
 				</div>
 
+				<div id="template-extra-fields-row" class="row mb-1 collapse">
+					<div class="col-sm-4"></div>
+					<div id="template-extra-fields-container" class="col-sm">
+
+					</div>
+				</div>
+
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="test-count">Test Count</label>
 					<div class="col-sm">

+ 49 - 7
views/template.php

@@ -3,6 +3,8 @@
 $tester_invoice_fields = $template->getTesterInvoiceFields();
 $customer_invoice_fields = $template->getCustomerInvoiceFields();
 
+$template_keys = $template->getTemplateKeys();
+
 ?>
 <div class="container pt-3 my-4">
 	<div class="row">
@@ -83,14 +85,50 @@ $customer_invoice_fields = $template->getCustomerInvoiceFields();
 		</div>
 	</div>
 
+	<?php
+	if($template_keys) {
+		?>
+		<div class="card mb-3">
+			<div class="card-header">
+				Template Tags
+			</div>
+			<div class="card-body">
+				<form method="get">
+					This template has the following toggleable tags:
+					<div class="form-group">
+						<?php
+						foreach($template_keys as $key) {
+							$checked = in_array($key, $keys) ? "checked" : "";
+							$id = sprintf("key-%s", $key);
+							?>
+							<div class="custom-control custom-switch">
+								<input type="checkbox" class="custom-control-input" name="keys[]" id="<?=$key?>" value="<?=$key?>" <?=$checked?> />
+								<label class="custom-control-label" for="<?=$key?>"><?=$key?></label>
+							</div>
+							<?php
+						}
+						?>
+					</div>
+					<button type="submit" class="btn btn-primary">Reload Page</button>
+				</form>
+			</div>
+		</div>
+		<?php
+	}
+	?>
+
 	<div class="card mb-3">
 		<div class="card-header">
 			Before Test Form Preview
 		</div>
 		<div class="card-body">
-		<?php
-		$template->renderBeforeTestForm();
-		?>
+			<div class="row">
+				<div class="col-md-6 offset-md-3">
+					<?php
+					$template->renderBeforeTestForm();
+					?>
+				</div>
+			</div>
 		</div>
 	</div>
 
@@ -99,10 +137,14 @@ $customer_invoice_fields = $template->getCustomerInvoiceFields();
 			During Test Form Preview
 		</div>
 		<div class="card-body">
-		<?php
-		$template->renderDuringTestForm();
-		?>
+			<div class="row">
+				<div class="col-md-6 offset-md-3">
+					<?php
+					$template->renderDuringTestForm();
+					?>
+				</div>
+			</div>
 		</div>
 	</div>
 
-</div>
+</div>

+ 12 - 1
views/test_form.php

@@ -1,3 +1,13 @@
+<?php
+/**
+ * @controller test.php
+ *
+ * @var Test $test
+ * @var Location $location
+ * @var Project $project
+ * @var Template $template
+ */
+?>
 <div class="container mt-3 py-4">
 <?php
 if($test->status == Test::STATUS_CONFIRMED) {
@@ -28,6 +38,7 @@ else if($test->status == Test::STATUS_PENDING_VERIFICATION) {
 			if(isset($test->$name)) $field->value = $test->$name;
 			if(isset($values[$name])) $field->value = $values[$name];
 		}
+		$field->template = $template;
 		Form::renderField($field);
 	}
 	?>
@@ -54,4 +65,4 @@ else {
 	<?php
 }
 ?>
-</div>
+</div>

+ 11 - 0
views/test_verify_form.php

@@ -1,8 +1,17 @@
 <?php
 
 /**
+ * @controller test.php
+ *
  * @var Test $test
+ * @var User $tester
+ * @var Location $location
+ * @var Project $project
  * @var Project[] $future_projects
+ * @var Customer $customer
+ * @var Template $template
+ * @var array $fields
+ * @var array $values
  * @var string $tester_message
  */
 
@@ -154,6 +163,8 @@ function render_card_list_group($details) {
 							if(isset($test->$name)) $field->value = $test->$name;
 							else if(isset($values[$name])) $field->value = $values[$name];
 						}
+
+						$field->template = $template;
 						Form::renderField($field);
 
 						if($field->type == "header") {

Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini