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