From fa3135b65b2f61450b06b418c718148eedcabd0c Mon Sep 17 00:00:00 2001 From: Philip Eichinski Date: Fri, 10 Aug 2018 13:25:04 +1000 Subject: [PATCH] feat(citizenScience): responses page for viewing locally saved responses --- src/app/app.js | 7 ++ .../citizenScience/bristlebird/bristlebird.js | 9 +- src/app/citizenScience/citizenScience.js | 1 + .../citizenScienceSamples.js | 70 +-------------- .../labels/citizenScienceLabels.js | 85 +++++++++++++++++++ .../citizenScienceSampleLabels.js | 70 ++++++++++++++- src/app/citizenScience/responses/responses.js | 53 ++++++++++++ .../responses/responses.tpl.html | 20 +++++ src/baw.paths.nobuild.js | 1 + 9 files changed, 242 insertions(+), 74 deletions(-) rename src/app/citizenScience/{dummyApi => datasetProgress}/citizenScienceSamples.js (75%) create mode 100644 src/app/citizenScience/labels/citizenScienceLabels.js rename src/app/citizenScience/{dummyApi => responses}/citizenScienceSampleLabels.js (72%) create mode 100644 src/app/citizenScience/responses/responses.js create mode 100644 src/app/citizenScience/responses/responses.tpl.html diff --git a/src/app/app.js b/src/app/app.js index c7956902..d7918c95 100644 --- a/src/app/app.js +++ b/src/app/app.js @@ -306,6 +306,13 @@ angular.module("baw", fullWidth: true, reloadOnUrl: false }). + when(convertRouteParams(paths.site.ngRoutes.citizenScience.responses), { + templateUrl: "citizenScience/responses/responses.tpl.html", + controller: "ResponsesController", + title: "Citizen Science Responses", + fullWidth: true, + reloadOnUrl: false + }). when("/citsci/ipswich", { templateUrl: "citizenScience/ipswich/about.tpl.html", controller: "IpswichAboutController", diff --git a/src/app/citizenScience/bristlebird/bristlebird.js b/src/app/citizenScience/bristlebird/bristlebird.js index 5dfc28f2..86ddf136 100644 --- a/src/app/citizenScience/bristlebird/bristlebird.js +++ b/src/app/citizenScience/bristlebird/bristlebird.js @@ -22,6 +22,7 @@ class BristlebirdController { $location, CitizenScienceCommon, CsSamples, + CsLabels, SampleLabels, backgroundImage, paths) { @@ -102,7 +103,7 @@ class BristlebirdController { this.showAudio = CitizenScienceCommon.bindShowAudio($scope); - CsSamples.getLabels($scope.csProject).then(function (labels) { + CsLabels.getLabels($scope.csProject).then(function (labels) { $scope.labels = labels; }); @@ -111,7 +112,7 @@ class BristlebirdController { /** * Retrieve settings about this citizen science project */ - CsSamples.getSettings($scope.csProject).then( + CsLabels.getSettings($scope.csProject).then( function (settings) { $scope.settings = settings; if ($scope.settings.hasOwnProperty("sampleDuration")) { @@ -175,7 +176,8 @@ angular "bawApp.components.citizenScienceThumbLabels", "bawApp.components.onboarding", "bawApp.components.background", - "bawApp.citizenScience.csSamples" + "bawApp.citizenScience.csSamples", + "bawApp.citizenScience.csLabels" ]) .controller( "BristlebirdController", @@ -185,6 +187,7 @@ angular "$location", "CitizenScienceCommon", "CsSamples", + "CsLabels", "SampleLabels", "backgroundImage", "conf.paths", diff --git a/src/app/citizenScience/citizenScience.js b/src/app/citizenScience/citizenScience.js index 9af32212..5d716cfc 100644 --- a/src/app/citizenScience/citizenScience.js +++ b/src/app/citizenScience/citizenScience.js @@ -2,6 +2,7 @@ angular .module("bawApp.citizenScience", [ "bawApp.citizenScience.bristlebird", "bawApp.citizenScience.ipswich", + "bawApp.citizenScience.responses" ]) .controller( "CitizenScienceController", diff --git a/src/app/citizenScience/dummyApi/citizenScienceSamples.js b/src/app/citizenScience/datasetProgress/citizenScienceSamples.js similarity index 75% rename from src/app/citizenScience/dummyApi/citizenScienceSamples.js rename to src/app/citizenScience/datasetProgress/citizenScienceSamples.js index ff4e1d1d..4a6c3078 100644 --- a/src/app/citizenScience/dummyApi/citizenScienceSamples.js +++ b/src/app/citizenScience/datasetProgress/citizenScienceSamples.js @@ -11,9 +11,6 @@ csSamples.factory("CsSamples", [ function CsSamples(CitizenScienceCommon, $http, DatasetItem) { var self = this; - self.useLocalData = true; - self.sheets_api_url = "http://" + window.location.hostname + ":8081"; - self.local_api_url = "/public/citizen_science"; // the dataset id for this citizen science project // todo: integrate with settings for cs project @@ -91,35 +88,6 @@ csSamples.factory("CsSamples", [ self.setCurrentItem(); - - /** - * Constructs a url for the request by concatenating the arguments, joined by "/" - * and appending to the relevant baseURL. Allows experimenting with different sources - * for the data without changing everything - * @returns {string|*} - */ - self.apiUrl = function () { - // convert to array - var base_url, url; - if (self.useLocalData) { - base_url = self.local_api_url; - } else { - base_url = self.sheets_api_url; - } - var args = Array.prototype.slice.call(arguments); - - url = [base_url].concat(args).join("/"); - - if (self.useLocalData) { - url = url + ".json"; - } - - return url; - }; - - - - /** * Adds a new page of items to the list of pages * @param thenSelectNewItem boolean; if true will update the current item to be the first item @@ -144,10 +112,10 @@ csSamples.factory("CsSamples", [ if (thenSelectNewItem) { self.currentIndex.page = nextPageNum - 1; self.currentIndex.item = 0; + self.setCurrentItem(); } - self.setCurrentItem(); - }); + }); }; @@ -196,38 +164,6 @@ csSamples.factory("CsSamples", [ return Boolean(nextIndex); }, - /** - * Gets all labels associated with the specified citizen science project - * @param project string - */ - getLabels: function (project) { - var response = $http.get(self.apiUrl( - "labels", - project - )); - - return response.then(function (response) { - var labels = []; - if (Array.isArray(response.data)) { - labels = response.data; - } - - return labels; - }); - }, - - /** - * Gets all settings associated with the specified citizen science project - * @param project string - * @returns {HttpPromise} - */ - getSettings: function (project) { - return $http.get(self.apiUrl( - "settings", - project - )); - }, - currentItem: function () { return self.currentItem; } @@ -237,5 +173,3 @@ csSamples.factory("CsSamples", [ return self.publicFunctions; }]); - - diff --git a/src/app/citizenScience/labels/citizenScienceLabels.js b/src/app/citizenScience/labels/citizenScienceLabels.js new file mode 100644 index 00000000..7d2be4ed --- /dev/null +++ b/src/app/citizenScience/labels/citizenScienceLabels.js @@ -0,0 +1,85 @@ +var csLabels = angular.module("bawApp.citizenScience.csLabels", ["bawApp.citizenScience.common"]); + + +/** + * Manages the data for labels that will be applied to cs samples + */ +csLabels.factory("CsLabels", [ + "CitizenScienceCommon", + "$http", + function CsLabels(CitizenScienceCommon, $http) { + + var self = this; + self.useLocalData = true; + self.sheets_api_url = "http://" + window.location.hostname + ":8081"; + self.local_api_url = "/public/citizen_science"; + + + /** + * Constructs a url for the request by concatenating the arguments, joined by "/" + * and appending to the relevant baseURL. Allows experimenting with different sources + * for the data without changing everything + * @returns {string|*} + */ + self.apiUrl = function () { + // convert to array + var base_url, url; + if (self.useLocalData) { + base_url = self.local_api_url; + } else { + base_url = self.sheets_api_url; + } + var args = Array.prototype.slice.call(arguments); + + url = [base_url].concat(args).join("/"); + + if (self.useLocalData) { + url = url + ".json"; + } + + return url; + }; + + + self.publicFunctions = { + + + /** + * Gets all labels associated with the specified citizen science project + * @param project string + */ + getLabels: function (project) { + var response = $http.get(self.apiUrl( + "labels", + project + )); + + return response.then(function (response) { + var labels = []; + if (Array.isArray(response.data)) { + labels = response.data; + } + + return labels; + }); + }, + + /** + * Gets all settings associated with the specified citizen science project + * @param project string + * @returns {HttpPromise} + */ + getSettings: function (project) { + return $http.get(self.apiUrl( + "settings", + project + )); + } + + }; + + return self.publicFunctions; + + }]); + + diff --git a/src/app/citizenScience/dummyApi/citizenScienceSampleLabels.js b/src/app/citizenScience/responses/citizenScienceSampleLabels.js similarity index 72% rename from src/app/citizenScience/dummyApi/citizenScienceSampleLabels.js rename to src/app/citizenScience/responses/citizenScienceSampleLabels.js index 7f17e7f4..4f5a5fef 100644 --- a/src/app/citizenScience/dummyApi/citizenScienceSampleLabels.js +++ b/src/app/citizenScience/responses/citizenScienceSampleLabels.js @@ -1,4 +1,5 @@ -var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels", ["bawApp.citizenScience.common"]); +var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels", +["bawApp.citizenScience.common", "baw"]); /** * @@ -20,10 +21,13 @@ var sampleLabels = angular.module("bawApp.citizenScience.sampleLabels", ["bawApp sampleLabels.factory("SampleLabels", [ "CitizenScienceCommon", "$http", - function SampleLabels(CitizenScienceCommon, $http) { + "DatasetItem", + function SampleLabels(CitizenScienceCommon, $http, DatasetItem) { var self = this; + self.datasetId = 3; + /** * checks the local storage for sampleLabels */ @@ -192,11 +196,71 @@ sampleLabels.factory("SampleLabels", [ clearLabels : function () { self.data = {}; self.writeToStorage(); - } + }, + + /** + * Combines the SampleLabels data with the samples and labels + * to return the full report of which labels have been applied to which samples + * @return {{}|*} + */ + getData : function (labels) { + + var d = self.data; + + var keys = Object.keys(d); + + if (keys.length === 0) { + return d; + } + + var currentKey = -1; + + var addItemDetails = function (response) { + + var datasetItemId = keys[currentKey]; + d[datasetItemId] = { + "sample" : response.data.data[0], + "labels" : JSON.parse(JSON.stringify(d[datasetItemId])) + }; + for (var labelId in d[datasetItemId].labels) { + if (d[datasetItemId].labels.hasOwnProperty(labelId)) { + d[datasetItemId].labels[labelId] = { + "label": labels.find(l => true), + "response": JSON.parse(JSON.stringify(d[datasetItemId].labels[labelId])) + }; + + } + + } + + requestNextItem(); + + + }; + + var requestFailed = function (response) { + requestNextItem(); + }; + + var requestNextItem = function () { + currentKey++; + // recurse to do the next dataset item + if (currentKey < keys.length) { + DatasetItem.datasetItem(self.datasetId, keys[currentKey]).then(addItemDetails, requestFailed); + } + }; + + // request the first dataset item and add it to the returned object. + // when that is finished it will do the next one. + requestNextItem(); + + + return d; + } }; diff --git a/src/app/citizenScience/responses/responses.js b/src/app/citizenScience/responses/responses.js new file mode 100644 index 00000000..e6f84bf4 --- /dev/null +++ b/src/app/citizenScience/responses/responses.js @@ -0,0 +1,53 @@ +class ResponsesController { + constructor($scope, + SampleLabels, + CsLabels) { + + var self = this; + + SampleLabels.init("ebb"); + + self.labels = false; + + CsLabels.getLabels("ebb").then(labels => { + $scope.responseData = SampleLabels.getData(labels); + self.labels = labels; + }); + + + + + $scope.$watch("responseData", function (newVal, oldVal) { + $scope.responseDataString = JSON.stringify(newVal, null, 4); + },true); + + $scope.deleteResponses = function () { + if (confirm("Are you sure you want to delete all the responses?")) { + SampleLabels.clearLabels(); + CsLabels.getLabels("ebb").then(labels => { + $scope.responseData = SampleLabels.getData(labels); + self.labels = labels; + }); + } + }; + + + + + } +} + + +angular + .module("bawApp.citizenScience.responses", [ + "bawApp.citizenScience.sampleLabels", + "bawApp.citizenScience.csLabels" + ]) + .controller( + "ResponsesController", + [ + "$scope", + "SampleLabels", + "CsLabels", + ResponsesController + ]); \ No newline at end of file diff --git a/src/app/citizenScience/responses/responses.tpl.html b/src/app/citizenScience/responses/responses.tpl.html new file mode 100644 index 00000000..5f39df02 --- /dev/null +++ b/src/app/citizenScience/responses/responses.tpl.html @@ -0,0 +1,20 @@ + +
+ +

Responses

+ + + +
{{responseDataString}}
+ +
+ + + + + + diff --git a/src/baw.paths.nobuild.js b/src/baw.paths.nobuild.js index 629fe1bd..db6c5b73 100644 --- a/src/baw.paths.nobuild.js +++ b/src/baw.paths.nobuild.js @@ -178,6 +178,7 @@ module.exports = function (environment) { "citizenScience": { "listenId":"/citsci/bristlebird/listen/{sampleNum}", "listen":"/citsci/bristlebird/listen", + "responses": "/citsci/bristlebird/responses" }, "demo": { "d3": "/demo/d3",