1
0

app-project.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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", ".test-checkbox", event => {
  70. const testIds = $(document).find(".test-checkbox:checked").map((index, element) => {
  71. return element.value;
  72. }).get();
  73. $("input[name=test_ids_json]").val(JSON.stringify(testIds));
  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="test-checkbox" type="checkbox" value="${location.testId}" /></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. if(location.paymentRequired) {
  154. if(location.paid) {
  155. locationNameTd.append(` <span class="badge badge-success">Paid</span>`);
  156. }
  157. else {
  158. locationNameTd.append(` <span class="badge badge-danger">Unpaid</span>`);
  159. }
  160. }
  161. locationNameTd.append("<br />");
  162. if(location.internalId) {
  163. var small = $("<span class=\"text-info\">").text("Internal ID: " + location.internalId);
  164. locationNameTd.append(small);
  165. locationNameTd.append("<br />");
  166. }
  167. var address = $("<span>").text(location.address1);
  168. locationNameTd.append(address);
  169. tr.append(locationNameTd);
  170. var statusTd = $("<td>").addClass("align-middle").addClass("text-center");
  171. statusTd.append($("<a>").addClass("badge").addClass("badge-" + cssClass).text(location.statusText).attr("href", `/account/tests/${location.testId}`));
  172. tr.append(statusTd);
  173. var passTd = $("<td>").addClass("align-middle").addClass("text-center").addClass("text-" + location.passClass);
  174. var div = $("<div>").attr("title", location.passText).attr("data-toggle", "tooltip");
  175. div.append($("<i>").addClass("fas").addClass("fa-fw").addClass("fa-" + location.passIcon));
  176. passTd.append(div);
  177. tr.append(passTd);
  178. div.tooltip();
  179. var buttonTd = $("<td>").addClass("align-middle").addClass("text-right");
  180. let button;
  181. if(cssClass == "danger" || cssClass == "warning") {
  182. 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();
  183. }
  184. else {
  185. 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();
  186. }
  187. buttonTd.append(button);
  188. tr.append(buttonTd);
  189. $("#tests-tbody").append(tr);
  190. });
  191. map.fitBounds(locationLatLngBounds);
  192. $("#unassigned-count").text(unassignedCount);
  193. $("#assigned-count").text(assignedCount);
  194. $("#pending-verification-count").text(pendingVerificationCount);
  195. $("#completed-count").text(completedCount);
  196. });
  197. }
  198. function handleFindTestersButtonClick() {
  199. const $container = $(this).closest("[data-location-id]");
  200. const data = $container.data();
  201. const locationId = parseInt(data.locationId);
  202. const testerId = parseInt(data.testerId);
  203. const lat = parseFloat(data.latitude);
  204. const lng = parseFloat(data.longitude);
  205. selectedLocation = {
  206. id: locationId,
  207. testerId: testerId,
  208. lat: lat,
  209. lng: lng
  210. };
  211. const latLng = {
  212. lat: lat,
  213. lng: lng
  214. };
  215. const interval = setInterval(() => {
  216. if(map !== null) {
  217. map.panTo(latLng);
  218. map.setZoom(12);
  219. radiusCircle.setCenter(latLng);
  220. clearInterval(interval);
  221. }
  222. }, 250);
  223. clearTesters();
  224. initialiseTesters();
  225. }
  226. function handleLocationClick() {
  227. // Info Window
  228. let content = "<strong><a href=\"/account/locations/" + this.location.id + "\" target=\"_blank\">" + this.location.name + "</a></strong><br />";
  229. if(this.location.internalId) {
  230. content += "Internal ID: " + this.location.internalId + "<br />";
  231. }
  232. content += "ID: " + this.location.id;
  233. infoWindow.setContent(content);
  234. infoWindow.open(map, this);
  235. // Set selected location
  236. selectedLocation = {
  237. id: this.location.id,
  238. testerId: this.location.testerId
  239. };
  240. // Pan map
  241. map.panTo(this.getPosition());
  242. // Update list groups
  243. radiusCircle.setCenter(this.getPosition());
  244. clearTesters();
  245. initialiseTesters();
  246. }
  247. function clearTesters() {
  248. if(markerClusterer) {
  249. markerClusterer.clearMarkers();
  250. }
  251. $("#tester-list-loading-container").text("Please select a location with a pending test.");
  252. $("#testers-tbody").empty();
  253. }
  254. function initialiseTesters() {
  255. let highlightClass = "table-warning";
  256. let testerId = selectedLocation.testerId;
  257. $("#tests-tbody tr").removeClass(highlightClass);
  258. $("#tests-tbody tr[data-location-id=" + selectedLocation.id + "]").addClass(highlightClass);
  259. map = maps["map"];
  260. if(!markerClusterer) {
  261. markerClusterer = new MarkerClusterer(map, [], {
  262. imagePath: '/img/markers/m',
  263. maxZoom: 10
  264. });
  265. }
  266. $("#tester-list-loading-container").text("Loading available testers...");
  267. let includeUnverified = $("#testers-include-unverified").is(":checked") ? 1 : 0;
  268. let minimumScore = $("#testers-minimum-score").val();
  269. let daysSinceLastTest = $("#testers-days-since-last-test").val();
  270. $.post(`/account/projects/${projectID}/get-testers`, {
  271. submit: $("#submit").val(),
  272. location_id: selectedLocation.id,
  273. include_unverified: includeUnverified,
  274. minimum_score: minimumScore,
  275. days_since_last_test: daysSinceLastTest
  276. }, function(response) {
  277. if(!response.success) return;
  278. let maxDistance = $("#testers-max-distance").val();
  279. let tbody = $("#testers-tbody").get(0);
  280. tbody.innerHTML = "";
  281. const setTbody = text => {
  282. const $tr = $(`<tr><td colspan="6" class="text-center"></td></tr>`);
  283. $tr.find("td").text(text);
  284. $(tbody).append($tr);
  285. };
  286. testers = response.testers.filter(tester => {
  287. return tester.distanceMiles <= maxDistance;
  288. });
  289. if(testers.length === 0) {
  290. setTbody("No testers are available for this location.");
  291. return;
  292. }
  293. let tbodyInnerHtml = "";
  294. testers.forEach(tester => {
  295. const position = {
  296. lat: tester.latitude,
  297. lng: tester.longitude
  298. };
  299. let icon = "user-green";
  300. if(tester.maxRadiusExceeded) {
  301. icon = "user-red";
  302. }
  303. else if(tester.pendingTestCount > 0) {
  304. icon = "user-orange";
  305. }
  306. let marker = new google.maps.Marker({
  307. map: map,
  308. position: position,
  309. icon: `/img/markers/${icon}.png`,
  310. title: tester.name,
  311. });
  312. marker.addressId = tester.id;
  313. marker.userId = tester.id;
  314. marker.tester = tester;
  315. marker.addListener("click", handleTesterClick);
  316. markerClusterer.addMarker(marker);
  317. var filterValueParts = [tester.name];
  318. var filterValue = filterValueParts.join("").toLowerCase();
  319. var testerClass = (tester.id == testerId) ? highlightClass : "";
  320. var scoreClass = "secondary";
  321. if(tester.score > 0) {
  322. scoreClass = "success";
  323. }
  324. else if(tester.score < 0) {
  325. scoreClass = "danger";
  326. }
  327. var distanceClass = "";
  328. if(tester.maxRadiusExceeded) {
  329. distanceClass = "text-danger";
  330. }
  331. var trHtml = "<tr class=\"" + testerClass + "\" data-tester-id=\"" + tester.id + "\" data-address-id=\"" + tester.addressId + "\" data-filter-value=\"" + filterValue + "\">";
  332. trHtml += "<td class=\"text-right\">" + tester.id + "</td>";
  333. 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 />";
  334. trHtml += "<span class=\"" + distanceClass + "\">";
  335. trHtml += tester.distanceMiles + " / " + tester.maxRadiusMiles + " miles";
  336. if(tester.maxRadiusExceeded) {
  337. trHtml += " <i class=\"fas fa-exclamation-circle\"></i>";
  338. }
  339. trHtml += "</span>";
  340. if(tester.rejected) {
  341. trHtml += "<br />";
  342. trHtml += `<span class="text-danger">This tester has last declined a test for this location on ${tester.dateLastRejected}. Decline count: ${tester.rejectCount}</span>`;
  343. }
  344. trHtml += "</td>";
  345. const registeredText = moment.unix(tester.timeRegistered).format("D/M/YYYY");
  346. const lastTestText = tester.timeLastTest ? moment.unix(tester.timeLastTest).format("D/M/YYYY") : "N/A";
  347. trHtml += "<td class=\"text-right align-middle\"><span class=\"text-danger\">" + registeredText + "</span><br /><span class=\"text-info\">" + lastTestText + "</span></td>";
  348. trHtml += "<td class=\"text-right align-middle\">" + tester.age + "</td>";
  349. trHtml += "<td class=\"text-right align-middle\">";
  350. if(!tester.termsAccepted) {
  351. trHtml += "<span class=\"badge badge-danger\" title=\"Terms and Conditions not accepted\">T&amp;C</span> ";
  352. }
  353. if(tester.reported) {
  354. trHtml += "<span class=\"badge badge-danger\" data-toggle=\"tooltip\" data-title=\"" + tester.reportedComment + "\">Reported</span> ";
  355. }
  356. trHtml += "<span class=\"badge badge-" + tester.levelClass + "\">" + tester.levelText + "</span><br />";
  357. trHtml += "<span>" + tester.phone + "</span>";
  358. trHtml += "<td class=\"text-right align-middle\">";
  359. if(!tester.isCurrentTester) {
  360. 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>";
  361. }
  362. trHtml += "</td>";
  363. trHtml += "</td>";
  364. trHtml += "</tr>";
  365. tbodyInnerHtml += trHtml;
  366. });
  367. tbody.innerHTML = tbodyInnerHtml;
  368. initialiseTooltips();
  369. $("input[data-filter-target]").trigger("keyup");
  370. });
  371. }
  372. function handleTesterClick() {
  373. // Info Window
  374. let content = "<strong><a href=\"/account/users/" + this.tester.id + "\" target=\"_blank\">" + this.tester.name + "</a></strong><br />";
  375. content += "ID: " + this.tester.id;
  376. infoWindow.setContent(content);
  377. infoWindow.open(map, this);
  378. }
  379. function handleAssignButtonClick() {
  380. let container = $(this).closest("[data-address-id]");
  381. let addressId = parseInt($(container).data("address-id"));
  382. let testerId = parseInt($(container).data("tester-id"));
  383. assignLatLngBounds = new google.maps.LatLngBounds;
  384. for(var i in assignMarkers) {
  385. let marker = assignMarkers[i];
  386. marker.setMap(null);
  387. marker = null;
  388. }
  389. // Find location
  390. var location = false;
  391. for(var i in locations) {
  392. if(locations[i].id == selectedLocation.id) {
  393. location = locations[i];
  394. break;
  395. }
  396. }
  397. let map = maps["assign-map"];
  398. if(location) {
  399. var position = {
  400. lat: parseFloat(location.latitude),
  401. lng: parseFloat(location.longitude)
  402. };
  403. assignLatLngBounds.extend(position);
  404. let options = {};
  405. options.map = map;
  406. options.position = position;
  407. options.icon = "/img/markers/building-green.png";
  408. options.title = location.name;
  409. let marker = new google.maps.Marker(options);
  410. assignMarkers.push(marker);
  411. $("#modal-assign-tester").find("[data-location-name]").text(location.name);
  412. if(location.retest) {
  413. $("#modal-assign-tester").find("[data-location-name]").append(" <span class=\"badge badge-primary\">Retest</span>");
  414. }
  415. $("#modal-assign-tester").find("[data-location-address]").text(location.address);
  416. }
  417. // Find tester
  418. var tester = false;
  419. for(var i in testers) {
  420. if(testers[i].addressId == addressId) {
  421. tester = testers[i];
  422. break;
  423. }
  424. }
  425. if(tester) {
  426. var position = {
  427. lat: parseFloat(tester.latitude),
  428. lng: parseFloat(tester.longitude)
  429. };
  430. assignLatLngBounds.extend(position);
  431. let options = {};
  432. options.map = map;
  433. options.position = position;
  434. options.icon = "/img/markers/user-green.png";
  435. options.title = tester.name;
  436. let marker = new google.maps.Marker(options);
  437. assignMarkers.push(marker);
  438. $("#modal-assign-tester").find("[data-tester-photo]").css("background-image", "url(/account/users/" + tester.id + "/photo)");
  439. $("#modal-assign-tester").find("[data-tester-name]").text(tester.name);
  440. $("#modal-assign-tester").find("[data-tester-age]").text(tester.age);
  441. $("#modal-assign-tester").find("[data-tester-level] span").attr("class", "badge badge-lg badge-" + tester.levelClass).text(tester.levelText);
  442. $("#modal-assign-tester").find("[data-tester-score]").text(tester.score);
  443. }
  444. $("#modal-assign-tester").modal("show");
  445. $("#assign-location-id").val(selectedLocation.id);
  446. $("#assign-address-id").val(addressId);
  447. }
  448. function handleConfirmAssignButtonClick(event) {
  449. event.preventDefault();
  450. var assignButton = this;
  451. var assignProgressContainer = $("#assign-progress-container").get(0);
  452. $(assignButton).attr("disabled", "");
  453. $(assignProgressContainer).removeClass("d-none");
  454. $(assignProgressContainer).removeClass("text-danger");
  455. $(assignProgressContainer).find("svg").addClass("fa-pulse");
  456. $(assignProgressContainer).find("span").text("Please wait...");
  457. let form = $(this).closest("form");
  458. let data = $(form).serialize() + "&submit=" + $("#submit").val();
  459. $.post("/account/projects/" + projectID + "/assign-tester", data, function(response) {
  460. $(assignButton).removeAttr("disabled");
  461. $(assignProgressContainer).find("svg").removeClass("fa-pulse");
  462. if(!response.success) {
  463. $(assignProgressContainer).addClass("text-danger");
  464. $(assignProgressContainer).find("span").text("Failed to assign tester.");
  465. $(assignProgressContainer).find("span").text(response.message);
  466. return;
  467. }
  468. $(assignProgressContainer).addClass("d-none");
  469. $("#modal-assign-tester").modal("hide");
  470. clearTesters();
  471. initialiseLocations();
  472. initialiseTesters();
  473. });
  474. }