| 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 |
1×
1×
| /**
* Created by cklimkowsky on 4/15/15.
* Updated by wesisd on 11/13/15.
*/
angular
.module('coopEval')
.controller('FormManagementController', FormManagementController);
function FormManagementController($scope, $rootScope, $stateParams, $location, $uibModal, administrationService, appService) {
appService.getUserInfo().then(function (data) {
$scope.currentUser = data;
});
if ($location.path() == '/administration/manage-student-forms') {
administrationService.getStudentForms()
.success(function (result) {
$scope.studentForms = result;
});
} else if ($location.path() == '/administration/manage-employer-forms') {
administrationService.getEmployerForms()
.success(function (result) {
$scope.employerForms = result;
});
} else if ($location.path() == '/administration/view-student-form') {
administrationService.getForm($stateParams.formName)
.success(function (result) {
$scope.studentForm = result;
});
} else if ($location.path() == '/administration/view-employer-form') {
administrationService.getForm($stateParams.formName)
.success(function (result) {
$scope.employerForm = result;
});
} else if ($location.path() == '/administration/edit-form') {
administrationService.getForm($stateParams.formName)
.success(function (result) {
$scope.form = result;
});
$scope.formType = $stateParams.formType;
}
// When the form for the Edit Form page changes, convert the isCommentAllowed and isRequired fields to
// numbers, since they come in as strings (this is necessary for checking the value of these fields)
$scope.$watch('form', function () {
if ($scope.form) {
for (var i = 0; i < $scope.form.questionGroups.length; i++) {
for (var j = 0; j < $scope.form.questionGroups[i].questions.length; j++) {
$scope.form.questionGroups[i].questions[j].isCommentAllowed = parseInt($scope.form.questionGroups[i].questions[j].isCommentAllowed);
$scope.form.questionGroups[i].questions[j].isRequired = parseInt($scope.form.questionGroups[i].questions[j].isRequired);
}
}
}
});
/**
* Determines whether a question of the given question type exists in the given question group.
* @param questionGroup
* @param questionType
* @returns {boolean} True if questionGroup contains a question of type questionType, false otherwise.
*/
$scope.questionGroupContainsQuestionType = function (questionGroup, questionType) {
if (questionGroup && questionGroup.questions) {
for (var i = 0; i < questionGroup.questions.length; i++) {
if (questionGroup.questions[i].questionType == questionType) {
return true;
}
}
}
return false;
};
/**
* Filters out any questions in a question group that do not have the specified question type.
* @param questionType The type of questions that are desired
* @returns {Function}
*/
$scope.questionTypeFilter = function (questionType) {
return function (question) {
return question.questionType == questionType;
}
};
/**
* Deletes the form with the respective formId from the database.
* @param form The form to be deleted
*/
$scope.deleteForm = function (form) {
administrationService.deleteForm(form.formId)
.success(function (result) {
$rootScope.message = {
type: 'success',
content: 'The form ' + form.name + ' was deleted successfully.',
location: $location.path()
};
if (form.isStudentForm == "1") {
for (var i = 0; i < $scope.studentForms.length; i++) {
if ($scope.studentForms[i].formId == form.formId) {
$scope.studentForms.splice(i, 1);
break;
}
}
} else {
for (var i = 0; i < $scope.employerForms.length; i++) {
if ($scope.employerForms[i].formId == form.formId) {
$scope.employerForms.splice(i, 1);
break;
}
}
}
})
.error(function(data, status, headers, config) {
if (status == 403) {
$rootScope.message = {
type: 'danger',
content: 'The form ' + form.name + ' could not be removed from the system ' +
'because it has one or more evaluations associated with it.',
location: $location.path()
};
} else {
$rootScope.message = {
type: 'danger',
content: 'The form ' + form.name + ' could not be removed from the system ' +
'because of an unknown error.',
location: $location.path()
};
}
});
};
/**
* Opens a modal window to create a new form.
* @param formType The type of form to be created (Student or Employer)
*/
$scope.addNewForm = function (formType) {
var modalInstance = $uibModal.open({
templateUrl: 'partials/administration/add-form-modal.html',
controller: 'AddFormModalController',
resolve: {
formType: function() {
return formType;
}
}
});
modalInstance.result.then(function (form) {
administrationService.createForm(form)
.success(function (result) {
$rootScope.message = {
type: 'success',
content: 'The new form ' + form.name + ' was created successfully.',
location: $location.path()
};
if (formType == 0) {
$scope.studentForms.push(result);
} else {
$scope.employerForms.push(result);
}
})
.error(function (data, status, headers, config) {
// TODO: Can't create a form with the same name as an existing form
});
});
};
/*
* Opens a modal window to add a question to an existing form.
*/
$scope.addQuestion = function (size) {
var modalInstance = $uibModal.open({
templateUrl: 'partials/administration/add-question-modal.html',
controller: 'AddQuestionModalController',
size: size,
resolve: {
questionGroups: function () {
return $scope.form.questionGroups
}
}
});
modalInstance.result.then(function (question) {
console.log(question);
question.questionOrder = $scope.form.questionGroups[question.questionGroupOrder].questions.length;
question.questionGroup = null;
$scope.form.questionGroups[question.questionGroupOrder].questions.splice(question.questionOrder, 0, question);
});
};
/*
* Opens a modal window to add a question group to an existing form.
*/
$scope.addQuestionGroup = function (size) {
var modalInstance = $uibModal.open({
templateUrl: 'partials/administration/add-question-group-modal.html',
controller: 'AddQuestionGroupModalController',
size: size,
resolve: {
questionGroups: function () {
return $scope.form.questionGroups
}
}
});
modalInstance.result.then(function (questionGroup) {
console.log(questionGroup);
for (var i = questionGroup.questionGroupOrder; i < $scope.form.questionGroups.length; i++) {
$scope.form.questionGroups[i].questionGroupOrder += 1;
for (var j = 0; j < $scope.form.questionGroups[i].questions.length; j++) {
$scope.form.questionGroups[i].questions[j].questionGroupOrder += 1;
}
}
$scope.form.questionGroups.splice(questionGroup.questionGroupOrder, 0, questionGroup);
});
};
/**
* Deletes the given question from the form.
* @param question
*/
$scope.deleteQuestion = function (question) {
console.log(question);
for (var j = question.questionOrder + 1; j < $scope.form.questionGroups[question.questionGroupOrder].questions.length; j++) {
$scope.form.questionGroups[question.questionGroupOrder].questions[j].questionOrder -= 1;
}
$scope.form.questionGroups[question.questionGroupOrder].questions.splice(question.questionOrder, 1);
};
/**
* Deletes the given question group from the form.
* @param questionGroup
*/
$scope.deleteQuestionGroup = function (questionGroup) {
for (var i = questionGroup.questionGroupOrder + 1; i < $scope.form.questionGroups.length; i++) {
console.log(i);
$scope.form.questionGroups[i].questionGroupOrder -= 1;
for (var j = 0; j < $scope.form.questionGroups[i].questions.length; j++) {
$scope.form.questionGroups[i].questions[j].questionGroupOrder -= 1;
}
}
$scope.form.questionGroups.splice(questionGroup.questionGroupOrder, 1);
};
/**
* Submits a request to save the changes to a form.
*/
$scope.save = function () {
administrationService.saveForm($scope.form)
.success(function (result) {
if ($scope.formType == 0) {
$rootScope.message = {
type: 'info',
content: 'The form ' + $scope.form.name +
' was saved successfully.',
location: '/administration/manage-student-forms'
};
$location.search({});
$location.path('/administration/manage-student-forms');
} else {
$rootScope.message = {
type: 'info',
content: 'The form ' + $scope.form.name +
' was saved successfully.',
location: '/administration/manage-employer-forms'
};
$location.search({});
$location.path('/administration/manage-employer-forms');
}
});
};
}
|