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