app-project.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. var locationMarkers = [];
  2. var assignMarkers = [];
  3. var selectedLocation = false;
  4. var locationLatLngBounds;
  5. var assignLatLngBounds;
  6. var map = null;
  7. var infoWindow = false;
  8. var markerClusterer = false;
  9. let radiusCircle;
  10. var locations = [];
  11. var testers = [];
  12. $(function() {
  13. $(document).on("click", "button[data-find-testers-button]", handleFindTestersButtonClick);
  14. $(document).on("click", "button[data-assign-button]", handleAssignButtonClick);
  15. $(document).on("click", "#assign-confirm-button", handleConfirmAssignButtonClick);
  16. $("#modal-assign-tester").on("shown.bs.modal", function() {
  17. maps["assign-map"].fitBounds(assignLatLngBounds);
  18. });
  19. $("input[data-filter-target]").keyup(function() {
  20. let value = $(this).val().toLowerCase();
  21. let target = $(this).data("filter-target");
  22. if(value.length == 0) {
  23. $(target).children().show();
  24. return true;
  25. }
  26. $(target).children().each(function() {
  27. const $tr = $(this);
  28. const data = $tr.data();
  29. if(!data.filterValue) {
  30. return;
  31. }
  32. let number = parseInt(value);
  33. var show;
  34. if(isNaN(number)) {
  35. show = data.filterValue.indexOf(value) != -1;
  36. }
  37. else {
  38. let test1 = (number == data.locationId);
  39. let test2 = (number == data.internalId);
  40. let test3 = (number == data.testerId);
  41. show = test1 || test2 || test3;
  42. }
  43. if(show) {
  44. $tr.show();
  45. }
  46. else {
  47. $tr.hide();
  48. }
  49. });
  50. return true;
  51. });
  52. $("#testers-max-distance, #testers-include-unverified, #testers-minimum-score").on("change", function() {
  53. if(selectedLocation) {
  54. clearTesters();
  55. initialiseTesters();
  56. }
  57. });
  58. // Auto-select Location
  59. const autoSelectLocationId = $("#auto-select-location-id").val();
  60. if(autoSelectLocationId) {
  61. const interval = setInterval(() => {
  62. const $tr = $(`tr[data-location-id='${autoSelectLocationId}']`);
  63. if($tr.length > 0) {
  64. $tr.find("[data-find-testers-button]").trigger("click");
  65. clearInterval(interval);
  66. }
  67. }, 1000);
  68. }
  69. $(document).on("change", ".location-checkbox", event => {
  70. const locationIds = $(document).find(".location-checkbox:checked").map((index, element) => {
  71. return element.value;
  72. }).get();
  73. $("input[name=location_ids_json]").val(JSON.stringify(locationIds));
  74. });
  75. });
  76. function clearMarkers() {
  77. locationMarkers.forEach(marker => marker.setMap(null));
  78. locationMarkers = [];
  79. }
  80. function initialiseLocations() {
  81. if(!infoWindow) {
  82. infoWindow = new google.maps.InfoWindow();
  83. }
  84. map = maps["map"];
  85. if(!radiusCircle) {
  86. radiusCircle = new google.maps.Circle({
  87. map: map,
  88. radius: 8064,
  89. strokeOpacity: 0.25,
  90. fillOpacity: 0.125
  91. });
  92. }
  93. clearMarkers();
  94. $.get("/account/projects/" + projectID + "/get-locations", {
  95. submit: $("#submit").val()
  96. }, function(response) {
  97. if(!response.locations) return;
  98. locations = response.locations;
  99. $("#tests-tbody").empty();
  100. locationLatLngBounds = new google.maps.LatLngBounds;
  101. var unassignedCount = "";
  102. var assignedCount = "";
  103. var pendingVerificationCount = "";
  104. var completedCount = "";
  105. response.locations.forEach(location => {
  106. let position = {
  107. lat: location.latitude,
  108. lng: location.longitude
  109. };
  110. locationLatLngBounds.extend(position);
  111. let cssClass = location.class;
  112. let icon;
  113. if(cssClass == "danger") {
  114. icon = "/img/markers/building-red.png";
  115. unassignedCount++;
  116. }
  117. else if(cssClass == "primary") {
  118. icon = "/img/markers/building-blue.png";
  119. pendingVerificationCount++;
  120. }
  121. else if(cssClass == "success") {
  122. icon = "/img/markers/building-green.png";
  123. completedCount++;
  124. }
  125. else {
  126. icon = "/img/markers/building-orange.png";
  127. assignedCount++;
  128. }
  129. let options = {};
  130. options.position = position;
  131. options.map = map;
  132. options.icon = icon;
  133. options.title = location.name;
  134. let marker = new google.maps.Marker(options);
  135. marker.addListener("click", handleLocationClick);
  136. marker.location = location;
  137. locationMarkers.push(marker);
  138. var filterValueParts = [location.name, location.address];
  139. var filterValue = filterValueParts.join("").toLowerCase();
  140. // Populate table
  141. 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>");
  142. const $checkboxCell = $(`<td class="text-center"><input class="location-checkbox" type="checkbox" value="${location.id}" /></td>`);
  143. tr.append($checkboxCell);
  144. var locationIdTd = $("<td class=\"text-right\"></td>");
  145. locationIdTd.append(location.id);
  146. tr.append(locationIdTd);
  147. var a = $("<a>").attr("href", "/account/locations/" + location.id).attr("target", "_blank").text(location.name);
  148. var locationNameTd = $("<td></td>");
  149. locationNameTd.append(a);
  150. if(location.retest) {
  151. locationNameTd.append(" <span class=\"badge badge-primary\">Retest</span>");
  152. }
  153. locationNameTd.append("<br />");
  154. if(location.internalId) {
  155. var small = $("<span class=\"text-info\">").text("Internal ID: " + location.internalId);
  156. locationNameTd.append(small);
  157. locationNameTd.append("<br />");
  158. }
  159. var address = $("<span>").text(location.address1);
  160. locationNameTd.append(address);
  161. tr.append(locationNameTd);
  162. var statusTd = $("<td>").addClass("align-middle").addClass("text-center");
  163. statusTd.append($("<span>").addClass("badge").addClass("badge-" + cssClass).text(location.statusText));
  164. if(location.paymentRequired) {
  165. statusTd.append(`<br /><span class="badge badge-danger">Unpaid</span>`);
  166. }
  167. tr.append(statusTd);
  168. var passTd = $("<td>").addClass("align-middle").addClass("text-center").addClass("text-" + location.passClass);
  169. var div = $("<div>").attr("title", location.passText).attr("data-toggle", "tooltip");
  170. div.append($("<i>").addClass("fas").addClass("fa-fw").addClass("fa-" + location.passIcon));
  171. passTd.append(div);
  172. tr.append(passTd);
  173. div.tooltip();
  174. var buttonTd = $("<td>").addClass("align-middle").addClass("text-right");
  175. let button;
  176. if(cssClass == "danger" || cssClass == "warning") {
  177. 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();
  178. }
  179. else {
  180. 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();
  181. }
  182. buttonTd.append(button);
  183. tr.append(buttonTd);
  184. $("#tests-tbody").append(tr);
  185. });
  186. map.fitBounds(locationLatLngBounds);
  187. $("#unassigned-count").text(unassignedCount);
  188. $("#assigned-count").text(assignedCount);
  189. $("#pending-verification-count").text(pendingVerificationCount);
  190. $("#completed-count").text(completedCount);
  191. });
  192. }
  193. function handleFindTestersButtonClick() {
  194. const $container = $(this).closest("[data-location-id]");
  195. const data = $container.data();
  196. const locationId = parseInt(data.locationId);
  197. const testerId = parseInt(data.testerId);
  198. const lat = parseFloat(data.latitude);
  199. const lng = parseFloat(data.longitude);
  200. selectedLocation = {
  201. id: locationId,
  202. testerId: testerId,
  203. lat: lat,
  204. lng: lng
  205. };
  206. const latLng = {
  207. lat: lat,
  208. lng: lng
  209. };
  210. const interval = setInterval(() => {
  211. if(map !== null) {
  212. map.panTo(latLng);
  213. map.setZoom(12);
  214. radiusCircle.setCenter(latLng);
  215. clearInterval(interval);
  216. }
  217. }, 250);
  218. clearTesters();
  219. initialiseTesters();
  220. }
  221. function handleLocationClick() {
  222. // Info Window
  223. let content = "<strong><a href=\"/account/locations/" + this.location.id + "\" target=\"_blank\">" + this.location.name + "</a></strong><br />";
  224. if(this.location.internalId) {
  225. content += "Internal ID: " + this.location.internalId + "<br />";
  226. }
  227. content += "ID: " + this.location.id;
  228. infoWindow.setContent(content);
  229. infoWindow.open(map, this);
  230. // Set selected location
  231. selectedLocation = {
  232. id: this.location.id,
  233. testerId: this.location.testerId
  234. };
  235. // Pan map
  236. map.panTo(this.getPosition());
  237. // Update list groups
  238. radiusCircle.setCenter(this.getPosition());
  239. clearTesters();
  240. initialiseTesters();
  241. }
  242. function clearTesters() {
  243. if(markerClusterer) {
  244. markerClusterer.clearMarkers();
  245. }
  246. $("#tester-list-loading-container").text("Please select a location with a pending test.");
  247. $("#testers-tbody").empty();
  248. }
  249. function initialiseTesters() {
  250. let highlightClass = "table-warning";
  251. let testerId = selectedLocation.testerId;
  252. $("#tests-tbody tr").removeClass(highlightClass);
  253. $("#tests-tbody tr[data-location-id=" + selectedLocation.id + "]").addClass(highlightClass);
  254. map = maps["map"];
  255. if(!markerClusterer) {
  256. markerClusterer = new MarkerClusterer(map, [], {
  257. imagePath: '/img/markers/m',
  258. maxZoom: 10
  259. });
  260. }
  261. $("#tester-list-loading-container").text("Loading available testers...");
  262. let includeUnverified = $("#testers-include-unverified").is(":checked") ? 1 : 0;
  263. let minimumScore = $("#testers-minimum-score").val();
  264. let daysSinceLastTest = $("#testers-days-since-last-test").val();
  265. $.post(`/account/projects/${projectID}/get-testers`, {
  266. submit: $("#submit").val(),
  267. location_id: selectedLocation.id,
  268. include_unverified: includeUnverified,
  269. minimum_score: minimumScore,
  270. days_since_last_test: daysSinceLastTest
  271. }, function(response) {
  272. if(!response.success) return;
  273. let maxDistance = $("#testers-max-distance").val();
  274. let tbody = $("#testers-tbody").get(0);
  275. tbody.innerHTML = "";
  276. const setTbody = text => {
  277. const $tr = $(`<tr><td colspan="6" class="text-center"></td></tr>`);
  278. $tr.find("td").text(text);
  279. $(tbody).append($tr);
  280. };
  281. testers = response.testers.filter(tester => {
  282. return tester.distanceMiles <= maxDistance;
  283. });
  284. if(testers.length === 0) {
  285. setTbody("No testers are available for this location.");
  286. return;
  287. }
  288. let tbodyInnerHtml = "";
  289. testers.forEach(tester => {
  290. const position = {
  291. lat: tester.latitude,
  292. lng: tester.longitude
  293. };
  294. let icon = "user-green";
  295. if(tester.maxRadiusExceeded) {
  296. icon = "user-red";
  297. }
  298. else if(tester.pendingTestCount > 0) {
  299. icon = "user-orange";
  300. }
  301. let marker = new google.maps.Marker({
  302. map: map,
  303. position: position,
  304. icon: `/img/markers/${icon}.png`,
  305. title: tester.name,
  306. });
  307. marker.addressId = tester.id;
  308. marker.userId = tester.id;
  309. marker.tester = tester;
  310. marker.addListener("click", handleTesterClick);
  311. markerClusterer.addMarker(marker);
  312. var filterValueParts = [tester.name];
  313. var filterValue = filterValueParts.join("").toLowerCase();
  314. var testerClass = (tester.id == testerId) ? highlightClass : "";
  315. var scoreClass = "secondary";
  316. if(tester.score > 0) {
  317. scoreClass = "success";
  318. }
  319. else if(tester.score < 0) {
  320. scoreClass = "danger";
  321. }
  322. var distanceClass = "";
  323. if(tester.maxRadiusExceeded) {
  324. distanceClass = "text-danger";
  325. }
  326. var trHtml = "<tr class=\"" + testerClass + "\" data-tester-id=\"" + tester.id + "\" data-address-id=\"" + tester.addressId + "\" data-filter-value=\"" + filterValue + "\">";
  327. trHtml += "<td class=\"text-right\">" + tester.id + "</td>";
  328. 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 />";
  329. trHtml += "<span class=\"" + distanceClass + "\">";
  330. trHtml += tester.distanceMiles + " / " + tester.maxRadiusMiles + " miles";
  331. if(tester.maxRadiusExceeded) {
  332. trHtml += " <i class=\"fas fa-exclamation-circle\"></i>";
  333. }
  334. trHtml += "</span>";
  335. if(tester.rejected) {
  336. trHtml += "<br />";
  337. trHtml += `<span class="text-danger">This tester has last declined a test for this location on ${tester.dateLastRejected}. Decline count: ${tester.rejectCount}</span>`;
  338. }
  339. trHtml += "</td>";
  340. const registeredText = moment.unix(tester.timeRegistered).format("D/M/YYYY");
  341. const lastTestText = tester.timeLastTest ? moment.unix(tester.timeLastTest).format("D/M/YYYY") : "N/A";
  342. trHtml += "<td class=\"text-right align-middle\"><span class=\"text-danger\">" + registeredText + "</span><br /><span class=\"text-info\">" + lastTestText + "</span></td>";
  343. trHtml += "<td class=\"text-right align-middle\">" + tester.age + "</td>";
  344. trHtml += "<td class=\"text-right align-middle\">";
  345. if(!tester.termsAccepted) {
  346. trHtml += "<span class=\"badge badge-danger\" title=\"Terms and Conditions not accepted\">T&amp;C</span> ";
  347. }
  348. if(tester.reported) {
  349. trHtml += "<span class=\"badge badge-danger\" data-toggle=\"tooltip\" data-title=\"" + tester.reportedComment + "\">Reported</span> ";
  350. }
  351. trHtml += "<span class=\"badge badge-" + tester.levelClass + "\">" + tester.levelText + "</span><br />";
  352. trHtml += "<span>" + tester.phone + "</span>";
  353. trHtml += "<td class=\"text-right align-middle\">";
  354. if(!tester.isCurrentTester) {
  355. 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>";
  356. }
  357. trHtml += "</td>";
  358. trHtml += "</td>";
  359. trHtml += "</tr>";
  360. tbodyInnerHtml += trHtml;
  361. });
  362. tbody.innerHTML = tbodyInnerHtml;
  363. initialiseTooltips();
  364. $("input[data-filter-target]").trigger("keyup");
  365. });
  366. }
  367. function handleTesterClick() {
  368. // Info Window
  369. let content = "<strong><a href=\"/account/users/" + this.tester.id + "\" target=\"_blank\">" + this.tester.name + "</a></strong><br />";
  370. content += "ID: " + this.tester.id;
  371. infoWindow.setContent(content);
  372. infoWindow.open(map, this);
  373. }
  374. function handleAssignButtonClick() {
  375. let container = $(this).closest("[data-address-id]");
  376. let addressId = parseInt($(container).data("address-id"));
  377. let testerId = parseInt($(container).data("tester-id"));
  378. assignLatLngBounds = new google.maps.LatLngBounds;
  379. for(var i in assignMarkers) {
  380. let marker = assignMarkers[i];
  381. marker.setMap(null);
  382. marker = null;
  383. }
  384. // Find location
  385. var location = false;
  386. for(var i in locations) {
  387. if(locations[i].id == selectedLocation.id) {
  388. location = locations[i];
  389. break;
  390. }
  391. }
  392. let map = maps["assign-map"];
  393. if(location) {
  394. var position = {
  395. lat: parseFloat(location.latitude),
  396. lng: parseFloat(location.longitude)
  397. };
  398. assignLatLngBounds.extend(position);
  399. let options = {};
  400. options.map = map;
  401. options.position = position;
  402. options.icon = "/img/markers/building-green.png";
  403. options.title = location.name;
  404. let marker = new google.maps.Marker(options);
  405. assignMarkers.push(marker);
  406. $("#modal-assign-tester").find("[data-location-name]").text(location.name);
  407. if(location.retest) {
  408. $("#modal-assign-tester").find("[data-location-name]").append(" <span class=\"badge badge-primary\">Retest</span>");
  409. }
  410. $("#modal-assign-tester").find("[data-location-address]").text(location.address);
  411. }
  412. // Find tester
  413. var tester = false;
  414. for(var i in testers) {
  415. if(testers[i].addressId == addressId) {
  416. tester = testers[i];
  417. break;
  418. }
  419. }
  420. if(tester) {
  421. var position = {
  422. lat: parseFloat(tester.latitude),
  423. lng: parseFloat(tester.longitude)
  424. };
  425. assignLatLngBounds.extend(position);
  426. let options = {};
  427. options.map = map;
  428. options.position = position;
  429. options.icon = "/img/markers/user-green.png";
  430. options.title = tester.name;
  431. let marker = new google.maps.Marker(options);
  432. assignMarkers.push(marker);
  433. $("#modal-assign-tester").find("[data-tester-photo]").css("background-image", "url(/account/users/" + tester.id + "/photo)");
  434. $("#modal-assign-tester").find("[data-tester-name]").text(tester.name);
  435. $("#modal-assign-tester").find("[data-tester-age]").text(tester.age);
  436. $("#modal-assign-tester").find("[data-tester-level] span").attr("class", "badge badge-lg badge-" + tester.levelClass).text(tester.levelText);
  437. $("#modal-assign-tester").find("[data-tester-score]").text(tester.score);
  438. }
  439. $("#modal-assign-tester").modal("show");
  440. $("#assign-location-id").val(selectedLocation.id);
  441. $("#assign-address-id").val(addressId);
  442. }
  443. function handleConfirmAssignButtonClick(event) {
  444. event.preventDefault();
  445. var assignButton = this;
  446. var assignProgressContainer = $("#assign-progress-container").get(0);
  447. $(assignButton).attr("disabled", "");
  448. $(assignProgressContainer).removeClass("d-none");
  449. $(assignProgressContainer).removeClass("text-danger");
  450. $(assignProgressContainer).find("svg").addClass("fa-pulse");
  451. $(assignProgressContainer).find("span").text("Please wait...");
  452. let form = $(this).closest("form");
  453. let data = $(form).serialize() + "&submit=" + $("#submit").val();
  454. $.post("/account/projects/" + projectID + "/assign-tester", data, function(response) {
  455. $(assignButton).removeAttr("disabled");
  456. $(assignProgressContainer).find("svg").removeClass("fa-pulse");
  457. if(!response.success) {
  458. $(assignProgressContainer).addClass("text-danger");
  459. $(assignProgressContainer).find("span").text("Failed to assign tester.");
  460. $(assignProgressContainer).find("span").text(response.message);
  461. return;
  462. }
  463. $(assignProgressContainer).addClass("d-none");
  464. $("#modal-assign-tester").modal("hide");
  465. clearTesters();
  466. initialiseLocations();
  467. initialiseTesters();
  468. });
  469. }