LP#1452950 pat. reg surveys and stat cats
authorBill Erickson <berickxx@gmail.com>
Mon, 26 Oct 2015 02:24:16 +0000 (22:24 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 25 Feb 2016 22:31:57 +0000 (17:31 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Open-ILS/src/templates/staff/circ/patron/t_edit.tt2
Open-ILS/web/js/ui/default/staff/circ/patron/regctl.js
Open-ILS/web/js/ui/default/staff/services/idl.js

index 7f45948..f8186e4 100644 (file)
     <div ng-if="cat.entries().length != 0">
       <div class="btn-group" dropdown>
         <button type="button" class="btn btn-default dropdown-toggle">
-          <span style="padding-right: 5px;"></span>
+          <span style="padding-right: 5px;">
+            {{stat_cat_entry_maps[cat.id()].value()}}</span>
           <span class="caret"></span>
         </button>
         <ul class="dropdown-menu">
           <li ng-repeat="entry in cat.entries()">
-            <a href ng-click=""> {{entry.value()}} </a>
+            <a href ng-click="stat_cat_entry_maps[cat.id()]=entry"> 
+              {{entry.value()}}
+            </a>
           </li>
         </ul>
       </div>
   <div class="col-md-3 reg-field-label">
     <label>{{survey.name()}}</label>
   </div>
-  <div class="col-md-6">
+  <div class="col-md-6 reg-field-input">
     <div class="row" ng-repeat="question in survey.questions()" 
       style="margin-bottom: 10px;">
       <div class="col-md-6">{{question.question()}}</div>
index f848dbe..443c84c 100644 (file)
@@ -14,7 +14,7 @@ angular.module('egCoreMod')
         survey_questions : {},
         survey_answers : {},
         survey_responses : {},     // survey.responses for loaded patron in progress
-        orig_survey_responses : {},// survey.responses for loaded patron
+        stat_cat_entry_maps : {},   // cat.id to selected entry object map
         virt_id : -1               // virtual ID for new objects
     };
 
@@ -46,7 +46,6 @@ angular.module('egCoreMod')
             {atomic : true}
         ).then(function(surveys) {
             service.surveys = surveys;
-
             angular.forEach(surveys, function(survey) {
                 angular.forEach(survey.questions(), function(question) {
                     service.survey_questions[question.id()] = question;
@@ -55,21 +54,6 @@ angular.module('egCoreMod')
                     });
                 });
             });
-
-            if (!service.patron_id) return;
-
-            // existing survey responses.
-            service.orig_survey_responses = [];
-
-            // when building responses in via the form, we track
-            // question ID's to answer objects.  these are later turned
-            // into survey_response's during save time.
-            return egCore.pcrud.search('asvr', {usr : service.patron_id})
-            .then(null, null, function(resp) {
-                service.orig_survey_responses.push(resp);
-                service.survey_responses[resp.question()] = 
-                    service.survey_answers[resp.answer()];
-            });
         });
     }
 
@@ -331,7 +315,6 @@ angular.module('egCoreMod')
         patron.profile = current.profile(); // pre-hash version
         patron.net_access_level = current.net_access_level();
         patron.ident_type = current.ident_type();
-        patron.survey_responses = service.orig_survey_responses;
 
         angular.forEach(
             ['juvenile', 'barred', 'active', 'master_account'],
@@ -349,6 +332,19 @@ angular.module('egCoreMod')
         angular.forEach(patron.addresses, 
             function(addr) { service.ingest_address(patron, addr) });
 
+        // toss entries for existing stat cat maps into our living 
+        // stat cat entry map, which is modified within the template.
+        angular.forEach(patron.stat_cat_entries, function(map) {
+            var entry;
+            angular.forEach(service.stat_cats, function(cat) {
+                angular.forEach(cat.entries(), function(ent) {
+                    if (ent.id() == map.stat_cat_entry)
+                        entry = ent;
+                });
+            });
+            service.stat_cat_entry_maps[map.stat_cat.id] = entry;
+        });
+
         return patron;
     }
 
@@ -360,7 +356,8 @@ angular.module('egCoreMod')
             address_type : egCore.strings.REG_ADDR_TYPE,
             _is_mailing : true,
             _is_billing : true,
