ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.SurveySingleChoiceQuestionGUI.php
Go to the documentation of this file.
1<?php
2 /*
3 +----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +----------------------------------------------------------------------------+
22*/
23
24include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestionGUI.php";
25
38{
39 protected function initObject()
40 {
41 include_once "./Modules/SurveyQuestionPool/classes/class.SurveySingleChoiceQuestion.php";
42 $this->object = new SurveySingleChoiceQuestion();
43 }
44
45
46 //
47 // EDITOR
48 //
49
50 public function setQuestionTabs()
51 {
52 $this->setQuestionTabsForClass("surveysinglechoicequestiongui");
53 }
54
55 protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
56 {
57 // orientation
58 $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
59 $orientation->setRequired(false);
60 $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
61 $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
62 $orientation->addOption(new ilRadioOption($this->lng->txt('combobox'), 2));
63 $a_form->addItem($orientation);
64
65 // Answers
66 include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
67 $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
68 $answers->setRequired(false);
69 $answers->setAllowMove(true);
70 $answers->setShowWizard(true);
71 $answers->setShowSavePhrase(true);
72 $answers->setUseOtherAnswer(true);
73 $answers->setShowNeutralCategory(true);
74 $answers->setNeutralCategoryTitle($this->lng->txt('svy_neutral_answer'));
75 $answers->setDisabledScale(false);
76 $a_form->addItem($answers);
77
78 // values
79 $orientation->setValue($this->object->getOrientation());
80 if (!$this->object->getCategories()->getCategoryCount())
81 {
82 $this->object->getCategories()->addCategory("");
83 }
84 $answers->setValues($this->object->getCategories());
85 }
86
87 protected function importEditFormValues(ilPropertyFormGUI $a_form)
88 {
89 $this->object->setOrientation($a_form->getInput("orientation"));
90
91 $this->object->categories->flushCategories();
92
93 foreach ($_POST['answers']['answer'] as $key => $value)
94 {
95 if (strlen($value)) $this->object->getCategories()->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
96 }
97 if (strlen($_POST['answers']['neutral']))
98 {
99 $this->object->getCategories()->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
100 }
101 }
102
103 public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
104 {
105 if(is_array($a_working_data))
106 {
107 $user_answer = $a_working_data[0];
108 }
109
110 $options = array();
111 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
112 {
113 $cat = $this->object->categories->getCategory($i);
114 $value = ($cat->scale) ? ($cat->scale - 1) : $i;
115
116 $checked = "unchecked";
117 $text = null;
118 if(is_array($a_working_data) &&
119 is_array($user_answer))
120 {
121 if($value == $user_answer["value"])
122 {
123 $checked = "checked";
124 if($user_answer["textanswer"])
125 {
126 $text = $user_answer["textanswer"];
127 }
128 }
129 }
130
131 // "other" options have to be last or horizontal will be screwed
132 $idx = $cat->other."_".$value;
133
134 if(!$a_only_user_anwers || $checked == "checked")
135 {
136 $options[$idx] = array(
137 "value" => $value
138 ,"title" => trim($cat->title)
139 ,"other" => (bool)$cat->other
140 ,"checked" => $checked
141 ,"textanswer" => $text
142 );
143 }
144
145 ksort($options);
146 }
147
148 return array_values($options);
149 }
150
156 function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
157 {
158 $options = $this->getParsedAnswers($a_working_data);
159
160 // rendering
161
162 $template = new ilTemplate("tpl.il_svy_qpl_sc_printview.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
163 switch ($this->object->orientation)
164 {
165 case 0:
166 // vertical orientation
167 foreach($options as $option)
168 {
169 if ($option["other"])
170 {
171 $template->setCurrentBlock("other_row");
172 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_".$option["checked"].".png")));
173 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
174 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
175 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
176 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
177 ? ilUtil::prepareFormOutput($option["textanswer"])
178 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
179 $template->parseCurrentBlock();
180 }
181 else
182 {
183
184 $template->setCurrentBlock("row");
185 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_".$option["checked"].".png")));
186 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
187 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
188 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
189 $template->parseCurrentBlock();
190 }
191 }
192 break;
193 case 1:
194 // horizontal orientation
195 foreach($options as $option)
196 {
197 $template->setCurrentBlock("radio_col");
198 $template->setVariable("IMAGE_RADIO", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_".$option["checked"].".png")));
199 $template->setVariable("ALT_RADIO", $this->lng->txt($option["checked"]));
200 $template->setVariable("TITLE_RADIO", $this->lng->txt($option["checked"]));
201 $template->parseCurrentBlock();
202 }
203 foreach($options as $option)
204 {
205 if ($option["other"])
206 {
207 $template->setCurrentBlock("other_text_col");
208 $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
209 $template->setVariable("OTHER_ANSWER", $option["textanswer"]
210 ? ilUtil::prepareFormOutput($option["textanswer"])
211 : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
212 $template->parseCurrentBlock();
213 }
214 else
215 {
216 $template->setCurrentBlock("text_col");
217 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
218 $template->parseCurrentBlock();
219 }
220 }
221 break;
222 case 2:
223 foreach($options as $option)
224 {
225 $template->setCurrentBlock("comborow");
226 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($option["title"]));
227 $template->setVariable("VALUE_SC", $option["value"]);
228 if($option["checked"] == "checked")
229 {
230 $template->setVariable("SELECTED_SC", ' selected="selected"');
231 }
232 $template->parseCurrentBlock();
233 }
234 $template->setCurrentBlock("combooutput");
235 $template->setVariable("QUESTION_ID", $this->object->getId());
236 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
237 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
238 $template->parseCurrentBlock();
239 break;
240 }
241 if ($question_title)
242 {
243 $template->setVariable("QUESTION_TITLE", $this->getPrintViewQuestionTitle($question_title));
244 }
245 if ($show_questiontext)
246 {
247 $this->outQuestionText($template);
248 }
249 $template->parseCurrentBlock();
250 return $template->get();
251 }
252
253
254 //
255 // EXECUTION
256 //
257
263 function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
264 {
265 $template = new ilTemplate("tpl.il_svy_out_sc.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
266 $template->setCurrentBlock("material");
267 $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
268 $template->parseCurrentBlock();
269 switch ($this->object->orientation)
270 {
271 case 0:
272 // vertical orientation
273 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
274 {
275 $cat = $this->object->categories->getCategory($i);
276 if ($cat->other)
277 {
278 $template->setCurrentBlock("other_row");
279 if (strlen($cat->title))
280 {
281 $template->setVariable("OTHER_LABEL", $cat->title);
282 }
283 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
284 $template->setVariable("QUESTION_ID", $this->object->getId());
285 if (is_array($working_data))
286 {
287 foreach ($working_data as $value)
288 {
289 if (strlen($value["value"]))
290 {
291 if ($value["value"] == $cat->scale-1)
292 {
293 if (strlen($value['textanswer'])) $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
294 if (!$value['uncheck'])
295 {
296 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
297 }
298 }
299 }
300 }
301 }
302 $template->parseCurrentBlock();
303 }
304 else
305 {
306 $template->setCurrentBlock("row");
307 if ($cat->neutral) $template->setVariable('ROWCLASS', ' class="neutral"');
308 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
309 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
310 $template->setVariable("QUESTION_ID", $this->object->getId());
311 if (is_array($working_data))
312 {
313 foreach ($working_data as $value)
314 {
315 if (strcmp($value["value"], "") != 0)
316 {
317 if ($value["value"] == $cat->scale-1)
318 {
319 if (!$value['uncheck'])
320 {
321 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
322 }
323 }
324 }
325 }
326 }
327 $template->parseCurrentBlock();
328 }
329 $template->touchBlock('outer_row');
330 }
331 break;
332 case 1:
333 // horizontal orientation
334 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
335 {
336 $cat = $this->object->categories->getCategory($i);
337 $template->setCurrentBlock("radio_col");
338 if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
339 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
340 $template->setVariable("QUESTION_ID", $this->object->getId());
341 if (is_array($working_data))
342 {
343 foreach ($working_data as $value)
344 {
345 if (strcmp($value["value"], "") != 0)
346 {
347 if ($value["value"] == $cat->scale-1)
348 {
349 if (!$value['uncheck'])
350 {
351 $template->setVariable("CHECKED_SC", " checked=\"checked\"");
352 }
353 }
354 }
355 }
356 }
357 $template->parseCurrentBlock();
358 }
359 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
360 {
361 $cat = $this->object->categories->getCategory($i);
362 if ($cat->other)
363 {
364 $template->setCurrentBlock("text_other_col");
365 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
366 $template->setVariable("QUESTION_ID", $this->object->getId());
367 if (strlen($cat->title))
368 {
369 $template->setVariable("OTHER_LABEL", $cat->title);
370 }
371 if (is_array($working_data))
372 {
373 foreach ($working_data as $value)
374 {
375 if (strlen($value["value"]))
376 {
377 if ($value["value"] == $cat->scale-1 && strlen($value['textanswer']))
378 {
379 $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
380 }
381 }
382 }
383 }
384 $template->parseCurrentBlock();
385 }
386 else
387 {
388 $template->setCurrentBlock("text_col");
389 if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
390 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
391 $template->setVariable("TEXT_SC", ilUtil::prepareFormOutput($cat->title));
392 $template->setVariable("QUESTION_ID", $this->object->getId());
393 $template->parseCurrentBlock();
394 }
395 $template->touchBlock('text_outer_col');
396 }
397 break;
398 case 2:
399 // combobox output
400 for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
401 {
402 $cat = $this->object->categories->getCategory($i);
403 $template->setCurrentBlock("comborow");
404 $template->setVariable("TEXT_SC", $cat->title);
405 $template->setVariable("VALUE_SC", ($cat->scale) ? ($cat->scale - 1) : $i);
406 if (is_array($working_data))
407 {
408 if (strcmp($working_data[0]["value"], "") != 0)
409 {
410 if ($working_data[0]["value"] == $cat->scale-1)
411 {
412 $template->setVariable("SELECTED_SC", " selected=\"selected\"");
413 }
414 }
415 }
416 $template->parseCurrentBlock();
417 }
418 $template->setCurrentBlock("combooutput");
419 $template->setVariable("QUESTION_ID", $this->object->getId());
420 $template->setVariable("SELECT_OPTION", $this->lng->txt("select_option"));
421 $template->setVariable("TEXT_SELECTION", $this->lng->txt("selection"));
422 $template->parseCurrentBlock();
423 break;
424 }
425 if ($question_title)
426 {
427 $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
428 }
429 $template->setCurrentBlock("question_data");
430 if (strcmp($error_message, "") != 0)
431 {
432 $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
433 }
434 if ($show_questiontext)
435 {
436 $this->outQuestionText($template);
437 }
438 $template->parseCurrentBlock();
439 return $template->get();
440 }
441
442
443 //
444 // EVALUTION
445 //
446
455 function getCumulatedResultsDetails($survey_id, $counter, $finished_ids)
456 {
457 if (count($this->cumulated) == 0)
458 {
459 if(!$finished_ids)
460 {
461 include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
462 $nr_of_users = ilObjSurvey::_getNrOfParticipants($survey_id);
463 }
464 else
465 {
466 $nr_of_users = sizeof($finished_ids);
467 }
468 $this->cumulated =& $this->object->getCumulatedResults($survey_id, $nr_of_users, $finished_ids);
469 }
470 $output = "";
471 include_once "./Services/UICore/classes/class.ilTemplate.php";
472 $template = new ilTemplate("tpl.il_svy_svy_cumulated_results_detail.html", TRUE, TRUE, "Modules/Survey");
473
474 $template->setCurrentBlock("detail_row");
475 $template->setVariable("TEXT_OPTION", $this->lng->txt("question"));
476 $questiontext = $this->object->getQuestiontext();
477 $template->setVariable("TEXT_OPTION_VALUE", $this->object->prepareTextareaOutput($questiontext, TRUE));
478 $template->parseCurrentBlock();
479 $template->setCurrentBlock("detail_row");
480 $template->setVariable("TEXT_OPTION", $this->lng->txt("question_type"));
481 $template->setVariable("TEXT_OPTION_VALUE", $this->lng->txt($this->getQuestionType()));
482 $template->parseCurrentBlock();
483 $template->setCurrentBlock("detail_row");
484 $template->setVariable("TEXT_OPTION", $this->lng->txt("users_answered"));
485 $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_ANSWERED"]);
486 $template->parseCurrentBlock();
487 $template->setCurrentBlock("detail_row");
488 $template->setVariable("TEXT_OPTION", $this->lng->txt("users_skipped"));
489 $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_SKIPPED"]);
490 $template->parseCurrentBlock();
491 /*
492 $template->setCurrentBlock("detail_row");
493 $template->setVariable("TEXT_OPTION", $this->lng->txt("mode"));
494 $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE"]);
495 $template->parseCurrentBlock();
496 $template->setCurrentBlock("detail_row");
497 $template->setVariable("TEXT_OPTION", $this->lng->txt("mode_nr_of_selections"));
498 $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE_NR_OF_SELECTIONS"]);
499 $template->parseCurrentBlock();
500 */
501 $template->setCurrentBlock("detail_row");
502 $template->setVariable("TEXT_OPTION", $this->lng->txt("median"));
503 $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MEDIAN"]);
504 $template->parseCurrentBlock();
505
506 $template->setCurrentBlock("detail_row");
507 $template->setVariable("TEXT_OPTION", $this->lng->txt("categories"));
508 $table = array();
509 $idx = $selsum = 0;
510 foreach ($this->cumulated["variables"] as $key => $value)
511 {
512 $table[] = array(
513 (++$idx).".",
514 $value["title"],
515 $value["selected"],
516 sprintf("%.2f", 100*$value["percentage"])."%"
517 );
518 $selsum += (int)$value["selected"];
519 }
520 $head = array(
521 "",
522 $this->lng->txt("title"),
523 $this->lng->txt("category_nr_selected"),
524 $this->lng->txt("percentage_of_selections")
525 );
526 $foot = array(null, null, $selsum, null);
527 $template->setVariable("TEXT_OPTION_VALUE",
528 $this->renderStatisticsDetailsTable($head, $table, $foot));
529 $template->parseCurrentBlock();
530
531 // add text answers to detailed results
532 if (is_array($this->cumulated["textanswers"]))
533 {
534 $template->setCurrentBlock("detail_row");
535 $template->setVariable("TEXT_OPTION", $this->lng->txt("freetext_answers"));
536 $html = "";
537 foreach ($this->cumulated["textanswers"] as $key => $answers)
538 {
539 $html .= $this->cumulated["variables"][$key]["title"] ."\n";
540 $html .= "<ul>\n";
541 foreach ($answers as $answer)
542 {
543 $html .= "<li>" . preg_replace("/\n/", "<br>\n", $answer) . "</li>\n";
544 }
545 $html .= "</ul>\n";
546 }
547 $template->setVariable("TEXT_OPTION_VALUE", $html);
548 $template->parseCurrentBlock();
549 }
550
551 // chart
552 $template->setCurrentBlock("detail_row");
553 $template->setVariable("TEXT_OPTION", $this->lng->txt("chart"));
554 $template->setVariable("TEXT_OPTION_VALUE", $this->renderChart("svy_ch_".$this->object->getId(), $this->cumulated["variables"]));
555 $template->parseCurrentBlock();
556
557 $template->setVariable("QUESTION_TITLE", "$counter. ".$this->object->getTitle());
558 return $template->get();
559 }
560}
561
562?>
Basic class for all survey question types.
getPrintViewQuestionTitle($question_title=1)
renderChart($a_id, $a_variables)
getMaterialOutput()
Creates the HTML output of the question material(s)
renderStatisticsDetailsTable(array $a_head, array $a_rows, array $a_foot=null)
SingleChoice survey question GUI representation.
getParsedAnswers(array $a_working_data=null, $a_only_user_anwers=false)
getCumulatedResultsDetails($survey_id, $counter, $finished_ids)
Creates the detailed output of the cumulated results for the question.
getPrintView($question_title=1, $show_questiontext=1, $survey_id=null, array $a_working_data=null)
Creates a HTML representation of the question.
getWorkingForm($working_data="", $question_title=1, $show_questiontext=1, $error_message="", $survey_id=null)
Creates the question output form for the learner.
This class represents a survey question category wizard property in a property form.
_getNrOfParticipants($survey_id)
Returns the number of participants for a survey.
This class represents a property form user interface.
addItem($a_item)
Add Item (Property, SectionHeader).
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
This class represents a property in a property form.
This class represents an option in a radio group.
special template class to simplify handling of ITX/PEAR
static getHtmlPath($relative_path)
get url of path
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$_POST['username']
Definition: cron.php:12
$html
Definition: example_001.php:87
$text
if(!is_array($argv)) $options