Alan Lam 8 лет назад
Родитель
Сommit
e935f10416

+ 34 - 3
controllers/practice.php

@@ -42,8 +42,6 @@ if($submit) {
 
 
 	$previous_timezone = date_default_timezone_get();
 	$previous_timezone = date_default_timezone_get();
 
 
-	date_default_timezone_set("UTC");
-
 	try {
 	try {
 		Core::validateCSRFToken($submit);
 		Core::validateCSRFToken($submit);
 
 
@@ -121,8 +119,28 @@ if($submit) {
 			}
 			}
 		}
 		}
 
 
+		// Overrides
+		$start_override = http::getDateTimeFromFields("start_override");
+		$end_override = http::getDateTimeFromFields("end_override");
+
+		$start_unix = null;
+		$end_unix = null;
+
+		if($start_override && $end_override) {
+			$start_unix = strtotime($start_override);
+			$end_unix = strtotime($end_override);
+
+			if($start_unix > $end_unix) $form_exception->addError("Test Start date/time must not be after Test End date/time.");
+			if(!$start_unix && $end_unix && $test->dts_start) {
+				$recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
+				if($end_unix < $recorded_start_unix) $form_exception->addError("Test End date/time cannot be later than the recorded start date/time.");
+			}
+		}
+
 		if($form_exception->hasErrors()) throw $form_exception;
 		if($form_exception->hasErrors()) throw $form_exception;
 
 
+		date_default_timezone_set("UTC");
+
 		// Before the Test
 		// Before the Test
 		if($is_confirmed) {
 		if($is_confirmed) {
 			$test->status = Test::STATUS_STARTED;
 			$test->status = Test::STATUS_STARTED;
@@ -142,6 +160,12 @@ if($submit) {
 				$unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
 				$unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
 
 
 				$test->dts_end = date("Y-m-d H:i:s", $unix_end);
 				$test->dts_end = date("Y-m-d H:i:s", $unix_end);
+
+				if($start_unix && $end_unix) {
+					$test->dts_start = date("Y-m-d H:i:s", $start_unix);
+					$test->dts_end = date("Y-m-d H:i:s", $end_unix);
+				}
+
 			}
 			}
 
 
 			$test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
 			$test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
@@ -150,12 +174,19 @@ if($submit) {
 			$tests[$id] = $test;
 			$tests[$id] = $test;
 			$_SESSION["practice_tests"] = $tests;
 			$_SESSION["practice_tests"] = $tests;
 
 
+			JSON::header();
 			JSON::success();
 			JSON::success();
 		}
 		}
 
 
 	}
 	}
 	catch(FormException $e) {
 	catch(FormException $e) {
-		$e->generateAlerts();
+		if(HTTP::isAJAX()) {
+			JSON::header();
+			$e->generateJSONError();
+		}
+		else {
+			$e->generateAlerts();
+		}
 	}
 	}
 	catch(Exception $e) {
 	catch(Exception $e) {
 		Core::addAlert("danger", $e->getMessage());
 		Core::addAlert("danger", $e->getMessage());

+ 2 - 0
controllers/project_csv.php

@@ -10,6 +10,7 @@ header("Cache-Control: private, max-age=0, must-revalidate");
 header("Pragma: public");
 header("Pragma: public");
 
 
 $template = $project->getTemplate();
 $template = $project->getTemplate();
+$processing_file_path = $template->getProcessingFilePath("project_csv.php");
 
 
 $object = json_decode($template->json);
 $object = json_decode($template->json);
 
 
@@ -33,6 +34,7 @@ $testers = User::load(array_column($tests, "tester_id"));
 
 
 $handle = fopen("php://output", "w");
 $handle = fopen("php://output", "w");
 
 
+require $processing_file_path;
 
 
 fclose($handle);
 fclose($handle);
 
 

+ 25 - 2
controllers/test.php

@@ -44,8 +44,6 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 
 
 		$previous_timezone = date_default_timezone_get();
 		$previous_timezone = date_default_timezone_get();
 
 
-		date_default_timezone_set("UTC");
-
 		try {
 		try {
 			Core::validateCSRFToken($submit);
 			Core::validateCSRFToken($submit);
 
 
@@ -133,8 +131,28 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 
 
 			}
 			}
 
 
+			// Date/Time Overrides
+			$start_override = http::getDateTimeFromFields("start_override");
+			$end_override = http::getDateTimeFromFields("end_override");
+
+			$start_unix = null;
+			$end_unix = null;
+
+			if($start_override && $end_override) {
+				$start_unix = strtotime($start_override);
+				$end_unix = strtotime($end_override);
+
+				if($start_unix > $end_unix) $form_exception->addError("Test Start date/time must not be after Test End date/time.");
+				if(!$start_unix && $end_unix && $test->dts_start) {
+					$recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
+					if($end_unix < $recorded_start_unix) $form_exception->addError("Test End date/time cannot be later than the recorded start date/time.");
+				}
+			}
+
 			if($form_exception->hasErrors()) throw $form_exception;
 			if($form_exception->hasErrors()) throw $form_exception;
 
 
+			date_default_timezone_set("UTC");
+
 			// Before the Test
 			// Before the Test
 			if($is_confirmed) {
 			if($is_confirmed) {
 				$test->status = Test::STATUS_STARTED;
 				$test->status = Test::STATUS_STARTED;
@@ -156,6 +174,11 @@ if(Core::$user->level == User::LEVEL_TESTER) {
 					$unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
 					$unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
 
 
 					$test->dts_end = date("Y-m-d H:i:s", $unix_end);
 					$test->dts_end = date("Y-m-d H:i:s", $unix_end);
+
+					if($start_unix && $end_unix) {
+						$test->dts_start = date("Y-m-d H:i:s", $start_unix);
+						$test->dts_end = date("Y-m-d H:i:s", $end_unix);
+					}
 				}
 				}
 
 
 				$test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
 				$test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before

+ 8 - 0
engine/form_exception.php

@@ -22,4 +22,12 @@ class FormException extends Exception {
 		}
 		}
 	}
 	}
 
 