-            within_city_limits : false
+            within_city_limits : false,
+            stat_cat_entries : []
         };
 
         var card = {
@@ -428,36 +425,57 @@ angular.module('egCoreMod')
             if (addr_hash._is_billing) patron.billing_address(addr);
         });
 
+        patron.survey_responses([]);
         angular.forEach(service.survey_responses, function(answer) {
+            var question = service.survey_questions[answer.question()];
+            var resp = new egCore.idl.asvr();
+            resp.isnew(true);
+            resp.survey(question.survey());
+            resp.question(question.id());
+            resp.answer(answer.id());
+            resp.usr(patron.id());
+            resp.answer_date('now');
+            patron.survey_responses().push(resp);
+        });
+        
+        // re-object-ify the patron stat cat entry maps
+        var maps = [];
+        angular.forEach(patron.stat_cat_entries(), function(entry) {
+            var e = egCore.idl.fromHash('actscecm', entry);
+            e.stat_cat(e.stat_cat().id);
+            maps.push(e);
+        });
+        patron.stat_cat_entries(maps);
 
-            var existing = patron.survey_responses().filter(
-                function(resp) {
-                    return resp.question() == answer.question();
-                })[0];
-
-            if (existing) {
-                if (existing.answer() != answer.id()) {
-                    // answer changed
-                    existing.answer(answer.id());
-                    existing.answer_date('now');
-                    existing.ischanged(true);
-                    existing.isnew(false);
-                    patron.survey_responses().push(existing);
-                }
-            } else {
-                // first-time answering this question
-
-                // find the question object linked to this answer
-                var question = service.survey_questions[answer.question()];
-                var resp = new egCore.idl.asvr();
-                resp.isnew(true);
-                resp.survey(question.survey());
-                resp.question(question.id());
-                resp.answer(answer.id());
-                resp.usr(patron.id());
-                resp.answer_date('now');
-                patron.survey_responses().push(resp);
+        // service.stat_cat_entry_maps maps stats to entries
+        // patron.stat_cat_entries is an array of stat_cat_entry_usr_map's
+        angular.forEach(service.stat_cat_entry_maps, function(entry) {
+
+            // see if we already have a mapping for this entry
+            var existing = patron.stat_cat_entries().filter(function(e) {
+                return e.stat_cat() == entry.stat_cat();
+            })[0];
+
+            if (existing) { // we have a mapping
+                // if the existing mapping matches the new one,
+                // there' nothing left to do
+                if (existing.stat_cat_entry() == entry.id()) return;
+
+                // mappings differ.  delete the old one and create
+                // a new one below.
+                existing.isdeleted(true);
             }
+
+            var newmap = new egCore.idl.actscecm();
+            newmap.target_usr(patron.id());
+            newmap.isnew(true);
+            newmap.stat_cat(entry.stat_cat());
+            newmap.stat_cat_entry(entry.id());
+            patron.stat_cat_entries().push(newmap);
+        });
+
+        angular.forEach(patron.stat_cat_entries(), function(entry) {
+            console.log(egCore.idl.toString(entry));
         });
 
         if (!patron.isnew()) patron.ischanged(true);
@@ -550,6 +568,7 @@ function PatronRegCtrl($scope, $routeParams,
         $scope.stat_cats = prs.stat_cats;
         $scope.surveys = prs.surveys;
         $scope.survey_responses = prs.survey_responses;
+        $scope.stat_cat_entry_maps = prs.stat_cat_entry_maps;
 
         $scope.user_settings = prs.user_settings;
         // clone the user settings back into the patronRegSvc so
index d5d7def..7e45256 100644 (file)
@@ -173,6 +173,19 @@ angular.module('egCoreMod')
         return hash;
     }
 
+    // returns a simple string key=value string of an IDL object.
+    service.toString = function(obj) {
+        var s = '';
+        angular.forEach(
+            service.classes[obj.classname].fields.sort(
+                function(a,b) {return a.name < b.name ? -1 : 1}),
+            function(field) {
+                s += field.name + '=' + obj[field.name]() + '\n';
+            }
+        );
+        return s;
+    }
+
     // hash-to-IDL object translater.  Does not support nested values.
     service.fromHash = function(cls, hash) {
         if (!service.classes[cls]) {