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');
21 var localeStrings = dojo.i18n.getLocalization('openils.conify', 'conify');
23 function drawSurvey(svyId) {
25 var surveyTable = dojo.byId('edit-pane');
26 var surveyHead = dojo.create('thead', {id: "survey_head"}, surveyTable);
27 var headRow = dojo.create('tr', null, surveyHead);
28 var headCell = dojo.create('td', {id: "head_cell", innerHTML: "<h3>" +dojo.string.substitute(localeStrings.SURVEY_ID, [svyId])+"</h3>" }, 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);
38 pane.fieldOrder = ['id', 'name', 'description', 'owner', 'start_date', 'end_date'];
39 pane.onCancel = cancelEdit;
41 var surveyFoot = dojo.create('tfoot', { id: "survey_foot"}, surveyTable);
42 var footRow = dojo.create('tr', {id: "foot_row"}, surveyFoot);
43 var footLabel = dojo.create('td', {id: "foot_label", innerHTML: "<h3>"+localeStrings.SURVEY_FOOT_LABEL+"</h3>"}, footRow);
44 var footCell = dojo.create('td', {innerHTML: "<hr>", id: "foot_cell"}, footRow);
45 getQuestions(svyId, survey);
49 function cancelEdit(){
50 document.location.href = "/eg/conify/global/action/survey";
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);
64 // all functions for question manipulation
66 function getQuestions(svyId, survey) {
70 var pcrud = new openils.PermaCrud();
71 var questions = pcrud.search('asvq', {survey:svyId});
73 for(var i in questions) {
74 questionId = questions[i].id();
75 var answers = pcrud.search('asva', {question:questionId});
77 drawQuestionBody(questions[i], answers, survey);
79 if ( startDate > today) newQuestionBody(surveyId);
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);
94 function drawQuestionBody(question, answers, survey){
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);
110 for (var i in answers) {
111 if(!answers) return'';
112 drawAnswer(answers[i], question.id(), surveyBody, survey);
114 drawNewAnswerRow(question.id(), surveyBody);
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);
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);
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();
150 delQuestion.id(quesId);
151 delQuestion.isdeleted(true);
152 surveyBody.parentNode.removeChild(surveyBody);
153 return pcrud.delete(delQuestion);
157 // all functions for answer manipulation
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);
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);
189 function deleteAnswer(ansId) {
190 var pcrud = new openils.PermaCrud();
191 var delAnswer = new asva();
193 delAnswer.isdeleted(true);
194 return pcrud.delete(delAnswer);
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);
203 answerRow.parentNode.removeChild(answerRow);
204 drawAnswer(answer, answer.question());
205 drawNewAnswerRow(quesId, surveyBody);
206 return pcrud.create(answer);
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);