Просмотр исходного кода

Record when tester declines a test and flag up when searching for testers on a project

Alan Lam 3 лет назад
Родитель
Сommit
39c59f36c8

+ 20 - 0
controllers/project_ajax.php

@@ -171,6 +171,15 @@ if($submit) {
 					return Geolocation::getDistance($lat_lng, $a->getGeolocation()) <=> Geolocation::getDistance($lat_lng, $b->getGeolocation());
 				});
 
+				$user_ids = array_column($addresses, "user_id");
+
+				$reject_data = [];
+
+				$reject_rows = UserLocationReject::query(["user_id" => $user_ids, "location_id" => $location_id]);
+				foreach($reject_rows as $reject_row) {
+					$reject_data[$reject_row->user_id] = $reject_row;
+				}
+
 				$output = [];
 
 				foreach($addresses as $address) {
@@ -199,6 +208,14 @@ if($submit) {
 						continue;
 					}
 
+					$rejected = $reject_data[$user->id] ?? null;
+					$last_rejected = null;
+					$reject_count = 0;
+					if($rejected) {
+						$last_rejected = $rejected->getDateTimeLastRejected()->format("d/m/Y");
+						$reject_count = $rejected->reject_count;
+					}
+
 					$distance_miles = Geolocation::getDistance($location->getGeolocation(), $address->getGeolocation());
 					$distance_kilometers = $distance_miles * Geolocation::KMS_IN_MILE;
 
@@ -251,6 +268,9 @@ if($submit) {
 						"reportedComment" => $user->reported_comment,
 						"timeRegistered" => strtotime($user->dts_registered . " UTC"),
 						"timeLastTest" => $time_last_test,
+						"rejected" => (bool) $rejected,
+						"rejectCount" => (int) $reject_count,
+						"dateLastRejected" => $last_rejected
 					];
 
 				}

+ 16 - 0
db/migrations/20230125210436_add_user_location_reject_table.php

@@ -0,0 +1,16 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddUserLocationRejectTable extends AbstractMigration {
+	public function change() {
+		$this->table("user_location_reject", ["id" => false, "primary_key" => ["user_id", "location_id"]])
+			->addColumn("user_id", "integer", ["signed" => false])
+			->addColumn("location_id", "integer", ["signed" => false])
+			->addColumn("reject_count", "integer", ["signed" => false, "default" => 0])
+			->addColumn("dts_last_rejected", "datetime")
+			->addForeignKey("user_id", "user", "id", ["update" => "CASCADE", "delete" => "CASCADE"])
+			->addForeignKey("location_id", "user", "id", ["update" => "CASCADE", "delete" => "CASCADE"])
+			->create();
+	}
+}

+ 18 - 1
engine/test.php

@@ -409,12 +409,29 @@ class Test extends Model {
 	public function decline(): bool {
 		$this->sendDeclineEmail();
 
+		$user_id = $this->tester_id;
+		$location_id = $this->location_id;
+
 		$this->status = self::STATUS_UNASSIGNED;
 		$this->tester_id = null;
 		$this->dts_due = null;
 		$this->dts_assigned = null;
 		$this->dts_updated = Util::getDateTime();
-		return $this->commit();
+		$this->commit();
+
+		$reject = UserLocationReject::get($user_id, $location_id);
+		if(!$reject) {
+			$reject = new UserLocationReject();
+			$reject->user_id = $user_id;
+			$reject->location_id = $location_id;
+			$reject->reject_count = 0;
+		}
+
+		$reject->reject_count++;
+		$reject->dts_last_rejected = DB::dts();
+		$reject->commit();
+
+		return true;
 	}
 
 	/**

+ 25 - 0
engine/user_location_reject.php

@@ -0,0 +1,25 @@
+<?php
+
+use Carbon\CarbonImmutable;
+
+/**
+ * Class UserLocationReject
+ *
+ * @property int $user_id
+ * @property int $location_id
+ * @property int $reject_count
+ * @property string $dts_last_rejected
+ */
+class UserLocationReject extends Model {
+
+	protected static $table = "user_location_reject";
+	protected static $id_field = ["user_id", "location_id"];
+	protected static $fillable_fields = ["user_id", "location_id", "reject_count", "dts_last_rejected"];
+
+	// Object methods
+
+	public function getDateTimeLastRejected(): CarbonImmutable {
+		return CarbonImmutable::parse($this->dts_last_rejected, "UTC");
+	}
+
+}

+ 6 - 0
public/js/app-project.js

@@ -381,6 +381,12 @@ function initialiseTesters() {
 			}
 
 			trHtml += "</span>";
+
+			if(tester.rejected) {
+				trHtml += "<br />";
+				trHtml += `<span class="text-danger">This tester has last declined a test for this location on ${tester.dateLastRejected}. Decline count: ${tester.rejectCount}</span>`;
+			}
+
 			trHtml += "</td>";
 
 			const registeredText = moment.unix(tester.timeRegistered).format("D/M/YYYY");