ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMatchingQuestionGUI.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 {
48  $id = -1
49  )
50  {
51  $this->assQuestionGUI();
52  include_once "./Modules/TestQuestionPool/classes/class.assMatchingQuestion.php";
53  $this->object = new assMatchingQuestion();
54  if ($id >= 0)
55  {
56  $this->object->loadFromDb($id);
57  }
58  }
59 
60  function getCommand($cmd)
61  {
62  if (substr($cmd, 0, 6) == "upload")
63  {
64  $cmd = "upload";
65  }
66 
67  return $cmd;
68  }
69 
77  function editQuestion($has_error = 0, $delete = false)
78  {
79  $multiline_answers = $this->object->getMultilineAnswerSetting();
80  $this->getQuestionTemplate();
81  $this->tpl->addBlockFile("QUESTION_DATA", "question_data", "tpl.il_as_qpl_matching.html", "Modules/TestQuestionPool");
82 
83  $tblrow = array("tblrow1top", "tblrow2top");
84 
85  for ($termcount = 1; $termcount <= 10; $termcount++)
86  {
87  $this->tpl->setCurrentBlock("termcountvalues");
88  $this->tpl->setVariable("TERMCOUNTVALUE", $termcount);
89  $this->tpl->parseCurrentBlock();
90  }
91 
92  if (($this->ctrl->getCmd() == "addTerm") && (!$has_error))
93  {
94  for ($i = 0; $i < $_POST["termcount"]; $i++)
95  {
96  $this->object->addTerm("");
97  }
98  }
99 
100  if ($this->object->getTermCount())
101  {
102  $this->tpl->setCurrentBlock("termfooter");
103  $this->tpl->setVariable("ARROW", "<img src=\"" . ilUtil::getImagePath("arrow_downright.gif") . "\" alt=\"".$this->lng->txt("arrow_downright")."\"/>");
104  $this->tpl->setVariable("DELETE", $this->lng->txt("delete"));
105  $this->tpl->setVariable("SELECT_ALL", $this->lng->txt("select_all"));
106  $this->tpl->parseCurrentBlock();
107 
108  $this->tpl->setCurrentBlock("termhead");
109  $this->tpl->setVariable("TEXT_TERMS", $this->lng->txt("terms"));
110  $this->tpl->parseCurrentBlock();
111  foreach ($this->object->getTerms() as $key => $value)
112  {
113  $this->tpl->setCurrentBlock("terms");
114  $this->tpl->setVariable("TERM_VALUE", ' value="' . ilUtil::prepareFormOutput($value) . '"');
115  $this->tpl->setVariable("TERM_ID", $key);
116  $this->tpl->parseCurrentBlock();
117  }
118  }
119 
120  // Check the creation of new answer text fields
121  $allow_add_pair = 1;
122  if ($this->object->getTermCount() == 0) $allow_add_pair = 0;
123  if ($allow_add_pair)
124  {
125  foreach ($_POST as $key => $value)
126  {
127  if (preg_match("/(termoption|picture|definition)_(\d+)_(\d+)/", $key, $matches))
128  {
129  if (strlen($value) == 0)
130  {
131  $allow_add_pair = 0;
132  }
133  }
134  }
135  if (!$allow_add_pair)
136  {
137  $this->error .= $this->lng->txt("fill_out_all_matching_pairs") . "<br />";
138  }
139  }
140  if (($this->ctrl->getCmd() == "addPair") && ($this->object->getTermCount() == 0))
141  {
142  $this->error .= $this->lng->txt("no_terms_message") . "<br />";
143  }
144  if (($this->ctrl->getCmd() == "addPair") and $allow_add_pair and (!$has_error))
145  {
146  $this->object-> addMatchingPair();
147  }
148 
149  if ($this->object->getMatchingPairCount() && $this->object->getTermCount())
150  {
151  // Vorhandene Anworten ausgeben
152  for ($i = 0; $i < $this->object->getMatchingPairCount(); $i++)
153  {
154  $thispair = $this->object->get_matchingpair($i);
155  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
156  {
157  $this->tpl->setCurrentBlock("pictures");
158  $this->tpl->setVariable("ANSWER_ORDER", $i);
159  $this->tpl->setVariable("PICTURE_ID", $thispair->getPictureId());
160  $this->tpl->setVariable("COLOR_CLASS", $tblrow[$i % 2]);
161  $filename = $thispair->getPicture();
162  if ($filename)
163  {
164  $imagepath = $this->object->getImagePathWeb() . $thispair->getPicture();
165  $this->tpl->setVariable("UPLOADED_IMAGE", "<img src=\"$imagepath.thumb.jpg\" alt=\"" . $this->lng->txt("qpl_display_fullsize_image") . "\" title=\"" . $this->lng->txt("qpl_display_fullsize_image") . "\" border=\"\" />");
166  $this->tpl->setVariable("IMAGE_FILENAME", $thispair->getPicture());
167  $this->tpl->setVariable("VALUE_PICTURE", $thispair->getPicture());
168  }
169  $this->tpl->setVariable("UPLOAD", $this->lng->txt("upload"));
170  }
171  elseif ($this->object->get_matching_type() == MT_TERMS_DEFINITIONS)
172  {
173  $this->tpl->setCurrentBlock("definitions");
174  $this->tpl->setVariable("ANSWER_ORDER", $i);
175  $this->tpl->setVariable("DEFINITION_ID", $thispair->getDefinitionId());
176  $this->tpl->setVariable("VALUE_DEFINITION", ilUtil::prepareFormOutput($thispair->getDefinition()));
177  $this->tpl->setVariable("COLOR_CLASS", $tblrow[$i % 2]);
178  }
179  $this->tpl->parseCurrentBlock();
180  foreach ($this->object->getTerms() as $termkey => $termvalue)
181  {
182  $this->tpl->setCurrentBlock("termoptions");
183  $this->tpl->setVariable("TERMOPTION_VALUE", $termkey);
184  $this->tpl->setVariable("TERMOPTION_TEXT", ilUtil::prepareFormOutput($termvalue));
185  if (strcmp($termkey, $thispair->getTerm()) == 0)
186  {
187  $this->tpl->setVariable("TERMOPTION_SELECTED", ' selected="selected"');
188  }
189  $this->tpl->parseCurrentBlock();
190  }
191  $this->tpl->setCurrentBlock("answers");
192  $this->tpl->setVariable("VALUE_ANSWER_COUNTER", $i + 1);
193  $this->tpl->setVariable("ANSWER_ORDER", $i);
194  $this->tpl->setVariable("TERM_ID", $thispair->getTermId());
195  $this->tpl->setVariable("VALUE_TERM", ilUtil::prepareFormOutput($thispair->getTerm()));
196  $this->tpl->setVariable("TEXT_MATCHES", $this->lng->txt("matches"));
197  $this->tpl->setVariable("VALUE_MATCHINGPAIR_POINTS", $thispair->getPoints());
198  $this->tpl->setVariable("COLOR_CLASS", $tblrow[$i % 2]);
199  $this->tpl->parseCurrentBlock();
200  }
201 
202  if ($this->object->get_matching_type() == MT_TERMS_DEFINITIONS)
203  {
204  /*
205  $this->tpl->setCurrentBlock("multiline_answers");
206  if ($multiline_answers)
207  {
208  $this->tpl->setVariable("SELECTED_SHOW_MULTILINE_ANSWERS", " selected=\"selected\"");
209  }
210  $this->tpl->setVariable("TEXT_HIDE_MULTILINE_ANSWERS", $this->lng->txt("multiline_definitions_hide"));
211  $this->tpl->setVariable("TEXT_SHOW_MULTILINE_ANSWERS", $this->lng->txt("multiline_definitions_show"));
212  $this->tpl->parseCurrentBlock();
213  */
214  }
215 
216  $this->tpl->setCurrentBlock("answerhead");
217  $this->tpl->setVariable("TEXT_POINTS", $this->lng->txt("points"));
218  $this->tpl->setVariable("TERM", $this->lng->txt("term"));
219  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
220  {
221  $this->tpl->setVariable("PICTURE_OR_DEFINITION", $this->lng->txt("picture"));
222  }
223  else
224  {
225  $this->tpl->setVariable("PICTURE_OR_DEFINITION", $this->lng->txt("definition"));
226  }
227  $this->tpl->parseCurrentBlock();
228  $this->tpl->setCurrentBlock("selectall");
229  $this->tpl->setVariable("SELECT_ALL", $this->lng->txt("select_all"));
230  $i++;
231  $this->tpl->setVariable("COLOR_CLASS", $tblrow[$i % 2]);
232  $this->tpl->parseCurrentBlock();
233  $this->tpl->setCurrentBlock("QFooter");
234  $this->tpl->setVariable("ARROW", "<img src=\"" . ilUtil::getImagePath("arrow_downright.gif") . "\" alt=\"".$this->lng->txt("arrow_downright")."\"/>");
235  $this->tpl->setVariable("DELETE", $this->lng->txt("delete"));
236  $this->tpl->parseCurrentBlock();
237  }
238  // call to other question data i.e. estimated working time block
239  $this->outOtherQuestionData();
240 
241  $internallinks = array(
242  "lm" => $this->lng->txt("obj_lm"),
243  "st" => $this->lng->txt("obj_st"),
244  "pg" => $this->lng->txt("obj_pg"),
245  "glo" => $this->lng->txt("glossary_term")
246  );
247  foreach ($internallinks as $key => $value)
248  {
249  $this->tpl->setCurrentBlock("internallink");
250  $this->tpl->setVariable("TYPE_INTERNAL_LINK", $key);
251  $this->tpl->setVariable("TEXT_INTERNAL_LINK", $value);
252  $this->tpl->parseCurrentBlock();
253  }
254 
255  $this->tpl->setCurrentBlock("HeadContent");
256  $this->tpl->addJavascript("./Services/JavaScript/js/Basic.js");
257  $javascript = "<script type=\"text/javascript\">ilAddOnLoad(initialSelect);\n".
258  "function initialSelect() {\n%s\n}</script>";
259  if ($delete)
260  {
261  if ($this->object->getMatchingPairCount() > 0)
262  {
263  $thispair = $this->object->get_matchingpair($this->object->getMatchingPairCount()-1);
264  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_matching.term_".($this->object->getMatchingPairCount()-1)."_" . $thispair->getTerm().".focus(); document.frm_matching.term_".($this->object->getMatchingPairCount()-1)."_" . $thispair->getTerm().".scrollIntoView(\"true\");"));
265  }
266  else
267  {
268  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_matching.title.focus();"));
269  }
270  }
271  else
272  {
273  switch ($this->ctrl->getCmd())
274  {
275  case "addPair":
276  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_matching.term_".($this->object->getMatchingPairCount())."_" . $add_random_id.".focus(); document.frm_matching.term_".($this->object->getMatchingPairCount())."_" . $add_random_id.".scrollIntoView(\"true\");"));
277  break;
278  default:
279  $this->tpl->setVariable("CONTENT_BLOCK", sprintf($javascript, "document.frm_matching.title.focus();"));
280  break;
281  }
282  }
283  $this->tpl->parseCurrentBlock();
284  $this->tpl->setCurrentBlock("question_data");
285  $this->tpl->setVariable("TEXT_TITLE", $this->lng->txt("title"));
286  $this->tpl->setVariable("TEXT_AUTHOR", $this->lng->txt("author"));
287  $this->tpl->setVariable("TEXT_COMMENT", $this->lng->txt("description"));
288  $this->tpl->setVariable("TEXT_QUESTION", $this->lng->txt("question"));
289  $this->tpl->setVariable("TEXT_SHUFFLE_ANSWERS", $this->lng->txt("shuffle_answers"));
290  $this->tpl->setVariable("TXT_SHUFFLE_TERMS", $this->lng->txt("matching_shuffle_terms"));
291  if ($this->object->get_matching_type() == MT_TERMS_DEFINITIONS)
292  {
293  $this->tpl->setVariable("TXT_YES", $this->lng->txt("matching_shuffle_terms_definitions"));
294  $this->tpl->setVariable("TXT_SHUFFLE_PICTURES", $this->lng->txt("matching_shuffle_definitions"));
295  }
296  else
297  {
298  $this->tpl->setVariable("TXT_YES", $this->lng->txt("matching_shuffle_terms_pictures"));
299  $this->tpl->setVariable("TXT_SHUFFLE_PICTURES", $this->lng->txt("matching_shuffle_pictures"));
300  }
301  $this->tpl->setVariable("TXT_NO", $this->lng->txt("no"));
302  switch ($this->object->getShuffle())
303  {
304  case 1:
305  $this->tpl->setVariable("SELECTED_YES", " selected=\"selected\"");
306  break;
307  case 2:
308  $this->tpl->setVariable("SELECTED_SHUFFLE_TERMS", " selected=\"selected\"");
309  break;
310  case 3:
311  $this->tpl->setVariable("SELECTED_SHUFFLE_PICTURES", " selected=\"selected\"");
312  break;
313  default:
314  $this->tpl->setVariable("SELECTED_NO", " selected=\"selected\"");
315  break;
316  }
317  $this->tpl->setVariable("MATCHING_ID", $this->object->getId());
318  $this->tpl->setVariable("VALUE_MATCHING_TITLE", ilUtil::prepareFormOutput($this->object->getTitle()));
319  $this->tpl->setVariable("VALUE_MATCHING_COMMENT", ilUtil::prepareFormOutput($this->object->getComment()));
320  $this->tpl->setVariable("VALUE_MATCHING_AUTHOR", ilUtil::prepareFormOutput($this->object->getAuthor()));
321  $questiontext = $this->object->getQuestion();
322  $this->tpl->setVariable("VALUE_QUESTION", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($questiontext)));
323  $this->tpl->setVariable("VALUE_ADD_ANSWER", $this->lng->txt("add_matching_pair"));
324  $this->tpl->setVariable("VALUE_ADD_TERM", $this->lng->txt("add"));
325  $this->tpl->setVariable("TEXT_TERMS", $this->lng->txt("terms"));
326  $this->tpl->setVariable("TEXT_TYPE", $this->lng->txt("type"));
327  $this->tpl->setVariable("TEXT_TYPE_TERMS_PICTURES", $this->lng->txt("match_terms_and_pictures"));
328  $this->tpl->setVariable("TEXT_TYPE_TERMS_DEFINITIONS", $this->lng->txt("match_terms_and_definitions"));
329  if ($this->object->get_matching_type() == MT_TERMS_DEFINITIONS)
330  {
331  $this->tpl->setVariable("SELECTED_DEFINITIONS", " selected=\"selected\"");
332  }
333  elseif ($this->object->get_matching_type() == MT_TERMS_PICTURES)
334  {
335  $this->tpl->setVariable("SELECTED_PICTURES", " selected=\"selected\"");
336  }
337  $this->tpl->setVariable("TEXT_SOLUTION_HINT", $this->lng->txt("solution_hint"));
338  if (count($this->object->suggested_solutions))
339  {
340  $solution_array = $this->object->getSuggestedSolution(0);
341  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
342  $href = assQuestion::_getInternalLinkHref($solution_array["internal_link"]);
343  $this->tpl->setVariable("TEXT_VALUE_SOLUTION_HINT", " <a href=\"$href\" target=\"content\">" . $this->lng->txt("solution_hint"). "</a> ");
344  $this->tpl->setVariable("BUTTON_REMOVE_SOLUTION", $this->lng->txt("remove"));
345  $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("change"));
346  $this->tpl->setVariable("VALUE_SOLUTION_HINT", $solution_array["internal_link"]);
347  }
348  else
349  {
350  $this->tpl->setVariable("BUTTON_ADD_SOLUTION", $this->lng->txt("add"));
351  }
352  $this->tpl->setVariable("SAVE", $this->lng->txt("save"));
353  $this->tpl->setVariable("SET_EDIT_MODE", $this->lng->txt("set_edit_mode"));
354  $this->tpl->setVariable("SAVE_EDIT", $this->lng->txt("save_edit"));
355  $this->tpl->setVariable("CANCEL", $this->lng->txt("cancel"));
356  $this->tpl->setVariable("TXT_REQUIRED_FLD", $this->lng->txt("required_field"));
357  $this->ctrl->setParameter($this, "sel_question_types", "assMatchingQuestion");
358  $this->tpl->setVariable("TEXT_QUESTION_TYPE", $this->outQuestionType());
359  $this->tpl->setVariable("ACTION_MATCHING_QUESTION", $this->ctrl->getFormAction($this));
360 
361  $this->tpl->parseCurrentBlock();
362  if ($this->error)
363  {
364  ilUtil::sendInfo($this->error);
365  }
366  include_once "./Services/RTE/classes/class.ilRTE.php";
367  $rtestring = ilRTE::_getRTEClassname();
368  include_once "./Services/RTE/classes/class.$rtestring.php";
369  $rte = new $rtestring();
370  $rte->addPlugin("latex");
371  $rte->addButton("latex"); $rte->addButton("pastelatex");
372  include_once "./classes/class.ilObject.php";
373  $obj_id = $_GET["q_id"];
374  $obj_type = ilObject::_lookupType($_GET["ref_id"], TRUE);
375  $rte->addRTESupport($obj_id, $obj_type, "assessment");
376  $this->tpl->setCurrentBlock("adm_content");
377  //$this->tpl->setVariable("BODY_ATTRIBUTES", " onload=\"initialSelect();\"");
378  $this->tpl->parseCurrentBlock();
379  }
380 
384  function addPair()
385  {
386  $result = $this->writePostData();
387  $this->editQuestion($result);
388  }
389 
393  function addTerm()
394  {
395  $result = $this->writePostData();
396  $this->editQuestion($result);
397  }
398 
402  function upload()
403  {
404  $this->writePostData();
405  $this->editQuestion();
406  }
407 
411  function deleteTerms()
412  {
413  $this->writePostData();
414  if (is_array($_POST["delete_terms"]))
415  {
416  $terms = $_POST["delete_terms"];
417  foreach ($terms as $key => $value)
418  {
419  $this->object->deleteTerm($value);
420  }
421  }
422  $this->editQuestion(0);
423  }
424 
428  function delete()
429  {
430  $this->writePostData();
431  if (is_array($_POST["chb_answer"]))
432  {
433  $deleteanswers = $_POST["chb_answer"];
434  rsort($deleteanswers);
435  foreach ($deleteanswers as $value)
436  {
437  $this->object->delete_matchingpair($value);
438  }
439  }
440  $this->editQuestion(0, true);
441  }
442 
451  function writePostData()
452  {
453  $saved = false;
454  $result = 0;
455 
456  if (!$this->checkInput())
457  {
458  $result = 1;
459  }
460 
461  if (($result) and ($_POST["cmd"]["addPair"]))
462  {
463  // You cannot add matching pairs before you enter the required data
464  $this->error .= $this->lng->txt("fill_out_all_required_fields_add_matching") . "<br />";
465  }
466 
467  if (($result) and ($_POST["cmd"]["addTerm"]))
468  {
469  // You cannot add matching pairs before you enter the required data
470  $this->error .= $this->lng->txt("empty_terms_exist") . "<br />";
471  }
472 
473  $this->object->setTitle(ilUtil::stripSlashes($_POST["title"]));
474  $this->object->setAuthor(ilUtil::stripSlashes($_POST["author"]));
475  $this->object->setComment(ilUtil::stripSlashes($_POST["comment"]));
476  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
477  $questiontext = ilUtil::stripSlashes($_POST["question"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
478  $this->object->setQuestion($questiontext);
479  $this->object->setSuggestedSolution($_POST["solution_hint"], 0);
480  $this->object->setShuffle($_POST["shuffle"]);
481  // adding estimated working time
482  $saved = $saved | $this->writeOtherPostData($result);
483  $this->object->setMatchingType($_POST["matching_type"]);
484  //$this->object->setMultilineAnswerSetting($_POST["multilineAnswers"]);
485 
486  // Delete all existing answers and create new answers from the form data
487  $this->object->flush_matchingpairs();
488  $this->object->flushTerms();
489  $saved = false;
490 
491  // Add all answers from the form into the object
492  $postvalues = $_POST;
493  foreach ($postvalues as $key => $value)
494  {
495  $matching_text = "";
496  if (preg_match("/terms_(.*)/", $key, $matches))
497  {
498  if (in_array($value, $this->object->getTerms()))
499  {
500  $result = 1;
501  $this->setErrorMessage($this->lng->txt("duplicate_term_message"));
502  }
503  $this->object->setTerm(ilUtil::stripSlashes($value), $matches[1]);
504  if (strlen($value) == 0)
505  {
506  $result = 1;
507  $this->setErrorMessage($this->lng->txt("empty_term_message"));
508  }
509  }
510  if (preg_match("/termoption_(\d+)_(\d+)/", $key, $matches))
511  {
512  // find out random id for term
513  foreach ($_POST as $key2 => $value2)
514  {
515  if (preg_match("/(definition|picture)_$matches[1]_(\d+)/", $key2, $matches2))
516  {
517  $matchingtext_id = $matches2[2];
518  if (strcmp($matches2[1], "definition") == 0)
519  {
520  $matching_text = $_POST["definition_$matches[1]_$matches2[2]"];
521  }
522  else
523  {
524  $matching_text = $_POST["picture_$matches[1]_$matches2[2]"];
525  }
526  }
527  }
528 
529  // save picture file if matching terms and pictures
530  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
531  {
532  foreach ($_FILES as $key2 => $value2)
533  {
534  if (preg_match("/picture_$matches[1]_(\d+)/", $key2, $matches2))
535  {
536  if ($value2["tmp_name"])
537  {
538  // upload the matching picture
539  if ($this->object->getId() <= 0)
540  {
541  $this->object->saveToDb();
542  $saved = true;
543  $this->error .= $this->lng->txt("question_saved_for_upload") . "<br />";
544  }
545  $value2['name'] = $this->object->createNewImageFileName($value2['name']);
546  $upload_result = $this->object->setImageFile($value2['name'], $value2['tmp_name']);
547  switch ($upload_result)
548  {
549  case 0:
550  $_POST["picture_$matches[1]_".$matches2[1]] = $value2['name'];
551  $matching_text = $value2['name'];
552  break;
553  case 1:
554  $this->error .= $this->lng->txt("error_image_upload_wrong_format") . "<br />";
555  break;
556  case 2:
557  $this->error .= $this->lng->txt("error_image_upload_copy_file") . "<br />";
558  break;
559  }
560  }
561  }
562  }
563  }
564  $points = $_POST["points_$matches[1]"];
565  if ($points < 0)
566  {
567  $result = 1;
568  $this->setErrorMessage($this->lng->txt("negative_points_not_allowed"));
569  }
570  $this->object->addMatchingPair(
571  ilUtil::stripSlashes($_POST["$key"]),
572  ilUtil::stripSlashes($matching_text),
573  ilUtil::stripSlashes($points),
574  ilUtil::stripSlashes($value),
575  ilUtil::stripSlashes($matchingtext_id)
576  );
577  }
578  }
579 
580  if ($saved)
581  {
582  // If the question was saved automatically before an upload, we have to make
583  // sure, that the state after the upload is saved. Otherwise the user could be
584  // irritated, if he presses cancel, because he only has the question state before
585  // the upload process.
586  $this->object->saveToDb();
587  $this->ctrl->setParameter($this, "q_id", $this->object->getId());
588  }
589  return $result;
590  }
591 
592  function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_postponed = FALSE, $user_post_solution = FALSE)
593  {
594  $test_output = $this->getTestOutput($active_id, $pass, $is_postponed, $user_post_solution);
595  $this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
596  $this->tpl->setVariable("FORMACTION", $formaction);
597  }
598 
599  function getSolutionOutput($active_id, $pass = NULL, $graphicalOutput = FALSE, $result_output = FALSE, $show_question_only = TRUE, $show_feedback = FALSE, $show_correct_solution = FALSE)
600  {
601  // generate the question output
602  include_once "./classes/class.ilTemplate.php";
603  $template = new ilTemplate("tpl.il_as_qpl_matching_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
604  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html",TRUE, TRUE, "Modules/TestQuestionPool");
605 
606  $keys = array_keys($this->object->matchingpairs);
607 
608  $solutions = array();
609  if (($active_id > 0) && (!$show_correct_solution))
610  {
611  include_once "./Modules/Test/classes/class.ilObjTest.php";
612  $solutions =& $this->object->getSolutionValues($active_id, $pass);
613  $solution_script .= "";
614  }
615  else
616  {
617  foreach ($this->object->matchingpairs as $pair)
618  {
619  array_push($solutions, array("value1" => $pair->getTerm(), "value2" => $pair->getDefinitionId()));
620  }
621  }
622  foreach ($keys as $idx)
623  {
624  $answer = $this->object->matchingpairs[$idx];
625  if (($active_id > 0) && (!$show_correct_solution))
626  {
627  if ($graphicalOutput)
628  {
629  // output of ok/not ok icons for user entered solutions
630  $ok = FALSE;
631  foreach ($solutions as $solution)
632  {
633  if ($answer->getDefinitionId() == $solution["value2"])
634  {
635  if ($answer->getTerm() == $solution["value1"])
636  {
637  $ok = TRUE;
638  }
639  }
640  }
641  if ($ok)
642  {
643  $template->setCurrentBlock("icon_ok");
644  $template->setVariable("ICON_OK", ilUtil::getImagePath("icon_ok.gif"));
645  $template->setVariable("TEXT_OK", $this->lng->txt("answer_is_right"));
646  $template->parseCurrentBlock();
647  }
648  else
649  {
650  $template->setCurrentBlock("icon_ok");
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  $template->parseCurrentBlock();
654  }
655  }
656  }
657 
658  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
659  {
660  $template->setCurrentBlock("standard_matching_pictures");
661  $template->setVariable("DEFINITION_ID", $answer->getPictureId());
662  $size = getimagesize($this->object->getImagePath() . $answer->getPicture() . ".thumb.jpg");
663  $template->setVariable("THUMBNAIL_WIDTH", $size[0]);
664  $template->setVariable("THUMBNAIL_HEIGHT", $size[1]);
665  $template->setVariable("THUMBNAIL_HREF", $this->object->getImagePathWeb() . $answer->getPicture() . ".thumb.jpg");
666  $template->setVariable("THUMB_ALT", $this->lng->txt("image"));
667  $template->setVariable("THUMB_TITLE", $this->lng->txt("image"));
668  $template->parseCurrentBlock();
669  }
670  else
671  {
672  $template->setCurrentBlock("standard_matching_terms");
673  $template->setVariable("DEFINITION", $this->object->prepareTextareaOutput($answer->getDefinition(), TRUE));
674  $template->parseCurrentBlock();
675  }
676 
677  $template->setCurrentBlock("standard_matching_row");
678  $template->setVariable("MATCHES", $this->lng->txt("matches"));
679  if ($result_output)
680  {
681  $points = $answer->getPoints();
682  $resulttext = ($points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
683  $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $points));
684  }
685  $hasoutput = FALSE;
686  foreach ($solutions as $solution)
687  {
688  if ($answer->getDefinitionId() == $solution["value2"])
689  {
690  foreach ($this->object->getTerms() as $termindex => $termvalue)
691  {
692  if ($termindex == $solution["value1"])
693  {
694  $template->setVariable("SOLUTION", ilUtil::prepareFormOutput($termvalue));
695  $hasoutput = TRUE;
696  }
697  }
698  }
699  }
700  if (!$hasoutput)
701  {
702  $template->setVariable("SOLUTION", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
703  }
704  $template->parseCurrentBlock();
705  }
706 
707  $questiontext = $this->object->getQuestion();
708  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
709  $questionoutput = $template->get();
710  $feedback = ($show_feedback) ? $this->getAnswerFeedbackOutput($active_id, $pass) : "";
711  if (strlen($feedback)) $solutiontemplate->setVariable("FEEDBACK", $feedback);
712  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
713 
714  $solutionoutput = $solutiontemplate->get();
715  if (!$show_question_only)
716  {
717  // get page object output
718  $solutionoutput = $this->getILIASPage($solutionoutput);
719  }
720  return $solutionoutput;
721  }
722 
723  function getPreview($show_question_only = FALSE)
724  {
725  // generate the question output
726  include_once "./classes/class.ilTemplate.php";
727  $template = new ilTemplate("tpl.il_as_qpl_matching_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
728 
729  // shuffle output
730  $keys = array_keys($this->object->matchingpairs);
731  $keys2 = array_keys($this->object->getTerms());
732  if ($this->object->getShuffle())
733  {
734  if (($this->object->getShuffle() == 3) || ($this->object->getShuffle() == 1))
735  $keys = $this->object->pcArrayShuffle(array_keys($this->object->matchingpairs));
736  if (($this->object->getShuffle() == 2) || ($this->object->getShuffle() == 1))
737  $keys2 = $this->object->pcArrayShuffle($keys2);
738  }
739 
740  $terms = $this->object->getTerms();
741  foreach ($keys as $idx)
742  {
743  $answer = $this->object->matchingpairs[$idx];
744  foreach ($keys2 as $termkey)
745  {
746  $comboanswer = $this->object->matchingpairs[$comboidx];
747  $template->setCurrentBlock("matching_selection");
748  $template->setVariable("VALUE_SELECTION", $termkey);
749  $template->setVariable("TEXT_SELECTION", ilUtil::prepareFormOutput($terms[$termkey]));
750  $template->parseCurrentBlock();
751  }
752  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
753  {
754  $template->setCurrentBlock("standard_matching_pictures");
755  $template->setVariable("DEFINITION_ID", $answer->getPictureId());
756  $template->setVariable("IMAGE_HREF", $this->object->getImagePathWeb() . $answer->getPicture());
757  $template->setVariable("THUMBNAIL_HREF", $this->object->getImagePathWeb() . $answer->getPicture() . ".thumb.jpg");
758  $template->setVariable("THUMB_ALT", $this->lng->txt("image"));
759  $template->setVariable("THUMB_TITLE", $this->lng->txt("image"));
760  $template->parseCurrentBlock();
761  }
762  else
763  {
764  $template->setCurrentBlock("standard_matching_terms");
765  $template->setVariable("DEFINITION", $this->object->prepareTextareaOutput($answer->getDefinition(), TRUE));
766  $template->parseCurrentBlock();
767  }
768 
769  $template->setCurrentBlock("standard_matching_row");
770  $template->setVariable("MATCHES", $this->lng->txt("matches"));
771  $template->setVariable("DEFINITION_ID", $answer->getDefinitionId());
772  $template->setVariable("PLEASE_SELECT", $this->lng->txt("please_select"));
773  $template->parseCurrentBlock();
774  }
775 
776  $questiontext = $this->object->getQuestion();
777  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
778  $questionoutput = $template->get();
779  if (!$show_question_only)
780  {
781  // get page object output
782  $questionoutput = $this->getILIASPage($questionoutput);
783  }
784  return $questionoutput;
785  }
786 
787  function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $user_post_solution = FALSE)
788  {
789  $allterms = $this->object->getTerms();
790  // generate the question output
791  include_once "./classes/class.ilTemplate.php";
792  $template = new ilTemplate("tpl.il_as_qpl_matching_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
793 
794  // shuffle output
795  $keys = array_keys($this->object->matchingpairs);
796  $keys2 = array_keys($this->object->getTerms());
797  if (is_array($user_post_solution))
798  {
799  $keys = $_SESSION["matching_keys"];
800  $keys2 = $_SESSION["matching_keys2"];
801  }
802  else
803  {
804  if ($this->object->getShuffle())
805  {
806  if (($this->object->getShuffle() == 3) || ($this->object->getShuffle() == 1))
807  $keys = $this->object->pcArrayShuffle(array_keys($this->object->matchingpairs));
808  if (($this->object->getShuffle() == 2) || ($this->object->getShuffle() == 1))
809  $keys2 = $this->object->pcArrayShuffle($keys2);
810  }
811  }
812  $_SESSION["matching_keys"] = $keys;
813  $_SESSION["matching_keys2"] = $keys2;
814 
815  if ($active_id)
816  {
817  $solutions = NULL;
818  include_once "./Modules/Test/classes/class.ilObjTest.php";
819  if (!ilObjTest::_getUsePreviousAnswers($active_id, true))
820  {
821  if (is_null($pass)) $pass = ilObjTest::_getPass($active_id);
822  }
823  if (is_array($user_post_solution))
824  {
825  $solutions = array();
826  foreach ($user_post_solution as $key => $value)
827  {
828  if (preg_match("/sel_matching_(\d+)/", $key, $matches))
829  {
830  array_push($solutions, array("value1" => $value, "value2" => $matches[1]));
831  }
832  }
833  }
834  else
835  {
836  $solutions =& $this->object->getSolutionValues($active_id, $pass);
837  }
838  foreach ($solutions as $idx => $solution_value)
839  {
840  if ($this->object->getOutputType() == OUTPUT_JAVASCRIPT)
841  {
842  if (($solution_value["value2"] > 1) && ($solution_value["value1"] > 1))
843  {
844  $template->setCurrentBlock("restoreposition");
845  $template->setVariable("TERM_ID", $solution_value["value1"]);
846  $template->setVariable("PICTURE_DEFINITION_ID", $solution_value["value2"]);
847  $template->parseCurrentBlock();
848  }
849  }
850  }
851  }
852  if ($this->object->getOutputType() == OUTPUT_JAVASCRIPT)
853  {
854  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
856 
857  // create pictures/definitions
858  $arrayindex = 0;
859  foreach ($keys as $idx)
860  {
861  $answer = $this->object->matchingpairs[$idx];
862  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
863  {
864  $template->setCurrentBlock("js_match_picture");
865  $template->setVariable("DEFINITION_ID", $answer->getPictureId());
866  $template->setVariable("IMAGE_HREF", $this->object->getImagePathWeb() . $answer->getPicture());
867  $template->setVariable("THUMBNAIL_HREF", $this->object->getImagePathWeb() . $answer->getPicture() . ".thumb.jpg");
868  $template->setVariable("THUMB_ALT", $this->lng->txt("image"));
869  $template->setVariable("THUMB_TITLE", $this->lng->txt("image"));
870  $template->parseCurrentBlock();
871  }
872  else
873  {
874  $template->setCurrentBlock("js_match_definition");
875  $template->setVariable("DEFINITION", $this->object->prepareTextareaOutput($answer->getDefinition(), TRUE));
876  $template->parseCurrentBlock();
877  }
878  $template->setCurrentBlock("droparea");
879  $template->setVariable("ID_DROPAREA", $answer->getDefinitionId());
880  $template->setVariable("ID_DROPAREA", $answer->getDefinitionId());
881  $template->parseCurrentBlock();
882  $template->setCurrentBlock("init_dropareas");
883  $template->setVariable("COUNTER", $arrayindex++);
884  $template->setVariable("ID_DROPAREA", $answer->getDefinitionId());
885  $template->parseCurrentBlock();
886  }
887 
888  // create terms
889  $arrayindex = 0;
890  foreach ($keys2 as $termid)
891  {
892  $template->setCurrentBlock("draggable");
893  $template->setVariable("ID_DRAGGABLE", $termid);
894  $template->setVariable("VALUE_DRAGGABLE", $this->object->prepareTextareaOutput($this->object->getTermWithId($termid)));
895  $template->parseCurrentBlock();
896  $template->setCurrentBlock("init_draggables");
897  $template->setVariable("COUNTER", $arrayindex++);
898  $template->setVariable("ID_DRAGGABLE", $termid);
899  $template->parseCurrentBlock();
900  }
901 
902  $template->setVariable("RESET_BUTTON", $this->lng->txt("reset_terms"));
903 
904  $this->tpl->setVariable("LOCATION_ADDITIONAL_STYLESHEET", ilUtil::getStyleSheetLocation("output", "test_javascript.css", "Modules/TestQuestionPool"));
905  }
906  else
907  {
908  foreach ($keys as $idx)
909  {
910  $answer = $this->object->matchingpairs[$idx];
911  foreach ($keys2 as $comboidx)
912  {
913  $template->setCurrentBlock("matching_selection");
914  $template->setVariable("VALUE_SELECTION", $comboidx);
915  $template->setVariable("TEXT_SELECTION", ilUtil::prepareFormOutput($allterms[$comboidx]));
916  foreach ($solutions as $solution)
917  {
918  if ((strcmp($solution["value1"], $comboidx) == 0) && ($answer->getDefinitionId() == $solution["value2"]))
919  {
920  $template->setVariable("SELECTED_SELECTION", " selected=\"selected\"");
921  }
922  }
923  $template->parseCurrentBlock();
924  }
925  if ($this->object->get_matching_type() == MT_TERMS_PICTURES)
926  {
927  $template->setCurrentBlock("standard_matching_pictures");
928  $template->setVariable("DEFINITION_ID", $answer->getPictureId());
929  $template->setVariable("IMAGE_HREF", $this->object->getImagePathWeb() . $answer->getPicture());
930  $template->setVariable("THUMBNAIL_HREF", $this->object->getImagePathWeb() . $answer->getPicture() . ".thumb.jpg");
931  $template->setVariable("THUMB_ALT", $this->lng->txt("image"));
932  $template->setVariable("THUMB_TITLE", $this->lng->txt("image"));
933  $template->parseCurrentBlock();
934  }
935  else
936  {
937  $template->setCurrentBlock("standard_matching_terms");
938  $template->setVariable("DEFINITION", $this->object->prepareTextareaOutput($answer->getDefinition(), TRUE));
939  $template->parseCurrentBlock();
940  }
941 
942  $template->setCurrentBlock("standard_matching_row");
943  $template->setVariable("MATCHES", $this->lng->txt("matches"));
944  $template->setVariable("DEFINITION_ID", $answer->getDefinitionId());
945  $template->setVariable("PLEASE_SELECT", $this->lng->txt("please_select"));
946  $template->parseCurrentBlock();
947  }
948  }
949 
950  $questiontext = $this->object->getQuestion();
951  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
952  $questionoutput = $template->get();
953  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
954  return $pageoutput;
955  }
956 
960  function checkInput()
961  {
962  if ((!$_POST["title"]) or (!$_POST["author"]) or (!$_POST["question"]))
963  {
964  return false;
965  }
966  return true;
967  }
968 
969 
971  {
972  $_SESSION["subquestion_index"] = 0;
973  if ($_POST["cmd"]["addSuggestedSolution"])
974  {
975  if ($this->writePostData())
976  {
978  $this->editQuestion();
979  return;
980  }
981  if (!$this->checkInput())
982  {
983  ilUtil::sendInfo($this->lng->txt("fill_out_all_required_fields_add_answer"));
984  $this->editQuestion();
985  return;
986  }
987  }
988  $this->object->saveToDb();
989  $this->ctrl->setParameter($this, "q_id", $this->object->getId());
990  $this->tpl->setVariable("HEADER", $this->object->getTitle());
991  $this->getQuestionTemplate();
993  }
994 
995  function editMode()
996  {
997  global $ilUser;
998 
999  //$this->object->setMultilineAnswerSetting($_POST["multilineAnswers"]);
1000  $this->object->setMatchingType($_POST["matching_type"]);
1001  $this->writePostData();
1002  $this->editQuestion();
1003  }
1004 
1012  function saveFeedback()
1013  {
1014  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
1015  $this->object->saveFeedbackGeneric(0, ilUtil::stripSlashes($_POST["feedback_incomplete"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment")));
1016  $this->object->saveFeedbackGeneric(1, ilUtil::stripSlashes($_POST["feedback_complete"], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment")));
1017  $this->object->cleanupMediaObjectUsage();
1019  }
1020 
1028  function feedback()
1029  {
1030  $this->tpl->addBlockFile("ADM_CONTENT", "feedback", "tpl.il_as_qpl_matching_feedback.html", "Modules/TestQuestionPool");
1031  $this->tpl->setVariable("FEEDBACK_TEXT", $this->lng->txt("feedback"));
1032  $this->tpl->setVariable("FEEDBACK_COMPLETE", $this->lng->txt("feedback_complete_solution"));
1033  $this->tpl->setVariable("VALUE_FEEDBACK_COMPLETE", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($this->object->getFeedbackGeneric(1)), FALSE));
1034  $this->tpl->setVariable("FEEDBACK_INCOMPLETE", $this->lng->txt("feedback_incomplete_solution"));
1035  $this->tpl->setVariable("VALUE_FEEDBACK_INCOMPLETE", ilUtil::prepareFormOutput($this->object->prepareTextareaOutput($this->object->getFeedbackGeneric(0)), FALSE));
1036  global $rbacsystem;
1037  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
1038  {
1039  $this->tpl->setVariable("SAVE", $this->lng->txt("save"));
1040  }
1041  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
1042 
1043  include_once "./Services/RTE/classes/class.ilRTE.php";
1044  $rtestring = ilRTE::_getRTEClassname();
1045  include_once "./Services/RTE/classes/class.$rtestring.php";
1046  $rte = new $rtestring();
1047  $rte->addPlugin("latex");
1048  $rte->addButton("latex"); $rte->addButton("pastelatex");
1049  include_once "./classes/class.ilObject.php";
1050  $obj_id = $_GET["q_id"];
1051  $obj_type = ilObject::_lookupType($_GET["ref_id"], TRUE);
1052  $rte->addRTESupport($obj_id, $obj_type, "assessment");
1053  }
1054 
1062  function setQuestionTabs()
1063  {
1064  global $rbacsystem, $ilTabs;
1065 
1066  $this->ctrl->setParameterByClass("ilpageobjectgui", "q_id", $_GET["q_id"]);
1067  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
1068  $q_type = $this->object->getQuestionType();
1069 
1070  if (strlen($q_type))
1071  {
1072  $classname = $q_type . "GUI";
1073  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
1074  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
1075  }
1076 
1077  if ($_GET["q_id"])
1078  {
1079  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
1080  {
1081  // edit page
1082  $ilTabs->addTarget("edit_content",
1083  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "edit"),
1084  array("edit", "insert", "exec_pg"),
1085  "", "", $force_active);
1086  }
1087 
1088  // edit page
1089  $ilTabs->addTarget("preview",
1090  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "preview"),
1091  array("preview"),
1092  "ilPageObjectGUI", "", $force_active);
1093  }
1094 
1095  $force_active = false;
1096  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
1097  {
1098  $url = "";
1099  if ($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
1100  // edit question properties
1101  $ilTabs->addTarget("edit_properties",
1102  $url,
1103  array("editQuestion", "save", "cancel", "addSuggestedSolution",
1104  "cancelExplorer", "linkChilds", "removeSuggestedSolution",
1105  "addPair", "addTerm", "delete", "deleteTerms", "editMode", "upload",
1106  "saveEdit"),
1107  $classname, "", $force_active);
1108  }
1109 
1110  if ($_GET["q_id"])
1111  {
1112  $ilTabs->addTarget("feedback",
1113  $this->ctrl->getLinkTargetByClass($classname, "feedback"),
1114  array("feedback", "saveFeedback"),
1115  $classname, "");
1116  }
1117 
1118  // Assessment of questions sub menu entry
1119  if ($_GET["q_id"])
1120  {
1121  $ilTabs->addTarget("statistics",
1122  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
1123  array("assessment"),
1124  $classname, "");
1125  }
1126 
1127  if (($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
1128  {
1129  $ref_id = $_GET["calling_test"];
1130  if (strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
1131  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
1132  }
1133  else
1134  {
1135  $ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
1136  }
1137  }
1138 }
1139 ?>