+	public function generateJSONError() {
+		$messages = $this->getErrors();
+
+		JSON::fail([
+			"message" => implode("\n", $messages)
+		]);
+	}
+
 }
 }

+ 4 - 0
engine/http.php

@@ -79,6 +79,10 @@ class HTTP {
 		$minute = http::value($minute_field, "int");
 		$minute = http::value($minute_field, "int");
 		$second = http::value($second_field, "int");
 		$second = http::value($second_field, "int");
 
 
+		if(strlen($hour) == 0) return false;
+		if(strlen($minute) == 0) return false;
+		if(strlen($second) == 0) return false;
+
 		list($hour, $minute, $second) = array_map(function($part) {
 		list($hour, $minute, $second) = array_map(function($part) {
 			return str_pad($part, 2, "0", STR_PAD_LEFT);
 			return str_pad($part, 2, "0", STR_PAD_LEFT);
 		}, [$hour, $minute, $second]);
 		}, [$hour, $minute, $second]);

+ 89 - 0
engine/template.php

@@ -93,6 +93,95 @@ class Template extends Model {
 				}
 				}
 				?>
 				?>
 
 
+				<h3>Date/Time Overrides</h3>
+
+				<p>If you are submitting this form outside of the actual test, please use the following fields to specify when you did the test.</p>
+
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="start-override-day">Test Start</label>
+					<div class="col-sm">
+						<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>
+						<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>
+
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="end-override-day">Test End</label>
+					<div class="col-sm">
+						<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>
+						<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>
+
+
 				<div class="bg-light text-center p-3 mt-3">
 				<div class="bg-light text-center p-3 mt-3">
 					<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
 					<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
 					<button class="btn btn-block btn-primary" type="submit" id="test-submit-button">Submit Test Results</button>
 					<button class="btn btn-block btn-primary" type="submit" id="test-submit-button">Submit Test Results</button>

+ 8 - 2
public/js/app-practice-test.js

@@ -82,7 +82,6 @@ function initialiseTestForm() {
 			success: function(data, text) {
 			success: function(data, text) {
 
 
 				try {
 				try {
-					var data = $.parseJSON(data);
 					if(!data.success) throw "Unsuccessful.";
 					if(!data.success) throw "Unsuccessful.";
 
 
 					$submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected in three seconds.");
 					$submitStatus.html("Submitted. Thank you for participating in this test. You will be redirected in three seconds.");
@@ -91,7 +90,14 @@ function initialiseTestForm() {
 					}, 3000);
 					}, 3000);
 				}
 				}
 				catch(err) {
 				catch(err) {
-					$submitStatus.html("Error, please try again later.");
+
+					if(data.message) {
+						$submitStatus.html("Error: " + data.message.replace("\n", "<br />"));
+					}
+					else {
+						$submitStatus.html("Error, please try again later.");
+					}
+
 					$submitButton.removeAttr("disabled");
 					$submitButton.removeAttr("disabled");
 				}
 				}
 			},
 			},

+ 21 - 0
public/js/app.js

@@ -128,4 +128,25 @@ function initialiseMaps() {
 
 
 	});
 	});
 
 
+	$("input[data-autocomplete]").each(function() {
+		let mapId = $(this).data("autocomplete");
+		let map = maps[mapId];
+
+		let autocomplete = new google.maps.places.Autocomplete(this);
+		autocomplete.bindTo("bounds", map);
+		autocomplete.setFields(["address_components", "geometry", "icon", "name"]);
+
+		autocomplete.addListener("place_changed", function() {
+			let place = autocomplete.getPlace();
+			if(place.geometry.viewport) {
+				map.fitBounds(place.geometry.viewport);
+			}
+			else {
+				map.setCenter(place.geometry.location);
+				map.setZoom(17);
+			}
+		});
+
+	});
+
 }
 }

