Преглед изворни кода

Add test map to tester's test page

Alan Lam пре 8 година
родитељ
комит
908a63b8f9

+ 64 - 2
controllers/tests_tester_list.php

@@ -1,9 +1,71 @@
 <?php
 
+Core::addScript("public/js/app-tests-tester-list.js");
+
 $tests = $user->getPendingTests();
-$testers = User::load(array_column($tests, "tester_id"));
+$locations = Location::load(array_column($tests, "location_id"));
+
+$bounds = new GeolocationBounds;
+
+foreach($tests as $test) {
+	$location = $test->getLocation();
+	$bounds->extend($location->latitude, $location->longitude);
+}
+
+$latitude = 51.3555177;
+$longitude = -1.1743197;
+
+if(HTTP::isAJAX()) {
+	JSON::header();
+
+	$action = http::value("action");
+	switch($action) {
+		case "get-tests":
+
+			$output = [];
+			foreach($tests as $test) {
+				$location = $test->getLocation();
+				$icon = null;
+
+				switch($test->status) {
+					case 2:
+						$icon = "building-orange";
+						break;
+					case 3:
+						$icon = "building-green";
+						break;
+					case 4:
+						$icon = "building-pink";
+						break;
+					case 8:
+						$icon = "building-grey";
+						break;
+				}
+
+				$output[] = (object) [
+					"status" => $test->getStatusText(),
+					"statusClass" => $test->getStatusClass(),
+					"name" => $location->name,
+					"address" => $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code", "state"]),
+					"icon" => $icon,
+					"geolocation" => $location->getGeolocation(),
+				];
+			}
+
+			JSON::success([
+				"tests" => $output,
+				"southWest" => $bounds->getSouthWest(),
+				"northEast" => $bounds->getNorthEast(),
+			]);
+
+			break;
+		default:
+			JSON::fail("Invalid action");
+	}
+
+}
 
 View::includeOnce("header", compact("page_title"));
-View::include("test_list_assigned", compact("tests"));
+View::include("test_list_assigned", compact("tests", "latitude", "longitude"));
 
 ?>

+ 44 - 0
engine/geolocation_bounds.php

@@ -0,0 +1,44 @@
+<?php
+
+class GeolocationBounds {
+
+	protected $minLat = 180;
+	protected $minLng = 180;
+	protected $maxLat = -180;
+	protected $maxLng = -180;
+
+	public function __construct() {
+	}
+
+	public function extend($lat, $lng) {
+
+		if($lat > $this->maxLat) $this->maxLat = $lat;
+		if($lng > $this->maxLng) $this->maxLng = $lng;
+
+		if($lat < $this->minLat) $this->minLat = $lat;
+		if($lng < $this->minLng) $this->minLng = $lng;
+
+	}
+
+	public function getCenter() {
+		return $this->getCentre();
+	}
+
+	public function getCentre() {
+		return [
+			($this->minLat + $this->maxLat) / 2,
+			($this->minLng + $this->maxLng) / 2,
+		];
+	}
+
+	public function getSouthWest() {
+		return [$this->minLat, $this->minLng];
+	}
+
+	public function getNorthEast() {
+		return [$this->maxLat, $this->maxLng];
+	}
+
+}
+
+?>

+ 59 - 0
public/js/app-tests-tester-list.js

@@ -0,0 +1,59 @@
+$(function() {
+
+});
+
+var infoWindow = false;
+
+function initialiseMarkers() {
+
+	if(infoWindow == false) {
+		infoWindow = new google.maps.InfoWindow();
+	}
+
+
+	$.get(window.location, {
+		action: "get-tests"
+	}, function(response) {
+
+		if(!response.success) return;
+
+		let tests = response.tests;
+		let map = maps["map"];
+		let bounds = new google.maps.LatLngBounds();
+
+		for(let i in tests) {
+			let test = tests[i];
+			let latLng = {
+				lat: test.geolocation[0],
+				lng: test.geolocation[1]
+			};
+
+			bounds.extend(latLng);
+
+			let markerOptions = {
+				position: latLng,
+				map: map,
+				title: test.name
+			};
+
+			if(test.icon) {
+				markerOptions.icon = "/img/markers/" + test.icon + ".png";
+			}
+
+			let marker = new google.maps.Marker(markerOptions);
+
+			marker.addListener("click", function() {
+				infoWindow.setContent("<strong>" + test.name + "</strong> " + "<span class=\"badge badge-" + test.statusClass + "\">" + test.status + "</span>" + "<br />" +
+				"<small>" + test.address + "</small>"
+				);
+				infoWindow.open(map, marker);
+			});
+
+		}
+
+		map.fitBounds(bounds);
+
+
+
+	});
+}

+ 3 - 1
views/test_list_assigned.php

@@ -3,7 +3,6 @@
 	<h2>Your Tests</h2>
 
 
-
 	<?php
 
 	if(empty($tests)) {
@@ -15,6 +14,9 @@
 	}
 	else {
 		?>
+		<div class="mb-3" id="map" data-maps data-lat="<?=$latitude?>" data-lng="<?=$longitude?>" data-zoom="7" data-callback="initialiseMarkers" style="height: 24rem; width: 100%;">
+		</div>
+
 		<div class="list-group">
 
 		<?php