ILIAS  eassessment Revision 61809
 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  //additional characters
165  include_once("./Services/Form/classes/class.ilAdditionalCharactersGUI.php");
166  $form->addItem(new ilAdditionalCharactersGUI());
167  if ($save)
168  {
169  $form->setValuesByPost();
170  $points->setValue($this->object->getMaximumPoints());
171  $errors = !$form->checkInput();
172  $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
173  if ($errors) $checkonly = false;
174  }
175 
176  if (!$checkonly) $this->tpl->setVariable("QUESTION_DATA", $form->getHTML());
177  return $errors;
178  }
179 
183  public function addanswers()
184  {
185  $this->writePostData(true);
186  $position = key($_POST['cmd']['addanswers']);
187  $this->object->addAnswer("", 0, $position+1);
188  $this->editQuestion();
189  }
190 
194  public function removeanswers()
195  {
196  $this->writePostData(true);
197  $position = key($_POST['cmd']['removeanswers']);
198  $this->object->deleteAnswer($position);
199  $this->editQuestion();
200  }
201 
202  function outQuestionForTest($formaction, $active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE)
203  {
204  $test_output = $this->getTestOutput($active_id, $pass, $is_postponed, $use_post_solutions);
205  $this->tpl->setVariable("QUESTION_OUTPUT", $test_output);
206  $this->tpl->setVariable("FORMACTION", $formaction);
207  }
208 
223  $active_id,
224  $pass = NULL,
225  $graphicalOutput = FALSE,
226  $result_output = FALSE,
227  $show_question_only = TRUE,
228  $show_feedback = FALSE,
229  $show_correct_solution = FALSE,
230  $show_manual_scoring = FALSE
231  )
232  {
233  // get the solution of the user for the active pass or from the last pass if allowed
234  $solutions = array();
235  if (($active_id > 0) && (!$show_correct_solution))
236  {
237  $solutions =& $this->object->getSolutionValues($active_id, $pass);
238  }
239  else
240  {
241  $rank = array();
242  foreach ($this->object->answers as $answer)
243  {
244  if ($answer->getPoints() > 0)
245  {
246  if (!is_array($rank[$answer->getPoints()]))
247  {
248  $rank[$answer->getPoints()] = array();
249  }
250  array_push($rank[$answer->getPoints()], $answer->getAnswertext());
251  }
252  }
253  krsort($rank, SORT_NUMERIC);
254  foreach ($rank as $index => $bestsolutions)
255  {
256  array_push($solutions, array("value1" => join(",", $bestsolutions), "points" => $index));
257  }
258  }
259 
260  // generate the question output
261  include_once "./classes/class.ilTemplate.php";
262  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output_solution.html", TRUE, TRUE, "Modules/TestQuestionPool");
263  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
264  $available_answers =& $this->object->getAvailableAnswers();
265  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
266  {
267  if ((!$test_id) && (strcmp($solutions[$i]["value1"], "") == 0))
268  {
269  }
270  else
271  {
272  if (($active_id > 0) && (!$show_correct_solution))
273  {
274  if ($graphicalOutput)
275  {
276  // output of ok/not ok icons for user entered solutions
277  $index = $this->object->isAnswerCorrect($available_answers, $solutions[$i]["value1"]);
278  $correct = FALSE;
279  if ($index !== FALSE)
280  {
281  unset($available_answers[$index]);
282  $correct = TRUE;
283  }
284  if ($correct)
285  {
286  $template->setCurrentBlock("icon_ok");
287  $template->setVariable("ICON_OK", ilUtil::getImagePath("icon_ok.gif"));
288  $template->setVariable("TEXT_OK", $this->lng->txt("answer_is_right"));
289  $template->parseCurrentBlock();
290  }
291  else
292  {
293  $template->setCurrentBlock("icon_ok");
294  $template->setVariable("ICON_NOT_OK", ilUtil::getImagePath("icon_not_ok.gif"));
295  $template->setVariable("TEXT_NOT_OK", $this->lng->txt("answer_is_wrong"));
296  $template->parseCurrentBlock();
297  }
298  }
299  }
300  $template->setCurrentBlock("textsubset_row");
301  $template->setVariable("SOLUTION", $solutions[$i]["value1"]);
302  $template->setVariable("COUNTER", $i+1);
303  if ($result_output)
304  {
305  $points = $solutions[$i]["points"];
306  $resulttext = ($points == 1) ? "(%s " . $this->lng->txt("point") . ")" : "(%s " . $this->lng->txt("points") . ")";
307  $template->setVariable("RESULT_OUTPUT", sprintf($resulttext, $points));
308  }
309  $template->parseCurrentBlock();
310  }
311  }
312  $questiontext = $this->object->getQuestion();
313  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
314  $questionoutput = $template->get();
315  $feedback = ($show_feedback) ? $this->getAnswerFeedbackOutput($active_id, $pass) : "";
316  if (strlen($feedback)) $solutiontemplate->setVariable("FEEDBACK", $feedback);
317  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
318 
319  $solutionoutput = $solutiontemplate->get();
320  if (!$show_question_only)
321  {
322  // get page object output
323  $solutionoutput = $this->getILIASPage($solutionoutput);
324  }
325  return $solutionoutput;
326  }
327 
328  function getPreview($show_question_only = FALSE)
329  {
330  // generate the question output
331  include_once "./classes/class.ilTemplate.php";
332  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
333  $width = $this->object->getMaxTextboxWidth();
334  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
335  {
336  $template->setCurrentBlock("textsubset_row");
337  $template->setVariable("COUNTER", $i+1);
338  $template->setVariable("TEXTFIELD_ID", sprintf("%02d", $i+1));
339  $template->setVariable("TEXTFIELD_SIZE", $width);
340  $template->parseCurrentBlock();
341  }
342  $questiontext = $this->object->getQuestion();
343  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
344  $questionoutput = $template->get();
345  if (!$show_question_only)
346  {
347  // get page object output
348  $questionoutput = $this->getILIASPage($questionoutput);
349  }
350  return $questionoutput;
351  }
352 
353  function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE)
354  {
355  // get the solution of the user for the active pass or from the last pass if allowed
356  $user_solution = "";
357  if ($active_id)
358  {
359  $solutions = NULL;
360  include_once "./Modules/Test/classes/class.ilObjTest.php";
361  if (!ilObjTest::_getUsePreviousAnswers($active_id, true))
362  {
363  if (is_null($pass)) $pass = ilObjTest::_getPass($active_id);
364  }
365  $solutions =& $this->object->getSolutionValues($active_id, $pass);
366  }
367 
368  // generate the question output
369  include_once "./classes/class.ilTemplate.php";
370  $template = new ilTemplate("tpl.il_as_qpl_textsubset_output.html", TRUE, TRUE, "Modules/TestQuestionPool");
371  $width = $this->object->getMaxTextboxWidth();
372  for ($i = 0; $i < $this->object->getCorrectAnswers(); $i++)
373  {
374  $template->setCurrentBlock("textsubset_row");
375  foreach ($solutions as $idx => $solution_value)
376  {
377  if ($idx == $i)
378  {
379  $template->setVariable("TEXTFIELD_VALUE", " value=\"" . $solution_value["value1"]."\"");
380  }
381  }
382  $template->setVariable("COUNTER", $i+1);
383  $template->setVariable("TEXTFIELD_ID", sprintf("%02d", $i+1));
384  $template->setVariable("TEXTFIELD_SIZE", $width);
385  $template->parseCurrentBlock();
386  }
387  $questiontext = $this->object->getQuestion();
388  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
389  $questionoutput = $template->get();
390  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
391  return $pageoutput;
392  }
393 
399  function saveFeedback()
400  {
401  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
402  $errors = $this->feedback(true);
403  $this->object->saveFeedbackGeneric(0, $_POST["feedback_incomplete"]);
404  $this->object->saveFeedbackGeneric(1, $_POST["feedback_complete"]);
405  $this->object->cleanupMediaObjectUsage();
407  }
408 
414  function setQuestionTabs()
415  {
416  global $rbacsystem, $ilTabs;
417 
418  $this->ctrl->setParameterByClass("ilpageobjectgui", "q_id", $_GET["q_id"]);
419  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
420  $q_type = $this->object->getQuestionType();
421 
422  if (strlen($q_type))
423  {
424  $classname = $q_type . "GUI";
425  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
426  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
427  }
428 
429  if ($_GET["q_id"])
430  {
431  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
432  {
433  // edit page
434  $ilTabs->addTarget("edit_content",
435  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "edit"),
436  array("edit", "insert", "exec_pg"),
437  "", "", $force_active);
438  }
439 
440  // edit page
441  $ilTabs->addTarget("preview",
442  $this->ctrl->getLinkTargetByClass("ilPageObjectGUI", "preview"),
443  array("preview"),
444  "ilPageObjectGUI", "", $force_active);
445  }
446 
447  $force_active = false;
448  if ($rbacsystem->checkAccess('write', $_GET["ref_id"]))
449  {
450  $url = "";
451  if ($classname) $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
452  // edit question properties
453  $ilTabs->addTarget("edit_properties",
454  $url,
455  array("editQuestion", "save", "saveEdit", "addanswers", "removeanswers", "originalSyncForm"),
456  $classname, "", $force_active);
457  }
458 
459  if ($_GET["q_id"])
460  {
461  $ilTabs->addTarget("feedback",
462  $this->ctrl->getLinkTargetByClass($classname, "feedback"),
463  array("feedback", "saveFeedback"),
464  $classname, "");
465  }
466 
467  if ($_GET["q_id"])
468  {
469  $ilTabs->addTarget("solution_hint",
470  $this->ctrl->getLinkTargetByClass($classname, "suggestedsolution"),
471  array("suggestedsolution", "saveSuggestedSolution", "outSolutionExplorer", "cancel",
472  "addSuggestedSolution","cancelExplorer", "linkChilds", "removeSuggestedSolution"
473  ),
474  $classname,
475  ""
476  );
477  }
478 
479  // Assessment of questions sub menu entry
480  if ($_GET["q_id"])
481  {
482  $ilTabs->addTarget("statistics",
483  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
484  array("assessment"),
485  $classname, "");
486  }
487 
488  if (($_GET["calling_test"] > 0) || ($_GET["test_ref_id"] > 0))
489  {
490  $ref_id = $_GET["calling_test"];
491  if (strlen($ref_id) == 0) $ref_id = $_GET["test_ref_id"];
492  $ilTabs->setBackTarget($this->lng->txt("backtocallingtest"), "ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=$ref_id");
493  }
494  else
495  {
496  $ilTabs->setBackTarget($this->lng->txt("qpl"), $this->ctrl->getLinkTargetByClass("ilobjquestionpoolgui", "questions"));
497  }
498  }
499 }
500 ?>