]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/action/survey/edit.js
2b78e9e3e04d78258c4a741e9688d1d074942b2c
[working/Evergreen.git] / Open-ILS / web / js / ui / default / conify / global / action / survey / edit.js
1 dojo.require('dojox.grid.DataGrid');
2 dojo.require('dojox.grid.cells.dijit');
3 dojo.require('dojo.data.ItemFileWriteStore');
4 dojo.require('dojo.date.stamp');
5 dojo.require('dijit.form.TextBox');
6 dojo.require('dijit.form.Button');
7 dojo.require('dijit.Dialog');
8 dojo.require('dojox.widget.PlaceholderMenuItem');
9 dojo.require('fieldmapper.OrgUtils');
10 dojo.require('openils.widget.OrgUnitFilteringSelect');
11 dojo.require('openils.PermaCrud');
12 dojo.require('openils.DojoPatch');
13 dojo.require('openils.widget.GridColumnPicker');
14 dojo.require('openils.widget.EditPane');
15 dojo.requireLocalization('openils.conify', 'conify');
16
17 var surveyId;
18 var startDate;
19 var endDate;
20 var today;
21 var localeStrings = dojo.i18n.getLocalization('openils.conify', 'conify');
22
23 function drawSurvey(svyId) {
24     today = new Date();    
25     var surveyTable = dojo.byId('edit-pane');
26     var surveyHead = dojo.create('thead', {style: "background-color: #99CCFF"},  surveyTable);
27     var headRow = dojo.create('tr', null,  surveyHead);
28     var headCell = dojo.create('td', {innerHTML: "<h3>" +dojo.string.substitute(localeStrings.SURVEY_ID, [svyId])+"</h3>", style: "width: 100%"}, headRow);
29     var pcrud = new openils.PermaCrud();
30     var survey = pcrud.retrieve('asv', svyId);
31     startDate = dojo.date.stamp.fromISOString(survey.start_date());
32     endDate = dojo.date.stamp.fromISOString(survey.end_date());
33     var pane = new openils.widget.EditPane({fmObject : survey, hideActionButtons:false}, dojo.byId('edit-pane'));
34     if ( endDate > today) {
35         var buttonBody = dojo.create( 'td', null, headRow);
36         var endButton = new dijit.form.Button({label: localeStrings.END_SURVEY, onClick:function() {endSurvey(survey.id())} }, buttonBody);
37     }   
38     pane.fieldOrder = ['id', 'name', 'description', 'owner', 'start_date', 'end_date'];
39     pane.onCancel = cancelEdit;
40     pane.startup();
41     var surveyFoot = dojo.create('tfoot', {style: "background-color: #99CCFF"}, surveyTable);
42     var footRow = dojo.create('tr', { style: "width: 100%"}, surveyFoot);  
43     var footLabel = dojo.create('td', {innerHTML: "<h3>"+localeStrings.SURVEY_FOOT_LABEL+"</h3>", style: "width: 100%"}, footRow);
44     var footCell = dojo.create('td', {innerHTML: "<hr>", style: "width: 100%"}, footRow);
45     getQuestions(svyId, survey);
46
47 }
48
49 function cancelEdit(){
50     document.location.href = "/eg/conify/global/action/survey";
51 }
52
53 function endSurvey(svyId) {
54     var pcrud = new openils.PermaCrud();
55     var survey = pcrud.retrieve('asv', svyId);
56     var today = new Date();
57     var date = dojo.date.stamp.toISOString(today);
58     survey.end_date(date);
59     survey.ischanged(true);
60     return pcrud.update(survey);
61
62 }
63
64 // all functions for question manipulation
65
66 function getQuestions(svyId, survey) {
67   
68     surveyId = svyId;
69       
70     var pcrud = new openils.PermaCrud();
71     var questions = pcrud.search('asvq', {survey:svyId});
72     
73     for(var i in questions) {
74         questionId = questions[i].id(); 
75         var answers = pcrud.search('asva', {question:questionId});
76         if (answers)
77             drawQuestionBody(questions[i], answers, survey);
78     }
79     if ( startDate > today) newQuestionBody(surveyId);
80 }
81  
82 function newQuestionBody(svyId) {
83     var surveyTable = dojo.byId("survey_table");
84     var surveyBody = dojo.create('tbody', {style: "background-color: #d9e8f9"}, surveyTable);
85     var questionRow = dojo.create('tr', null, surveyBody);
86     var questionLabel = dojo.create('td',{ innerHTML: localeStrings.SURVEY_QUESTION}, questionRow, "first");
87     var questionTextbox = dojo.create('td', null, questionRow, "second");
88     var qInput = new dijit.form.TextBox(null, questionTextbox);
89     var questionButton = dojo.create('td', null , questionRow);
90     var qButton = new dijit.form.Button({ label: localeStrings.SURVEY_SAVE_ADD, onClick:function() {newQuestion(svyId, qInput.getValue(), questionRow)} }, questionButton);
91     
92 }
93
94 function drawQuestionBody(question, answers, survey){
95
96     var surveyTable = dojo.byId('survey_table');
97     var surveyBody = dojo.create( 'tbody', {quid:question.id(), id:("q" + question.id()), style: "background-color: #d9e8f9"}, surveyTable);
98     var questionRow = dojo.create('tr', {quid:question.id()}, surveyBody);
99     var questionLabel = dojo.create('td', {quid:question.id(), innerHTML: localeStrings.SURVEY_QUESTION}, questionRow, "first");
100     var questionTextbox = dojo.create('td', {quid: question.id() }, questionRow, "second");
101     var qInput = new dijit.form.TextBox(null, questionTextbox);
102     qInput.attr('value', question.question());
103     if (startDate > today){
104         var questionButton = dojo.create('td', {quid: question.id()}, questionRow);
105         var qButton = new dijit.form.Button({label: localeStrings.SURVEY_DELETE_QUESTION, onClick:function() {deleteQuestion(question.id(), surveyBody) }}, questionButton);
106         var qChangesButton = dojo.create('td', {quid: question.id()}, questionRow);
107         var qcButton = new dijit.form.Button({label: localeStrings.SURVEY_SAVE_CHANGES, onClick:function() {changeQuestion(question.id(), qInput.attr('value')) }}, qChangesButton);
108        
109     }
110     for (var i in answers) {
111         if(!answers) return'';
112         drawAnswer(answers[i], question.id(), surveyBody, survey);
113     }
114     drawNewAnswerRow(question.id(), surveyBody);  
115 }
116
117 function newQuestion(svyId, questionText, questionRow) {
118     var pcrud = new openils.PermaCrud();
119     var question = new asvq();
120     question.survey(svyId);
121     question.question(questionText);
122     question.isnew(true);
123     pcrud.create(question, 
124         {oncomplete: function(r) 
125              { var q = openils.Util.readResponse(r); 
126                  questionRow.parentNode.removeChild(questionRow);
127                  drawQuestionBody(q, null);
128                  newQuestionBody(svyId);
129              } 
130         }
131     ); 
132 }
133
134 function changeQuestion(quesId, questionText) {
135     var pcrud = new openils.PermaCrud();
136     var question = pcrud.retrieve('asvq', quesId);
137     question.question(questionText);
138     question.ischanged(true);
139     return pcrud.update(question);
140 }
141
142 function deleteQuestion(quesId, surveyBody) {
143     var pcrud = new openils.PermaCrud();
144     var delQuestion = new asvq();
145     var answers = pcrud.search('asva', {question:quesId});
146     for(var i in answers){
147         var ansId = answers[i].id();
148         deleteAnswer(ansId);
149     }
150     delQuestion.id(quesId);
151     delQuestion.isdeleted(true);
152     surveyBody.parentNode.removeChild(surveyBody);
153     return pcrud.delete(delQuestion);
154
155 }
156
157 // all functions for answer manipulation
158
159 function drawAnswer(answer, qid, surveyBody, survey) {
160     var surveyBody = dojo.byId(("q" + qid)); 
161     var answerRow = dojo.create('tr', {anid: answer.id(), style: "background-color: #FFF"}, surveyBody);
162     var answerSpacer =  dojo.create('td', {anid: answer.id()}, answerRow, "first");
163     var answerLabel =  dojo.create('td', {anid: answer.id(), style: "float: right", innerHTML: localeStrings.SURVEY_ANSWER }, answerRow, "second");
164     var answerTextbox = dojo.create('td', {anid: answer.id() }, answerRow, "third");
165     var input = new dijit.form.TextBox(null, answerTextbox);
166     input.attr('value', answer.answer());
167     if (startDate > today){
168         var answerSpacer =  dojo.create('td', {anid: answer.id()}, answerRow);
169         var delanswerButton = dojo.create('td', {anid: answer.id()}, answerRow);
170         var aid = answer.id();
171         var aButton = new dijit.form.Button({label: localeStrings.SURVEY_DELETE_ANSWER, onClick:function(){deleteAnswer(aid);answerRow.parentNode.removeChild(answerRow)} }, delanswerButton);
172         var aChangesButton = dojo.create('td', {anid: qid}, answerRow);
173         var acButton = new dijit.form.Button({label: localeStrings.SURVEY_SAVE_CHANGES, onClick:function() {changeAnswer(answer.id(), input.attr('value')) }}, aChangesButton);
174     }
175 }
176
177 function drawNewAnswerRow(qid, surveyBody) {
178     var answerRow = dojo.create('tr', {quid: qid, style: "background-color: #FFF"}, surveyBody);
179     var answerSpacer =  dojo.create('td', {quid: qid}, answerRow, "first");
180     var answerLabel =  dojo.create('td', {quid: qid, innerHTML: localeStrings.SURVEY_ANSWER, style: "float:right" }, answerRow, "second");
181     var answerTextbox = dojo.create('td', {quid: qid }, answerRow, "third");
182     var input = new dijit.form.TextBox(null, answerTextbox);
183     var answerButton = dojo.create('td', {anid: qid}, answerRow);
184     var aButton = new dijit.form.Button({label: localeStrings.SURVEY_ADD_ANSWER, onClick:function() {newAnswer(qid, input.attr('value'), answerRow, surveyBody)} }, answerButton);
185
186 }
187
188
189 function deleteAnswer(ansId) {
190     var pcrud = new openils.PermaCrud();
191     var delAnswer = new asva();
192     delAnswer.id(ansId);
193     delAnswer.isdeleted(true);
194     return pcrud.delete(delAnswer);
195   
196 }
197 function newAnswer(quesId, answerText, answerRow, surveyBody) {
198     var pcrud = new openils.PermaCrud();
199     var answer = new asva();
200     answer.question(quesId);
201     answer.answer(answerText);
202     answer.isnew(true);
203     answerRow.parentNode.removeChild(answerRow);
204     drawAnswer(answer, answer.question());
205     drawNewAnswerRow(quesId, surveyBody);
206     return pcrud.create(answer);
207 }
208
209
210 function changeAnswer(ansId, answerText) {
211     var pcrud = new openils.PermaCrud();
212     var answer = pcrud.retrieve('asva', ansId);
213     answer.answer(answerText);
214     answer.ischanged(true);
215     return pcrud.update(answer);
216 }
217