ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assTextSubsetGUI.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 {
47  function __construct($id = -1)
48  {
50  include_once "./Modules/TestQuestionPool/classes/class.assTextSubset.php";
51  $this->object = new assTextSubset();
52  if ($id >= 0)
53  {
54  $this->object->loadFromDb($id);
55  }
56  }
57 
58  function getCommand($cmd)
59  {
60  return $cmd;
61  }
62 
69  function writePostData($always = false)
70  {
71  $hasErrors = (!$always) ? $this->editQuestion(true) : false;
72  if (!$hasErrors)
73  {
74  $this->object->setTitle($_POST["title"]);
75  $this->object->setAuthor($_POST["author"]);
76  $this->object->setComment($_POST["comment"]);
77  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
78  $questiontext = $_POST["question"];
79  $this->object->setQuestion($questiontext);
80  $this->object->setCorrectAnswers($_POST["correctanswers"]);
81  $this->object->setTextRating($_POST["text_rating"]);
82  // Delete all existing answers and create new answers from the form data
83  $this->object->flushAnswers();
84  foreach ($_POST['answers']['answer'] as $index => $answer)
85  {
86  $answertext = $answer;
87  $this->object->addAnswer($answertext, $_POST['answers']['points'][$index], $index);
88  }
89  return 0;
90  }
91  else
92  {
93  return 1;
94  }
95  }
96 
102  public function editQuestion($checkonly = FALSE)
103  {
104  $save = ((strcmp($this->ctrl->getCmd(), "save") == 0) || (strcmp($this->ctrl->getCmd(), "saveEdit") == 0)) ? TRUE : FALSE;
105  $this->getQuestionTemplate();
106 
107  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
108  $form = new ilPropertyFormGUI();
109  $form->setFormAction($this->ctrl->getFormAction($this));
110  $form->setTitle($this->outQuestionType());
111  $form->setMultipart(FALSE);
112  $form->setTableWidth("100%");
113  $form->setId("asstextsubset");
114 
115  $this->addBasicQuestionFormProperties($form);
116 
117  // number of requested answers
118  $correctanswers = new ilNumberInputGUI($this->lng->txt("nr_of_correct_answers"), "correctanswers");
119  $correctanswers->setMinValue(1);
120  $correctanswers->setDecimals(0);
121  $correctanswers->setSize(3);
122  $correctanswers->setValue($this->object->getCorrectAnswers());
123  $correctanswers->setRequired(true);
124  $form->addItem($correctanswers);
125 
126  // maximum available points
127  $points = new ilNumberInputGUI($this->lng->txt("maximum_points"), "points");
128  $points->setMinValue(0.25);
129  $points->setSize(6);
130  $points->setDisabled(true);
131  $points->setValue($this->object->getMaximumPoints());
132  $points->setRequired(false);
133  $form->addItem($points);
134 
135  // text rating
136  $textrating = new ilSelectInputGUI($this->lng->txt("text_rating"), "text_rating");
137  $text_options = array(
138  "ci" => $this->lng->txt("cloze_textgap_case_insensitive"),
139  "cs" => $this->lng->txt("cloze_textgap_case_sensitive"),
140  "l1" => sprintf($this->lng->txt("cloze_textgap_levenshtein_of"), "1"),
141  "l2" => sprintf($this->lng->txt("cloze_textgap_levenshtein_of"), "2"),
142  "l3" => sprintf($this->lng->txt("cloze_textgap_levenshtein_of"), "3"),
143  "l4" => sprintf($this->lng->txt("cloze_textgap_levenshtein_of"), "4"),
144  "l5" => sprintf($this->lng->txt("cloze_textgap_levenshtein_of"), "5")
145  );
146  $textrating->setOptions($text_options);
147  $textrating->setValue($this->object->getTextRating());
148  $form->addItem($textrating);
149 
150  // Choices
151  include_once "./Modules/TestQuestionPool/classes/class.ilAnswerWizardInputGUI.php";
152  $choices = new ilAnswerWizardInputGUI($this->lng->txt("answers"), "answers");
153  $choices->setRequired(true);
154  $choices->setQuestionObject($this->object);
155  $choices->setSingleline(true);
156  $choices->setAllowMove(false);
157  if ($this->object->getAnswerCount() == 0) $this->object->addAnswer("", 0, 0);
158  $choices->setValues($this->object->getAnswers());
159  $form->addItem($choices);
160 
161  $this->addQuestionFormCommandButtons($form);
162 
163  $errors = false;
164 
165  if ($save)
166  {
167  $form->setValuesByPost();
168  $points->setValue($this->object->getMaximumPoints());
169  $errors = !$form->checkInput();
170  $form->setValuesByPost(); // again, because checkInput now performs the whole stripSlashes handling and we need this if we don't want to have duplication of backslashes
171  if ($errors) $checkonly = false;
172  }
173 
174  if (!$checkonly) $this->tpl->setVariable("QUESTION_DATA", $form->getHTML());
175  return $errors;
176  }
177 
181  public function addanswers()
182  {
183  $this->writePostData(true);
184  $position = key($_POST['cmd']['addanswers']);
185  $this->object->addAnswer("", 0, $position+1);
186  $this->editQuestion();
187  }
188 
192  public function removeanswers()
193  {
194  $this->writePostData(true);
195  $position = key($_POST['cmd']['removeanswers']);
196  $this->object->deleteAnswer($position);
197  $this->editQuestion();
198  }
199 
200  function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE)
201  {
202  $test_output = $this->getTestOutput($active_id, $pass, $is_postponed, $use_post_solutions);
203  $this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
204  $this->tpl->setVariable("FORMACTION", $formaction);
205  }
206 
221  $active_id,
222  $pass = NULL,
223  $graphicalOutput = FALSE,
224  $result_output = FALSE,
225  $show_question_only = TRUE,
226  $show_feedback = FALSE,
227  $show_correct_solution = FALSE,
228  $show_manual_scoring = FALSE
229  )
230  {
231  // get the solution of the user for the active pass or from the last pass if allowed
232  $solutions = array();
233  if (($active_id > 0) && (!$show_correct_solution))
234  {
235  $solutions =& $this->object->getSolutionValues($active_id, $pass);
236  }
237  else
238  {
239  $rank = array();
240  foreach ($this->object->answers as $answer)
241  {
242  if ($answer->getPoints() > 0)
243  {
244  if (!is_array($rank[$answer->getPoints()]))
245  {
246  $rank[$answer->getPoints()] = array();
247  }
248  array_push($rank[$answer->getPoints()], $answer->getAnswertext());
249  }
250  }
251  krsort($rank, SORT_NUMERIC);
252  foreach ($rank as $index => $bestsolutions)
253  {
254  array_push($solutions, array("value1" => join(",", $bestsolutions), "points" => $index));
255  }
256  }
257 
258  // generate the question output
259  include_once "./classes/class.ilTemplate.php";
260  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
261  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
262  $available_answers =& $this->object->getAvailableAnswers();
263  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
264  {
265  if ((!$test_id) && (strcmp($solutions[$i]["value1"], "") == 0))
266  {
267  }
268  else
269  {
270  if (($active_id > 0) && (!$show_correct_solution))
271  {
272  if ($graphicalOutput)
273  {
274  // output of ok/not ok icons for user entered solutions
275  $index = $this->object->isAnswerCorrect($available_answers, $solutions[$i]["value1"]);
276  $correct = FALSE;
277  if ($index !== FALSE)
278  {
279  unset($available_answers[$index]);
280  $correct = TRUE;
281  }
282  if ($correct)
283  {
284  $template->setCurrentBlock("icon_ok");
285  $template->setVariable("ICON_OK", ilUtil::getImagePath("icon_ok.gif"));
286  $template->setVariable("TEXT_OK", $this->lng->txt("answer_is_right"));
287  $template->parseCurrentBlock();
288  }
289  else
290  {
291  $template->setCurrentBlock("icon_ok");
292  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_not_ok.gif"));
293  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_wrong"));
294  $template->parseCurrentBlock();
295  }
296  }
297  }
298  $template->setCurrentBlock("textsubset_row");
299  $template->setVariable("SOLUTION", $solutions[$i]["value1"]);
300  $template->setVariable("COUNTER", $i+1);
301  if ($result_output)
302  {
303  $points = $solutions[$i]["points"];
304  $resulttext = ($points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
305  $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $points));
306  }
307  $template->parseCurrentBlock();
308  }
309  }
310  $questiontext = $this->object->getQuestion();
311  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
312  $questionoutput = $template->get();
313  $feedback = ($show_feedback) ? $this->getAnswerFeedbackOutput($active_id, $pass) : "";
314  if (strlen($feedback)) $solutiontemplate->setVariable("FEEDBACK", $feedback);
315  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
316 
317  $solutionoutput = $solutiontemplate->get();
318  if (!$show_question_only)
319  {
320  // get page object output
321  $solutionoutput = $this->getILIASPage($solutionoutput);
322  }
323  return $solutionoutput;
324  }
325 
326  function getPreview($show_question_only = FALSE)
327  {
328  // generate the question output
329  include_once "./classes/class.ilTemplate.php";
330  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
331  $width = $this->object->getMaxTextboxWidth();
332  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
333  {
334  $template->setCurrentBlock("textsubset_row");
335  $template->setVariable("COUNTER", $i+1);
336  $template->setVariable("TEXTFIELD_ID", sprintf("%02d", $i+1));
337  $template->setVariable("TEXTFIELD_SIZE", $width);
338  $template->parseCurrentBlock();
339  }
340  $questiontext = $this->object->getQuestion();
341  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
342  $questionoutput = $template->get();
343  if (!$show_question_only)
344  {
345  // get page object output
346  $questionoutput = $this->getILIASPage($questionoutput);
347  }
348  return $questionoutput;
349  }
350 
351  function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE)
352  {
353  // get the solution of the user for the active pass or from the last pass if allowed
354  $user_solution = "";
355  if ($active_id)
356  {
357  $solutions = NULL;
358  include_once "./Modules/Test/classes/class.ilObjTest.php";
359  if (!ilObjTest::_getUsePreviousAnswers($active_id, true))
360  {
361  if (is_null($pass)) $pass = ilObjTest::_getPass($active_id);
362  }
363  $solutions =& $this->object->getSolutionValues($active_id, $pass);
364  }
365 
366  // generate the question output
367  include_once "./classes/class.ilTemplate.php";
368  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
369  $width = $this->object->getMaxTextboxWidth();
370  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
371  {
372  $template->setCurrentBlock("textsubset_row");
373  foreach ($solutions as $idx => $solution_value)
374  {
375  if ($idx == $i)
376  {
377  $template->setVariable("TEXTFIELD_VALUE", " value=\"" . $solution_value["value1"]."\"");
378  }
379  }
380  $template->setVariable("COUNTER", $i+1);
381  $template->setVariable("TEXTFIELD_ID", sprintf("%02d", $i+1));
382  $template->setVariable("TEXTFIELD_SIZE", $width);
383  $template->parseCurrentBlock();
384  }
385  $questiontext = $this->object->getQuestion();
386  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
387  $questionoutput = $template->get();
388  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
389  return $pageoutput;
390  }
391 
397  function saveFeedback()
398  {
399  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
400  $errors = $this->feedback(true);
401  $this->object->saveFeedbackGeneric(0, $_POST["feedback_incomplete"]);
402  $this->object->saveFeedbackGeneric(1, $_POST["feedback_complete"]);
403  $this->object->cleanupMediaObjectUsage();
405  }
406 
412  function setQuestionTabs()
413  {
414  global $rbacsystem, $ilTabs;
415 
416  $this->ctrl->setParameterByClass("ilpageobjectgui", "q_id", $_GET["q_id"]);
417  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
418  $q_type = $this->object->getQuestionType();
419 
420  if (strlen($q_type))
421  {
422  $classname = $q_type . "GUI";
423  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
424  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
425  }
426 
427  if ($_GET["q_id"])
428  {
429  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
430  {
431  // edit page
432  $ilTabs->addTarget("edit_content",
433  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "edit"),
434  array("edit", "insert", "exec_pg"),
435  "", "", $force_active);
436  }
437 
438  // edit page
439  $ilTabs->addTarget("preview",
440  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "preview"),
441  array("preview"),
442  "ilPageObjectGUI", "", $force_active);
443  }
444 
445  $force_active = false;
446  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
447  {
448  $url = "";
449  if ($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
450  // edit question properties
451  $ilTabs->addTarget("edit_properties",
452  $url,
453  array("editQuestion", "save", "saveEdit", "addanswers", "removeanswers", "originalSyncForm"),
454  $classname, "", $force_active);
455  }
456 
457  if ($_GET["q_id"])
458  {
459  $ilTabs->addTarget("feedback",
460  $this->ctrl->getLinkTargetByClass($classname, "feedback"),
461  array("feedback", "saveFeedback"),
462  $classname, "");
463  }
464 
465  if ($_GET["q_id"])
466  {
467  $ilTabs->addTarget("solution_hint",
468  $this->ctrl->getLinkTargetByClass($classname, "suggestedsolution"),
469  array("suggestedsolution", "saveSuggestedSolution", "outSolutionExplorer", "cancel",
470  "addSuggestedSolution","cancelExplorer", "linkChilds", "removeSuggestedSolution"
471  ),
472  $classname,
473  ""
474  );
475  }
476 
477  // Assessment of questions sub menu entry
478  if ($_GET["q_id"])
479  {
480  $ilTabs->addTarget("statistics",
481  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
482  array("assessment"),
483  $classname, "");
484  }
485 
486  if (($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
487  {
488  $ref_id = $_GET["calling_test"];
489  if (strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
490  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
491  }
492  else
493  {
494  $ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
495  }
496  }
497 }
498 ?>