فهرست منبع

Factorise assign tester code

Alan Lam 1 سال پیش
والد
کامیت
52aafbf0a1
3فایلهای تغییر یافته به همراه54 افزوده شده و 44 حذف شده
  1. 1 1
      controllers/project.php
  2. 13 30
      controllers/project_ajax.php
  3. 40 13
      engine/test.php

+ 1 - 1
controllers/project.php

@@ -101,7 +101,7 @@ if($action) {
 		case "unassign":
 			$tests = Test::query(["id" => $test_ids, "project_id" => $id, "status" => Test::STATUS_ASSIGNED]);
 			foreach($tests as $test) {
-				$test->unassign();
+				$test->unassignTester();
 			}
 
 

+ 13 - 30
controllers/project_ajax.php

@@ -277,13 +277,11 @@ if($submit) {
 				$month = http::value("month", "int");
 				$day = http::value("day", "int");
 
-				$unix_due = 0;
-				if($year && $month && $day) {
-
-					$month = str_pad($month, 2, "0", STR_PAD_LEFT);
-					$day = str_pad($day, 2, "0", STR_PAD_LEFT);
+				$dt_due = null;
 
-					$unix_due = strtotime("$year-$month-$day");
+				if($year && $month && $day) {
+					$dt_due = CarbonImmutable::now(TIMEZONE);
+					$dt_due = $dt_due->setDate($year, $month, $day)->endOfDay();
 				}
 
 				$message = http::value("message");
@@ -302,38 +300,23 @@ if($submit) {
 				];
 
 				$test = Test::queryOne($nvp);
-				if(!$test) throw new Exception("A test does not exist for this location.");
+				if(!$test) {
+					throw new RuntimeException("A test does not exist for this location.");
+				}
 
 				$address_id = http::value("address_id", "int");
 				$address = UserAddress::get($address_id);
-				if(!$address) throw new Exception("User address cannot be found.");
+				if(!$address) {
+					throw new RuntimeException("User address cannot be found.");
+				}
 
 				$user = $address->getUser();
-				if($user->level == User::LEVEL_BANNED) throw new Exception("A test cannot be assigned to an banned user.");
-				if($user->level == User::LEVEL_REGISTERED) throw new Exception("A test cannot be assigned to an unverified user.");
-				if($user->level == User::LEVEL_UNVERIFIED) throw new Exception("A test cannot be assigned to an unverified user.");
-				if($user->level == User::LEVEL_DEACTIVATED) throw new Exception("A test cannot be assigned to a deactivated user.");
-				if($user->level != User::LEVEL_TESTER) throw new Exception("A test cannot be assigned to a user that's not a tester.");
-
-				$test->status = Test::STATUS_ASSIGNED;
-				$test->tester_id = $user->id;
-				$test->dts_assigned = Util::getDateTime();
-				$test->dts_rejected = null;
-				$test->dts_updated = Util::getDateTime();
-
-				if($unix_due) {
-					$test->dts_due = "$year-$month-$day 23:59:59";
-
-					if(strcmp($test->dts_due, $project->dts_start) < 0) throw new Exception("The date due cannot be before the project start date.");
-					if(strcmp($test->dts_due, $project->dts_deadline) > 0) throw new Exception("The date due cannot be beyond the project deadline.");
 
+				$success = $test->assignTester($user, $dt_due, $message);
+				if(!$success) {
+					throw new RuntimeException("Test cannot be assigned, please try again later.");
 				}
 
-				$success = $test->commit();
-				if(!$success) throw new Exception("Test cannot be assigned, please try again later.");
-
-				$test->sendAssignEmail($message);
-
 				JSON::success();
 
 				break;

+ 40 - 13
engine/test.php

@@ -615,28 +615,54 @@ class Test extends Model {
 
 	/**
 	 * @param User $tester
+	 * @param CarbonImmutable|null $dt_due
+	 * @param string|null $message
 	 * @return bool
 	 * @throws Exception
 	 */
-	public function assignTester(User $tester): bool {
+	public function assignTester(User $tester, ?CarbonImmutable $dt_due = null, ?string $message = null): bool {
+
+		if($tester->level === User::LEVEL_BANNED) {
+			throw new RuntimeException("A test cannot be assigned to an banned user.");
+		}
+		if($tester->level === User::LEVEL_REGISTERED) {
+			throw new RuntimeException("A test cannot be assigned to an unverified user.");
+		}
+		if($tester->level === User::LEVEL_UNVERIFIED) {
+			throw new RuntimeException("A test cannot be assigned to an unverified user.");
+		}
+		if($tester->level === User::LEVEL_DEACTIVATED) {
+			throw new RuntimeException("A test cannot be assigned to a deactivated user.");
+		}
+		if($tester->level !== User::LEVEL_TESTER) {
+			throw new RuntimeException("A test cannot be assigned to a user that's not a tester.");
+		}
+
 		$this->tester_id = $tester->id;
+		$this->status = self::STATUS_ASSIGNED;
 		$this->dts_assigned = Util::getDateTime();
 		$this->dts_rejected = null;
-		// TODO: Notify tester
+		$this->dts_updated = Util::getDateTime();
+		$this->dts_due = $dt_due ? $dt_due->utc()->toDateTimeString() : null;
 
-		return $this->commit();
-	}
+		if($this->dts_due) {
+			$project = $this->getProject();
 
-	/**
-	 * @return bool
-	 * @throws Exception
-	 */
-	public function unassign(): bool {
-		$this->tester_id = null;
-		$this->dts_assigned = null;
-		$this->dts_rejected = null;
+			if(strcmp($this->dts_due, $project->dts_start) < 0) {
+				throw new RuntimeException("The date due cannot be before the project start date.");
+			}
+			if(strcmp($this->dts_due, $project->dts_deadline) > 0) {
+				throw new RuntimeException("The date due cannot be beyond the project deadline.");
+			}
+		}
 
-		return $this->commit();
+		$success = $this->commit();
+
+		if($success) {
+			$this->sendAssignEmail($message ?? "");
+		}
+
+		return $success;
 	}
 
 	/**
@@ -703,6 +729,7 @@ class Test extends Model {
 		$this->dts_due = null;
 		$this->dts_assigned = null;
 		$this->dts_rejected = null;
+		$this->dts_updated = Util::getDateTime();
 		return $this->commit();
 	}