]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/conify/global/action/survey/edit.js
LP#1575177 Add text informing users they cannot add survey questions
[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.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     if (startDate <= today) {
47       var warnRow = dojo.create('tr', {id: "warn_row"}, surveyFoot);
48       var warnText = dojo.create('td', {id: "warn_text", style: "padding: 5px", innerHTML: localeStrings.SURVEY_WARN_TEXT}, warnRow);
49     }
50     var footRule = dojo.create('tr', {id:"foot_rule"}, surveyFoot);
51     var footCell = dojo.create('td', {innerHTML: "<hr>", id: "foot_cell"}, footRule);
52     getQuestions(svyId, survey);
53
54 }
55
56 function cancelEdit(){
57     document.location.href = oilsBasePath + "/conify/global/action/survey";
58 }
59
60 function endSurvey(svyId) {
61     var pcrud = new openils.PermaCrud();
62     var survey = pcrud.retrieve('asv', svyId);
63     var today = new Date();
64     var date = dojo.date.stamp.toISOString(today);
65     survey.end_date(date);
66     survey.ischanged(true);
67     return pcrud.update(survey);
68
69 }
70
71 // all functions for question manipulation
72
73 function getQuestions(svyId, survey) {
74   
75     surveyId = svyId;
76       
77     var pcrud = new openils.PermaCrud();
78     var questions = pcrud.search('asvq', {survey:svyId});
79     
80     for(var i in questions) {
81         questionId = questions[i].id(); 
82         var answers = pcrud.search('asva', {question:questionId});
83         if (answers)
84             drawQuestionBody(questions[i], answers, survey);
85     }
86     if ( startDate > today) newQuestionBody(surveyId);
87 }
88  
89 function newQuestionBody(svyId) {
90     var surveyTable = dojo.byId("survey_table");
91     var surveyBody = dojo.create('tbody', {style: "background-color: #d9e8f9"}, surveyTable);
92     var questionRow = dojo.create('tr', null, surveyBody);
93     var questionLabel = dojo.create('td',{ innerHTML: localeStrings.SURVEY_QUESTION}, questionRow, "first");
94     var questionTextbox = dojo.create('td', null, questionRow, "second");
95     var qInput = new dijit.form.TextBox(null, questionTextbox);
96     var questionButton = dojo.create('td', null , questionRow);
97     var qButton = new dijit.form.Button({ label: localeStrings.SURVEY_SAVE_ADD, onClick:function() {newQuestion(svyId, qInput.getValue(), questionRow)} }, questionButton);
98     
99 }
100
101 function drawQuestionBody(question, answers, survey){
102
103     var surveyTable = dojo.byId('survey_table');
104     var surveyBody = dojo.create( 'tbody', {quid:question.id(), id:("q" + question.id()), style: "background-color: #d9e8f9"}, surveyTable);
105     var questionRow = dojo.create('tr', {quid:question.id()}, surveyBody);
106     var questionLabel = dojo.create('td', {quid:question.id(), innerHTML: localeStrings.SURVEY_QUESTION}, questionRow, "first");
107     var questionTextbox = dojo.create('td', {quid: question.id() }, questionRow, "second");
108     var qInput = new dijit.form.TextBox(null, questionTextbox);
109     qInput.attr('value', question.question());
110     if (startDate > today){
111         var questionButton = dojo.create('td', {quid: question.id()}, questionRow);
112         var qButton = new dijit.form.Button({label: localeStrings.SURVEY_DELETE_QUESTION, onClick:function() {deleteQuestion(question.id(), surveyBody) }}, questionButton);
113         var qChangesButton = dojo.create('td', {quid: question.id()}, questionRow);
114         var qcButton = new dijit.form.Button({label: localeStrings.SURVEY_SAVE_CHANGES, onClick:function() {changeQuestion(question.id(), qInput.attr('value')) }}, qChangesButton);
115        
116     }
117     for (var i in answers) {
118         if(!answers) return'';
119         drawAnswer(answers[i], question.id(), surveyBody, survey);
120     }
121     drawNewAnswerRow(question.id(), surveyBody);  
122 }
123
124 function newQuestion(svyId, questionText, questionRow) {
125     var pcrud = new openils.PermaCrud();
126     var question = new asvq();
127     question.survey(svyId);
128     question.question(questionText);
129     question.isnew(true);
130     pcrud.create(question, 
131         {oncomplete: function(r, qs) 
132              { var q = qs[0];
133                  questionRow.parentNode.removeChild(questionRow);
134                  drawQuestionBody(q, null);
135                  newQuestionBody(svyId);
136              } 
137         }
138     ); 
139 }
140
141 function changeQuestion(quesId, questionText) {
142     var pcrud = new openils.PermaCrud();
143     var question = pcrud.retrieve('asvq', quesId);
144     question.question(questionText);
145     question.ischanged(true);
146     return pcrud.update(question);
147 }
148
149 function deleteQuestion(quesId, surveyBody) {
150     var pcrud = new openils.PermaCrud();
151     var delQuestion = new asvq();
152     var answers = pcrud.search('asva', {question:quesId});
153     for(var i in answers){
154         var ansId = answers[i].id();
155         deleteAnswer(ansId);
156     }
157     delQuestion.id(quesId);
158     delQuestion.isdeleted(true);
159     surveyBody.parentNode.removeChild(surveyBody);
160     return pcrud.eliminate(delQuestion);
161
162 }
163
164 // all functions for answer manipulation
165
166 function drawAnswer(answer, qid, surveyBody, survey) {
167     var surveyBody = dojo.byId(("q" + qid)); 
168     var answerRow = dojo.create('tr', {anid: answer.id(), style: "background-color: #FFF"}, surveyBody);
169     var answerSpacer =  dojo.create('td', {anid: answer.id()}, answerRow, "first");
170     var answerLabel =  dojo.create('td', {anid: answer.id(), style: "float: right", innerHTML: localeStrings.SURVEY_ANSWER }, answerRow, "second");
171     var answerTextbox = dojo.create('td', {anid: answer.id() }, answerRow, "third");
172     var input = new dijit.form.TextBox(null, answerTextbox);
173     input.attr('value', answer.answer());
174     if (startDate > today){
175         var answerSpacer =  dojo.create('td', {anid: answer.id()}, answerRow);
176         var delanswerButton = dojo.create('td', {anid: answer.id()}, answerRow);
177         var aid = answer.id();
178         var aButton = new dijit.form.Button({label: localeStrings.SURVEY_DELETE_ANSWER, onClick:function(){deleteAnswer(aid);answerRow.parentNode.removeChild(answerRow)} }, delanswerButton);
179         var aChangesButton = dojo.create('td', {anid: qid}, answerRow);
180         var acButton = new dijit.form.Button({label: localeStrings.SURVEY_SAVE_CHANGES, onClick:function() {changeAnswer(answer.id(), input.attr('value')) }}, aChangesButton);
181     }
182 }
183
184 function drawNewAnswerRow(qid, surveyBody) {
185     var answerRow = dojo.create('tr', {quid: qid, style: "background-color: #FFF"}, surveyBody);
186     var answerSpacer =  dojo.create('td', {quid: qid}, answerRow, "first");
187     var answerLabel =  dojo.create('td', {quid: qid, innerHTML: localeStrings.SURVEY_ANSWER, style: "float:right" }, answerRow, "second");
188     var answerTextbox = dojo.create('td', {quid: qid }, answerRow, "third");
189     var input = new dijit.form.TextBox(null, answerTextbox);
190     var answerButton = dojo.create('td', {anid: qid}, answerRow);
191     var aButton = new dijit.form.Button({label: localeStrings.SURVEY_ADD_ANSWER, onClick:function() {newAnswer(qid, input.attr('value'), answerRow, surveyBody)} }, answerButton);
192
193 }
194
195
196 function deleteAnswer(ansId) {
197     var pcrud = new openils.PermaCrud();
198     var delAnswer = new asva();
199     delAnswer.id(ansId);
200     delAnswer.isdeleted(true);
201     return pcrud.eliminate(delAnswer);
202   
203 }
204 function newAnswer(quesId, answerText, answerRow, surveyBody) {
205     var pcrud = new openils.PermaCrud();
206     var answer = new asva();
207     answer.question(quesId);
208     answer.answer(answerText);
209     answer.isnew(true);
210     answerRow.parentNode.removeChild(answerRow);
211     drawAnswer(answer, answer.question());
212     drawNewAnswerRow(quesId, surveyBody);
213     return pcrud.create(answer);
214 }
215
216
217 function changeAnswer(ansId, answerText) {
218     var pcrud = new openils.PermaCrud();
219     var answer = pcrud.retrieve('asva', ansId);
220     answer.answer(answerText);
221     answer.ischanged(true);
222     return pcrud.update(answer);
223 }
224