]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/javascript/widgets/Survey.js
removing old opac images and css
[working/Evergreen.git] / Open-ILS / src / javascript / widgets / Survey.js
1 /* */
2
3
4 function SurveyQuestion(question, poll) {
5         debug("Creating new survey question " + question.question() );
6         this.question = question;
7         this.node = createAppElement("div");
8         this.node.id    = "survey_question_" + question.id();
9         add_css_class( this.node, "survey_question" );
10         var div = createAppElement("div");
11         add_css_class(div, "survey_question_question");
12         div.appendChild(        
13                 createAppTextNode(question.question()));
14         this.node.appendChild(div);
15
16         if(poll) {
17                 this.selector = createAppElement("div");
18         } else {
19                 this.selector = createAppElement("select");
20         }
21
22         add_css_class( this.selector, "survey_answer_selector" );
23         this.selector.name = "survey_question_" + question.id();
24         this.selector.value = "survey_question_" + question.id();
25         this.node.appendChild(this.selector);
26         this.answers = new Array();
27 }
28
29
30 SurveyQuestion.prototype.getNode = function() {
31         return this.node;
32 }
33
34 SurveyQuestion.prototype.addAnswer = function(answer,poll) {
35         if(poll) {
36                 var ans = new SurveyAnswer(answer, this.question.id(), poll);
37                 this.answers.push(ans);
38
39                 this.selector.appendChild(ans.getNode());
40                 this.selector.appendChild(createAppTextNode(answer.answer()));
41                 this.selector.appendChild(createAppElement("br"));
42
43         } else {
44                 var ans = new SurveyAnswer(answer, this.question.id());
45                 this.answers.push(ans);
46                 this.selector.options[ this.selector.options.length ] = ans.getNode();
47         }
48 }
49
50
51 function SurveyAnswer(answer,qid, poll) {
52         this.answer = answer;
53
54         if(poll) {
55
56                 if(IE) {
57                         this.node = createAppElement(
58                                 "<input name='survey_answer_" + qid + "' type='radio' value='" + answer.id() + "'></input>" );
59                 } else {
60
61                         this.node = createAppElement("input");
62                         this.node.setAttribute("type", "radio");
63                         this.node.setAttribute("name", "survey_answer_" + qid);
64                         this.node.setAttribute("value", answer.id());
65                 }
66
67         } else {
68                 this.node = new Option( answer.answer(), answer.id() );
69         }
70
71         add_css_class( this.node, "survey_answer" );
72 }
73
74 SurveyAnswer.prototype.getNode = function() {
75         return this.node;
76 }
77
78
79
80
81 Survey.prototype                                        = new ListBox();
82 Survey.prototype.constructor    = Survey;
83 Survey.baseClass                                        = ListBox.constructor;
84
85 function Survey(survey, onclick) {
86
87         this.survey = survey;
88         debug("Creating new survey " + survey.name() );
89
90         if( survey.poll() == 0 ) survey.poll(false);
91         if( survey.poll() == 1 ) survey.poll(true);
92
93         if( survey.poll() )
94                 this.listBoxInit( false, survey.name(), true, false );
95         else
96                 this.listBoxInit( true, survey.name(), true, false );
97
98
99         this.questions                  = new Array();
100
101         this.addCaption( survey.description() );
102
103         for( var i in survey.questions() ) {
104                 this.addQuestion( survey.questions()[i] );
105         }
106
107         
108         this.button = createAppElement("input");
109         this.button.setAttribute("type", "submit");
110         this.button.value = "Submit Survey";
111
112         if(onclick)
113                 this.button.onclick = onclick;
114         this.addFooter(this.button);
115
116         var obj = this;
117         this.setAction( function() { obj.submit(); });
118 }
119
120 Survey.prototype.setUserSession = function(userSession) {
121         this.userSession = userSession;
122 }
123
124 Survey.prototype.setAnswerDate = function(date) {
125         this.answerDate = date;
126 }
127
128 Survey.prototype.setEffectiveDate = function(date) {
129         this.effectiveDate = date;
130 }
131
132 Survey.prototype.setAction = function(onclick) {
133         this.button.onclick = onclick;
134 }
135
136 Survey.prototype.getName = function() {
137         debug("getting name for " + this.survey.name() ); 
138         return this.survey.name();
139 }
140
141 Survey.prototype.addQuestion = function(question) {
142         var questionObj = new SurveyQuestion(question, this.survey.poll());
143         this.questions.push(questionObj);
144         for( var i in question.answers() ) {
145                 questionObj.addAnswer(question.answers()[i], this.survey.poll());
146         }
147
148         this.addItem(questionObj.getNode());
149 }
150
151 Survey.prototype.setUser = function(userid) {
152         this.userId = userid;
153 }
154
155 Survey.prototype.setSubmitCallback = function(callback) {
156         this.submitCallback = callback;
157 }
158
159 Survey.prototype.submit = function() {
160
161         var responses = this.buildSurveyResponse();
162
163         if( this.commitCallback) {
164                 this.commitCallback(responses);
165
166         } else {
167                 this.commit(responses);
168         }
169         
170         var bool = true;
171         if( this.submitCallback )
172                 bool = this.submitCallback(this);
173         
174         this.removeFooter();
175
176 }
177
178 Survey.prototype.commit = function(responses) {
179         var method;
180         if( this.userId ) 
181                 method = "open-ils.circ.survey.submit.user_id";
182         else {
183                 if( this.userSession )
184                         method = "open-ils.circ.survey.submit.session";
185                 else 
186                         method = "open-ils.circ.survey.submit.anon";
187         }
188
189         var request = new RemoteRequest(
190                 "open-ils.circ", method, responses );
191         request.send(true);
192
193         /* there is nothing to return, just check for exceptions */
194         request.getResultObject();
195 }
196
197
198 Survey.prototype.buildSurveyResponse = function() {
199
200         var responses = new Array();
201
202         for( var index in this.questions ) {
203                 var que = this.questions[index];
204                 var ans = null;  
205                 for( var ansindex in que.answers ) {
206                         ansobj = que.answers[ansindex];
207                         if( ansobj.getNode().selected || ansobj.getNode().checked ) {
208                                 ans = ansobj.answer.id();
209                                 debug("User selected answer " + ans );
210                                 break;
211                         }
212                 }
213                 var qid = que.question.id()
214                 var sur = new asvr();
215                 if( this.userId )
216                         sur.usr(this.userId);
217                 else
218                         sur.usr(this.userSession);
219                 sur.survey(this.survey.id());
220                 sur.question(qid);
221                 sur.answer(ans);
222                 sur.answer_date(this.answerDate);
223                 sur.effective_date(this.effectiveDate);
224                 responses.push(sur);
225         }
226
227         return responses;
228 }
229
230 /* Global survey retrieval functions.  In each case, if recvCallback
231         is not null, the retrieval will be asynchronous and will
232         call recvCallback(survey) on each survey retrieved.  Otherwise
233         an array of surveys is returned.
234         */
235
236 Survey._retrieve = function(request, surveyTaker, recvCallback) {
237
238
239         if( recvCallback ) {
240
241                 debug("Retrieving random survey asynchronously");
242                 var c = function(req) {
243                         var surveys = req.getResultObject();
244                         if(!surveys) return null;
245
246                         if( typeof surveys != 'object' || 
247                                         surveys.constructor != Array )
248                                 surveys = [surveys];
249
250                         for( var i in surveys ) {
251                                 var s = surveys[i];
252                                 debug("Retrieved survey " + s.name() );
253                                 var surv = new Survey(s);
254                                 surv.setUserSession(surveyTaker);
255                                 recvCallback(surv);
256                         }
257                 }
258
259                 request.setCompleteCallback(c);
260                 request.send();
261
262         } else {
263
264                 request.send(true);
265                 var surveys = new Array();
266                 var results = request.getResultObject();
267                 for(var index in results) {
268                         var s = results[index];
269                         debug("Retrieved survey " + s.name());
270                         var surv = new Survey(s);
271                         surv.setUserSession(surveyTaker);
272                         surveys.push(surv);
273                 }
274                 return surveys;
275         }
276
277 }
278
279 /* this needs a different method for retrieving the correct survey */
280 Survey.retrieveOpacRandom = function(user_session, recvCallback) {
281
282         var request = new RemoteRequest( 
283                 "open-ils.circ", 
284                 "open-ils.circ.survey.retrieve.opac.random", 
285                 user_session );
286         return Survey._retrieve(request, user_session, recvCallback );
287 }
288
289
290 Survey.retrieveAll = function(user_session, recvCallback) {
291         var request = new RemoteRequest( 
292                 "open-ils.circ", 
293                 "open-ils.circ.survey.retrieve.all", 
294                 user_session );
295         return Survey._retrieve(request, user_session, recvCallback );
296 }
297
298
299 Survey.retrieveRequired = function(user_session, recvCallback) {
300         var request = new RemoteRequest( 
301                 "open-ils.circ", 
302                 "open-ils.circ.survey.retrieve.required", 
303                 user_session );
304         return Survey._retrieve(request, user_session, recvCallback );
305 }
306
307 Survey.retrieveById = function(user_session, id, recvCallback) {
308         var request = new RemoteRequest(
309                 "open-ils.circ",
310                 "open-ils.circ.survey.fleshed.retrieve",
311                 id );
312         return Survey._retrieve(request, user_session, recvCallback );
313 }
314
315 Survey.retrieveOpacRandomGlobal = function(recvCallback) {
316         var request = new RemoteRequest(
317                 "open-ils.circ",
318                 "open-ils.circ.survey.retrieve.opac.random.global");
319         return Survey._retrieve(request, null, recvCallback );
320 }
321
322