← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20507: tracker-capture: attribute inheritance during relationship

 

------------------------------------------------------------
revno: 20507
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-10-05 15:56:43 +0200
message:
  tracker-capture: attribute inheritance during relationship
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add.html
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/views/tei.html


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2015-09-28 13:08:36 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/registration/registration-controller.js	2015-10-05 13:56:43 +0000
@@ -67,19 +67,22 @@
     });
 
     //watch for selection of program
-    $scope.$watch('selectedProgram', function() {        
-        $scope.trackedEntityForm = null;
-        $scope.customForm = null;        
-        $scope.allProgramRules = {constants: [], programIndicators: {}, programValidations: [], programVariables: [], programRules: []};
-        if( angular.isObject($scope.selectedProgram) && $scope.selectedProgram.id ){
-            TrackerRulesFactory.getRules($scope.selectedProgram.id).then(function(rules){                    
-                $scope.allProgramRules = rules;
-            });
-        }
-        
-        if($scope.registrationMode === 'REGISTRATION'){
-            $scope.getAttributes($scope.registrationMode);
-        }        
+    $scope.$watch('selectedProgram', function(newValue, oldValue) {
+        if( newValue !== oldValue )
+        {
+            $scope.trackedEntityForm = null;
+            $scope.customForm = null;        
+            $scope.allProgramRules = {constants: [], programIndicators: {}, programValidations: [], programVariables: [], programRules: []};
+            if( angular.isObject($scope.selectedProgram) && $scope.selectedProgram.id ){
+                TrackerRulesFactory.getRules($scope.selectedProgram.id).then(function(rules){                    
+                    $scope.allProgramRules = rules;
+                });
+            }
+
+            if($scope.registrationMode === 'REGISTRATION'){
+                $scope.getAttributes($scope.registrationMode);
+            }
+        }                
     }); 
     
     //listen to modes of registration
@@ -97,7 +100,7 @@
         if($scope.registrationMode === 'PROFILE'){
             $scope.selectedEnrollment = args.enrollment;
         }
-        
+
         $scope.getAttributes($scope.registrationMode);
     });
         

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js	2015-10-05 08:58:13 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add-controller.js	2015-10-05 13:56:43 +0000
@@ -58,7 +58,7 @@
     $scope.selectedAttribute = selectedAttribute;
     $scope.selectedProgram = selectedProgram;
     $scope.relatedProgramRelationship = relatedProgramRelationship;
-    $scope.selectedTei = selectedTei;    
+    $scope.mainTei = selectedTei;    
     $scope.attributesById = CurrentSelection.getAttributesById();
     $scope.addingTeiAssociate = false;
     
@@ -69,9 +69,9 @@
     $scope.relationship = {};
     
     var invalidTeis = [];
