Parcourir la source

Fix practice folder bug; add PHPDoc to Test class

Alan Lam il y a 7 ans
Parent
commit
d5fdcf7c3d
3 fichiers modifiés avec 112 ajouts et 29 suppressions
  1. 2 1
      controllers/practice.php
  2. 6 1
      engine/practice_test.php
  3. 104 27
      engine/test.php

+ 2 - 1
controllers/practice.php

@@ -23,6 +23,7 @@ if(!$test || $reset) {
 	$test = new PracticeTest;
 	$test->tester_id = Core::$user->id;
 	$test->status = Test::STATUS_CONFIRMED;
+	$test->setTemplateId($template->id);
 	$tests[$id] = $test;
 	$_SESSION["practice_tests"] = $tests;
 
@@ -198,4 +199,4 @@ View::includeOnce("header", compact("page_title"));
 View::include("practice_form", compact("test", "template", "fields"));
 View::includeOnce("footer");
 
-?>
+?>

+ 6 - 1
engine/practice_test.php

@@ -2,7 +2,8 @@
 
 class PracticeTest extends Test {
 
-	protected $values = [];
+	private $values = [];
+	private $template_id = null;
 
 	// Constants
 
@@ -32,4 +33,8 @@ class PracticeTest extends Test {
 		if(!file_exists($directory)) mkdir($directory, 0777, true);
 		return $directory;
 	}
+
+	public function setTemplateId(int $id) {
+		$this->template_id = $id;
+	}
 }

+ 104 - 27
engine/test.php

@@ -1,4 +1,7 @@
-<?php
+<?php /** @noinspection HtmlDeprecatedTag */
+/** @noinspection HtmlDeprecatedAttribute */
+
+use Mpdf\Mpdf;
 
 /**
  * @property int $id
@@ -90,7 +93,12 @@ class Test extends Model {
 	const TEST_TIME_NIGHT = 3;
 
 	// Static methods
-	public static function getStatusTesterInvoiceText($status) {
+
+	/**
+	 * @param int $status
+	 * @return string
+	 */
+	public static function getStatusTesterInvoiceText(int $status): string {
 		switch($status) {
 			case Test::STATUS_TESTER_INVOICE_NONE:
 				return __("No Invoice Generated");
@@ -102,7 +110,11 @@ class Test extends Model {
 		return __("Unknown");
 	}
 
-	public static function getStatusTesterInvoiceClass($status) {
+	/**
+	 * @param int $status
+	 * @return string
+	 */
+	public static function getStatusTesterInvoiceClass(int $status): string {
 		switch($status) {
 			case Test::STATUS_TESTER_INVOICE_NONE:
 				return "secondary";
@@ -114,7 +126,12 @@ class Test extends Model {
 		return "";
 	}
 
-	public static function exportCSV($processing_file, $tests = []) {
+	/**
+	 * @param string $processing_file
+	 * @param array $tests
+	 * @throws Exception
+	 */
+	public static function exportCSV(string $processing_file, array $tests = []): void {
 
 		// Preload
 		Project::load(array_column($tests, "project_id"));
@@ -134,7 +151,13 @@ class Test extends Model {
 	}
 
 	// Object methods
-	public function sendAssignEmail($message) {
+
+	/**
+	 * @param string $message
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function sendAssignEmail(string $message): bool {
 
 		$tester = $this->getTester();
 		$location = $this->getLocation();
@@ -195,9 +218,15 @@ class Test extends Model {
 		$mail->addAddress($tester->email, $tester->getDisplayName());
 		$mail->setSubject(__("You have been allocated a test"));
 		$mail->send();
+
+		return true;
 	}
 
-	public function sendClaimedEmail() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function sendClaimedEmail(): bool {
 
 		$tester = $this->getTester();
 		$location = $this->getLocation();
@@ -240,9 +269,15 @@ class Test extends Model {
 		$mail->addAddress($tester->email, $tester->getDisplayName());
 		$mail->setSubject(__("You have claimed a test"));
 		$mail->send();
+
+		return true;
 	}
 
-	public function sendDeclineEmail() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function sendDeclineEmail(): bool {
 
 		$tester = $this->getTester();
 		$location = $this->getLocation();
@@ -274,9 +309,16 @@ class Test extends Model {
 		$mail->addAddress(EMAIL_CONTACT, APP_NAME . " Contact");
 		$mail->setSubject("Test declined: $project->name / $location->name");
 		$mail->send();
+
+		return true;
 	}
 
-	public function sendResultEmail($to_address) {
+	/**
+	 * @param string $to_address
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function sendResultEmail(string $to_address): bool {
 
 		$location = $this->getLocation();
 		$project = $this->getProject();
@@ -336,9 +378,15 @@ class Test extends Model {
 		$mail->setSubject($subject);
 
 		$mail->send();
+
+		return true;
 	}
 
-	public function decline() {
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function decline(): bool {
 		$this->sendDeclineEmail();
 
 		$this->status = Test::STATUS_UNASSIGNED;
@@ -349,16 +397,25 @@ class Test extends Model {
 		return $this->commit();
 	}
 
-	public function getChallengeStageText() {
+	/**
+	 * @return string
+	 */
+	public function getChallengeStageText(): string {
 		if(is_null($this->challenge_stage)) return "";
 		return Test::STAGES[$this->challenge_stage];
 	}
 
-	public function getStatusText() {
+	/**
+	 * @return string
+	 */
+	public function getStatusText(): string {
 		return Test::STATUSES[$this->status];
 	}
 
-	public function getStatusClass() {
+	/**
+	 * @return string
+	 */
+	public function getStatusClass(): string {
 		switch($this->status) {
 			case Test::STATUS_CANCELLED:
 				return "secondary";
@@ -373,34 +430,47 @@ class Test extends Model {
 			case Test::STATUS_COMPLETED:
 				return "success";
 		}
+		return "";
 	}
 
-	public function getProject() {
+	/**
+	 * @return Project|null
+	 * @throws Exception
+	 */
+	public function getProject(): ?Project {
 		return Project::get($this->project_id);
 	}
 
 	/**
 	 * @return Location
+	 * @throws Exception
 	 */
-	public function getLocation() {
+	public function getLocation(): ?Location {
 		return Location::get($this->location_id);
 	}
 
 	/**
 	 * @return User
+	 * @throws Exception
 	 */
-	public function getTester() {
+	public function getTester(): ?User {
 		return User::get($this->tester_id);
 	}
 
 	/**
 	 * @return User
+	 * @throws Exception
 	 */
-	public function getVerifier() {
+	public function getVerifier(): ?User {
 		return User::get($this->verifier_id);
 	}
 
-	public function getValues(bool $as_assoc_array = false) {
+	/**
+	 * @param bool $as_assoc_array
+	 * @return array
+	 * @throws Exception
+	 */
+	public function getValues(bool $as_assoc_array = false): array {
 		$rows = TestValue::query(["test_id" => $this->id]);
 
 		if($as_assoc_array) {
@@ -415,22 +485,28 @@ class Test extends Model {
 		return $values;
 	}
 
-	public function getDirectory() {
+	/**
+	 * @return string
+	 */
+	public function getDirectory(): string {
 		$directory = ROOT_DIR . "/files/tests/" . $this->id;
-		if($this->project_id == "PRACTICE") {
-			$directory = ROOT_DIR . "/files/users/$this->tester_id/practice_tests/$this->template_id";
-		}
 		if(!file_exists($directory)) mkdir($directory, 0777, true);
 		return $directory;
 	}
 
+	/**
+	 * @return bool|float|int
+	 */
 	public function getDuration() {
 		if(is_null($this->dts_end)) return false;
 		$seconds = Util::getDurationFromDTS($this->dts_start, $this->dts_end);
 		return $seconds;
 	}
 
-	public function getDisplayDuration() {
+	/**
+	 * @return string
+	 */
+	public function getDisplayDuration(): string {
 		$secs = $this->getDuration();
 		$mins = floor($secs / 60);
 		$secs = $secs % 60;
@@ -441,7 +517,7 @@ class Test extends Model {
 	/**
 	 * @return string Bootstrap class name
 	 */
-	public function getPassClass() {
+	public function getPassClass(): string {
 		if(is_null($this->pass)) return "secondary";
 		return $this->pass ? "success" : "danger";
 	}
@@ -449,7 +525,7 @@ class Test extends Model {
 	/**
 	 * @return string FA icon name without prefix
 	 */
-	public function getPassIcon() {
+	public function getPassIcon(): string {
 		if(is_null($this->pass)) return "clock";
 		return $this->pass ? "check-circle" : "times-circle";
 	}
@@ -457,12 +533,12 @@ class Test extends Model {
 	/**
 	 * @return string Pending, Pass or Fail
 	 */
-	public function getPassText() {
+	public function getPassText(): string {
 		if(is_null($this->pass)) return __("Pending");
 		return $this->pass ? __("Pass") : __("Fail");
 	}
 
-	public function getMPDF() {
+	public function getMPDF(): Mpdf {
 		/** @var Mpdf $mpdf */
 		$id = $this->id;
 		$output = false;
@@ -475,7 +551,7 @@ class Test extends Model {
 	 * @return bool
 	 * @throws Exception
 	 */
-	public function assignTester(User $tester) {
+	public function assignTester(User $tester): bool {
 		$this->tester_id = $tester->id;
 		$this->dts_assigned = Util::getDateTime();
 		$success = $this->commit();
@@ -614,6 +690,7 @@ class Test extends Model {
 
 	/**
 	 * @return string
+	 * @throws Exception
 	 */
 	public function getTesterInvoiceReference(): string {