ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMultipleChoiceGUI.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/TestQuestionPool/classes/class.assQuestionGUI.php";
25 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
26 
38 {
40 
50  $id = -1
51  )
52  {
53  $this->assQuestionGUI();
54  include_once "./Modules/TestQuestionPool/classes/class.assMultipleChoice.php";
55  $this->object = new assMultipleChoice();
56  if ($id >= 0)
57  {
58  $this->object->loadFromDb($id);
59  }
60  }
61 
62  function getCommand($cmd)
63  {
64  if (substr($cmd, 0, 6) == "upload")
65  {
66  $cmd = "upload";
67  }
68  if (substr($cmd, 0, 11) == "deleteImage")
69  {
70  $cmd = "deleteImage";
71  }
72  return $cmd;
73  }
74 
82  function editQuestion()
83  {
84  //$this->tpl->setVariable("HEADER", $this->object->getTitle());
85 
86  $this->tpl->addJavascript("./Services/JavaScript/js/Basic.js");
87  $javascript = "<script type=\"text/javascript\">ilAddOnLoad(initialSelect);\n".
88  "function initialSelect() {\n%s\n}</script>";
89  $graphical_answer_setting = $this->object->getGraphicalAnswerSetting();
90  $multiline_answers = $this->object->getMultilineAnswerSetting();
91  if ($graphical_answer_setting == 0)
92  {
93  for ($i = 0; $i < $this->object->getAnswerCount(); $i++)
94  {
95  $answer = $this->object->getAnswer($i);
96  if (strlen($answer->getImage())) $graphical_answer_setting = 1;
97  }
98  }
99  $this->getQuestionTemplate();
100  $this->tpl->addBlockFile("QUESTION_DATA", "question_data", "tpl.il_as_qpl_mc_mr.html", "Modules/TestQuestionPool");
101 
102  if ($this->object->getAnswerCount() > 0)
103  {
104  $this->tpl->setCurrentBlock("answersheading");
105  $this->tpl->setVariable("TEXT_POINTS_CHECKED", $this->lng->txt("points_checked"));
106  $this->tpl->setVariable("TEXT_POINTS_UNCHECKED", $this->lng->txt("points_unchecked"));
107  $this->tpl->setVariable("TEXT_ANSWER_TEXT", $this->lng->txt("answer_text"));
108  $this->tpl->parseCurrentBlock();
109  $this->tpl->setCurrentBlock("selectall");
110  $this->tpl->setVariable("SELECT_ALL", $this->lng->txt("select_all"));
111  $this->tpl->parseCurrentBlock();
112  $this->tpl->setCurrentBlock("existinganswers");
113  $this->tpl->setVariable("DELETE", $this->lng->txt("delete"));
114  $this->tpl->setVariable("MOVE", $this->lng->txt("move"));
115  $this->tpl->setVariable("ARROW", "<img src=\"" . ilUtil::getImagePath("arrow_downright.gif") . "\" alt=\"".$this->lng->txt("arrow_downright")."\">");
116  $this->tpl->parseCurrentBlock();
117  }
118  for ($i = 0; $i < $this->object->getAnswerCount(); $i++)
119  {
120  $answer = $this->object->getAnswer($i);
121  if ($graphical_answer_setting == 1)
122  {
123  $imagefilename = $this->object->getImagePath() . $answer->getImage();
124  if (!@file_exists($imagefilename))
125  {
126  $answer->setImage("");
127  }
128  if (strlen($answer->getImage()))
129  {
130  $imagepath = $this->object->getImagePathWeb() . $answer->getImage();
131  $this->tpl->setCurrentBlock("graphical_answer_image");
132  $this->tpl->setVariable("IMAGE_FILE", $imagepath);
133  if (strlen($answer->getAnswertext()))
134  {
135  $this->tpl->setVariable("IMAGE_ALT", ilUtil::prepareFormOutput($answer->getAnswertext()));
136  }
137  else
138  {
139  $this->tpl->setVariable("IMAGE_ALT", $this->lng->txt("image"));
140  }
141  $this->tpl->setVariable("ANSWER_ORDER", $answer->getOrder());
142  $this->tpl->setVariable("DELETE_IMAGE", $this->lng->txt("delete_image"));
143  $this->tpl->parseCurrentBlock();
144  }
145  $this->tpl->setCurrentBlock("graphical_answer");
146  $this->tpl->setVariable("ANSWER_ORDER", $answer->getOrder());
147  $this->tpl->setVariable("UPLOAD_IMAGE", $this->lng->txt("upload_image"));
148  $this->tpl->setVariable("VALUE_IMAGE", $answer->getImage());
149  $this->tpl->parseCurrentBlock();
150  }
151  if ($multiline_answers)
152  {
153  $this->tpl->setCurrentBlock("show_textarea");
154  $this->tpl->setVariable("ANSWER_ANSWER_ORDER", $answer->getOrder());
155  $this->tpl->setVariable("VALUE_ANSWER", ilUtil::prepareFormOutput($answer->getAnswertext()));
156  $this->tpl->parseCurrentBlock();
157  }
158  else
159  {
160  $this->tpl->setCurrentBlock("show_textinput");
161  $this->tpl->setVariable("ANSWER_ANSWER_ORDER", $answer->getOrder());
162  $this->tpl->setVariable("VALUE_ANSWER", ilUtil::prepareFormOutput($answer->getAnswertext()));
163  $this->tpl->parseCurrentBlock();
164  }
165  $this->tpl->setCurrentBlock("answers");
166  $this->tpl->setVariable("VALUE_MULTIPLE_CHOICE_POINTS_CHECKED", $answer->getPoints());
167  $this->tpl->setVariable("VALUE_MULTIPLE_CHOICE_POINTS_UNCHECKED", $answer->getPointsUnchecked());
168  $this->tpl->setVariable("ANSWER_ORDER", $answer->getOrder());
169  $this->tpl->parseCurrentBlock();
170  }
171 
172  // call to other question data i.e. estimated working time block
173  $this->outOtherQuestionData();
174 
175  $internallinks = array(
176  "lm" => $this->lng->txt("obj_lm"),
177  "st" => $this->lng->txt("obj_st"),
178  "pg" => $this->lng->txt("obj_pg"),
179  "glo" => $this->lng->txt("glossary_term")
180  );
181  foreach ($internallinks as $key => $value)
182  {
183  $this->tpl->setCurrentBlock("internallink");
184  $this->tpl->setVariable("TYPE_INTERNAL_LINK", $key);
185  $this->tpl->setVariable("TEXT_INTERNAL_LINK", $value);
186  $this->tpl->parseCurrentBlock();
187  }
188 
189  $this->tpl->setCurrentBlock("HeadContent");
190 
191  if ($this->object->getAnswerCount() == 0)
192  {
193  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_multiple_choice.title.focus();"));
194  }
195  else
196  {
197  switch ($this->ctrl->getCmd())
198  {
199  case "add":
200  $nrOfAnswers = $_POST["nrOfAnswers"];
201  if ((strcmp($nrOfAnswers, "yn") == 0) || (strcmp($nrOfAnswers, "tf") == 0)) $nrOfAnswers = 2;
202  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_multiple_choice.answer_".($this->object->getAnswerCount() - $nrOfAnswers).".focus(); document.getElementById('answer_".($this->object->getAnswerCount() - $nrOfAnswers)."').scrollIntoView(\"true\");"));
203  break;
204  case "deleteAnswer":
205  if ($this->object->getAnswerCount() == 0)
206  {
207  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_multiple_choice.title.focus();"));
208  }
209  else
210  {
211  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_multiple_choice.answer_".($this->object->getAnswerCount() - 1).".focus(); document.getElementById('answer_".($this->object->getAnswerCount() - 1)."').scrollIntoView(\"true\");"));
212  }
213  break;
214  default:
215  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_multiple_choice.title.focus();"));
216  break;
217  }
218  }
219  $this->tpl->parseCurrentBlock();
220 
221  for ($i = 1; $i < 10; $i++)
222  {
223  $this->tpl->setCurrentBlock("numbers");
224  $this->tpl->setVariable("VALUE_NUMBER", $i);
225  if ($i == 1)
226  {
227  $this->tpl->setVariable("TEXT_NUMBER", $i . " " . $this->lng->txt("answer"));
228  }
229  else
230  {
231  $this->tpl->setVariable("TEXT_NUMBER", $i . " " . $this->lng->txt("answers"));
232  }
233  $this->tpl->parseCurrentBlock();
234  }
235 
236  $this->tpl->setCurrentBlock("question_data");
237  $this->tpl->setVariable("TEXT_TITLE", $this->lng->txt("title"));
238  $this->tpl->setVariable("TEXT_AUTHOR", $this->lng->txt("author"));
239  $this->tpl->setVariable("TEXT_COMMENT", $this->lng->txt("description"));
240  $this->tpl->setVariable("TEXT_QUESTION", $this->lng->txt("question"));
241  $this->tpl->setVariable("MULTIPLE_CHOICE_ID", $this->object->getId());
242  $this->tpl->setVariable("VALUE_MULTIPLE_CHOICE_TITLE", ilUtil::prepareFormOutput($this->object->getTitle()));
243  $this->tpl->setVariable("VALUE_MULTIPLE_CHOICE_COMMENT", ilUtil::prepareFormOutput($this->object->getComment()));
244  $this->tpl->setVariable("VALUE_MULTIPLE_CHOICE_AUTHOR", ilUtil::prepareFormOutput($this->object->getAuthor()));
245  $this->tpl->setVariable("TEXT_GRAPHICAL_ANSWERS", $this->lng->txt("graphical_answers"));
246  $this->tpl->setVariable("TEXT_HIDE_GRAPHICAL_ANSWER_SUPPORT", $this->lng->txt("graphical_answers_hide"));
247  $this->tpl->setVariable("TEXT_SHOW_GRAPHICAL_ANSWER_SUPPORT", $this->lng->txt("graphical_answers_show"));
248  if ($this->object->getGraphicalAnswerSetting() == 1)
249  {
250  $this->tpl->setVariable("SELECTED_SHOW_GRAPHICAL_ANSWER_SUPPORT", " selected=\"selected\"");
251  }
252  if ($multiline_answers)
253  {
254  $this->tpl->setVariable("SELECTED_SHOW_MULTILINE_ANSWERS", " selected=\"selected\"");
255  }
256  $this->tpl->setVariable("TEXT_HIDE_MULTILINE_ANSWERS", $this->lng->txt("multiline_answers_hide"));
257  $this->tpl->setVariable("TEXT_SHOW_MULTILINE_ANSWERS", $this->lng->txt("multiline_answers_show"));
258  $this->tpl->setVariable("SET_EDIT_MODE", $this->lng->txt("set_edit_mode"));
259  $questiontext = $this->object->getQuestion();
260  $this->tpl->setVariable("VALUE_QUESTION", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($questiontext)));
261  $this->tpl->setVariable("VALUE_ADD_ANSWER", $this->lng->txt("add"));
262  $this->tpl->setVariable("TEXT_SHUFFLE_ANSWERS", $this->lng->txt("shuffle_answers"));
263  $this->tpl->setVariable("TXT_YES", $this->lng->txt("yes"));
264  $this->tpl->setVariable("TXT_NO", $this->lng->txt("no"));
265  if ($this->object->getShuffle())
266  {
267  $this->tpl->setVariable("SELECTED_YES", " selected=\"selected\"");
268  }
269  else
270  {
271  $this->tpl->setVariable("SELECTED_NO", " selected=\"selected\"");
272  }
273  $this->tpl->setVariable("TEXT_SOLUTION_HINT", $this->lng->txt("solution_hint"));
274  if (count($this->object->suggested_solutions))
275  {
276  $solution_array = $this->object->getSuggestedSolution(0);
277  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
278  $href = assQuestion::_getInternalLinkHref($solution_array["internal_link"]);
279  $this->tpl->setVariable("TEXT_VALUE_SOLUTION_HINT", " <a href=\"$href\" target=\"content\">" . $this->lng->txt("solution_hint"). "</a> ");
280  $this->tpl->setVariable("BUTTON_REMOVE_SOLUTION", $this->lng->txt("remove"));
281  $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("change"));
282  $this->tpl->setVariable("VALUE_SOLUTION_HINT", $solution_array["internal_link"]);
283  }
284  else
285  {
286  $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("add"));
287  }
288  $this->tpl->setVariable("SAVE",$this->lng->txt("save"));
289  $this->tpl->setVariable("SAVE_EDIT", $this->lng->txt("save_edit"));
290  $this->tpl->setVariable("TXT_REQUIRED_FLD", $this->lng->txt("required_field"));
291  $this->tpl->setVariable("CANCEL", $this->lng->txt("cancel"));
292  $this->ctrl->setParameter($this, "sel_question_types", "assMultipleChoice");
293  $this->tpl->setVariable("ACTION_MULTIPLE_CHOICE_TEST", $this->ctrl->getFormAction($this));
294  $this->tpl->setVariable("TEXT_QUESTION_TYPE", $this->outQuestionType());
295  $this->tpl->parseCurrentBlock();
296 
297  include_once "./Services/RTE/classes/class.ilRTE.php";
298  $rtestring = ilRTE::_getRTEClassname();
299  include_once "./Services/RTE/classes/class.$rtestring.php";
300  $rte = new $rtestring();
301  $rte->addPlugin("latex");
302  $rte->addButton("latex"); $rte->addButton("pastelatex");
303  include_once "./classes/class.ilObject.php";
304  $obj_id = $_GET["q_id"];
305  $obj_type = ilObject::_lookupType($_GET["ref_id"], TRUE);
306  $rte->addRTESupport($obj_id, $obj_type, "assessment");
307  $this->tpl->setCurrentBlock("adm_content");
308  //$this->tpl->setVariable("BODY_ATTRIBUTES", " onload=\"initialSelect();\"");
309  $this->tpl->parseCurrentBlock();
310  }
311 
315  function add()
316  {
317  //$this->setObjectData();
318  $this->writePostData();
319 
320  if (!$this->checkInput())
321  {
322  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields_add_answer"));
323  }
324  else
325  {
326  // add an answer template
327  $nrOfAnswers = $_POST["nrOfAnswers"];
328  switch ($nrOfAnswers)
329  {
330  case "tf":
331  // add a true/false answer template
332  $this->object->addAnswer(
333  $this->lng->txt("true"),
334  0,
335  0,
336  count($this->object->answers),
337  ""
338  );
339  $this->object->addAnswer(
340  $this->lng->txt("false"),
341  0,
342  0,
343  count($this->object->answers),
344  ""
345  );
346  break;
347  case "yn":
348  // add a yes/no answer template
349  $this->object->addAnswer(
350  $this->lng->txt("yes"),
351  0,
352  0,
353  count($this->object->answers),
354  ""
355  );
356  $this->object->addAnswer(
357  $this->lng->txt("no"),
358  0,
359  0,
360  count($this->object->answers),
361  ""
362  );
363  break;
364  default:
365  for ($i = 0; $i < $nrOfAnswers; $i++)
366  {
367  $this->object->addAnswer(
368  $this->lng->txt(""),
369  0,
370  0,
371  count($this->object->answers),
372  ""
373  );
374  }
375  break;
376  }
377  }
378 
379  $this->editQuestion();
380  }
381 
385  function deleteAnswer()
386  {
387  $this->writePostData();
388  $answers = $_POST["chb_answers"];
389  if (is_array($answers))
390  {
391  arsort($answers);
392  foreach ($answers as $answer)
393  {
394  $this->object->deleteAnswer($answer);
395  }
396  }
397  $this->editQuestion();
398  }
399 
403  function checkInput()
404  {
405  $cmd = $this->ctrl->getCmd();
406 
407  if ((!$_POST["title"]) or (!$_POST["author"]) or (!$_POST["question"]))
408  {
409 //echo "<br>checkInput1:FALSE";
410  return false;
411  }
412  foreach ($_POST as $key => $value)
413  {
414  if (preg_match("/answer_(\d+)/", $key, $matches))
415  {
416  if (strlen($value) == 0)
417  {
418  if (strlen($_POST["uploaded_image_".$matches[1]]) == 0)
419  {
420  return false;
421  }
422  }
423  }
424  }
425 
426  return true;
427  }
428 
437  function writePostData()
438  {
439 //echo "here!"; exit;
440 //echo "<br>assMultipleChoiceGUI->writePostData()";
441  $result = 0;
442  if ((!$_POST["title"]) or (!$_POST["author"]) or (!$_POST["question"]))
443  {
444  $result = 1;
445  }
446 
447  if (($result) and (($_POST["cmd"]["add"]) or ($_POST["cmd"]["add_tf"]) or ($_POST["cmd"]["add_yn"])))
448  {
449  // You cannot add answers before you enter the required data
450  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields_add_answer"));
451  $_POST["cmd"]["add"] = "";
452  $_POST["cmd"]["add_yn"] = "";
453  $_POST["cmd"]["add_tf"] = "";
454  }
455 
456  // Check the creation of new answer text fields
457  if ($_POST["cmd"]["add"] or $_POST["cmd"]["add_yn"] or $_POST["cmd"]["add_tf"])
458  {
459  foreach ($_POST as $key => $value)
460  {
461  if (preg_match("/answer_(\d+)/", $key, $matches))
462  {
463  if (!$value)
464  {
465  $_POST["cmd"]["add"] = "";
466  $_POST["cmd"]["add_yn"] = "";
467  $_POST["cmd"]["add_tf"] = "";
468  ilUtil::sendInfo($this->lng->txt("fill_out_all_answer_fields"));
469  }
470  }
471  }
472  }
473 
474  $this->object->setTitle(ilUtil::stripSlashes($_POST["title"]));
475  $this->object->setAuthor(ilUtil::stripSlashes($_POST["author"]));
476  $this->object->setComment(ilUtil::stripSlashes($_POST["comment"]));
477  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
478  $questiontext = ilUtil::stripSlashes($_POST["question"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
479  $this->object->setQuestion($questiontext);
480  $this->object->setSuggestedSolution($_POST["solution_hint"], 0);
481  $this->object->setShuffle($_POST["shuffle"]);
482 
483  $saved = $this->writeOtherPostData($result);
484  $this->object->setMultilineAnswerSetting($_POST["multilineAnswers"]);
485  $this->object->setGraphicalAnswerSetting($_POST["graphicalAnswerSupport"]);
486 
487  // Delete all existing answers and create new answers from the form data
488  $this->object->flushAnswers();
489  $graphical_answer_setting = $this->object->getGraphicalAnswerSetting();
490  // Add all answers from the form into the object
491  foreach ($_POST as $key => $value)
492  {
493  if (preg_match("/answer_(\d+)/", $key, $matches))
494  {
495  $answer_image = $_POST["uploaded_image_".$matches[1]];
496  if ($graphical_answer_setting == 1)
497  {
498  foreach ($_FILES as $key2 => $value2)
499  {
500  if (preg_match("/image_(\d+)/", $key2, $matches2))
501  {
502  if ($matches[1] == $matches2[1])
503  {
504  if ($value2["tmp_name"])
505  {
506  // upload the image
507  if ($this->object->getId() <= 0)
508  {
509  $this->object->saveToDb();
510  $saved = true;
511  $this->error .= $this->lng->txt("question_saved_for_upload") . "<br />";
512  }
513  $value2['name'] = $this->object->createNewImageFileName($value2['name']);
514  $upload_result = $this->object->setImageFile($value2['name'], $value2['tmp_name']);
515  switch ($upload_result)
516  {
517  case 0:
518  $_POST["image_".$matches2[1]] = $value2['name'];
519  $answer_image = $value2['name'];
520  break;
521  case 1:
522  $this->error .= $this->lng->txt("error_image_upload_wrong_format") . "<br />";
523  break;
524  case 2:
525  $this->error .= $this->lng->txt("error_image_upload_copy_file") . "<br />";
526  break;
527  }
528  }
529  }
530  }
531  }
532  }
533  $points = $_POST["points_checked_$matches[1]"];
534  $points_unchecked = $_POST["points_unchecked_$matches[1]"];
535  $answertext = ilUtil::stripSlashes($_POST["$key"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
536  $this->object->addAnswer(
537  $answertext,
538  ilUtil::stripSlashes($points),
539  ilUtil::stripSlashes($points_unchecked),
540  ilUtil::stripSlashes($matches[1]),
541  $answer_image
542  );
543  }
544  }
545 
546  if ($this->object->getMaximumPoints() < 0)
547  {
548  $result = 1;
549  $this->setErrorMessage($this->lng->txt("enter_enough_positive_points"));
550  }
551 
552  // Set the question id from a hidden form parameter
553  if ($_POST["multiple_choice_id"] > 0)
554  {
555  $this->object->setId($_POST["multiple_choice_id"]);
556  }
557 
558  if ($saved)
559  {
560  // If the question was saved automatically before an upload, we have to make
561  // sure, that the state after the upload is saved. Otherwise the user could be
562  // irritated, if he presses cancel, because he only has the question state before
563  // the upload process.
564  $this->object->saveToDb();
565  $this->ctrl->setParameter($this, "q_id", $this->object->getId());
566  }
567 
568  return $result;
569  }
570 
571  function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
572  {
573  $test_output = $this->getTestOutput($active_id, $pass, $is_postponed, $use_post_solutions, $show_feedback);
574  $this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
575  $this->tpl->setVariable("FORMACTION", $formaction);
576  }
577 
578  function getSolutionOutput($active_id, $pass = NULL, $graphicalOutput = FALSE, $result_output = FALSE, $show_question_only = TRUE, $show_feedback = FALSE, $show_correct_solution = FALSE)
579  {
580  // shuffle output
581  $keys = $this->getChoiceKeys();
582 
583  // get the solution of the user for the active pass or from the last pass if allowed
584  $user_solution = array();
585  if (($active_id > 0) && (!$show_correct_solution))
586  {
587  $solutions =& $this->object->getSolutionValues($active_id, $pass);
588  foreach ($solutions as $idx => $solution_value)
589  {
590  array_push($user_solution, $solution_value["value1"]);
591  }
592  }
593  else
594  {
595  // take the correct solution instead of the user solution
596  foreach ($this->object->answers as $index => $answer)
597  {
598  $points_checked = $answer->getPointsChecked();
599  $points_unchecked = $answer->getPointsUnchecked();
600  if ($points_checked > $points_unchecked)
601  {
602  if ($points_checked > 0)
603  {
604  array_push($user_solution, $index);
605  }
606  }
607  }
608  }
609 
610  // generate the question output
611  include_once "./classes/class.ilTemplate.php";
612  $template = new ilTemplate("tpl.il_as_qpl_mc_mr_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
613  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html",TRUE, TRUE, "Modules/TestQuestionPool");
614  foreach ($keys as $answer_id)
615  {
616  $answer = $this->object->answers[$answer_id];
617  if (($active_id > 0) && (!$show_correct_solution))
618  {
619  if ($graphicalOutput)
620  {
621  // output of ok/not ok icons for user entered solutions
622  $ok = FALSE;
623  $checked = FALSE;
624  foreach ($user_solution as $mc_solution)
625  {
626  if (strcmp($mc_solution, $answer_id) == 0)
627  {
628  $checked = TRUE;
629  }
630  }
631  if ($checked)
632  {
633  if ($answer->getPointsChecked() > $answer->getPointsUnchecked())
634  {
635  $ok = TRUE;
636  }
637  else
638  {
639  $ok = FALSE;
640  }
641  }
642  else
643  {
644  if ($answer->getPointsChecked() > $answer->getPointsUnchecked())
645  {
646  $ok = FALSE;
647  }
648  else
649  {
650  $ok = TRUE;
651  }
652  }
653  if ($ok)
654  {
655  $template->setCurrentBlock("icon_ok");
656  $template->setVariable("ICON_OK", ilUtil::getImagePath("icon_ok.gif"));
657  $template->setVariable("TEXT_OK", $this->lng->txt("answer_is_right"));
658  $template->parseCurrentBlock();
659  }
660  else
661  {
662  $template->setCurrentBlock("icon_ok");
663  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_not_ok.gif"));
664  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_wrong"));
665  $template->parseCurrentBlock();
666  }
667  }
668  }
669  if (strlen($answer->getImage()))
670  {
671  $template->setCurrentBlock("answer_image");
672  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
673  list($width, $height, $type, $attr) = getimagesize($this->object->getImagePath() . $answer->getImage());
674  $alt = $answer->getImage();
675  if (strlen($answer->getAnswertext()))
676  {
677  $alt = $answer->getAnswertext();
678  }
679  $alt = preg_replace("/<[^>]*?>/", "", $alt);
680  $template->setVariable("ATTR", $attr);
681  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
682  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
683  $template->parseCurrentBlock();
684  }
685  if ($show_feedback)
686  {
687  foreach ($user_solution as $mc_solution)
688  {
689  if (strcmp($mc_solution, $answer_id) == 0)
690  {
691  $fb = $this->object->getFeedbackSingleAnswer($answer_id);
692  if (strlen($fb))
693  {
694  $template->setCurrentBlock("feedback");
695  $template->setVariable("FEEDBACK", $fb);
696  $template->parseCurrentBlock();
697  }
698  }
699  }
700  }
701  $template->setCurrentBlock("answer_row");
702  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
703  $checked = FALSE;
704  if ($result_output)
705  {
706  $pointschecked = $this->object->answers[$answer_id]->getPointsChecked();
707  $pointsunchecked = $this->object->answers[$answer_id]->getPointsUnchecked();
708  $resulttextchecked = ($pointschecked == 1) || ($pointschecked == -1) ? "%s " . $this->lng->txt("point") : "%s " . $this->lng->txt("points");
709  $resulttextunchecked = ($pointsunchecked == 1) || ($pointsunchecked == -1) ? "%s " . $this->lng->txt("point") : "%s " . $this->lng->txt("points");
710  $template->setVariable("RESULT_OUTPUT", sprintf("(" . $this->lng->txt("checkbox_checked") . " = $resulttextchecked, " . $this->lng->txt("checkbox_unchecked") . " = $resulttextunchecked)", $pointschecked, $pointsunchecked));
711  }
712  foreach ($user_solution as $mc_solution)
713  {
714  if (strcmp($mc_solution, $answer_id) == 0)
715  {
716  $template->setVariable("SOLUTION_IMAGE", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_checked.gif")));
717  $template->setVariable("SOLUTION_ALT", $this->lng->txt("checked"));
718  $checked = TRUE;
719  }
720  }
721  if (!$checked)
722  {
723  $template->setVariable("SOLUTION_IMAGE", ilUtil::getHtmlPath(ilUtil::getImagePath("checkbox_unchecked.gif")));
724  $template->setVariable("SOLUTION_ALT", $this->lng->txt("unchecked"));
725  }
726  $template->parseCurrentBlock();
727  }
728  $questiontext = $this->object->getQuestion();
729  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
730  $questionoutput = $template->get();
731  $feedback = ($show_feedback) ? $this->getAnswerFeedbackOutput($active_id, $pass) : "";
732  if (strlen($feedback)) $solutiontemplate->setVariable("FEEDBACK", $feedback);
733  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
734 
735  $solutionoutput = $solutiontemplate->get();
736  if (!$show_question_only)
737  {
738  // get page object output
739  $solutionoutput = $this->getILIASPage($solutionoutput);
740  }
741  return $solutionoutput;
742  }
743 
744  function getPreview($show_question_only = FALSE)
745  {
746  // shuffle output
747  $keys = $this->getChoiceKeys();
748 
749  // generate the question output
750  include_once "./classes/class.ilTemplate.php";
751  $template = new ilTemplate("tpl.il_as_qpl_mc_mr_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
752  foreach ($keys as $answer_id)
753  {
754  $answer = $this->object->answers[$answer_id];
755  if (strlen($answer->getImage()))
756  {
757  $template->setCurrentBlock("answer_image");
758  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
759  $alt = $answer->getImage();
760  if (strlen($answer->getAnswertext()))
761  {
762  $alt = $answer->getAnswertext();
763  }
764  $alt = preg_replace("/<[^>]*?>/", "", $alt);
765  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
766  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
767  $template->parseCurrentBlock();
768  }
769  $template->setCurrentBlock("answer_row");
770  $template->setVariable("ANSWER_ID", $answer_id);
771  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
772  $template->parseCurrentBlock();
773  }
774  $questiontext = $this->object->getQuestion();
775  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
776  $questionoutput = $template->get();
777  if (!$show_question_only)
778  {
779  // get page object output
780  $questionoutput = $this->getILIASPage($questionoutput);
781  }
782  return $questionoutput;
783  }
784 
785  function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
786  {
787  // shuffle output
788  $keys = $this->getChoiceKeys();
789 
790  // get the solution of the user for the active pass or from the last pass if allowed
791  $user_solution = array();
792  if ($active_id)
793  {
794  $solutions = NULL;
795  include_once "./Modules/Test/classes/class.ilObjTest.php";
796  if (!ilObjTest::_getUsePreviousAnswers($active_id, true))
797  {
798  if (is_null($pass)) $pass = ilObjTest::_getPass($active_id);
799  }
800  $solutions =& $this->object->getSolutionValues($active_id, $pass);
801  foreach ($solutions as $idx => $solution_value)
802  {
803  array_push($user_solution, $solution_value["value1"]);
804  }
805  }
806  // generate the question output
807  include_once "./classes/class.ilTemplate.php";
808  $template = new ilTemplate("tpl.il_as_qpl_mc_mr_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
809  foreach ($keys as $answer_id)
810  {
811  $answer = $this->object->answers[$answer_id];
812  if (strlen($answer->getImage()))
813  {
814  $template->setCurrentBlock("answer_image");
815  $template->setVariable("ANSWER_IMAGE_URL", $this->object->getImagePathWeb() . $answer->getImage());
816  $alt = $answer->getImage();
817  if (strlen($answer->getAnswertext()))
818  {
819  $alt = $answer->getAnswertext();
820  }
821  $alt = preg_replace("/<[^>]*?>/", "", $alt);
822  $template->setVariable("ANSWER_IMAGE_ALT", ilUtil::prepareFormOutput($alt));
823  $template->setVariable("ANSWER_IMAGE_TITLE", ilUtil::prepareFormOutput($alt));
824  $template->parseCurrentBlock();
825  }
826 
827  foreach ($user_solution as $mc_solution)
828  {
829  if (strcmp($mc_solution, $answer_id) == 0)
830  {
831  if ($show_feedback)
832  {
833  $feedback = $this->object->getFeedbackSingleAnswer($answer_id);
834  if (strlen($feedback))
835  {
836  $template->setCurrentBlock("feedback");
837  $template->setVariable("FEEDBACK", $feedback);
838  $template->parseCurrentBlock();
839  }
840  }
841  }
842  }
843 
844  $template->setCurrentBlock("answer_row");
845  $template->setVariable("ANSWER_ID", $answer_id);
846  $template->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
847  foreach ($user_solution as $mc_solution)
848  {
849  if (strcmp($mc_solution, $answer_id) == 0)
850  {
851  $template->setVariable("CHECKED_ANSWER", " checked=\"checked\"");
852  }
853  }
854  $template->parseCurrentBlock();
855  }
856  $questiontext = $this->object->getQuestion();
857  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
858  $questionoutput = $template->get();
859  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
860  return $pageoutput;
861  }
862 
864  {
865  $_SESSION["subquestion_index"] = 0;
866  if ($_POST["cmd"]["addSuggestedSolution"])
867  {
868  if ($this->writePostData())
869  {
871  $this->editQuestion();
872  return;
873  }
874  if (!$this->checkInput())
875  {
876  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields_add_answer"));
877  $this->editQuestion();
878  return;
879  }
880  }
881  $this->object->saveToDb();
882  $this->ctrl->setParameter($this, "q_id", $this->object->getId());
883  $this->tpl->setVariable("HEADER", $this->object->getTitle());
884  $this->getQuestionTemplate();
886  }
887 
891  function upload()
892  {
893  $this->writePostData();
894  $this->editQuestion();
895  }
896 
897  function deleteImage()
898  {
899  if ($this->writePostData())
900  {
902  $this->editQuestion();
903  return;
904  }
905  $imageorder = "";
906  foreach ($_POST["cmd"] as $key => $value)
907  {
908  if (preg_match("/deleteImage_(\d+)/", $key, $matches))
909  {
910  $imageorder = $matches[1];
911  }
912  }
913  for ($i = 0; $i < $this->object->getAnswerCount(); $i++)
914  {
915  $answer = $this->object->getAnswer($i);
916  if ($answer->getOrder() == $imageorder)
917  {
918  $this->object->deleteImage($answer->getImage());
919  $this->object->answers[$i]->setImage("");
920  }
921  }
922  $this->editQuestion();
923  }
924 
925  function editMode()
926  {
927  global $ilUser;
928 
929  $this->object->setMultilineAnswerSetting($_POST["multilineAnswers"]);
930  $this->object->setGraphicalAnswerSetting($_POST["graphicalAnswerSupport"]);
931  $this->writePostData();
932  $this->editQuestion();
933  }
934 
942  function saveFeedback()
943  {
944  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
945  $this->object->saveFeedbackGeneric(0, ilUtil::stripSlashes($_POST["feedback_incomplete"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment")));
946  $this->object->saveFeedbackGeneric(1, ilUtil::stripSlashes($_POST["feedback_complete"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment")));
947  foreach ($this->object->answers as $index => $answer)
948  {
949  $this->object->saveFeedbackSingleAnswer($index, ilUtil::stripSlashes($_POST["feedback_answer_$index"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment")));
950  }
951  $this->object->cleanupMediaObjectUsage();
953  }
954 
962  function feedback()
963  {
964  $this->tpl->addBlockFile("ADM_CONTENT", "feedback", "tpl.il_as_qpl_mc_mr_feedback.html", "Modules/TestQuestionPool");
965  foreach ($this->object->answers as $index => $answer)
966  {
967  $this->tpl->setCurrentBlock("feedback_answer");
968  $this->tpl->setVariable("FEEDBACK_TEXT_ANSWER", $this->lng->txt("feedback"));
969  $this->tpl->setVariable("ANSWER_TEXT", $this->object->prepareTextareaOutput($answer->getAnswertext(), TRUE));
970  $this->tpl->setVariable("ANSWER_ID", $index);
971  $this->tpl->setVariable("VALUE_FEEDBACK_ANSWER", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($this->object->getFeedbackSingleAnswer($index)), FALSE));
972  $this->tpl->parseCurrentBlock();
973  }
974  $this->tpl->setVariable("FEEDBACK_TEXT", $this->lng->txt("feedback"));
975  $this->tpl->setVariable("FEEDBACK_COMPLETE", $this->lng->txt("feedback_complete_solution"));
976  $this->tpl->setVariable("VALUE_FEEDBACK_COMPLETE", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($this->object->getFeedbackGeneric(1)), FALSE));
977  $this->tpl->setVariable("FEEDBACK_INCOMPLETE", $this->lng->txt("feedback_incomplete_solution"));
978  $this->tpl->setVariable("VALUE_FEEDBACK_INCOMPLETE", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($this->object->getFeedbackGeneric(0)), FALSE));
979  $this->tpl->setVariable("FEEDBACK_ANSWERS", $this->lng->txt("feedback_answers"));
980  global $rbacsystem;
981  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
982  {
983  $this->tpl->setVariable("SAVE", $this->lng->txt("save"));
984  }
985  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
986 
987  include_once "./Services/RTE/classes/class.ilRTE.php";
988  $rtestring = ilRTE::_getRTEClassname();
989  include_once "./Services/RTE/classes/class.$rtestring.php";
990  $rte = new $rtestring();
991  $rte->addPlugin("latex");
992  $rte->addButton("latex"); $rte->addButton("pastelatex");
993  include_once "./classes/class.ilObject.php";
994  $obj_id = $_GET["q_id"];
995  $obj_type = ilObject::_lookupType($_GET["ref_id"], TRUE);
996  $rte->addRTESupport($obj_id, $obj_type, "assessment");
997  }
998 
1006  function setQuestionTabs()
1007  {
1008  global $rbacsystem, $ilTabs;
1009 
1010  $this->ctrl->setParameterByClass("ilpageobjectgui", "q_id", $_GET["q_id"]);
1011  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
1012  $q_type = $this->object->getQuestionType();
1013 
1014  if (strlen($q_type))
1015  {
1016  $classname = $q_type . "GUI";
1017  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
1018  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
1019  }
1020 
1021  if ($_GET["q_id"])
1022  {
1023  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
1024  {
1025  // edit page
1026  $ilTabs->addTarget("edit_content",
1027  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "edit"),
1028  array("edit", "insert", "exec_pg"),
1029  "", "", $force_active);
1030  }
1031 
1032  // edit page
1033  $ilTabs->addTarget("preview",
1034  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "preview"),
1035  array("preview"),
1036  "ilPageObjectGUI", "", $force_active);
1037  }
1038  $force_active = false;
1039  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
1040  {
1041  $url = "";
1042  if ($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
1043  $force_active = false;
1044  $commands = $_POST["cmd"];
1045  if (is_array($commands))
1046  {
1047  foreach ($commands as $key => $value)
1048  {
1049  if (preg_match("/^deleteImage_.*/", $key, $matches) ||
1050  preg_match("/^upload_.*/", $key, $matches)
1051  )
1052  {
1053  $force_active = true;
1054  }
1055  }
1056  }
1057  // edit question properties
1058  $ilTabs->addTarget("edit_properties",
1059  $url,
1060  array("editQuestion", "save", "cancel", "addSuggestedSolution",
1061  "cancelExplorer", "linkChilds", "removeSuggestedSolution",
1062  "toggleGraphicalAnswers", "setMediaMode", "uploadingImage", "add", "editMode", "deleteAnswer",
1063  "saveEdit"),
1064  $classname, "", $force_active);
1065  }
1066 
1067  if ($_GET["q_id"])
1068  {
1069  $ilTabs->addTarget("feedback",
1070  $this->ctrl->getLinkTargetByClass($classname, "feedback"),
1071  array("feedback", "saveFeedback"),
1072  $classname, "");
1073  }
1074 
1075  // Assessment of questions sub menu entry
1076  if ($_GET["q_id"])
1077  {
1078  $ilTabs->addTarget("statistics",
1079  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
1080  array("assessment"),
1081  $classname, "");
1082  }
1083 
1084  if (($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
1085  {
1086  $ref_id = $_GET["calling_test"];
1087  if (strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
1088  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
1089  }
1090  else
1091  {
1092  $ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
1093  }
1094  }
1095 
1096  /*
1097  * Create the key index numbers for the array of choices
1098  *
1099  * @return array
1100  */
1101  function getChoiceKeys()
1102  {
1103  if (strcmp($_GET["activecommand"], "directfeedback") == 0)
1104  {
1105  if (is_array($_SESSION["choicekeys"])) $this->choiceKeys = $_SESSION["choicekeys"];
1106  }
1107  if (!is_array($this->choiceKeys))
1108  {
1109  $this->choiceKeys = array_keys($this->object->answers);
1110  if ($this->object->getShuffle())
1111  {
1112  $this->choiceKeys = $this->object->pcArrayShuffle($this->choiceKeys);
1113  }
1114  }
1115  $_SESSION["choicekeys"] = $this->choiceKeys;
1116  return $this->choiceKeys;
1117  }
1118 }
1119 ?>