-    invalidTeis.push($scope.selectedTei.trackedEntityInstance);
-    if($scope.selectedTei.relationships){
-        angular.forEach($scope.selectedTei.relationships, function(rel){
+    invalidTeis.push($scope.mainTei.trackedEntityInstance);
+    if($scope.mainTei.relationships){
+        angular.forEach($scope.mainTei.relationships, function(rel){
             invalidTeis.push(rel.trackedEntityInstanceB);
         });
     }    
@@ -81,7 +81,7 @@
 
     //Selections
     $scope.selectedOrgUnit = SessionStorageService.get('SELECTED_OU');
-    $scope.selectedTeiForDisplay = angular.copy($scope.selectedTei);
+    $scope.selectedTeiForDisplay = angular.copy($scope.mainTei);
     $scope.ouModes = [{name: 'SELECTED'}, {name: 'CHILDREN'}, {name: 'DESCENDANTS'}, {name: 'ACCESSIBLE'}];         
     $scope.selectedOuMode = $scope.ouModes[0];
     
@@ -105,6 +105,7 @@
     if($scope.addingRelationship){
         $scope.teiAddLabel = $translate.instant('add_relationship');
         $scope.programs = selections.prs;
+        CurrentSelection.setRelationshipOwner($scope.mainTei);
     }
     else{        
         $scope.teiAddLabel = $scope.selectedAttribute && $scope.selectedAttribute.name ? $scope.selectedAttribute.name : $translate.instant('tracker_associate');
@@ -114,7 +115,8 @@
             $scope.selectedProgram = response.selectedProgram;
         });
         
-        $scope.selectedTei = {};
+        CurrentSelection.setRelationshipOwner({});
+        //$scope.selectedTei = {};
     }
     
     if(angular.isObject($scope.programs) && $scope.programs.length === 1){
@@ -169,6 +171,7 @@
         if(args.result === 'SUCCESS'){
             var relationshipInfo = CurrentSelection.getRelationshipInfo();
             $scope.teiForRelationship = relationshipInfo.tei;
+            $scope.addRelationship(); 
         }
         
         if(args.result === 'CANCEL'){
@@ -352,17 +355,11 @@
     
     $scope.showRegistration = function(){
         $scope.showRegistrationDiv = !$scope.showRegistrationDiv;        
-        $scope.showTrackedEntityDiv = !$scope.showRegistrationDiv;
-        if($scope.addingRelationship){
-            CurrentSelection.setRelationshipOwner($scope.selectedTei);
-        }
-        else{
-            CurrentSelection.setRelationshipOwner({});
-        }
+        $scope.showTrackedEntityDiv = !$scope.showRegistrationDiv;        
     };
     
     $scope.close = function () {
-        $modalInstance.close($scope.selectedTei.relationships ? $scope.selectedTei.relationships : []);
+        $modalInstance.close($scope.mainTei.relationships ? $scope.mainTei.relationships : []);
         $rootScope.showAddRelationshipDiv = !$rootScope.showAddRelationshipDiv;
     };
     
@@ -378,8 +375,7 @@
     $scope.assignRelationship = function(relativeTei){
         $scope.teiForRelationship = relativeTei;
         $rootScope.showAddRelationshipDiv = !$rootScope.showAddRelationshipDiv;
-    };
-    
+    };    
     
     $scope.back = function(){
         $scope.teiForRelationship = null;
@@ -388,18 +384,18 @@
     
     $scope.addRelationship = function(){
         if($scope.addingRelationship){
-            if($scope.selectedTei && $scope.teiForRelationship && $scope.relationship.selected){            
-                var tei = angular.copy($scope.selectedTei);
+            if($scope.mainTei && $scope.teiForRelationship && $scope.relationship.selected){            
+                var tei = angular.copy($scope.mainTei);
                 var relationship = {};
                 relationship.relationship = $scope.relationship.selected.id;
                 relationship.displayName = $scope.relationship.selected.name;
                 relationship.relative = {};
                 
-                relationship.trackedEntityInstanceA = $scope.selectedRelationship.aIsToB === $scope.relationship.selected.aIsToB ? $scope.selectedTei.trackedEntityInstance : $scope.teiForRelationship.id;
-                relationship.trackedEntityInstanceB = $scope.selectedRelationship.bIsToA === $scope.relationship.selected.bIsToA ? $scope.teiForRelationship.id : $scope.selectedTei.trackedEntityInstance;
+                relationship.trackedEntityInstanceA = $scope.selectedRelationship.aIsToB === $scope.relationship.selected.aIsToB ? $scope.mainTei.trackedEntityInstance : $scope.teiForRelationship.id;
+                relationship.trackedEntityInstanceB = $scope.selectedRelationship.bIsToA === $scope.relationship.selected.bIsToA ? $scope.teiForRelationship.id : $scope.mainTei.trackedEntityInstance;
 
                 tei.relationships = [];
-                angular.forEach($scope.selectedTei.relationships, function(rel){
+                angular.forEach($scope.mainTei.relationships, function(rel){
                     tei.relationships.push({relationship: rel.relationship, displayName: rel.displayName, trackedEntityInstanceA: rel.trackedEntityInstanceA, trackedEntityInstanceB: rel.trackedEntityInstanceB});
                 });
                 tei.relationships.push(relationship);
@@ -417,14 +413,14 @@
                     relationship.relative.processed = true;
                     relationship.relative.attributes = $scope.teiForRelationship;
 
-                    if($scope.selectedTei.relationships){
-                        $scope.selectedTei.relationships.push(relationship);
+                    if($scope.mainTei.relationships){
+                        $scope.mainTei.relationships.push(relationship);
                     }
                     else{
-                        $scope.selectedTei.relationships = [relationship];
+                        $scope.mainTei.relationships = [relationship];
                     }
 
-                    $modalInstance.close($scope.selectedTei.relationships);
+                    $modalInstance.close($scope.mainTei.relationships);
                 });
             }
             else{
@@ -496,12 +492,11 @@
                 TrackerRulesFactory,
                 CustomFormService,
                 TEService,
-                TEIService,
                 EnrollmentService,
                 DialogService,
                 CurrentSelection,
-                OptionSetService,
                 DateUtils,
+                RegistrationService,
                 SessionStorageService) {
     $scope.selectedOrgUnit = SessionStorageService.get('SELECTED_OU');
     $scope.enrollment = {enrollmentDate: '', incidentDate: ''};    
@@ -535,14 +530,20 @@
         });
     }
     
-    var assignInheritance = function(){
+    var assignInheritance = function(){        
         if($scope.addingRelationship){
-            var mainTei = CurrentSelection.getRelationshipOwner();
+            $scope.selectedTei = {};
+            var t = angular.copy( CurrentSelection.getRelationshipOwner() );
+            angular.forEach(t.attributes, function(att){
+                t[att.attribute] = att.value;
+            });
+            
             angular.forEach($scope.attributes, function(att){
-                if(att.inherit && mainTei[att.id]){
-                    $scope.selectedTei[att.id] = mainTei[att.id];
+                if(att.inherit && t[att.id]){
+                    $scope.selectedTei[att.id] = t[att.id];
                 }
             });
+            t = {};
         }
         else{
             $scope.selectedTei = {};
@@ -602,7 +603,7 @@
     $scope.registerEntity = function(){
         
         //check for form validity
-        $scope.outerForm.submitted = true;        
+        $scope.outerForm.submitted = true;
         if( $scope.outerForm.$invalid ){
             return false;
         }
@@ -617,52 +618,35 @@
         //get tei attributes and their values
         //but there could be a case where attributes are non-mandatory and
         //registration form comes empty, in this case enforce at least one value
-        $scope.valueExists = false;
-        var registrationAttributes = [];    
-        angular.forEach($scope.attributes, function(attribute){
-            var val = attribute.value;
-            if(!angular.isUndefined(val)){                
-                if(attribute.valueType === 'date'){
-                    val = DateUtils.formatFromUserToApi(val);
-                }
-                if(attribute.optionSetValue &&
-                        attribute.optionSet &&
-                        attribute.optionSet.id &&                
-                        $scope.optionSets[attribute.optionSet.id] &&
-                        $scope.optionSets[attribute.optionSet.id].options ){  
-                    
-                    val = OptionSetService.getCode($scope.optionSets[attribute.optionSet.id].options, val);
-                }
-                registrationAttributes.push({attribute: attribute.id, value: val});
-                $scope.valueExists = true;
-            } 
-        });       
-        
-        if(!$scope.valueExists){
-            //registration form is empty
+        $scope.tei = {trackedEntity: selectedTrackedEntity, orgUnit: $scope.selectedOrgUnit.id};
+        var result = RegistrationService.processForm($scope.tei, $scope.selectedTei, $scope.attributesById);
+        $scope.formEmpty = result.formEmpty;
+        $scope.tei = result.tei;
+                
+        if($scope.formEmpty){//registration form is empty
             return false;
         }
         
-        //prepare tei model and do registration
-        $scope.tei = {trackedEntity: selectedTrackedEntity, orgUnit: $scope.selectedOrgUnit.id, attributes: registrationAttributes };   
-        var teiId = '';
-
-        TEIService.register($scope.tei, $scope.optionSets, $scope.attributesById).then(function(registrationResponse){            
-            var reg = registrationResponse.response && registrationResponse.response.importSummaries && registrationResponse.response.importSummaries[0] ? registrationResponse.response.importSummaries[0] : {};
+        RegistrationService.registerOrUpdate($scope.tei, $scope.optionSets, $scope.attributesById).then(function(registrationResponse){
+            var reg = registrationResponse.response ? registrationResponse.response : {};
             if(reg.reference && reg.status === 'SUCCESS'){                
-                teiId = reg.reference;                
+                $scope.tei.trackedEntityInstance = $scope.tei.id = reg.reference; 
+                
                 //registration is successful and check for enrollment
                 if($scope.selectedProgramForRelative){    
                     //enroll TEI
-                    var enrollment = {trackedEntityInstance: teiId,
-                                program: $scope.selectedProgramForRelative.id,
-                                status: 'ACTIVE',
-                                dateOfEnrollment: DateUtils.formatFromUserToApi($scope.enrollment.enrollmentDate),
-                                dateOfIncident: $scope.enrollment.incidentDate === '' ? DateUtils.formatFromUserToApi($scope.enrollment.enrollmentDate) : DateUtils.formatFromUserToApi($scope.enrollment.incidentDate)
-                            };
+                    var enrollment = {};
+                        enrollment.trackedEntityInstance = $scope.tei.trackedEntityInstance;
+                        enrollment.program = $scope.selectedProgramForRelative.id;
+                        enrollment.status = 'ACTIVE';
+                        enrollment.orgUnit = $scope.selectedOrgUnit.id;
+                        enrollment.enrollmentDate = $scope.selectedEnrollment.enrollmentDate;
+                        enrollment.incidentDate = $scope.selectedEnrollment.incidentDate === '' ? $scope.selectedEnrollment.enrollmentDate : $scope.selectedEnrollment.incidentDate;
                     EnrollmentService.enroll(enrollment).then(function(enrollmentResponse){
                         var en = enrollmentResponse.response && enrollmentResponse.response.importSummaries && enrollmentResponse.response.importSummaries[0] ? enrollmentResponse.response.importSummaries[0] : {};
-                        if(en.reference && en.status === 'SUCCESS'){
+                        if(en.reference && en.status === 'SUCCESS'){                            
+                        }
+                        else{
                             //enrollment has failed
                             var dialogOptions = {
                                     headerText: 'enrollment_error',
@@ -684,19 +668,11 @@
                 return;
             }
             
-            $timeout(function() { 
-                //reset form
-                angular.forEach($scope.attributes, function(attribute){
-                    delete attribute.value;                
-                });            
-
-                $scope.enrollment.enrollmentDate = '';
-                $scope.enrollment.incidentDate =  '';
+            $timeout(function(){
+                $scope.selectedEnrollment.enrollmentDate = '';
+                $scope.selectedEnrollment.incidentDate =  '';
                 $scope.outerForm.submitted = false; 
-                
-                $scope.tei.id = teiId;
-                $scope.broadCastSelections();
-                
+                $scope.broadCastSelections();                
             }, 100);        
             
         });
@@ -714,8 +690,16 @@
             CurrentSelection.setRelationshipInfo({tei: $scope.tei});
             
             $timeout(function() { 
-                $rootScope.$broadcast('relationship', {});
+                $rootScope.$broadcast('relationship', {result: 'SUCCESS'});
             }, 100);
         }        
     };
+    
+    $scope.interacted = function(field) {
+        var status = false;
+        if(field){            
+            status = $scope.outerForm.submitted || field.$dirty;
+        }
+        return status;        
+    };
 });

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add.html	2015-09-24 12:19:33 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/components/teiadd/tei-add.html	2015-10-05 13:56:43 +0000
@@ -15,7 +15,7 @@
 
     <div class="row" ng-if="relationship.selected || !addingRelationship">
         <hr>
-        <div class="col-md-4" ng-if="addingRelationship">
+        <div class="col-md-3" ng-if="addingRelationship">
             <select name="foo" 
                     ng-model="selectedRelationship.aIsToB" 
                     class="form-control" 
@@ -38,7 +38,7 @@
             </div>
         </div>
         
-        <div ng-class="{true: 'col-md-8 pull-right', false: 'col-sm-12'} [addingRelationship]">
+        <div ng-class="{true: 'col-md-9 pull-right', false: 'col-sm-12'} [addingRelationship]">
             
             <div ng-if="addingRelationship">
                 <select name="foo" 

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/views/tei.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/views/tei.html	2015-09-23 15:04:50 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/views/tei.html	2015-10-05 13:56:43 +0000
@@ -30,9 +30,9 @@
                     <tbody id="list">
                         <tr ng-if="!addingTeiAssociate"
                             ng-repeat="trackedEntity in trackedEntityList.rows | orderBy:d2Sort:reverse" 
-                            ng-click="selectedTei.trackedEntityInstance ? assignRelationship(trackedEntity) : showDashboard(trackedEntity)"
+                            ng-click="mainTei.trackedEntityInstance ? assignRelationship(trackedEntity) : showDashboard(trackedEntity)"
                             ng-class="{true: 'inactive-tei'} [trackedEntity.inactive]"
-                            title="{{selectedTei.trackedEntityInstance ? 'select' : 'go_to_dashboard'| translate}}">
+                            title="{{mainTei.trackedEntityInstance ? 'select' : 'go_to_dashboard'| translate}}">
                             <td ng-repeat="gridColumn in gridColumns| filter:{show: true}">
                                 <span ng-switch="gridColumn.valueType">
                                     <span ng-switch-when="bool">