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