+ 1 - 1
views/footer.php

@@ -70,7 +70,7 @@
 		}
 		}
 		?>
 		?>
 
 
-		<script src="https://maps.googleapis.com/maps/api/js?key=<?=GOOGLE_MAPS_API_KEY?>&callback=initialiseMaps" async defer></script>
+		<script src="https://maps.googleapis.com/maps/api/js?key=<?=GOOGLE_MAPS_API_KEY?>&amp;libraries=places&amp;callback=initialiseMaps" async defer></script>
 		<!--<script src="https://www.google.com/recaptcha/api.js" async defer></script>-->
 		<!--<script src="https://www.google.com/recaptcha/api.js" async defer></script>-->
 	</body>
 	</body>
 </html>
 </html>

+ 11 - 0
views/map_overview.php

@@ -4,6 +4,17 @@
 
 
 	<h2>Map Overview</h2>
 	<h2>Map Overview</h2>
 
 
+	<div class="input-group">
+		<div class="input-group-prepend">
+			<span class="input-group-text">Location</span>
+		</div>
+		<input type="text" class="form-control" placeholder="Type location" data-autocomplete="map" />
+	</div>
+
+	<div class="help-text text-muted mb-3">
+		<small>Type location and select to jump to location.</small>
+	</div>
+
 	<div class="mb-4" id="map" data-maps data-lat="51.3555177" data-lng="-1.1743197" data-zoom="7" style="height: 512px;"></div>
 	<div class="mb-4" id="map" data-maps data-lat="51.3555177" data-lng="-1.1743197" data-zoom="7" style="height: 512px;"></div>
 
 
 	<div class="row">
 	<div class="row">

+ 22 - 0
views/practice_form.php

@@ -48,6 +48,28 @@ else {
 		}
 		}
 
 
 	}
 	}
+
+	?>
+
+	<hr />
+
+	<h3>Test Date/Time</h3>
+
+	<div class="row mb-1">
+		<label class="col-sm-4 col-form-label">Test Start</label>
+		<div class="col-sm">
+			<?=htmlspecialchars($test->dts_start)?>
+		</div>
+	</div>
+
+	<div class="row mb-1">
+		<label class="col-sm-4 col-form-label">Test End</label>
+		<div class="col-sm">
+			<?=htmlspecialchars($test->dts_end)?>
+		</div>
+	</div>
+
+	<?php
 }
 }
 ?>
 ?>
 </div>
 </div>

+ 2 - 2
views/project_form.php

@@ -231,7 +231,7 @@ $time_of_day_options = [
 									<?=$location->id?>
 									<?=$location->id?>
 								</div>
 								</div>
 								<div class="col">
 								<div class="col">
-									<?=htmlspecialchars($location->name)?>
+									<a href="/account/locations/<?=$location->id?>" target="_blank"><?=htmlspecialchars($location->name)?></a>
 									<?php
 									<?php
 									if($location->internal_id) {
 									if($location->internal_id) {
 										?>
 										?>
@@ -293,7 +293,7 @@ $time_of_day_options = [
 									<?=$location->id?>
 									<?=$location->id?>
 								</div>
 								</div>
 								<div class="col">
 								<div class="col">
-									<?=htmlspecialchars($location->name)?>
+									<a href="/account/locations/<?=$location->id?>" target="_blank"><?=htmlspecialchars($location->name)?></a>
 									<?php
 									<?php
 									if($location->internal_id) {
 									if($location->internal_id) {
 										?>
 										?>

+ 2 - 2
views/test_verify_form.php

@@ -205,7 +205,7 @@ function render_card_list_group($details) {
 								<select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
 								<select class="form-control w-auto d-inline-block" name="start_override_year" id="start-override-year">
 									<option value="">Year</option>
 									<option value="">Year</option>
 									<?php
 									<?php
-									Form::renderYearOptionsDescending(2017, date("Y"));
+									Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
 									?>
 									?>
 								</select>
 								</select>
 								<select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
 								<select class="form-control w-auto d-inline-block" name="start_override_hour" id="start-override-hour">
@@ -247,7 +247,7 @@ function render_card_list_group($details) {
 								<select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
 								<select class="form-control w-auto d-inline-block" name="end_override_year" id="end-override-year">
 									<option value="">Year</option>
 									<option value="">Year</option>
 									<?php
 									<?php
-									Form::renderYearOptionsDescending(2017, date("Y"));
+									Form::renderYearOptionsDescending(2017, date("Y"), date("Y"));
 									?>
 									?>
 								</select>
 								</select>
 								<select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">
 								<select class="form-control w-auto d-inline-block" name="end_override_hour" id="end-override-hour">