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