1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 1× 1× 1× 1× 1× 1× 1× | /** * Created by cklimkowsky on 2/25/15. */ angular .module('coopEval') .controller('FormController', FormController); function FormController($scope, $rootScope, $stateParams, $location, formService) { $scope.formType = $stateParams.formType; $scope.placementId = $stateParams.placementId; // Based on the form type, get a specific Work Report or Employer Evaluation. // The form data needs to be genericized before use so that the HTML pages do not need // to be duplicated (i.e. one for Student Work Reports, and one for Employer Evaluations). Iif ($scope.formType && $scope.placementId) { if ($scope.formType == 0) { formService.getStudentWorkReport($scope.placementId) .success(function (result) { $scope.evaluation = result; // Question answers for Double Likert questions arrive in the form of "3,3,true", so this // single value needs to be split into the three separate values that it represents. // If a Double Likert question does not have an answer associated with it yet, then // the three separate properties still need to be created for binding with the UI. for (var i = 0; i < $scope.evaluation.questionGroups.length; i++) { for (var j = 0; j < $scope.evaluation.questionGroups[i].questions.length; j++) { $scope.evaluation.questionGroups[i].questions[j].isCommentAllowed = parseInt($scope.evaluation.questionGroups[i].questions[j].isCommentAllowed); $scope.evaluation.questionGroups[i].questions[j].isRequired = parseInt($scope.evaluation.questionGroups[i].questions[j].isRequired); if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'Numeric') { if ($scope.evaluation.questionGroups[i].questions[j].answer) { $scope.evaluation.questionGroups[i].questions[j].answer = parseFloat($scope.evaluation.questionGroups[i].questions[j].answer); } } else if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'DoubleLikert') { if ($scope.evaluation.questionGroups[i].questions[j].answer) { var answers = $scope.evaluation.questionGroups[i].questions[j].answer.split(','); $scope.evaluation.questionGroups[i].questions[j].answer = answers[0]; $scope.evaluation.questionGroups[i].questions[j].answer2 = answers[1]; $scope.evaluation.questionGroups[i].questions[j].answer3 = answers[2]; } else { $scope.evaluation.questionGroups[i].questions[j].answer = null; $scope.evaluation.questionGroups[i].questions[j].answer2 = null; $scope.evaluation.questionGroups[i].questions[j].answer3 = false; } } } } }); } else { formService.getEmployerEvaluation($scope.placementId) .success(function (result) { $scope.evaluation = result; // Question answers for Double Likert questions arrive in the form of "3,3,true", so this // single value needs to be split into the three separate values that it represents. // If a Double Likert question does not have an answer associated with it yet, then // the three separate properties still need to be created for binding with the UI. for (var i = 0; i < $scope.evaluation.questionGroups.length; i++) { for (var j = 0; j < $scope.evaluation.questionGroups[i].questions.length; j++) { $scope.evaluation.questionGroups[i].questions[j].isCommentAllowed = parseInt($scope.evaluation.questionGroups[i].questions[j].isCommentAllowed); $scope.evaluation.questionGroups[i].questions[j].isRequired = parseInt($scope.evaluation.questionGroups[i].questions[j].isRequired); if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'Numeric') { if ($scope.evaluation.questionGroups[i].questions[j].answer) { $scope.evaluation.questionGroups[i].questions[j].answer = parseFloat($scope.evaluation.questionGroups[i].questions[j].answer); } } else if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'DoubleLikert') { if ($scope.evaluation.questionGroups[i].questions[j].answer) { var answers = $scope.evaluation.questionGroups[i].questions[j].answer.split(','); $scope.evaluation.questionGroups[i].questions[j].answer = answers[0]; $scope.evaluation.questionGroups[i].questions[j].answer2 = answers[1]; $scope.evaluation.questionGroups[i].questions[j].answer3 = answers[2]; } else { $scope.evaluation.questionGroups[i].questions[j].answer = null; $scope.evaluation.questionGroups[i].questions[j].answer2 = null; $scope.evaluation.questionGroups[i].questions[j].answer3 = false; } } } } }); } } else { // Cannot fetch form data because form type and/or form ID is missing } /** * Validates a form and, if it is valid, sends a request for it to be saved in the database. * @param form */ $scope.submitForm = function (form) { // Trigger validation flag $scope.submitted = true; // If form is invalid, return and let AngularJS show validation errors if (form.$invalid) { $rootScope.message = { type: 'danger', content: 'Please complete all required questions.', location: $location.path() }; return; } // The three values for answers to Double Likert questions need to be consolidated into one value for (var i = 0; i < $scope.evaluation.questionGroups.length; i++) { for (var j = 0; j < $scope.evaluation.questionGroups[i].questions.length; j++) { if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'DoubleLikert') { var doubleLikertAnswer = $scope.evaluation.questionGroups[i].questions[j].answer; doubleLikertAnswer += ',' + $scope.evaluation.questionGroups[i].questions[j].answer2; doubleLikertAnswer += ',' + $scope.evaluation.questionGroups[i].questions[j].answer3; $scope.evaluation.questionGroups[i].questions[j].answer = doubleLikertAnswer; delete $scope.evaluation.questionGroups[i].questions[j].answer2; delete $scope.evaluation.questionGroups[i].questions[j].answer3; } } } formService.submitForm($scope.evaluation) .success(function(data, status, headers, config) { $scope.submitted = false; // Redirect to the respective Overview page with an alert message if ($scope.formType == 0) { $rootScope.message = { type: 'success', content: 'Work report for ' + $scope.evaluation.companyName + ' (' + $scope.evaluation.termCode + ') submitted.', location: '/students' }; $location.search({}); $location.path('/students'); } else { $rootScope.message = { type: 'info', content: 'Evaluation for ' + $scope.evaluation.studentFirstName + ' ' + $scope.evaluation.studentLastName + ' (' + $scope.evaluation.termCode + ') submitted.', location: '/employers' }; $location.search({}); $location.path('/employers'); } }) .error(function(data, status, headers, config) { // TODO: Handle error }); }; /** * Sends a request to save a form for completion at a later date. * @param form */ $scope.saveForm = function (form) { // The three values for answers to Double Likert questions need to be consolidated into one value for (var i = 0; i < $scope.evaluation.questionGroups.length; i++) { for (var j = 0; j < $scope.evaluation.questionGroups[i].questions.length; j++) { if ($scope.evaluation.questionGroups[i].questions[j].questionType == 'DoubleLikert') { var doubleLikertAnswer = $scope.evaluation.questionGroups[i].questions[j].answer; doubleLikertAnswer += ',' + $scope.evaluation.questionGroups[i].questions[j].answer2; doubleLikertAnswer += ',' + $scope.evaluation.questionGroups[i].questions[j].answer3; $scope.evaluation.questionGroups[i].questions[j].answer = doubleLikertAnswer; delete $scope.evaluation.questionGroups[i].questions[j].answer2; delete $scope.evaluation.questionGroups[i].questions[j].answer3; } } } // Don't need to validate for completeness/correctness here formService.saveForm($scope.evaluation) .success(function(data, status, headers, config) { // Redirect to the respective Overview page with an alert message if ($scope.formType == 0) { $rootScope.message = { type: 'info', content: 'Work report for ' + $scope.evaluation.companyName + ' (' + $scope.evaluation.termCode + ') saved.', location: '/students' }; $location.search({}); $location.path('/students'); } else { $rootScope.message = { type: 'info', content: 'Evaluation for ' + $scope.evaluation.studentFirstName + ' ' + $scope.evaluation.studentLastName + ' (' + $scope.evaluation.termCode + ') saved.', location: '/employers' }; $location.search({}); $location.path('/employers'); } }) .error(function (data, status, headers, config) { // TODO: Handle error }); }; } |