1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {ActivatedRoute} from '@angular/router';
3 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
4 import {StringComponent} from '@eg/share/string/string.component';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {NetService} from '@eg/core/net.service';
7 import {AuthService} from '@eg/core/auth.service';
8 import {IdlObject, IdlService } from '@eg/core/idl.service';
9 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
12 templateUrl: './survey-edit.component.html'
15 export class SurveyEditComponent implements OnInit {
19 newAnswerArray: object[];
20 newQuestionText: string;
23 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
25 @ViewChild('createAnswerString', { static: true })
26 createAnswerString: StringComponent;
27 @ViewChild('createAnswerErrString', { static: true })
28 createAnswerErrString: StringComponent;
29 @ViewChild('createQuestionString', { static: true })
30 createQuestionString: StringComponent;
31 @ViewChild('createQuestionErrString', { static: true })
32 createQuestionErrString: StringComponent;
34 @ViewChild('updateQuestionSuccessStr', { static: true })
35 updateQuestionSuccessStr: StringComponent;
36 @ViewChild('updateQuestionFailStr', { static: true })
37 updateQuestionFailStr: StringComponent;
38 @ViewChild('updateAnswerSuccessStr', { static: true })
39 updateAnswerSuccessStr: StringComponent;
40 @ViewChild('updateAnswerFailStr', { static: true })
41 updateAnswerFailStr: StringComponent;
43 @ViewChild('delAnswerSuccessStr', { static: true })
44 delAnswerSuccessStr: StringComponent;
45 @ViewChild('delAnswerFailStr', { static: true })
46 delAnswerFailStr: StringComponent;
47 @ViewChild('delQuestionSuccessStr', { static: true })
48 delQuestionSuccessStr: StringComponent;
49 @ViewChild('delQuestionFailStr', { static: true })
50 delQuestionFailStr: StringComponent;
52 @ViewChild('endSurveyFailedString', { static: true })
53 endSurveyFailedString: StringComponent;
54 @ViewChild('endSurveySuccessString', { static: true })
55 endSurveySuccessString: StringComponent;
56 @ViewChild('questionAlreadyStartedErrString', { static: true })
57 questionAlreadyStartedErrString: StringComponent;
60 private auth: AuthService,
61 private net: NetService,
62 private route: ActivatedRoute,
63 private toast: ToastService,
64 private idl: IdlService,
69 this.surveyId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
74 this.newQuestionText = '';
77 'open-ils.circ.survey.fleshed.retrieve',
81 this.buildLocalArray(res);
86 onTabChange(event: NgbTabChangeEvent) {
87 this.surveyTab = event.nextId;
90 buildLocalArray(res) {
92 this.newAnswerArray = [];
93 const allQuestions = res.questions();
94 allQuestions.forEach((question, index) => {
95 this.newAnswerArray.push({inputText: ''});
96 question.words = question.question();
97 question.answers = question.answers();
98 this.localArray.push(question);
99 question.answers.forEach(answer => {
100 answer.words = answer.answer();
102 this.sortAnswers(index);
104 this.sortQuestions();
108 this.localArray.sort(function(a, b) {
109 const q1 = a.question().toUpperCase();
110 const q2 = b.question().toUpperCase();
111 return (q1 < q2) ? -1 : (q1 > q2) ? 1 : 0;
115 sortAnswers(questionIndex) {
116 this.localArray[questionIndex].answers.sort(function(a, b) {
117 const a1 = a.answer().toUpperCase();
118 const a2 = b.answer().toUpperCase();
119 return (a1 < a2) ? -1 : (a1 > a2) ? 1 : 0;
123 updateQuestion(questionToChange) {
124 if (this.surveyHasBegun()) {
127 questionToChange.question(questionToChange.words);
128 questionToChange.ischanged(true);
131 'open-ils.circ.survey.update',
132 this.auth.token(), this.surveyObj
135 this.updateQuestionFailStr.current().then(msg => this.toast.warning(msg));
138 this.surveyObj = res;
139 this.buildLocalArray(this.surveyObj);
140 this.updateQuestionSuccessStr.current().then(msg => this.toast.success(msg));
146 deleteQuestion(questionToDelete) {
147 if (this.surveyHasBegun()) {
150 questionToDelete.isdeleted(true);
153 'open-ils.circ.survey.update',
154 this.auth.token(), this.surveyObj
157 this.delQuestionFailStr.current().then(msg => this.toast.warning(msg));
160 this.surveyObj = res;
161 this.buildLocalArray(this.surveyObj);
162 this.delQuestionSuccessStr.current().then(msg => this.toast.success(msg));
169 createQuestion(newQuestionText) {
170 if (this.surveyHasBegun()) {
173 const newQuestion = this.idl.create('asvq');
174 newQuestion.question(newQuestionText);
175 newQuestion.isnew(true);
176 let questionObjects = [];
177 questionObjects = this.surveyObj.questions();
178 questionObjects.push(newQuestion);
179 this.surveyObj.questions(questionObjects);
182 'open-ils.circ.survey.update',
183 this.auth.token(), this.surveyObj
186 this.newQuestionText = '';
187 this.createQuestionErrString.current().then(msg => this.toast.warning(msg));
190 this.surveyObj = res;
191 this.buildLocalArray(this.surveyObj);
192 this.newQuestionText = '';
193 this.createQuestionString.current().then(msg => this.toast.success(msg));
200 deleteAnswer(answerObj) {
201 if (this.surveyHasBegun()) {
204 answerObj.isdeleted(true);
207 'open-ils.circ.survey.update',
208 this.auth.token(), this.surveyObj
211 this.delAnswerFailStr.current().then(msg => this.toast.warning(msg));
214 this.surveyObj = res;
215 this.buildLocalArray(this.surveyObj);
216 this.delAnswerSuccessStr.current().then(msg => this.toast.success(msg));
222 updateAnswer(answerObj) {
223 if (this.surveyHasBegun()) {
226 answerObj.answer(answerObj.words);
227 answerObj.ischanged(true);
230 'open-ils.circ.survey.update',
231 this.auth.token(), this.surveyObj
234 this.updateAnswerFailStr.current().then(msg => this.toast.warning(msg));
237 this.surveyObj = res;
238 this.buildLocalArray(this.surveyObj);
239 this.updateAnswerSuccessStr.current().then(msg => this.toast.success(msg));
245 createAnswer(newAnswerText, questionObj) {
246 // Create answer *is* allowed if survey has already begun
247 const questionId = questionObj.id();
248 const newAnswer = this.idl.create('asva');
249 newAnswer.answer(newAnswerText);
250 newAnswer.question(questionId);
251 newAnswer.isnew(true);
252 questionObj.answers.push(newAnswer);
255 'open-ils.circ.survey.update',
256 this.auth.token(), this.surveyObj
259 this.createAnswerErrString.current().then(msg => this.toast.warning(msg));
262 this.surveyObj = res;
263 this.buildLocalArray(this.surveyObj);
264 this.createAnswerString.current().then(msg => this.toast.success(msg));
271 const today = new Date().toISOString();
272 this.surveyObj.end_date(today);
273 this.surveyObj.ischanged(true);
274 // to get fm-editor to display changed date we need to set
275 // this.surveyObj to null temporarily
276 const surveyClone = this.idl.clone(this.surveyObj);
277 this.surveyObj = null;
280 'open-ils.circ.survey.update',
281 this.auth.token(), surveyClone
284 this.endSurveyFailedString.current().then(msg => this.toast.warning(msg));
287 this.surveyObj = res;
288 this.surveyObj.ischanged(false);
289 this.buildLocalArray(this.surveyObj);
290 this.endSurveySuccessString.current().then(msg => this.toast.success(msg));
297 const surveyStartDate = new Date(this.surveyObj.start_date());
298 const now = new Date();
299 if (surveyStartDate <= now) {
300 this.questionAlreadyStartedErrString.current().then(msg =>
301 this.toast.warning(msg));