ILIAS  Release_4_4_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 
180  function getPrintView($question_title = 1, $show_questiontext = 1, $survey_id = null)
181  {
182  $template = new ilTemplate("tpl.il_svy_qpl_mc_printview.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
183  switch ($this->object->getOrientation())
184  {
185  case 0:
186  // vertical orientation
187  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
188  {
189  $cat = $this->object->categories->getCategory($i);
190  if ($cat->other)
191  {
192  $template->setCurrentBlock("other_row");
193  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.png")));
194  $template->setVariable("ALT_CHECKBOX", $this->lng->txt("unchecked"));
195  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt("unchecked"));
196  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($cat->title));
197  $template->setVariable("OTHER_ANSWER", "&nbsp;");
198  $template->parseCurrentBlock();
199  }
200  else
201  {
202  $template->setCurrentBlock("mc_row");
203  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.png")));
204  $template->setVariable("ALT_CHECKBOX", $this->lng->txt("unchecked"));
205  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt("unchecked"));
206  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
207  $template->parseCurrentBlock();
208  }
209  }
210  break;
211  case 1:
212  // horizontal orientation
213  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
214  {
215  $template->setCurrentBlock("checkbox_col");
216  $template->setVariable("IMAGE_CHECKBOX", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.png")));
217  $template->setVariable("ALT_CHECKBOX", $this->lng->txt("unchecked"));
218  $template->setVariable("TITLE_CHECKBOX", $this->lng->txt("unchecked"));
219  $template->parseCurrentBlock();
220  }
221  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
222  {
223  $cat = $this->object->categories->getCategory($i);
224  if ($cat->other)
225  {
226  $template->setCurrentBlock("other_text_col");
227  $template->setVariable("OTHER_LABEL", ilUtil::prepareFormOutput($cat->title));
228  $template->setVariable("OTHER_ANSWER", "&nbsp;");
229  $template->parseCurrentBlock();
230  }
231  else
232  {
233  $template->setCurrentBlock("text_col");
234  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
235  $template->parseCurrentBlock();
236  }
237  }
238  break;
239  }
240 
241  if ($this->object->use_min_answers)
242  {
243  $template->setCurrentBlock('min_max_msg');
244  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0)
245  {
246  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
247  }
248  else if ($this->object->nr_min_answers > 0)
249  {
250  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
251  }
252  else if ($this->object->nr_max_answers > 0)
253  {
254  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
255  }
256  $template->parseCurrentBlock();
257  }
258  if ($show_questiontext)
259  {
260  $this->outQuestionText($template);
261  }
262  if ($question_title)
263  {
264  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
265  }
266  $template->parseCurrentBlock();
267  return $template->get();
268  }
269 
270 
271  //
272  // EXECUTION
273  //
274 
280  function getWorkingForm($working_data = "", $question_title = 1, $show_questiontext = 1, $error_message = "", $survey_id = null)
281  {
282  $template = new ilTemplate("tpl.il_svy_out_mc.html", TRUE, TRUE, "Modules/SurveyQuestionPool");
283  $template->setCurrentBlock("material");
284  $template->setVariable("TEXT_MATERIAL", $this->getMaterialOutput());
285  $template->parseCurrentBlock();
286  switch ($this->object->getOrientation())
287  {
288  case 0:
289  // vertical orientation
290  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
291  {
292  $cat = $this->object->categories->getCategory($i);
293  if ($cat->other)
294  {
295  $template->setCurrentBlock("other_row");
296  if (strlen($cat->title))
297  {
298  $template->setVariable("OTHER_LABEL", $cat->title);
299  }
300  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
301  $template->setVariable("QUESTION_ID", $this->object->getId());
302  if (is_array($working_data))
303  {
304  foreach ($working_data as $value)
305  {
306  if (strlen($value["value"]))
307  {
308  if ($value["value"] == $cat->scale-1)
309  {
310  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
311  if (!$value['uncheck'])
312  {
313  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
314  }
315  }
316  }
317  }
318  }
319  $template->parseCurrentBlock();
320  }
321  else
322  {
323  $template->setCurrentBlock("mc_row");
324  if ($cat->neutral) $template->setVariable('ROWCLASS', ' class="neutral"');
325  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
326  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
327  $template->setVariable("QUESTION_ID", $this->object->getId());
328  if (is_array($working_data))
329  {
330  foreach ($working_data as $value)
331  {
332  if (strlen($value["value"]))
333  {
334  if ($value["value"] == $cat->scale-1)
335  {
336  if (!$value['uncheck'])
337  {
338  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
339  }
340  }
341  }
342  }
343  }
344  $template->parseCurrentBlock();
345  }
346  $template->touchBlock('outer_row');
347  }
348  break;
349  case 1:
350  // horizontal orientation
351 
352  // #15477 - reverting the categorizing of answers
353  for ($i = 0; $i < $this->object->categories->getCategoryCount(); $i++)
354  {
355  $cat = $this->object->categories->getCategory($i);
356 
357  // checkbox
358  $template->setCurrentBlock("checkbox_col");
359  if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
360  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
361  $template->setVariable("QUESTION_ID", $this->object->getId());
362  if (is_array($working_data))
363  {
364  foreach ($working_data as $value)
365  {
366  if (strlen($value["value"]))
367  {
368  if ($value["value"] == $cat->scale-1)
369  {
370  if (!$value['uncheck'])
371  {
372  $template->setVariable("CHECKED_MC", " checked=\"checked\"");
373  }
374  }
375  }
376  }
377  }
378  $template->parseCurrentBlock();
379 
380  // answer & input
381  if ($cat->other)
382  {
383  $template->setCurrentBlock("text_other_col");
384  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
385  $template->setVariable("QUESTION_ID", $this->object->getId());
386  if (strlen($cat->title))
387  {
388  $template->setVariable("OTHER_LABEL", $cat->title);
389  }
390  if (is_array($working_data))
391  {
392  foreach ($working_data as $value)
393  {
394  if (strlen($value["value"]))
395  {
396  if ($value["value"] == $cat->scale-1)
397  {
398  $template->setVariable("OTHER_VALUE", ' value="' . ilUtil::prepareFormOutput($value['textanswer']) . '"');
399  }
400  }
401  }
402  }
403  $template->parseCurrentBlock();
404  }
405  // answer
406  else
407  {
408  $template->setCurrentBlock("text_col");
409  if ($cat->neutral) $template->setVariable('COLCLASS', ' neutral');
410  $template->setVariable("VALUE_MC", ($cat->scale) ? ($cat->scale - 1) : $i);
411  $template->setVariable("TEXT_MC", ilUtil::prepareFormOutput($cat->title));
412  $template->setVariable("QUESTION_ID", $this->object->getId());
413  $template->parseCurrentBlock();
414  }
415  $template->touchBlock('text_outer_col');
416  }
417  break;
418  }
419 
420  $template->setCurrentBlock("question_data");
421  if ($this->object->use_min_answers)
422  {
423  $template->setCurrentBlock('min_max_msg');
424  if ($this->object->nr_min_answers > 0 && $this->object->nr_max_answers > 0)
425  {
426  if ($this->object->nr_min_answers == $this->object->nr_max_answers)
427  {
428  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_exact_answers'), $this->object->nr_min_answers));
429  }
430  else
431  {
432  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_max_nr_answers'), $this->object->nr_min_answers, $this->object->nr_max_answers));
433  }
434  }
435  else if ($this->object->nr_min_answers > 0)
436  {
437  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_min_nr_answers'), $this->object->nr_min_answers));
438  }
439  else if ($this->object->nr_max_answers > 0)
440  {
441  $template->setVariable('MIN_MAX_MSG', sprintf($this->lng->txt('msg_max_nr_answers'), $this->object->nr_max_answers));
442  }
443  $template->parseCurrentBlock();
444  }
445  if (strcmp($error_message, "") != 0)
446  {
447  $template->setVariable("ERROR_MESSAGE", "<p class=\"warning\">$error_message</p>");
448  }
449  if ($show_questiontext)
450  {
451  $this->outQuestionText($template);
452  }
453  if ($question_title)
454  {
455  $template->setVariable("QUESTION_TITLE", $this->object->getTitle());
456  }
457  $template->parseCurrentBlock();
458  return $template->get();
459  }
460 
461 
462  //
463  // EVALUTION
464  //
465 
474  function getCumulatedResultsDetails($survey_id, $counter, $finished_ids)
475  {
476  if (count($this->cumulated) == 0)
477  {
478  if(!$finished_ids)
479  {
480  include_once "./Modules/Survey/classes/class.ilObjSurvey.php";
481  $nr_of_users = ilObjSurvey::_getNrOfParticipants($survey_id);
482  }
483  else
484  {
485  $nr_of_users = sizeof($finished_ids);
486  }
487  $this->cumulated =& $this->object->getCumulatedResults($survey_id, $nr_of_users, $finished_ids);
488  }
489 
490  $output = "";
491  include_once "./Services/UICore/classes/class.ilTemplate.php";
492  $template = new ilTemplate("tpl.il_svy_svy_cumulated_results_detail.html", TRUE, TRUE, "Modules/Survey");
493 
494  $template->setCurrentBlock("detail_row");
495  $template->setVariable("TEXT_OPTION", $this->lng->txt("question"));
496  $questiontext = $this->object->getQuestiontext();
497  $template->setVariable("TEXT_OPTION_VALUE", $this->object->prepareTextareaOutput($questiontext, TRUE));
498  $template->parseCurrentBlock();
499  $template->setCurrentBlock("detail_row");
500  $template->setVariable("TEXT_OPTION", $this->lng->txt("question_type"));
501  $template->setVariable("TEXT_OPTION_VALUE", $this->lng->txt($this->getQuestionType()));
502  $template->parseCurrentBlock();
503  $template->setCurrentBlock("detail_row");
504  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_answered"));
505  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_ANSWERED"]);
506  $template->parseCurrentBlock();
507  $template->setCurrentBlock("detail_row");
508  $template->setVariable("TEXT_OPTION", $this->lng->txt("users_skipped"));
509  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["USERS_SKIPPED"]);
510  $template->parseCurrentBlock();
511  /*
512  $template->setCurrentBlock("detail_row");
513  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode"));
514  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE"]);
515  $template->parseCurrentBlock();
516  $template->setCurrentBlock("detail_row");
517  $template->setVariable("TEXT_OPTION", $this->lng->txt("mode_nr_of_selections"));
518  $template->setVariable("TEXT_OPTION_VALUE", $this->cumulated["MODE_NR_OF_SELECTIONS"]);
519  $template->parseCurrentBlock();
520  */
521  $template->setCurrentBlock("detail_row");
522  $template->setVariable("TEXT_OPTION", $this->lng->txt("categories"));
523  $categories = "";
524  if (is_array($this->cumulated["variables"]))
525  {
526  foreach ($this->cumulated["variables"] as $key => $value)
527  {
528  $categories .= "<li>" . $value["title"] . ": n=" . $value["selected"] .
529  " (" . sprintf("%.2f", 100*$value["percentage"]) . "%)</li>";
530  }
531  }
532  $categories = "<ol>$categories</ol>";
533  $template->setVariable("TEXT_OPTION_VALUE", $categories);
534  $template->parseCurrentBlock();
535 
536  // add text answers to detailed results
537  if (is_array($this->cumulated["textanswers"]))
538  {
539  $template->setCurrentBlock("detail_row");
540  $template->setVariable("TEXT_OPTION", $this->lng->txt("freetext_answers"));
541  $html = "";
542  foreach ($this->cumulated["textanswers"] as $key => $answers)
543  {
544  $html .= $this->cumulated["variables"][$key]["title"] ."\n";
545  $html .= "<ul>\n";
546  foreach ($answers as $answer)
547  {
548  $html .= "<li>" . preg_replace("/\n/", "<br>\n", $answer) . "</li>\n";
549  }
550  $html .= "</ul>\n";
551  }
552  $template->setVariable("TEXT_OPTION_VALUE", $html);
553  $template->parseCurrentBlock();
554  }
555 
556  // chart
557  $template->setCurrentBlock("detail_row");
558  $template->setVariable("TEXT_OPTION", $this->lng->txt("chart"));
559  $template->setVariable("TEXT_OPTION_VALUE", $this->renderChart("svy_ch_".$this->object->getId(), $this->cumulated["variables"]));
560  $template->parseCurrentBlock();
561 
562  $template->setVariable("QUESTION_TITLE", "$counter. ".$this->object->getTitle());
563  return $template->get();
564  }
565 }
566 
567 ?>