Sfoglia il codice sorgente

Add days since last test filter to project page

Alan Lam 4 anni fa
parent
commit
984ebcbb04
5 ha cambiato i file con 133 aggiunte e 75 eliminazioni
  1. 25 23
      controllers/project_ajax.php
  2. 10 0
      engine/constraint.php
  3. 16 0
      engine/project.php
  4. 42 36
      public/js/app-project.js
  5. 40 16
      views/project.php

+ 25 - 23
controllers/project_ajax.php

@@ -5,12 +5,16 @@
  * @var string $action
  */
 
+use Carbon\CarbonImmutable;
+
 JSON::header();
 
 $project = Project::get($id);
 if(!$project) throw new Exception("Project cannot be found.");
 if(!Core::$user->hasPermission($project)) throw new Exception("You do not have permission to access this resource.");
 
+$dt_now = CarbonImmutable::now(TIMEZONE);
+
 $submit = http::value("submit");
 
 if($submit) {
@@ -105,16 +109,17 @@ if($submit) {
 				$location_id = http::value("location_id", "int");
 				$include_unverified = http::value("include_unverified", "int");
 				$minimum_score = http::value("minimum_score", "int");
-				$location = Location::get($location_id);
-				if(!$location) throw new Exception("Location not found.");
+				$days_since_last_test = http::value("days_since_last_test", "int");
 
-				$status_constraint = new Constraint("MORE_THAN_OR_EQUAL");
-				$status_constraint->setValues(Test::STATUS_UNASSIGNED);
+				$location = Location::get($location_id);
+				if(!$location) {
+					throw new RuntimeException("Location not found.");
+				}
 
 				$test = Test::queryOne([
 					"location_id" => $location_id,
 					"project_id" => $project->id,
-					"status" => $status_constraint,
+					"status" => Constraint::moreThanOrEquals(Test::STATUS_UNASSIGNED),
 				]);
 				if(!$test) {
 					throw new RuntimeException("A test does not exist for this project.");
@@ -134,26 +139,21 @@ if($submit) {
 
 				$bounds = Geolocation::getBounds($location->getGeolocation(), $max_distance);
 
-				$lat_constraint = new Constraint("BETWEEN");
-				$lat_constraint->setValues($bounds->southWest[0], $bounds->northEast[0]);
-
-				$lng_constraint = new Constraint("BETWEEN");
-				$lng_constraint->setValues($bounds->southWest[1], $bounds->northEast[1]);
-
 				$addresses = UserAddress::query([
-					"latitude" => $lat_constraint,
-					"longitude" => $lng_constraint
+					"latitude" => Constraint::between($bounds->southWest[0], $bounds->northEast[0]),
+					"longitude" => Constraint::between($bounds->southWest[1], $bounds->northEast[1])
 				]);
 
 				$users = User::load(array_column($addresses, "user_id"));
 				$user_last_tests = Test::query([
 					"tester_id" => array_column($users, "id"),
-					"dts_start" => new Constraint("IS_NOT_NULL"),
+					"dts_start" => Constraint::isNotNull(),
 				]);
 
 				$user_last_test_times = [];
 				foreach($user_last_tests as $user_last_test) {
-					$unix_start = strtotime($user_last_test->dts_start . " UTC");
+					$dt_start = $user_last_test->getDateTimeStart();
+					$unix_start = $dt_start->getTimestamp();
 					$current_start = $user_last_test_times[$user_last_test->tester_id] ?? 0;
 					if($unix_start > $current_start) {
 						$user_last_test_times[$user_last_test->tester_id] = $unix_start;
@@ -210,13 +210,7 @@ if($submit) {
 					$age = $user->getAge();
 
 					// Age check
-					if($project->min_age && $project->max_age && ($age < $project->min_age || $age > $project->max_age)) {
-						continue;
-					}
-					else if($project->min_age && $age < $project->min_age) {
-						continue;
-					}
-					else if($project->max_age && $age > $project->max_age) {
+					if(!$project->isAgeValid($age)) {
 						continue;
 					}
 
@@ -224,6 +218,14 @@ if($submit) {
 
 					$time_last_test = $user_last_test_times[$user->id] ?? false;
 
+					if($time_last_test && $days_since_last_test) {
+						$dt_last_test = CarbonImmutable::createFromTimestamp($time_last_test, TIMEZONE);
+						$days = $dt_last_test->diffInDays($dt_now);
+						if($days < $days_since_last_test) {
+							continue;
+						}
+					}
+
 					$output[] = [
 						"id" => (int) $user->id,
 						"addressId" => (int) $address->id,
@@ -244,7 +246,7 @@ if($submit) {
 						"isUnverified" => $is_unverified,
 						"levelText" => $user->getLevelText(),
 						"levelClass" => $user->getLevelClass(),
-						"termsAccepted" => boolval($user->terms_accepted),
+						"termsAccepted" => (bool) $user->terms_accepted,
 						"reported" => (int) $user->reported,
 						"reportedComment" => $user->reported_comment,
 						"timeRegistered" => strtotime($user->dts_registered . " UTC"),

+ 10 - 0
engine/constraint.php

@@ -55,6 +55,16 @@ class Constraint {
 		return $constraint;
 	}
 
+	public static function moreThanOrEquals(string $value): Constraint {
+		$constraint = new Constraint("MORE_THAN_OR_EQUAL");
+		$constraint->setValues($value);
+		return $constraint;
+	}
+
+	public static function isNotNull(): Constraint {
+		return new Constraint("IS_NOT_NULL");
+	}
+
 	/**
 	 * Constraint constructor.
 	 * @param string $type

+ 16 - 0
engine/project.php

@@ -591,5 +591,21 @@ class Project extends Model {
 		return time() > $this->getTimeDeadline();
 	}
 
+	public function isAgeValid(int $age): bool {
+		if($this->min_age && $this->max_age && ($age < $this->min_age || $age > $this->max_age)) {
+			return false;
+		}
+
+		if($this->min_age && $age < $this->min_age) {
+			return false;
+		}
+
+		if($this->max_age && $age > $this->max_age) {
+			return false;
+		}
+
+		return true;
+	}
+
 
 }

+ 42 - 36
public/js/app-project.js

@@ -207,12 +207,13 @@ function initialiseLocations() {
 
 function handleFindTestersButtonClick() {
 
-	var container = $(this).closest("[data-location-id]");
+	const $container = $(this).closest("[data-location-id]");
+	const data = $container.data();
 
-	var locationId = parseInt($(container).attr("data-location-id"));
-	var testerId = parseInt($(container).attr("data-tester-id"));
-	var lat = parseFloat($(container).attr("data-latitude"));
-	var lng = parseFloat($(container).attr("data-longitude"));
+	const locationId = parseInt(data.locationId);
+	const testerId = parseInt(data.testerId);
+	const lat = parseFloat(data.latitude);
+	const lng = parseFloat(data.longitude);
 
 	selectedLocation = {
 		id: locationId,
@@ -221,7 +222,7 @@ function handleFindTestersButtonClick() {
 		lng: lng
 	};
 
-	var latLng = {
+	const latLng = {
 		lat: lat,
 		lng: lng
 	};
@@ -290,51 +291,58 @@ function initialiseTesters() {
 
 	let includeUnverified = $("#testers-include-unverified").is(":checked") ? 1 : 0;
 	let minimumScore = $("#testers-minimum-score").val();
+	let daysSinceLastTest = $("#testers-days-since-last-test").val();
 
-	$.post("/account/projects/" + projectID + "/get-testers", {
+	$.post(`/account/projects/${projectID}/get-testers`, {
 		submit: $("#submit").val(),
 		location_id: selectedLocation.id,
 		include_unverified: includeUnverified,
-		minimum_score: minimumScore
+		minimum_score: minimumScore,
+		days_since_last_test: daysSinceLastTest
 	}, function(response) {
 		if(!response.success) return;
-		if(!response.testers || response.testers.length == 0) {
-			$("#tester-list-loading-container").text("No testers are available for this location.");
-			return;
-		}
-
-		testers = response.testers;
-
-		$("#tester-list-loading-container").text(response.testers.length + " tester addresses found in the vicinity.");
 
+		let maxDistance = $("#testers-max-distance").val();
 		let tbody = $("#testers-tbody").get(0);
 		tbody.innerHTML = "";
 
-		let tbodyInnerHtml = "";
+		const setTbody = text => {
+			const $tr = $(`<tr><td colspan="5" class="text-center p-3"></td></tr>`);
+			$tr.find("td").text(text);
+			$(tbody).append($tr);
+		};
 
-		let maxDistance = $("#testers-max-distance").val();
+		testers = response.testers.filter(tester => {
+			return tester.distanceMiles <= maxDistance;
+		});
 
-		for(let i in response.testers) {
-			let tester = response.testers[i];
+		if(testers.length === 0) {
+			setTbody("No testers are available for this location.");
+			return;
+		}
 
-			if(tester.distanceMiles > maxDistance) continue;
+		let tbodyInnerHtml = "";
 
-			let position = {
+		testers.forEach(tester => {
+			const position = {
 				lat: tester.latitude,
 				lng: tester.longitude
 			};
 
 			let icon = "user-green";
-			if(tester.maxRadiusExceeded) icon = "user-red";
-			else if(tester.pendingTestCount > 0) icon = "user-orange";
-
-			let options = {};
-			options.map = map;
-			options.position = position;
-			options.icon = "/img/markers/" + icon + ".png";
-			options.title = tester.name;
+			if(tester.maxRadiusExceeded) {
+				icon = "user-red";
+			}
+			else if(tester.pendingTestCount > 0) {
+				icon = "user-orange";
+			}
 
-			let marker = new google.maps.Marker(options);
+			let marker = new google.maps.Marker({
+				map: map,
+				position: position,
+				icon: `/img/markers/${icon}.png`,
+				title: tester.name,
+			});
 			marker.addressId = tester.id;
 			marker.userId = tester.id;
 			marker.tester = tester;
@@ -375,9 +383,8 @@ function initialiseTesters() {
 			trHtml += "</span>";
 			trHtml += "</td>";
 
-			var registeredText = moment.unix(tester.timeRegistered).format("D/M/YYYY");
-			var lastTestText = tester.timeLastTest ? moment.unix(tester.timeLastTest).format("D/M/YYYY") : "N/A";
-
+			const registeredText = moment.unix(tester.timeRegistered).format("D/M/YYYY");
+			const lastTestText = tester.timeLastTest ? moment.unix(tester.timeLastTest).format("D/M/YYYY") : "N/A";
 
 			trHtml += "<td class=\"text-right align-middle\"><span class=\"text-danger\">" + registeredText + "</span><br /><span class=\"text-info\">" + lastTestText + "</span></td>";
 			trHtml += "<td class=\"text-right align-middle\">" + tester.age + "</td>";
@@ -401,12 +408,11 @@ function initialiseTesters() {
 			}
 			trHtml += "</td>";
 
-
 			trHtml += "</td>";
 			trHtml += "</tr>";
 
 			tbodyInnerHtml += trHtml;
-		}
+		});
 
 		tbody.innerHTML = tbodyInnerHtml;
 

+ 40 - 16
views/project.php

@@ -218,32 +218,56 @@ var projectID = <?=$project->id?>;
 				</div>
 			</div>
 
-			<div class="row">
-				<div class="col-md">
+			<div class="form-row">
+				<div class="col-md-6">
 					<div class="input-group mb-3">
 						<div class="input-group-prepend">
-							<span class="input-group-text">
+							<label for="name-id" class="input-group-text">
 								<i class="fal fa-filter"></i>
-							</span>
+							</label>
+						</div>
+						<input class="form-control" type="text" id="name-id" placeholder="Name / ID" data-filter-target="#testers-tbody" />
+					</div>
+				</div>
+				<div class="col-md-6">
+					<div class="input-group mb-3">
+						<div class="input-group-prepend">
+							<label for="testers-minimum-score" class="input-group-text">
+								<i class="fal fa-chart-line"></i>
+							</label>
 						</div>
-						<input class="form-control" type="text" placeholder="Name / ID" data-filter-target="#testers-tbody" />
+						<select class="form-control" id="testers-minimum-score">
+							<option value="-999">Any score</option>
+							<option value="0">0+ score</option>
+							<option value="5">5+ score</option>
+							<option value="10">10+ score</option>
+							<option value="20">20+ score</option>
+						</select>
 					</div>
 				</div>
-				<div class="col-md">
-					<select class="form-control mb-3" id="testers-minimum-score">
-						<option value="-999">Any score</option>
-						<option value="0">0+ score</option>
-						<option value="5">5+ score</option>
-						<option value="10">10+ score</option>
-						<option value="20">20+ score</option>
-					</select>
+				<div class="col-md-6">
+					<div class="input-group mb-3">
+						<div class="input-group-prepend">
+							<label for="testers-days-since-last-test" class="input-group-text">
+								<i class="fal fa-clock"></i>
+							</label>
+						</div>
+						<select class="form-control" id="testers-days-since-last-test">
+							<option value=""><?=htmlspecialchars(__("Days since last test"))?></option>
+							<option value="30">30 days or over</option>
+							<option value="60">60 days or over</option>
+							<option value="90">90 days or over</option>
+							<option value="120">120 days or over</option>
+							<option value="3650">10 years or over</option>
+						</select>
+					</div>
 				</div>
-				<div class="col-md">
+				<div class="col-md-6">
 					<div class="input-group mb-3">
 						<div class="input-group-prepend">
-							<span class="input-group-text">
+							<label for="testers-max-distance" class="input-group-text">
 								<i class="fal fa-car"></i>
-							</span>
+							</label>
 						</div>
 						<select class="form-control" id="testers-max-distance">
 							<option value="5">5 miles</option>