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

Add coloured markers for testers in Map Overview page

Alan Lam 4 лет назад
Родитель
Сommit
fb8692c29c
2 измененных файлов с 42 добавлено и 22 удалено
  1. 12 1
      controllers/addresses_ajax.php
  2. 30 21
      public/js/app-map-overview.js

+ 12 - 1
controllers/addresses_ajax.php

@@ -1,6 +1,13 @@
 <?php
 
+/**
+ * @var string $action
+ */
+
+use Carbon\CarbonImmutable;
+
 $user = Core::$user;
+$dt_now = CarbonImmutable::now(TIMEZONE);
 
 JSON::header();
 
@@ -40,9 +47,12 @@ switch($action) {
 
 			$last_test_timestamp = $user_test_timestamps[$address->user_id] ?? null;
 			$last_test_date = null;
+			$days_since_last_test = null;
+
 			if($last_test_timestamp) {
-				$dt_last_test = DateTime::createFromFormat("U", $last_test_timestamp, new DateTimeZone(TIMEZONE));
+				$dt_last_test = CarbonImmutable::createFromTimestamp($last_test_timestamp, TIMEZONE);
 				$date_last_test = $dt_last_test->format("d/m/Y");
+				$days_since_last_test = $dt_last_test->diffInDays($dt_now);
 			}
 
 			$output[] = [
@@ -56,6 +66,7 @@ switch($action) {
 					"score" => (int) $user->score,
 					"dateRegistered" => $dt_registered->format("d/m/Y"),
 					"dateLastTest" => $date_last_test,
+					"daysSinceLastTest" => $days_since_last_test
 				]
 			];
 		}

+ 30 - 21
public/js/app-map-overview.js

@@ -222,7 +222,6 @@ function addLocationMarker(location) {
 }
 
 function loadAllAddresses() {
-
 	if(infoWindow === false) {
 		infoWindow = new google.maps.InfoWindow();
 	}
@@ -233,45 +232,55 @@ function loadAllAddresses() {
 	 * @property {Boolean} response.success
 	 */
 
-	let $allAddresses = $("a[data-all-addresses]");
+	const $allAddresses = $("a[data-all-addresses]");
 
 	$.get("/api/addresses/get-all", {
 		submit: $("#submit").val()
 	}, function(response) {
 		if(!response.success) return false;
 
-		let map = maps["map"];
-		let key = "all-addresses";
+		const map = maps["map"];
+		const key = "all-addresses";
 
 		markerGroups[key] = [];
 
-		let addressCount = response.addresses.length;
+		response.addresses.forEach(address => {
 
-		for(let i = 0; i < addressCount; ++i) {
+			const user = address.user;
+			let icon = "user-grey.png";
 
-			let address = response.addresses[i];
-
-			let position = {
-				lat: address.latitude,
-				lng: address.longitude
-			};
+			if(user.daysSinceLastTest !== null) {
+				console.log(user.daysSinceLastTest);
+				if(user.daysSinceLastTest > 90) {
+					icon = "user-grey.png";
+				}
+				else if(user.daysSinceLastTest > 60) {
+					icon = "user-green.png";
+				}
+				else if(user.daysSinceLastTest > 30) {
+					icon = "user-orange.png";
+				}
+				else {
+					icon = "user-red.png";
+				}
+			}
 
-			var options = {
-				position: position,
+			let marker = new google.maps.Marker({
+				position: {
+					lat: address.latitude,
+					lng: address.longitude
+				},
 				title: address.name,
-				map: map
-			};
-
-			let marker = new google.maps.Marker(options);
+				map: map,
+				icon: `/img/markers/${icon}`
+			});
 			addMarkerClickListenerForAddress(marker, address);
 
 			markerGroups[key].push(marker);
-
-		}
+		});
 
 		$allAddresses.data("selected", true);
 		$allAddresses.find(".badge").addClass("badge-danger").text(response.addresses.length);
-
 	});
 }