ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assSingleChoiceGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/TestQuestionPool/classes/class.assQuestionGUI.php';
5 require_once './Modules/TestQuestionPool/interfaces/interface.ilGuiQuestionScoringAdjustable.php';
6 require_once './Modules/TestQuestionPool/interfaces/interface.ilGuiAnswerScoringAdjustable.php';
7 require_once './Modules/Test/classes/inc.AssessmentConstants.php';
8 
23 {
25 
33  public function __construct($id = -1)
34  {
36  include_once "./Modules/TestQuestionPool/classes/class.assSingleChoice.php";
37  $this->object = new assSingleChoice();
38  if ($id >= 0)
39  {
40  $this->object->loadFromDb($id);
41  }
42  }
43 
51  public function writePostData($always = false)
52  {
53  $hasErrors = (!$always) ? $this->editQuestion(true) : false;
54  if (!$hasErrors)
55  {
59  $this->saveTaxonomyAssignments();
60  return 0;
61  }
62  return 1;
63  }
64 
69  public function editQuestion($checkonly = FALSE)
70  {
71  $save = $this->isSaveCommand();
72  $this->getQuestionTemplate();
73 
74  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
75  $form = new ilPropertyFormGUI();
76  $form->setFormAction($this->ctrl->getFormAction($this));
77  $form->setTitle($this->outQuestionType());
78  $isSingleline = ($this->object->lastChange == 0 && !array_key_exists('types', $_POST)) ? (($this->object->getMultilineAnswerSetting()) ? false : true) : $this->object->isSingleline;
79  if ($checkonly) $isSingleline = ($_POST['types'] == 0) ? true : false;
80  if ($isSingleline)
81  {
82  $form->setMultipart(TRUE);
83  }
84  else
85  {
86  $form->setMultipart(FALSE);
87  }
88  $form->setTableWidth("100%");
89  $form->setId("asssinglechoice");
90 
91  $this->addBasicQuestionFormProperties( $form );
92  $this->populateQuestionSpecificFormPart( $form );
93  $this->populateAnswerSpecificFormPart( $form );
94 
95 
96  $this->populateTaxonomyFormSection($form);
97 
98  $this->addQuestionFormCommandButtons($form);
99 
100  $errors = false;
101 
102  if ($save)
103  {
104  $form->setValuesByPost();
105  $errors = !$form->checkInput();
106  $form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
107  if ($errors) $checkonly = false;
108  }
109 
110  if (!$checkonly) $this->tpl->setVariable("QUESTION_DATA", $form->getHTML());
111  return $errors;
112  }
113 
117  public function uploadchoice()
118  {
119  $this->writePostData(true);
120  $position = key($_POST['cmd']['uploadchoice']);
121  $this->editQuestion();
122  }
123 
127  public function removeimagechoice()
128  {
129  $this->writePostData(true);
130  $position = key($_POST['cmd']['removeimagechoice']);
131  $filename = $_POST['choice']['imagename'][$position];
132  $this->object->removeAnswerImage($position);
133  $this->editQuestion();
134  }
135 
139  public function addchoice()
140  {
141  $this->writePostData(true);
142  $position = key($_POST['cmd']['addchoice']);
143  $this->object->addAnswer("", 0, $position+1);
144  $this->editQuestion();
145  }
146 
150  public function removechoice()
151  {
152  $this->writePostData(true);
153  $position = key($_POST['cmd']['removechoice']);
154  $this->object->deleteAnswer($position);
155  $this->editQuestion();
156  }
157 
158  function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_question_postponed = FALSE,
159  $user_post_solutions = FALSE, $show_inline_specific_feedback = FALSE)
160  {
161  $test_output = $this->getTestOutput(
162  $active_id,
163  $pass,
164  $is_question_postponed,
165  $user_post_solutions,
166  $show_inline_specific_feedback
167  );
168 
169  $this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
170  $this->tpl->setVariable("FORMACTION", $formaction);
171  }
172 
187  $active_id,
188  $pass = NULL,
189  $graphicalOutput = FALSE,
190  $result_output = FALSE,
191  $show_question_only = TRUE,
192  $show_feedback = FALSE,
193  $show_correct_solution = FALSE,
194  $show_manual_scoring = FALSE,
195  $show_question_text = TRUE
196  )
197  {
198  // shuffle output
199  $keys = $this->getChoiceKeys();
200 
201  // get the solution of the user for the active pass or from the last pass if allowed
202  $user_solution = "";
203  if (($active_id > 0) && (!$show_correct_solution))
204  {
205  $solutions =& $this->object->getSolutionValues($active_id, $pass);
206  foreach ($solutions as $idx => $solution_value)
207  {
208  $user_solution = $solution_value["value1"];
209  }
210  }
211  else
212  {
213  $found_index = -1;
214  $max_points = 0;
215  foreach ($this->object->answers as $index => $answer)
216  {
217  if ($answer->getPoints() > $max_points)
218  {
219  $max_points = $answer->getPoints();
220  $found_index = $index;
221  }
222  }
223  $user_solution = $found_index;
224  }
225  // generate the question output
226  include_once "./Services/UICore/classes/class.ilTemplate.php";
227  $template = new ilTemplate("tpl.il_as_qpl_mc_sr_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
228  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html",TRUE, TRUE, "Modules/TestQuestionPool");
229  foreach ($keys as $answer_id)
230  {
231  $answer = $this->object->answers[$answer_id];
232  if (($active_id > 0) && (!$show_correct_solution))
233  {
234  if ($graphicalOutput)
235  {
236  // output of ok/not ok icons for user entered solutions
237  $ok = FALSE;
238  if (strcmp($user_solution, $answer_id) == 0)
239  {
240  if ($answer->getPoints() == $this->object->getMaximumPoints())
241  {
242  $ok = TRUE;
243  }
244  else
245  {
246  $ok = FALSE;
247  }
248  if ($ok)
249  {
250  $template->setCurrentBlock("icon_ok");
251  $template->setVariable("ICON_OK", ilUtil::getImagePath("icon_ok.png"));
252  $template->setVariable("TEXT_OK", $this->lng->txt("answer_is_right"));
253  $template->parseCurrentBlock();
254  }
255  else
256  {
257  $template->setCurrentBlock("icon_not_ok");
258  if ($answer->getPoints() > 0)
259  {
260  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_mostly_ok.png"));
261  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_not_correct_but_positive"));
262  }
263  else
264  {
265  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_not_ok.png"));
266  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_wrong"));
267  }
268  $template->parseCurrentBlock();
269  }
270  }
271  if (strlen($user_solution) == 0)
272  {
273  $template->setCurrentBlock("icon_not_ok");
274  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_not_ok.png"));
275  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_wrong"));
276  $template->parseCurrentBlock();
277  }
278  }
279  }
280  if (strlen($answer->getImage()))
281  {
282  $template->setCurrentBlock("answer_image");
283  if ($this->object->getThumbSize())
284  {
285  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $this->object->getThumbPrefix() . $answer->getImage());
286  }
287  else
288  {
289  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
290  }
291  $alt = $answer->getImage();
292  if (strlen($answer->getAnswertext()))
293  {
294  $alt = $answer->getAnswertext();
295  }
296  $alt = preg_replace("/<[^>]*?>/", "", $alt);
297  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
298  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
299  $template->parseCurrentBlock();
300  }
301  if ($show_feedback)
302  {
303  $feedbackOutputRequired = false;
304 
305  switch( $this->object->getSpecificFeedbackSetting() )
306  {
307  case 1:
308  $feedbackOutputRequired = true;
309  break;
310 
311  case 2:
312  if (strcmp($user_solution, $answer_id) == 0)
313  {
314  $feedbackOutputRequired = true;
315  }
316  break;
317 
318  case 3:
319  if ($this->object->getAnswer($answer_id)->getPoints() > 0)
320  {
321  $feedbackOutputRequired = true;
322  }
323  break;
324  }
325 
326  if($feedbackOutputRequired)
327  {
328  $fb = $this->object->feedbackOBJ->getSpecificAnswerFeedbackTestPresentation(
329  $this->object->getId(), $answer_id
330  );
331  if (strlen($fb))
332  {
333  $template->setCurrentBlock("feedback");
334  $template->setVariable("FEEDBACK", $this->object->prepareTextareaOutput( $fb, true ));
335  $template->parseCurrentBlock();
336  }
337  }
338  }
339  $template->setCurrentBlock("answer_row");
340  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
341  if (strcmp($user_solution, $answer_id) == 0)
342  {
343  $template->setVariable("SOLUTION_IMAGE", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_checked.png")));
344  $template->setVariable("SOLUTION_ALT", $this->lng->txt("checked"));
345  }
346  else
347  {
348  $template->setVariable("SOLUTION_IMAGE", ilUtil::getHtmlPath(ilUtil::getImagePath("radiobutton_unchecked.png")));
349  $template->setVariable("SOLUTION_ALT", $this->lng->txt("unchecked"));
350  }
351  if ($result_output)
352  {
353  $points = $this->object->answers[$answer_id]->getPoints();
354  $resulttext = ($points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
355  $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $points));
356  }
357  $template->parseCurrentBlock();
358  }
359  $questiontext = $this->object->getQuestion();
360  if ($show_question_text==true)
361  {
362  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
363  }
364  $questionoutput = $template->get();
365  $feedback = ($show_feedback) ? $this->getAnswerFeedbackOutput($active_id, $pass) : "";
366  if (strlen($feedback)) $solutiontemplate->setVariable("FEEDBACK", $this->object->prepareTextareaOutput( $feedback, true ));
367  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
368 
369  $solutionoutput = $solutiontemplate->get();
370  if (!$show_question_only)
371  {
372  // get page object output
373  $solutionoutput = '<div class="ilc_question_Standard">'.$solutionoutput."</div>";
374  }
375  return $solutionoutput;
376  }
377 
378  function getPreview($show_question_only = FALSE)
379  {
380  $keys = $this->getChoiceKeys();
381 
382  // generate the question output
383  include_once "./Services/UICore/classes/class.ilTemplate.php";
384  $template = new ilTemplate("tpl.il_as_qpl_mc_sr_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
385  foreach ($keys as $answer_id)
386  {
387  $answer = $this->object->answers[$answer_id];
388  if (strlen($answer->getImage()))
389  {
390  if ($this->object->getThumbSize())
391  {
392  $template->setCurrentBlock("preview");
393  $template->setVariable("URL_PREVIEW", $this->object->getImagePathWeb() . $answer->getImage());
394  $template->setVariable("TEXT_PREVIEW", $this->lng->txt('preview'));
395  $template->setVariable("IMG_PREVIEW", ilUtil::getImagePath('enlarge.png'));
396  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $this->object->getThumbPrefix() . $answer->getImage());
397  list($width, $height, $type, $attr) = getimagesize($this->object->getImagePath() . $answer->getImage());
398  $alt = $answer->getImage();
399  if (strlen($answer->getAnswertext()))
400  {
401  $alt = $answer->getAnswertext();
402  }
403  $alt = preg_replace("/<[^>]*?>/", "", $alt);
404  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
405  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
406  $template->parseCurrentBlock();
407  }
408  else
409  {
410  $template->setCurrentBlock("answer_image");
411  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
412  list($width, $height, $type, $attr) = getimagesize($this->object->getImagePath() . $answer->getImage());
413  $alt = $answer->getImage();
414  if (strlen($answer->getAnswertext()))
415  {
416  $alt = $answer->getAnswertext();
417  }
418  $alt = preg_replace("/<[^>]*?>/", "", $alt);
419  $template->setVariable("ATTR", $attr);
420  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
421  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
422  $template->parseCurrentBlock();
423  }
424  }
425  $template->setCurrentBlock("answer_row");
426  $template->setVariable("QID", $this->object->getId().'ID');
427  $template->setVariable("ANSWER_ID", $answer_id);
428  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
429  $template->parseCurrentBlock();
430  }
431  $questiontext = $this->object->getQuestion();
432  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
433  $questionoutput = $template->get();
434  if (!$show_question_only)
435  {
436  // get page object output
437  $questionoutput = $this->getILIASPage($questionoutput);
438  }
439  return $questionoutput;
440  }
441 
442  function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
443  {
444  $keys = $this->getChoiceKeys();
445 
446  // get the solution of the user for the active pass or from the last pass if allowed
447  $user_solution = "";
448  if ($active_id)
449  {
450  $solutions = NULL;
451  include_once "./Modules/Test/classes/class.ilObjTest.php";
452  if (!ilObjTest::_getUsePreviousAnswers($active_id, true))
453  {
454  if (is_null($pass)) $pass = ilObjTest::_getPass($active_id);
455  }
456  $solutions =& $this->object->getSolutionValues($active_id, $pass);
457  foreach ($solutions as $idx => $solution_value)
458  {
459  $user_solution = $solution_value["value1"];
460  }
461  }
462 
463  // generate the question output
464  include_once "./Services/UICore/classes/class.ilTemplate.php";
465  $template = new ilTemplate("tpl.il_as_qpl_mc_sr_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
466  foreach ($keys as $answer_id)
467  {
468  $answer = $this->object->answers[$answer_id];
469  if (strlen($answer->getImage()))
470  {
471  if ($this->object->getThumbSize())
472  {
473  $template->setCurrentBlock("preview");
474  $template->setVariable("URL_PREVIEW", $this->object->getImagePathWeb() . $answer->getImage());
475  $template->setVariable("TEXT_PREVIEW", $this->lng->txt('preview'));
476  $template->setVariable("IMG_PREVIEW", ilUtil::getImagePath('enlarge.png'));
477  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $this->object->getThumbPrefix() . $answer->getImage());
478  list($width, $height, $type, $attr) = getimagesize($this->object->getImagePath() . $answer->getImage());
479  $alt = $answer->getImage();
480  if (strlen($answer->getAnswertext()))
481  {
482  $alt = $answer->getAnswertext();
483  }
484  $alt = preg_replace("/<[^>]*?>/", "", $alt);
485  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
486  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
487  $template->parseCurrentBlock();
488  }
489  else
490  {
491  $template->setCurrentBlock("answer_image");
492  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
493  list($width, $height, $type, $attr) = getimagesize($this->object->getImagePath() . $answer->getImage());
494  $alt = $answer->getImage();
495  if (strlen($answer->getAnswertext()))
496  {
497  $alt = $answer->getAnswertext();
498  }
499  $alt = preg_replace("/<[^>]*?>/", "", $alt);
500  $template->setVariable("ATTR", $attr);
501  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
502  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
503  $template->parseCurrentBlock();
504  }
505  }
506  if ($show_feedback)
507  {
508  $feedbackOutputRequired = false;
509 
510  switch( $this->object->getSpecificFeedbackSetting() )
511  {
512  case 1:
513  $feedbackOutputRequired = true;
514  break;
515 
516  case 2:
517  if (strcmp($user_solution, $answer_id) == 0)
518  {
519  $feedbackOutputRequired = true;
520  }
521  break;
522 
523  case 3:
524  if ($this->object->getAnswer($answer_id)->getPoints() > 0)
525  {
526  $feedbackOutputRequired = true;
527  }
528  break;
529  }
530 
531  if($feedbackOutputRequired)
532  {
533  $fb = $this->object->feedbackOBJ->getSpecificAnswerFeedbackTestPresentation(
534  $this->object->getId(), $answer_id
535  );
536  if (strlen($fb))
537  {
538  $template->setCurrentBlock("feedback");
539  $template->setVariable("FEEDBACK", $this->object->prepareTextareaOutput( $fb, true ));
540  $template->parseCurrentBlock();
541  }
542  }
543  }
544  $template->setCurrentBlock("answer_row");
545  $template->setVariable("ANSWER_ID", $answer_id);
546  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
547  if (strcmp($user_solution, $answer_id) == 0)
548  {
549  $template->setVariable("CHECKED_ANSWER", " checked=\"checked\"");
550  }
551  $template->parseCurrentBlock();
552  }
553  $questiontext = $this->object->getQuestion();
554  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
555  $questionoutput = $template->get();
556  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
557  return $pageoutput;
558  }
559 
567  function setQuestionTabs()
568  {
569  global $rbacsystem, $ilTabs;
570 
571  $this->ctrl->setParameterByClass("ilAssQuestionPageGUI", "q_id", $_GET["q_id"]);
572  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
573  $q_type = $this->object->getQuestionType();
574 
575  if (strlen($q_type))
576  {
577  $classname = $q_type . "GUI";
578  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
579  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
580  }
581 
582  if ($_GET["q_id"])
583  {
584  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
585  {
586  // edit page
587  $ilTabs->addTarget("edit_page",
588  $this->ctrl->getLinkTargetByClass("ilAssQuestionPageGUI", "edit"),
589  array("edit", "insert", "exec_pg"),
590  "", "", $force_active);
591  }
592 
593  // edit page
594  $ilTabs->addTarget("preview",
595  $this->ctrl->getLinkTargetByClass("ilAssQuestionPageGUI", "preview"),
596  array("preview"),
597  "ilAssQuestionPageGUI", "", $force_active);
598  }
599 
600  $force_active = false;
601  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
602  {
603  $url = "";
604  if ($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
605  // edit question properties
606  $ilTabs->addTarget("edit_question",
607  $url,
608  array("editQuestion", "save", "saveEdit", "addchoice", "removechoice", "removeimagechoice", "uploadchoice", "originalSyncForm"),
609  $classname, "", $force_active);
610  }
611 
612  // add tab for question feedback within common class assQuestionGUI
613  $this->addTab_QuestionFeedback($ilTabs);
614 
615  // add tab for question hint within common class assQuestionGUI
616  $this->addTab_QuestionHints($ilTabs);
617 
618  if ($_GET["q_id"])
619  {
620  $ilTabs->addTarget("solution_hint",
621  $this->ctrl->getLinkTargetByClass($classname, "suggestedsolution"),
622  array("suggestedsolution", "saveSuggestedSolution", "outSolutionExplorer", "cancel",
623  "addSuggestedSolution","cancelExplorer", "linkChilds", "removeSuggestedSolution"
624  ),
625  $classname,
626  ""
627  );
628  }
629 
630  // Assessment of questions sub menu entry
631  if ($_GET["q_id"])
632  {
633  $ilTabs->addTarget("statistics",
634  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
635  array("assessment"),
636  $classname, "");
637  }
638 
639  if (($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
640  {
641  $ref_id = $_GET["calling_test"];
642  if (strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
643 
644  global $___test_express_mode;
645 
646  if (!$_GET['test_express_mode'] && !$___test_express_mode) {
647  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
648  }
649  else {
651  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), $link);
652  }
653  }
654  else
655  {
656  $ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
657  }
658  }
659 
660  /*
661  * Create the key index numbers for the array of choices
662  *
663  * @return array
664  */
665  function getChoiceKeys()
666  {
667  if (strcmp($_GET["activecommand"], "directfeedback") == 0)
668  {
669  if (is_array($_SESSION["choicekeys"])) $this->choiceKeys = $_SESSION["choicekeys"];
670  }
671  if (!is_array($this->choiceKeys))
672  {
673  $this->choiceKeys = array_keys($this->object->answers);
674  if ($this->object->getShuffle())
675  {
676  $this->choiceKeys = $this->object->pcArrayShuffle($this->choiceKeys);
677  }
678  }
679  $_SESSION["choicekeys"] = $this->choiceKeys;
680  return $this->choiceKeys;
681  }
682 
683  function getSpecificFeedbackOutput($active_id, $pass)
684  {
685  // No return value, this question type supports inline specific feedback.
686  $output = "";
687  return $this->object->prepareTextareaOutput($output, TRUE);
688  }
689 
690  public function writeQuestionSpecificPostData($always = true)
691  {
692  $this->object->setShuffle( $_POST["shuffle"] );
693  $this->object->setMultilineAnswerSetting( $_POST["types"] );
694  if (is_array( $_POST['choice']['imagename'] ) && $_POST["types"] == 1)
695  {
696  $this->object->isSingleline = true;
697  ilUtil::sendInfo( $this->lng->txt( 'info_answer_type_change' ), true );
698  }
699  else
700  {
701  $this->object->isSingleline = ($_POST["types"] == 0) ? true : false;
702  }
703  $this->object->setThumbSize( (strlen( $_POST["thumb_size"] )) ? $_POST["thumb_size"] : "" );
704  }
705 
707  {
708  $isSingleline = ($this->object->lastChange == 0 && !array_key_exists( 'types',$_POST)) ? (($this->object->getMultilineAnswerSetting()) ? false : true) : $this->object->isSingleline;
709  // shuffle
710  $shuffle = new ilCheckboxInputGUI($this->lng->txt( "shuffle_answers" ), "shuffle");
711  $shuffle->setValue( 1 );
712  $shuffle->setChecked( $this->object->getShuffle() );
713  $shuffle->setRequired( FALSE );
714  $form->addItem( $shuffle );
715 
716  if ($this->object->getId())
717  {
718  $hidden = new ilHiddenInputGUI("", "ID");
719  $hidden->setValue( $this->object->getId() );
720  $form->addItem( $hidden );
721  }
722 
723  if (!$this->object->getSelfAssessmentEditingMode())
724  {
725  // Answer types
726  $types = new ilSelectInputGUI($this->lng->txt( "answer_types" ), "types");
727  $types->setRequired( false );
728  $types->setValue( ($isSingleline) ? 0 : 1 );
729  $types->setOptions( array(
730  0 => $this->lng->txt( 'answers_singleline' ),
731  1 => $this->lng->txt( 'answers_multiline' ),
732  )
733  );
734  $form->addItem( $types );
735  }
736 
737  if ($isSingleline)
738  {
739  // thumb size
740  $thumb_size = new ilNumberInputGUI($this->lng->txt( "thumb_size" ), "thumb_size");
741  $thumb_size->setSuffix($this->lng->txt("thumb_size_unit_pixel"));
742  $thumb_size->setMinValue( 20 );
743  $thumb_size->setDecimals( 0 );
744  $thumb_size->setSize( 6 );
745  $thumb_size->setInfo( $this->lng->txt( 'thumb_size_info' ) );
746  $thumb_size->setValue( $this->object->getThumbSize() );
747  $thumb_size->setRequired( false );
748  $form->addItem( $thumb_size );
749  }
750  return $form;
751  }
752 
763  {
764  return array('shuffle','types','thumb_size');
765  }
766 
767  public function writeAnswerSpecificPostData($always = true)
768  {
769  // Delete all existing answers and create new answers from the form data
770  $this->object->flushAnswers();
771  if ($this->object->isSingleline)
772  {
773  foreach ($_POST['choice']['answer'] as $index => $answertext)
774  {
775  $picturefile = $_POST['choice']['imagename'][$index];
776  $file_org_name = $_FILES['choice']['name']['image'][$index];
777  $file_temp_name = $_FILES['choice']['tmp_name']['image'][$index];
778 
779  if (strlen( $file_temp_name ))
780  {
781  // check suffix
782  $suffix = strtolower( array_pop( explode( ".", $file_org_name ) ) );
783  if (in_array( $suffix, array( "jpg", "jpeg", "png", "gif" ) ))
784  {
785  // upload image
786  $filename = $this->object->createNewImageFileName( $file_org_name );
787  if ($this->object->setImageFile( $filename, $file_temp_name ) == 0)
788  {
789  $picturefile = $filename;
790  }
791  }
792  }
793 
794  $this->object->addAnswer( $answertext, $_POST['choice']['points'][$index], $index, $picturefile );
795  }
796  }
797  else
798  {
799  foreach ($_POST['choice']['answer'] as $index => $answer)
800  {
801  $answertext = $answer;
802  $this->object->addAnswer( $answertext, $_POST['choice']['points'][$index], $index );
803  }
804  }
805  }
806 
808  {
809  $isSingleline = ($this->object->lastChange == 0 && !array_key_exists('types', $_POST)) ? (($this->object->getMultilineAnswerSetting()) ? false : true) : $this->object->isSingleline;
810 
811  // Choices
812  include_once "./Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php";
813  $choices = new ilSingleChoiceWizardInputGUI($this->lng->txt( "answers" ), "choice");
814  $choices->setRequired( true );
815  $choices->setQuestionObject( $this->object );
816  $choices->setSingleline( $isSingleline );
817  $choices->setAllowMove( false );
818  if ($this->object->getSelfAssessmentEditingMode())
819  {
820  $choices->setSize( 40 );
821  $choices->setMaxLength( 800 );
822  }
823  if ($this->object->getAnswerCount() == 0)
824  $this->object->addAnswer( "", 0, 0 );
825  $choices->setValues( $this->object->getAnswers() );
826  $form->addItem( $choices );
827  }
828 
839  {
840  return array();
841  }
842 
843  public function reworkFormForCorrectionMode(ilPropertyFormGUI $form)
844  {
846  $singlechoice_wizardinputgui = $form->getItemByPostVar('choice');
847  $singlechoice_wizardinputgui->setDisableUpload(true);
848  $singlechoice_wizardinputgui->setDisableActions(true);
849  $singlechoice_wizardinputgui->setDisableText(true);
850  return $form;
851  }
852 
861  public function getAggregatedAnswersView($relevant_answers)
862  {
863  $passcount = count($relevant_answers);
864  foreach($relevant_answers as $pass)
865  {
866  $actives[$pass['active_fi']] = $pass['active_fi'];
867  }
868  $usercount = count($actives);
869  $tpl = new ilTemplate('tpl.il_as_aggregated_answers_header.html', true, true, "Modules/TestQuestionPool");
870  $tpl->setVariable('HEADERTEXT', $this->lng->txt('overview'));
871  $tpl->setVariable('NUMBER_OF_USERS_INFO', $this->lng->txt('number_of_users'));
872  $tpl->setVariable('NUMBER_OF_USERS', $usercount);
873  $tpl->setVariable('NUMBER_OF_PASSES_INFO', $this->lng->txt('number_of_passes'));
874  $tpl->setVariable('NUMBER_OF_PASSES', $passcount);
875 
876  return $tpl->get() . $this->renderAggregateView(
877  $this->aggregateAnswers( $relevant_answers, $this->object->getAnswers() ) )->get();
878  }
879 
880  public function aggregateAnswers($relevant_answers_chosen, $answers_defined_on_question)
881  {
882  $aggregate = array();
883  foreach ($answers_defined_on_question as $answer)
884  {
885  $aggregated_info_for_answer = array();
886  $aggregated_info_for_answer['answertext'] = $answer->getAnswerText();
887  $aggregated_info_for_answer['count_checked'] = 0;
888 
889  foreach ($relevant_answers_chosen as $relevant_answer)
890  {
891  if ($relevant_answer['value1'] == $answer->getOrder())
892  {
893  $aggregated_info_for_answer['count_checked']++;
894  }
895  }
896  $aggregated_info_for_answer['count_unchecked'] =
897  ceil(count($relevant_answers_chosen) / count($answers_defined_on_question))
898  - $aggregated_info_for_answer['count_checked'];
899 
900  $aggregate[] = $aggregated_info_for_answer;
901  }
902  return $aggregate;
903  }
904 
910  public function renderAggregateView($aggregate)
911  {
912  $tpl = new ilTemplate('tpl.il_as_aggregated_answers_table.html', true, true, "Modules/TestQuestionPool");
913  $tpl->setVariable( 'OPTION_HEADER', $this->lng->txt('option') );
914  $tpl->setVariable( 'COUNT_HEADER', $this->lng->txt('count') );
915  $tpl->setVariable( 'AGGREGATION_HEADER', $this->lng->txt('aggregated_answers_header') );
916  foreach ($aggregate as $line_data)
917  {
918  $tpl->setCurrentBlock( 'aggregaterow' );
919  $tpl->setVariable( 'OPTION', $line_data['answertext'] );
920  $tpl->setVariable( 'COUNT', $line_data['count_checked'] );
921  $tpl->parseCurrentBlock();
922  }
923  return $tpl;
924  }
925 }