ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveyMultipleChoiceQuestionGUI.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 
24 include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestionGUI.php";
25 
38 {
39  protected function initObject( )
40  {
41  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyMultipleChoiceQuestion.php";
42  $this->object = new SurveyMultipleChoiceQuestion();
43  }
44 
45  //
46  // EDITOR
47  //
48 
49  public function setQuestionTabs()
50  {
51  $this->setQuestionTabsForClass("surveymultiplechoicequestiongui");
52  }
53 
54  protected function addFieldsToEditForm(ilPropertyFormGUI $a_form)
55  {
56  // orientation
57  $orientation = new ilRadioGroupInputGUI($this->lng->txt("orientation"), "orientation");
58  $orientation->setRequired(false);
59  $orientation->addOption(new ilRadioOption($this->lng->txt('vertical'), 0));
60  $orientation->addOption(new ilRadioOption($this->lng->txt('horizontal'), 1));
61  $a_form->addItem($orientation);
62 
63  // minimum answers
64  $minanswers = new ilCheckboxInputGUI($this->lng->txt("use_min_answers"), "use_min_answers");
65  $minanswers->setValue(1);
66  $minanswers->setOptionTitle($this->lng->txt("use_min_answers_option"));
67  $minanswers->setRequired(FALSE);
68 
69  $nranswers = new ilNumberInputGUI($this->lng->txt("nr_min_answers"), "nr_min_answers");
70  $nranswers->setSize(5);
71  $nranswers->setDecimals(0);
72  $nranswers->setRequired(false);
73  $nranswers->setMinValue(1);
74  $minanswers->addSubItem($nranswers);
75 
76  $nrmaxanswers = new ilNumberInputGUI($this->lng->txt("nr_max_answers"), "nr_max_answers");
77  $nrmaxanswers->setSize(5);
78  $nrmaxanswers->setDecimals(0);
79  $nrmaxanswers->setRequired(false);
80  $nrmaxanswers->setMinValue(1);
81  $minanswers->addSubItem($nrmaxanswers);
82 
83  $a_form->addItem($minanswers);
84 
85  // Answers
86  include_once "./Modules/SurveyQuestionPool/classes/class.ilCategoryWizardInputGUI.php";
87  $answers = new ilCategoryWizardInputGUI($this->lng->txt("answers"), "answers");
88  $answers->setRequired(false);
89  $answers->setAllowMove(true);
90  $answers->setShowWizard(false);
91  $answers->setShowSavePhrase(false);
92  $answers->setUseOtherAnswer(true);
93  $answers->setShowNeutralCategory(true);
94  $answers->setNeutralCategoryTitle($this->lng->txt('svy_neutral_answer'));
95  $answers->setDisabledScale(false);
96  $a_form->addItem($answers);
97 
98 
99  // values
100  $orientation->setValue($this->object->getOrientation());
101  $minanswers->setChecked($this->object->use_min_answers);
102  $nranswers->setValue($this->object->nr_min_answers);
103  $nrmaxanswers->setValue($this->object->nr_max_answers);
104  if (!$this->object->getCategories()->getCategoryCount())
105  {
106  $this->object->getCategories()->addCategory("");
107  }
108  $answers->setValues($this->object->getCategories());
109  }
110 
111  protected function validateEditForm(ilPropertyFormGUI $a_form)
112  {
113  if($a_form->getInput("use_min_answers"))
114  {
115  // #13927 - see importEditFormValues()
116  $cnt_answers = 0;
117  foreach ($_POST['answers']['answer'] as $key => $value)
118  {
119  if (strlen($value))
120  {
121  $cnt_answers++;
122  }
123  }
124  if (strlen($_POST['answers']['neutral']))
125  {
126  $cnt_answers++;
127  }
128  /* this would be the DB-values
129  $cnt_answers = $a_form->getItemByPostVar("answers");
130  $cnt_answers = $cnt_answers->getCategoryCount();
131  */
132  $min_anwers = $a_form->getInput("nr_min_answers");
133  $max_anwers = $a_form->getInput("nr_max_answers");
134 
135  if ($min_anwers &&
136  $min_anwers > $cnt_answers)
137  {
138  $a_form->getItemByPostVar("nr_min_answers")->setAlert($this->lng->txt('err_minvalueganswers'));
139  $errors = true;
140  }
141  if ($max_anwers > 0 &&
142  ($max_anwers > $cnt_answers || $max_anwers < $min_anwers))
143  {
144  $a_form->getItemByPostVar("nr_max_answers")->setAlert($this->lng->txt('err_maxvaluegeminvalue'));
145  $errors = true;
146  }
147  }
148 
149  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
150  return !$errors;
151  }
152 
153  protected function importEditFormValues(ilPropertyFormGUI $a_form)
154  {
155  $this->object->setOrientation($a_form->getInput("orientation"));
156  $this->object->use_other_answer = ($a_form->getInput('use_other_answer')) ? 1 : 0;
157  $this->object->other_answer_label = $this->object->use_other_answer ? $a_form->getInput('other_answer_label') : null;
158  $this->object->use_min_answers = ($a_form->getInput('use_min_answers')) ? true : false;
159  $this->object->nr_min_answers = ($a_form->getInput('nr_min_answers') > 0) ? $a_form->getInput('nr_min_answers') : null;
160  $this->object->nr_max_answers = ($a_form->getInput('nr_max_answers') > 0) ? $a_form->getInput('nr_max_answers') : null;
161  $this->object->label = $a_form->getInput('label');
162 
163  $this->object->categories->flushCategories();
164 
165  foreach ($_POST['answers']['answer'] as $key => $value)
166  {
167  if (strlen($value)) $this->object->getCategories()->addCategory($value, $_POST['answers']['other'][$key], 0, null, $_POST['answers']['scale'][$key]);
168  }
169  if (strlen($_POST['answers']['neutral']))
170  {
171  $this->object->getCategories()->addCategory($_POST['answers']['neutral'], 0, 1, null, $_POST['answers_neutral_scale']);
172  }
173  }
174 
175  public function getParsedAnswers(array $a_working_data = null, $a_only_user_anwers = false)
176  {
177  if(is_array($a_working_data))
178  {
179  $user_answers = $a_working_data;
180  }
181 
182  $options = array();
183  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
184  {
185  $cat = $this->object->categories->getCategory($i);
186  $value = ($cat->scale) ? ($cat->scale - 1) : $i;
187 
188  $checked = "unchecked";
189  $text = null;
190  if(is_array($a_working_data))
191  {
192  foreach($user_answers as $user_answer)
193  {
194  if($value == $user_answer["value"])
195  {
196  $checked = "checked";
197  if($user_answer["textanswer"])
198  {
199  $text = $user_answer["textanswer"];
200  }
201  break;
202  }
203  }
204  }
205 
206  // "other" options have to be last or horizontal will be screwed
207  $idx = $cat->other."_".$value;
208 
209  if(!$a_only_user_anwers || $checked == "checked")
210 
211  $options[$idx] = array(
212  "value" => $value
213  ,"title" => trim($cat->title)
214  ,"other" => (bool)$cat->other
215  ,"checked" => $checked
216  ,"textanswer" => $text
217  );
218 
219  ksort($options);
220  }
221 
222  return array_values($options);
223  }
224 
230  function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null, array $a_working_data = null)
231  {
232  $options = $this->getParsedAnswers($a_working_data);
233 
234  $template = new ilTemplate("tpl.il_svy_qpl_mc_printview.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
235  switch ($this->object->getOrientation())
236  {
237  case 0:
238  // vertical orientation
239  foreach($options as $option)
240  {
241  if ($option["other"])
242  {
243  $template->setCurrentBlock("other_row");
244  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_".$option["checked"].".png")));
245  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
246  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
247  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
248  $template->setVariable("OTHER_ANSWER", $option["textanswer"]
249  ? ilUtil::prepareFormOutput($option["textanswer"])
250  : "&nbsp;");
251  $template->parseCurrentBlock();
252  }
253  else
254  {
255  $template->setCurrentBlock("mc_row");
256  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_".$option["checked"].".png")));
257  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
258  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
259  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($option["title"]));
260  $template->parseCurrentBlock();
261  }
262  }
263  break;
264  case 1:
265  // horizontal orientation
266  foreach($options as $option)
267  {
268  $template->setCurrentBlock("checkbox_col");
269  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_".$option["checked"].".png")));
270  $template->setVariable("ALT_CHECKBOX", $this->lng->txt($option["checked"]));
271  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt($option["checked"]));
272  $template->parseCurrentBlock();
273  }
274  foreach($options as $option)
275  {
276  if ($option["other"])
277  {
278  $template->setCurrentBlock("other_text_col");
279  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($option["title"]));
280  $template->setVariable("OTHER_ANSWER", $option["textanswer"]
281  ? ilUtil::prepareFormOutput($option["textanswer"])
282  : "&nbsp;");
283  $template->parseCurrentBlock();
284  }
285  else
286  {
287  $template->setCurrentBlock("text_col");
288  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($option["title"]));
289  $template->parseCurrentBlock();
290  }
291  }
292  break;
293  }
294 
295  if ($this->object->use_min_answers)
296  {
297  $template->setCurrentBlock('min_max_msg');
298  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0)
299  {
300  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
301  }
302  else if ($this->object->nr_min_answers > 0)
303  {
304  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
305  }
306  else if ($this->object->nr_max_answers > 0)
307  {
308  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
309  }
310  $template->parseCurrentBlock();
311  }
312  if ($show_questiontext)
313  {
314  $this->outQuestionText($template);
315  }
316  if ($question_title)
317  {
318  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
319  }
320  $template->parseCurrentBlock();
321  return $template->get();
322  }
323 
324 
325  //
326  // EXECUTION
327  //
328 
334  function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
335  {
336  $template = new ilTemplate("tpl.il_svy_out_mc.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
337  $template->setCurrentBlock("material");
338  $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
339  $template->parseCurrentBlock();
340  switch ($this->object->getOrientation())
341  {
342  case 0:
343  // vertical orientation
344  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
345  {
346  $cat = $this->object->categories->getCategory($i);
347  if ($cat->other)
348  {
349  $template->setCurrentBlock("other_row");
350  if (strlen($cat->title))
351  {
352  $template->setVariable("OTHER_LABEL", $cat->title);
353  }
354  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
355  $template->setVariable("QUESTION_ID", $this->object->getId());
356  if (is_array($working_data))
357  {
358  foreach ($working_data as $value)
359  {
360  if (strlen($value["value"]))
361  {
362  if ($value["value"] == $cat->scale-1)
363  {
364  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
365  if (!$value['uncheck'])
366  {
367  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
368  }
369  }
370  }
371  }
372  }
373  $template->parseCurrentBlock();
374  }
375  else
376  {
377  $template->setCurrentBlock("mc_row");
378  if ($cat->neutral) $template->setVariable('ROWCLASS', ' class="neutral"');
379  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
380  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
381  $template->setVariable("QUESTION_ID", $this->object->getId());
382  if (is_array($working_data))
383  {
384  foreach ($working_data as $value)
385  {
386  if (strlen($value["value"]))
387  {
388  if ($value["value"] == $cat->scale-1)
389  {
390  if (!$value['uncheck'])
391  {
392  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
393  }
394  }
395  }
396  }
397  }
398  $template->parseCurrentBlock();
399  }
400  $template->touchBlock('outer_row');
401  }
402  break;
403  case 1:
404  // horizontal orientation
405 
406  // #15477 - reverting the categorizing of answers
407  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
408  {
409  $cat = $this->object->categories->getCategory($i);
410 
411  // checkbox
412  $template->setCurrentBlock("checkbox_col");
413  if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
414  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
415  $template->setVariable("QUESTION_ID", $this->object->getId());
416  if (is_array($working_data))
417  {
418  foreach ($working_data as $value)
419  {
420  if (strlen($value["value"]))
421  {
422  if ($value["value"] == $cat->scale-1)
423  {
424  if (!$value['uncheck'])
425  {
426  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
427  }
428  }
429  }
430  }
431  }
432  $template->parseCurrentBlock();
433 
434  // answer & input
435  if ($cat->other)
436  {
437  $template->setCurrentBlock("text_other_col");
438  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
439  $template->setVariable("QUESTION_ID", $this->object->getId());
440  if (strlen($cat->title))
441  {
442  $template->setVariable("OTHER_LABEL", $cat->title);
443  }
444  if (is_array($working_data))
445  {
446  foreach ($working_data as $value)
447  {
448  if (strlen($value["value"]))
449  {
450  if ($value["value"] == $cat->scale-1)
451  {
452  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
453  }
454  }
455  }
456  }
457  $template->parseCurrentBlock();
458  }
459  // answer
460  else
461  {
462  $template->setCurrentBlock("text_col");
463  if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
464  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
465  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
466  $template->setVariable("QUESTION_ID", $this->object->getId());
467  $template->parseCurrentBlock();
468  }
469  $template->touchBlock('text_outer_col');
470  }
471  break;
472  }
473 
474  $template->setCurrentBlock("question_data");
475  if ($this->object->use_min_answers)
476  {
477  $template->setCurrentBlock('min_max_msg');
478  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0)
479  {
480  if ($this->object->nr_min_answers == $this->object->nr_max_answers)
481  {
482  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_exact_answers'), $this->object->nr_min_answers));
483  }
484  else
485  {
486  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
487  }
488  }
489  else if ($this->object->nr_min_answers > 0)
490  {
491  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
492  }
493  else if ($this->object->nr_max_answers > 0)
494  {
495  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
496  }
497  $template->parseCurrentBlock();
498  }
499  if (strcmp($error_message, "") != 0)
500  {
501  $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
502  }
503  if ($show_questiontext)
504  {
505  $this->outQuestionText($template);
506  }
507  if ($question_title)
508  {
509  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
510  }
511  $template->parseCurrentBlock();
512  return $template->get();
513  }
514 
515 
516  //
517  // EVALUTION
518  //
519 
528  function getCumulatedResultsDetails($survey_id, $counter, $finished_ids)
529  {
530  if (count($this->cumulated) == 0)
531  {
532  if(!$finished_ids)
533  {
534  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
535  $nr_of_users = ilObjSurvey::_getNrOfParticipants($survey_id);
536  }
537  else
538  {
539  $nr_of_users = sizeof($finished_ids);
540  }
541  $this->cumulated =& $this->object->getCumulatedResults($survey_id, $nr_of_users, $finished_ids);
542  }
543 
544  $output = "";
545  include_once "./Services/UICore/classes/class.ilTemplate.php";
546  $template = new ilTemplate("tpl.il_svy_svy_cumulated_results_detail.html", TRUE, TRUE, "Modules/Survey");
547 
548  $template->setCurrentBlock("detail_row");
549  $template->setVariable("TEXT_OPTION", $this->lng->txt("question"));
550  $questiontext = $this->object->getQuestiontext();
551  $template->setVariable("TEXT_OPTION_VALUE", $this->object->prepareTextareaOutput($questiontext, TRUE));
552  $template->parseCurrentBlock();
553  $template->setCurrentBlock("detail_row");
554  $template->setVariable("TEXT_OPTION", $this->lng->txt("question_type"));
555  $template->setVariable("TEXT_OPTION_VALUE", $this->lng->txt($this->getQuestionType()));
556  $template->parseCurrentBlock();
557  $template->setCurrentBlock("detail_row");
558  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_answered"));
559  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_ANSWERED"]);
560  $template->parseCurrentBlock();
561  $template->setCurrentBlock("detail_row");
562  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_skipped"));
563  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_SKIPPED"]);
564  $template->parseCurrentBlock();
565  /*
566  $template->setCurrentBlock("detail_row");
567  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode"));
568  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE"]);
569  $template->parseCurrentBlock();
570  $template->setCurrentBlock("detail_row");
571  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode_nr_of_selections"));
572  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE_NR_OF_SELECTIONS"]);
573  $template->parseCurrentBlock();
574  */
575  $template->setCurrentBlock("detail_row");
576  $template->setVariable("TEXT_OPTION", $this->lng->txt("categories"));
577  $categories = "";
578  if (is_array($this->cumulated["variables"]))
579  {
580  foreach ($this->cumulated["variables"] as $key => $value)
581  {
582  $categories .= "<li>" . $value["title"] . ": n=" . $value["selected"] .
583  " (" . sprintf("%.2f", 100*$value["percentage"]) . "%)</li>";
584  }
585  }
586  $categories = "<ol>$categories</ol>";
587  $template->setVariable("TEXT_OPTION_VALUE", $categories);
588  $template->parseCurrentBlock();
589 
590  // add text answers to detailed results
591  if (is_array($this->cumulated["textanswers"]))
592  {
593  $template->setCurrentBlock("detail_row");
594  $template->setVariable("TEXT_OPTION", $this->lng->txt("freetext_answers"));
595  $html = "";
596  foreach ($this->cumulated["textanswers"] as $key => $answers)
597  {
598  $html .= $this->cumulated["variables"][$key]["title"] ."\n";
599  $html .= "<ul>\n";
600  foreach ($answers as $answer)
601  {
602  $html .= "<li>" . preg_replace("/\n/", "<br>\n", $answer) . "</li>\n";
603  }
604  $html .= "</ul>\n";
605  }
606  $template->setVariable("TEXT_OPTION_VALUE", $html);
607  $template->parseCurrentBlock();
608  }
609 
610  // chart
611  $template->setCurrentBlock("detail_row");
612  $template->setVariable("TEXT_OPTION", $this->lng->txt("chart"));
613  $template->setVariable("TEXT_OPTION_VALUE", $this->renderChart("svy_ch_".$this->object->getId(), $this->cumulated["variables"]));
614  $template->parseCurrentBlock();
615 
616  $template->setVariable("QUESTION_TITLE", "$counter. ".$this->object->getTitle());
617  return $template->get();
618  }
619 }
620 
621 ?>