|
|
@@ -0,0 +1,175 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+Core::addScript("public/js/app-test.js");
|
|
|
+
|
|
|
+$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.");
|
|
|
+
|
|
|
+$page_title = "Practice";
|
|
|
+
|
|
|
+Core::addBreadcrumb("/account/practice", $page_title);
|
|
|
+Core::addBreadcrumb("/account/practice/$id", "Form");
|
|
|
+
|
|
|
+$json = json_decode($template->json);
|
|
|
+
|
|
|
+// Reset
|
|
|
+$reset = http::value("reset");
|
|
|
+
|
|
|
+$tests = $_SESSION["practice_tests"] ?? [];
|
|
|
+$test = $tests[$id] ?? false;
|
|
|
+if(!$test || $reset) {
|
|
|
+ $test = new PracticeTest;
|
|
|
+ $test->tester_id = Core::$user->id;
|
|
|
+ $test->status = Test::STATUS_CONFIRMED;
|
|
|
+ $tests[$id] = $test;
|
|
|
+ $_SESSION["practice_tests"] = $tests;
|
|
|
+
|
|
|
+ if($reset) {
|
|
|
+ // Force redirect if resetting
|
|
|
+ http::redirect("/account/practice/$id");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+$is_confirmed = $test->status == Test::STATUS_CONFIRMED;
|
|
|
+$is_started = $test->status == Test::STATUS_STARTED;
|
|
|
+$is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
|
|
|
+
|
|
|
+$submit = http::value("submit");
|
|
|
+if($submit) {
|
|
|
+
|
|
|
+ $previous_timezone = date_default_timezone_get();
|
|
|
+
|
|
|
+ date_default_timezone_set("UTC");
|
|
|
+
|
|
|
+ try {
|
|
|
+ Core::validateCSRFToken($submit);
|
|
|
+
|
|
|
+ $form_exception = new FormException;
|
|
|
+
|
|
|
+ if($is_confirmed) {
|
|
|
+ // Parse before the test form
|
|
|
+ $fields = $json->beforeTest;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if($is_started || $is_unverified) {
|
|
|
+ // Parse after the test form
|
|
|
+ $fields = $json->duringTest;
|
|
|
+
|
|
|
+ if($is_started) {
|
|
|
+ $duration = http::value("duration", "int");
|
|
|
+ if(!$duration) $form_exception->addError("Duration was not submitted.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach($fields as $field) {
|
|
|
+
|
|
|
+ $name = $field->name ?? false;
|
|
|
+ if(empty($name)) continue; // Not a field
|
|
|
+
|
|
|
+ $label = $field->label ?? "";
|
|
|
+ $type = $field->type;
|
|
|
+ $required = $field->required ?? false;
|
|
|
+ $values = $field->values ?? [];
|
|
|
+
|
|
|
+ if($type == "file") {
|
|
|
+ $file = (object) $_FILES[$name];
|
|
|
+
|
|
|
+ try {
|
|
|
+ if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
|
|
|
+ if($required && $file->error == UPLOAD_ERR_NO_FILE) {
|
|
|
+ throw new Exception("$label is required.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Detect upload error
|
|
|
+ if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
|
|
|
+
|
|
|
+ // Get image details
|
|
|
+ $size = @getimagesize($file->tmp_name);
|
|
|
+ if(!$size) throw new Exception("The uploaded file for $label is not an image.");
|
|
|
+ list($width, $height, $type) = $size;
|
|
|
+ if($type != IMAGETYPE_JPEG) throw new Exception("The uploaded file for $label is not a JPEG.");
|
|
|
+
|
|
|
+ // Resize image
|
|
|
+ $output_image = Image::normalise($file->tmp_name);
|
|
|
+
|
|
|
+ // Write image to disk
|
|
|
+ $output_path = $test->getDirectory() . "/" . $name . ".jpg";
|
|
|
+
|
|
|
+ imagejpeg($output_image, $output_path, 60);
|
|
|
+ imagedestroy($output_image);
|
|
|
+ }
|
|
|
+ catch(Exception $e) {
|
|
|
+ $form_exception->addError($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $value = http::value($name, "trim");
|
|
|
+
|
|
|
+ if($required && empty($value)) $form_exception->addError("$label is required.");
|
|
|
+ if($values && !in_array($value, $values)) $form_exception->addError("Value for $label is invalid.");
|
|
|
+
|
|
|
+ if(in_array($name, Test::TEST_FIELDS)) {
|
|
|
+ $test->$name = $value;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $test->setValue($name, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($form_exception->hasErrors()) throw $form_exception;
|
|
|
+
|
|
|
+ // Before the Test
|
|
|
+ if($is_confirmed) {
|
|
|
+ $test->status = Test::STATUS_STARTED;
|
|
|
+ $test->dts_start = Util::getDateTime();
|
|
|
+ $test->dts_updated = Util::getDateTime();
|
|
|
+ $tests[$id] = $test;
|
|
|
+ $_SESSION["practice_tests"] = $tests;
|
|
|
+ HTTP::redirect("/account/practice/$id");
|
|
|
+ }
|
|
|
+
|
|
|
+ // During the test
|
|
|
+ if($is_started || $is_unverified) {
|
|
|
+ $test->status = Test::STATUS_PENDING_VERIFICATION;
|
|
|
+
|
|
|
+ if($is_started) {
|
|
|
+ $unix_start = strtotime($test->dts_start);
|
|
|
+ $unix_end = $unix_start + floor($duration / 1000); // $duration is milliseconds
|
|
|
+
|
|
|
+ $test->dts_end = date("Y-m-d H:i:s", $unix_end);
|
|
|
+ }
|
|
|
+
|
|
|
+ $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
|
|
|
+ $test->dts_updated = Util::getDateTime();
|
|
|
+
|
|
|
+ $tests[$id] = $test;
|
|
|
+ $_SESSION["practice_tests"] = $tests;
|
|
|
+
|
|
|
+ JSON::success();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch(FormException $e) {
|
|
|
+ $e->generateAlerts();
|
|
|
+ }
|
|
|
+ catch(Exception $e) {
|
|
|
+ Core::addAlert("danger", $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ date_default_timezone_set($previous_timezone);
|
|
|
+}
|
|
|
+
|
|
|
+if($is_unverified) {
|
|
|
+ $json = json_decode($template->json);
|
|
|
+ $fields = array_merge($json->beforeTest, $json->duringTest);
|
|
|
+}
|
|
|
+
|
|
|
+View::includeOnce("header", compact("page_title"));
|
|
|
+View::include("practice_form", compact("test", "template", "fields"));
|
|
|
+View::includeOnce("footer");
|
|
|
+
|
|
|
+?>
|