| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- var locationMarkers = [];
- var assignMarkers = [];
- var selectedLocation = false;
- var locationLatLngBounds;
- var assignLatLngBounds;
- var map = null;
- var infoWindow = false;
- var markerClusterer = false;
- let radiusCircle;
- var locations = [];
- var testers = [];
- $(function() {
- $(document).on("click", "button[data-find-testers-button]", handleFindTestersButtonClick);
- $(document).on("click", "button[data-assign-button]", handleAssignButtonClick);
- $(document).on("click", "#assign-confirm-button", handleConfirmAssignButtonClick);
- $("#modal-assign-tester").on("shown.bs.modal", function() {
- maps["assign-map"].fitBounds(assignLatLngBounds);
- });
- $("input[data-filter-target]").keyup(function() {
- let value = $(this).val().toLowerCase();
- let target = $(this).data("filter-target");
- if(value.length == 0) {
- $(target).children().show();
- return true;
- }
- $(target).children().each(function() {
- const $tr = $(this);
- const data = $tr.data();
- if(!data.filterValue) {
- return;
- }
- let number = parseInt(value);
- var show;
- if(isNaN(number)) {
- show = data.filterValue.indexOf(value) != -1;
- }
- else {
- let test1 = (number == data.locationId);
- let test2 = (number == data.internalId);
- let test3 = (number == data.testerId);
- show = test1 || test2 || test3;
- }
- if(show) {
- $tr.show();
- }
- else {
- $tr.hide();
- }
- });
- return true;
- });
- $("#testers-max-distance, #testers-include-unverified, #testers-minimum-score").on("change", function() {
- if(selectedLocation) {
- clearTesters();
- initialiseTesters();
- }
- });
- // Auto-select Location
- const autoSelectLocationId = $("#auto-select-location-id").val();
- if(autoSelectLocationId) {
- const interval = setInterval(() => {
- const $tr = $(`tr[data-location-id='${autoSelectLocationId}']`);
- if($tr.length > 0) {
- $tr.find("[data-find-testers-button]").trigger("click");
- clearInterval(interval);
- }
- }, 1000);
- }
- $(document).on("change", ".test-checkbox", event => {
- const testIds = $(document).find(".test-checkbox:checked").map((index, element) => {
- return element.value;
- }).get();
- $("input[name=test_ids_json]").val(JSON.stringify(testIds));
- });
- });
- function clearMarkers() {
- locationMarkers.forEach(marker => marker.setMap(null));
- locationMarkers = [];
- }
- function initialiseLocations() {
- if(!infoWindow) {
- infoWindow = new google.maps.InfoWindow();
- }
- map = maps["map"];
- if(!radiusCircle) {
- radiusCircle = new google.maps.Circle({
- map: map,
- radius: 8064,
- strokeOpacity: 0.25,
- fillOpacity: 0.125
- });
- }
- clearMarkers();
- $.get("/account/projects/" + projectID + "/get-locations", {
- submit: $("#submit").val()
- }, function(response) {
- if(!response.locations) return;
- locations = response.locations;
- $("#tests-tbody").empty();
- locationLatLngBounds = new google.maps.LatLngBounds;
- var unassignedCount = "";
- var assignedCount = "";
- var pendingVerificationCount = "";
- var completedCount = "";
- response.locations.forEach(location => {
- let position = {
- lat: location.latitude,
- lng: location.longitude
- };
- locationLatLngBounds.extend(position);
- let cssClass = location.class;
- let icon;
- if(cssClass == "danger") {
- icon = "/img/markers/building-red.png";
- unassignedCount++;
- }
- else if(cssClass == "primary") {
- icon = "/img/markers/building-blue.png";
- pendingVerificationCount++;
- }
- else if(cssClass == "success") {
- icon = "/img/markers/building-green.png";
- completedCount++;
- }
- else {
- icon = "/img/markers/building-orange.png";
- assignedCount++;
- }
- let options = {};
- options.position = position;
- options.map = map;
- options.icon = icon;
- options.title = location.name;
- let marker = new google.maps.Marker(options);
- marker.addListener("click", handleLocationClick);
- marker.location = location;
- locationMarkers.push(marker);
- var filterValueParts = [location.name, location.address];
- var filterValue = filterValueParts.join("").toLowerCase();
- // Populate table
- var tr = $("<tr data-location-id=\"" + location.id + "\" data-internal-id=\"" + location.internalId + "\" data-tester-id=\"" + location.testerId + "\" data-latitude=\"" + location.latitude + "\" data-longitude=\"" + location.longitude + "\" data-filter-value=\"" + filterValue + "\"></tr>");
- const $checkboxCell = $(`<td class="text-center"><input class="test-checkbox" type="checkbox" value="${location.testId}" /></td>`);
- tr.append($checkboxCell);
- var locationIdTd = $("<td class=\"text-right\"></td>");
- locationIdTd.append(location.id);
- tr.append(locationIdTd);
- var a = $("<a>").attr("href", "/account/locations/" + location.id).attr("target", "_blank").text(location.name);
- var locationNameTd = $("<td></td>");
- locationNameTd.append(a);
- if(location.retest) {
- locationNameTd.append(" <span class=\"badge badge-primary\">Retest</span>");
- }
- if(location.paymentRequired) {
- if(location.paid) {
- locationNameTd.append(` <span class="badge badge-success">Paid</span>`);
- }
- else {
- locationNameTd.append(` <span class="badge badge-danger">Unpaid</span>`);
- }
- }
- locationNameTd.append("<br />");
- if(location.internalId) {
- var small = $("<span class=\"text-info\">").text("Internal ID: " + location.internalId);
- locationNameTd.append(small);
- locationNameTd.append("<br />");
- }
- var address = $("<span>").text(location.address1);
- locationNameTd.append(address);
- tr.append(locationNameTd);
- var statusTd = $("<td>").addClass("align-middle").addClass("text-center");
- statusTd.append($("<a>").addClass("badge").addClass("badge-" + cssClass).text(location.statusText).attr("href", `/account/tests/${location.testId}`));
- tr.append(statusTd);
- var passTd = $("<td>").addClass("align-middle").addClass("text-center").addClass("text-" + location.passClass);
- var div = $("<div>").attr("title", location.passText).attr("data-toggle", "tooltip");
- div.append($("<i>").addClass("fas").addClass("fa-fw").addClass("fa-" + location.passIcon));
- passTd.append(div);
- tr.append(passTd);
- div.tooltip();
- var buttonTd = $("<td>").addClass("align-middle").addClass("text-right");
- let button;
- if(cssClass == "danger" || cssClass == "warning") {
- button = $("<button data-find-testers-button title=\"Find Testers\" data-toggle=\"tooltip\">").addClass("btn").addClass("btn-outline-primary").html("<i class=\"fal fa-fw fa-search\"></i>").tooltip();
- }
- else {
- button = $("<a class=\"btn btn-outline-success\" href=\"/account/tests/" + location.testId + "\" title=\"View Test\" data-toggle=\"tooltip\" >").html("<i class=\"fal fa-fw fa-clipboard-check\"></i>").tooltip();
- }
- buttonTd.append(button);
- tr.append(buttonTd);
- $("#tests-tbody").append(tr);
- });
- map.fitBounds(locationLatLngBounds);
- $("#unassigned-count").text(unassignedCount);
- $("#assigned-count").text(assignedCount);
- $("#pending-verification-count").text(pendingVerificationCount);
- $("#completed-count").text(completedCount);
- });
- }
- function handleFindTestersButtonClick() {
- const $container = $(this).closest("[data-location-id]");
- const data = $container.data();
- const locationId = parseInt(data.locationId);
- const testerId = parseInt(data.testerId);
- const lat = parseFloat(data.latitude);
- const lng = parseFloat(data.longitude);
- selectedLocation = {
- id: locationId,
- testerId: testerId,
- lat: lat,
- lng: lng
- };
- const latLng = {
- lat: lat,
- lng: lng
- };
- const interval = setInterval(() => {
- if(map !== null) {
- map.panTo(latLng);
- map.setZoom(12);
- radiusCircle.setCenter(latLng);
- clearInterval(interval);
- }
- }, 250);
- clearTesters();
- initialiseTesters();
- }
- function handleLocationClick() {
- // Info Window
- let content = "<strong><a href=\"/account/locations/" + this.location.id + "\" target=\"_blank\">" + this.location.name + "</a></strong><br />";
- if(this.location.internalId) {
- content += "Internal ID: " + this.location.internalId + "<br />";
- }
- content += "ID: " + this.location.id;
- infoWindow.setContent(content);
- infoWindow.open(map, this);
- // Set selected location
- selectedLocation = {
- id: this.location.id,
- testerId: this.location.testerId
- };
- // Pan map
- map.panTo(this.getPosition());
- // Update list groups
- radiusCircle.setCenter(this.getPosition());
- clearTesters();
- initialiseTesters();
- }
- function clearTesters() {
- if(markerClusterer) {
- markerClusterer.clearMarkers();
- }
- $("#tester-list-loading-container").text("Please select a location with a pending test.");
- $("#testers-tbody").empty();
- }
- function initialiseTesters() {
- let highlightClass = "table-warning";
- let testerId = selectedLocation.testerId;
- $("#tests-tbody tr").removeClass(highlightClass);
- $("#tests-tbody tr[data-location-id=" + selectedLocation.id + "]").addClass(highlightClass);
- map = maps["map"];
- if(!markerClusterer) {
- markerClusterer = new MarkerClusterer(map, [], {
- imagePath: '/img/markers/m',
- maxZoom: 10
- });
- }
- $("#tester-list-loading-container").text("Loading available testers...");
- 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`, {
- submit: $("#submit").val(),
- location_id: selectedLocation.id,
- include_unverified: includeUnverified,
- minimum_score: minimumScore,
- days_since_last_test: daysSinceLastTest
- }, function(response) {
- if(!response.success) return;
- let maxDistance = $("#testers-max-distance").val();
- let tbody = $("#testers-tbody").get(0);
- tbody.innerHTML = "";
- const setTbody = text => {
- const $tr = $(`<tr><td colspan="6" class="text-center"></td></tr>`);
- $tr.find("td").text(text);
- $(tbody).append($tr);
- };
- testers = response.testers.filter(tester => {
- return tester.distanceMiles <= maxDistance;
- });
- if(testers.length === 0) {
- setTbody("No testers are available for this location.");
- return;
- }
- let tbodyInnerHtml = "";
- 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 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;
- marker.addListener("click", handleTesterClick);
- markerClusterer.addMarker(marker);
- var filterValueParts = [tester.name];
- var filterValue = filterValueParts.join("").toLowerCase();
- var testerClass = (tester.id == testerId) ? highlightClass : "";
- var scoreClass = "secondary";
- if(tester.score > 0) {
- scoreClass = "success";
- }
- else if(tester.score < 0) {
- scoreClass = "danger";
- }
- var distanceClass = "";
- if(tester.maxRadiusExceeded) {
- distanceClass = "text-danger";
- }
- var trHtml = "<tr class=\"" + testerClass + "\" data-tester-id=\"" + tester.id + "\" data-address-id=\"" + tester.addressId + "\" data-filter-value=\"" + filterValue + "\">";
- trHtml += "<td class=\"text-right\">" + tester.id + "</td>";
- trHtml += "<td><a href=\"/account/users/" + tester.id + "\" target=\"_blank\">" + tester.name + "</a> <span class=\"badge badge-pill badge-" + scoreClass + "\" title=\"Score\">" + tester.score + "</span><br />";
- trHtml += "<span class=\"" + distanceClass + "\">";
- trHtml += tester.distanceMiles + " / " + tester.maxRadiusMiles + " miles";
- if(tester.maxRadiusExceeded) {
- trHtml += " <i class=\"fas fa-exclamation-circle\"></i>";
- }
- 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");
- 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>";
- trHtml += "<td class=\"text-right align-middle\">";
- if(!tester.termsAccepted) {
- trHtml += "<span class=\"badge badge-danger\" title=\"Terms and Conditions not accepted\">T&C</span> ";
- }
- if(tester.reported) {
- trHtml += "<span class=\"badge badge-danger\" data-toggle=\"tooltip\" data-title=\"" + tester.reportedComment + "\">Reported</span> ";
- }
- trHtml += "<span class=\"badge badge-" + tester.levelClass + "\">" + tester.levelText + "</span><br />";
- trHtml += "<span>" + tester.phone + "</span>";
- trHtml += "<td class=\"text-right align-middle\">";
- if(!tester.isCurrentTester) {
- trHtml += "<button class=\"btn btn-outline-primary\" data-assign-button title=\"Assign\" data-toggle=\"tooltip\"><i class=\"fal fa-fw fa-user-plus\"></i></button>";
- }
- trHtml += "</td>";
- trHtml += "</td>";
- trHtml += "</tr>";
- tbodyInnerHtml += trHtml;
- });
- tbody.innerHTML = tbodyInnerHtml;
- initialiseTooltips();
- $("input[data-filter-target]").trigger("keyup");
- });
- }
- function handleTesterClick() {
- // Info Window
- let content = "<strong><a href=\"/account/users/" + this.tester.id + "\" target=\"_blank\">" + this.tester.name + "</a></strong><br />";
- content += "ID: " + this.tester.id;
- infoWindow.setContent(content);
- infoWindow.open(map, this);
- }
- function handleAssignButtonClick() {
- let container = $(this).closest("[data-address-id]");
- let addressId = parseInt($(container).data("address-id"));
- let testerId = parseInt($(container).data("tester-id"));
- assignLatLngBounds = new google.maps.LatLngBounds;
- for(var i in assignMarkers) {
- let marker = assignMarkers[i];
- marker.setMap(null);
- marker = null;
- }
- // Find location
- var location = false;
- for(var i in locations) {
- if(locations[i].id == selectedLocation.id) {
- location = locations[i];
- break;
- }
- }
- let map = maps["assign-map"];
- if(location) {
- var position = {
- lat: parseFloat(location.latitude),
- lng: parseFloat(location.longitude)
- };
- assignLatLngBounds.extend(position);
- let options = {};
- options.map = map;
- options.position = position;
- options.icon = "/img/markers/building-green.png";
- options.title = location.name;
- let marker = new google.maps.Marker(options);
- assignMarkers.push(marker);
- $("#modal-assign-tester").find("[data-location-name]").text(location.name);
- if(location.retest) {
- $("#modal-assign-tester").find("[data-location-name]").append(" <span class=\"badge badge-primary\">Retest</span>");
- }
- $("#modal-assign-tester").find("[data-location-address]").text(location.address);
- }
- // Find tester
- var tester = false;
- for(var i in testers) {
- if(testers[i].addressId == addressId) {
- tester = testers[i];
- break;
- }
- }
- if(tester) {
- var position = {
- lat: parseFloat(tester.latitude),
- lng: parseFloat(tester.longitude)
- };
- assignLatLngBounds.extend(position);
- let options = {};
- options.map = map;
- options.position = position;
- options.icon = "/img/markers/user-green.png";
- options.title = tester.name;
- let marker = new google.maps.Marker(options);
- assignMarkers.push(marker);
- $("#modal-assign-tester").find("[data-tester-photo]").css("background-image", "url(/account/users/" + tester.id + "/photo)");
- $("#modal-assign-tester").find("[data-tester-name]").text(tester.name);
- $("#modal-assign-tester").find("[data-tester-age]").text(tester.age);
- $("#modal-assign-tester").find("[data-tester-level] span").attr("class", "badge badge-lg badge-" + tester.levelClass).text(tester.levelText);
- $("#modal-assign-tester").find("[data-tester-score]").text(tester.score);
- }
- $("#modal-assign-tester").modal("show");
- $("#assign-location-id").val(selectedLocation.id);
- $("#assign-address-id").val(addressId);
- }
- function handleConfirmAssignButtonClick(event) {
- event.preventDefault();
- var assignButton = this;
- var assignProgressContainer = $("#assign-progress-container").get(0);
- $(assignButton).attr("disabled", "");
- $(assignProgressContainer).removeClass("d-none");
- $(assignProgressContainer).removeClass("text-danger");
- $(assignProgressContainer).find("svg").addClass("fa-pulse");
- $(assignProgressContainer).find("span").text("Please wait...");
- let form = $(this).closest("form");
- let data = $(form).serialize() + "&submit=" + $("#submit").val();
- $.post("/account/projects/" + projectID + "/assign-tester", data, function(response) {
- $(assignButton).removeAttr("disabled");
- $(assignProgressContainer).find("svg").removeClass("fa-pulse");
- if(!response.success) {
- $(assignProgressContainer).addClass("text-danger");
- $(assignProgressContainer).find("span").text("Failed to assign tester.");
- $(assignProgressContainer).find("span").text(response.message);
- return;
- }
- $(assignProgressContainer).addClass("d-none");
- $("#modal-assign-tester").modal("hide");
- clearTesters();
- initialiseLocations();
- initialiseTesters();
- });
- }
|