ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.assFormulaQuestionGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "./Modules/TestQuestionPool/classes/class.assQuestionGUI.php";
5 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestion.php";
6 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionResult.php";
7 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionVariable.php";
8 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionUnit.php";
9 include_once "./Modules/TestQuestionPool/classes/class.assFormulaQuestionUnitCategory.php";
10 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
11 require_once './Modules/TestQuestionPool/interfaces/interface.ilGuiAnswerScoringAdjustable.php';
12 
23 {
30  public function __construct($id = -1)
31  {
32  parent::__construct();
33  $this->object = new assFormulaQuestion();
34  $this->newUnitId = null;
35  if ($id >= 0) {
36  $this->object->loadFromDb($id);
37  }
38  }
39 
45  public function setQuestionTabs()
46  {
47  global $DIC;
48  $rbacsystem = $DIC['rbacsystem'];
49  $ilTabs = $DIC['ilTabs'];
50 
51  $ilTabs->clearTargets();
52 
53  $this->ctrl->setParameterByClass("ilAssQuestionPageGUI", "q_id", $_GET["q_id"]);
54  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
55  $q_type = $this->object->getQuestionType();
56 
57  if (strlen($q_type)) {
58  $classname = $q_type . "GUI";
59  $this->ctrl->setParameterByClass(strtolower($classname), "sel_question_types", $q_type);
60  $this->ctrl->setParameterByClass(strtolower($classname), "q_id", $_GET["q_id"]);
61  }
62 
63  if ($_GET["q_id"]) {
64  if ($rbacsystem->checkAccess('write', $_GET["ref_id"])) {
65  // edit page
66  $ilTabs->addTarget(
67  "edit_page",
68  $this->ctrl->getLinkTargetByClass("ilAssQuestionPageGUI", "edit"),
69  array("edit", "insert", "exec_pg"),
70  "",
71  "",
72  $force_active
73  );
74  }
75 
76  $this->addTab_QuestionPreview($ilTabs);
77  }
78 
79  $force_active = false;
80  if ($rbacsystem->checkAccess('write', $_GET["ref_id"])) {
81  $url = "";
82 
83  if ($classname) {
84  $url = $this->ctrl->getLinkTargetByClass($classname, "editQuestion");
85  }
86  $commands = $_POST["cmd"];
87  if (is_array($commands)) {
88  foreach ($commands as $key => $value) {
89  if (preg_match("/^suggestrange_.*/", $key, $matches)) {
90  $force_active = true;
91  }
92  }
93  }
94  // edit question properties
95  $ilTabs->addTarget(
96  "edit_question",
97  $url,
98  array(
99  "editQuestion", "save", "cancel", "addSuggestedSolution",
100  "cancelExplorer", "linkChilds", "removeSuggestedSolution",
101  "parseQuestion", "saveEdit", "suggestRange"
102  ),
103  $classname,
104  "",
105  $force_active
106  );
107  }
108 
109  if ($_GET["q_id"]) {
110  // add tab for question feedback within common class assQuestionGUI
111  $this->addTab_QuestionFeedback($ilTabs);
112  }
113 
114  if ($_GET["q_id"]) {
115  // add tab for question hint within common class assQuestionGUI
116  $this->addTab_QuestionHints($ilTabs);
117  }
118 
119  // Unit editor
120  if ($_GET['q_id']) {
121  // add tab for question hint within common class assQuestionGUI
122  $this->addTab_Units($ilTabs);
123  }
124 
125  // Assessment of questions sub menu entry
126  if ($_GET["q_id"]) {
127  $ilTabs->addTarget(
128  "statistics",
129  $this->ctrl->getLinkTargetByClass($classname, "assessment"),
130  array("assessment"),
131  $classname,
132  ""
133  );
134  }
135 
136  $this->addBackTab($ilTabs);
137  }
138 
139  public function getCommand($cmd)
140  {
141  if (preg_match("/suggestrange_(.*?)/", $cmd, $matches)) {
142  $cmd = "suggestRange";
143  }
144  return $cmd;
145  }
146 
151  public function suggestRange()
152  {
153  if ($this->writePostData()) {
155  }
156  $this->editQuestion();
157  }
158 
162  protected function writePostData($always = false)
163  {
164  $hasErrors = (!$always) ? $this->editQuestion(true) : false;
165  $checked = true;
166  if (!$hasErrors) {
167  $this->object->setTitle($_POST["title"]);
168  $this->object->setAuthor($_POST["author"]);
169  $this->object->setComment($_POST["comment"]);
170  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
171  $questiontext = ilUtil::stripOnlySlashes($_POST["question"]);
172  $this->object->setQuestion($questiontext);
173  $this->object->setEstimatedWorkingTime(
174  $_POST["Estimated"]["hh"],
175  $_POST["Estimated"]["mm"],
176  $_POST["Estimated"]["ss"]
177  );
178 
179  $this->object->parseQuestionText();
180  $found_vars = array();
181  $found_results = array();
182 
183 
184  foreach ($_POST as $key => $value) {
185  if (preg_match("/^unit_(\\\$v\d+)$/", $key, $matches)) {
186  array_push($found_vars, $matches[1]);
187  }
188  if (preg_match("/^unit_(\\\$r\d+)$/", $key, $matches)) {
189  array_push($found_results, $matches[1]);
190  }
191  }
192 
193  // if(!$this->object->checkForDuplicateVariables())
194  // {
195 //
196  // $this->addErrorMessage($this->lng->txt("err_duplicate_variables"));
197  // $checked = FALSE;
198  // }
199  if (!$this->object->checkForDuplicateResults()) {
200  $this->addErrorMessage($this->lng->txt("err_duplicate_results"));
201  $checked = false;
202  }
203 
204  foreach ($found_vars as $variable) {
205  if ($this->object->getVariable($variable) != null) {
206  $varObj = new assFormulaQuestionVariable($variable, $_POST["range_min_$variable"], $_POST["range_max_$variable"], $this->object->getUnitrepository()->getUnit($_POST["unit_$variable"]), $_POST["precision_$variable"], $_POST["intprecision_$variable"]);
207  $varObj->setRangeMinTxt($_POST["range_min_$variable"]);
208  $varObj->setRangeMaxTxt($_POST["range_max_$variable"]);
209  $this->object->addVariable($varObj);
210  }
211  }
212 
213  $tmp_form_vars = array();
214  $tmp_quest_vars = array();
215  foreach ($found_results as $result) {
216  $tmp_res_match = preg_match_all("/([$][v][0-9]*)/", $_POST["formula_$result"], $form_vars);
217  $tmp_form_vars = array_merge($tmp_form_vars, $form_vars[0]);
218 
219  $tmp_que_match = preg_match_all("/([$][v][0-9]*)/", $_POST['question'], $quest_vars);
220  $tmp_quest_vars = array_merge($tmp_quest_vars, $quest_vars[0]);
221  }
222  $result_has_undefined_vars = array_diff($tmp_form_vars, $found_vars);
223  $question_has_unused_vars = array_diff($tmp_quest_vars, $tmp_form_vars);
224 
225  if (count($result_has_undefined_vars) > 0 || count($question_has_unused_vars) > 0) {
226  $error_message = '';
227  if (count($result_has_undefined_vars) > 0) {
228  $error_message .= $this->lng->txt("res_contains_undef_var") . '<br>';
229  }
230  if (count($question_has_unused_vars) > 0) {
231  $error_message .= $this->lng->txt("que_contains_unused_var");
232  }
233  $checked = false;
234  if ($this->isSaveCommand()) {
235  ilUtil::sendFailure($error_message);
236  }
237  }
238  foreach ($found_results as $result) {
239  if (is_object($this->object->getUnitrepository()->getUnit($_POST["unit_$result"]))) {
240  $tmp_result_unit = $this->object->getUnitrepository()->getUnit($_POST["unit_$result"]);
241  } else {
242  $tmp_result_unit = null;
243  }
244 
245  if ($this->object->getResult($result) != null) {
246  $use_simple_rating = ($_POST["rating_advanced_$result"] == 1) ? false : true;
247  $resObj = new assFormulaQuestionResult(
248  $result,
249  $_POST["range_min_$result"],
250  $_POST["range_max_$result"],
251  $_POST["tolerance_$result"],
252  $tmp_result_unit,
253  $_POST["formula_$result"],
254  $_POST["points_$result"],
255  $_POST["precision_$result"],
256  $use_simple_rating,
257  ($_POST["rating_advanced_$result"] == 1) ? $_POST["rating_sign_$result"] : "",
258  ($_POST["rating_advanced_$result"] == 1) ? $_POST["rating_value_$result"] : "",
259  ($_POST["rating_advanced_$result"] == 1) ? $_POST["rating_unit_$result"] : "",
260  $_POST["result_type_$result"] != 0 ? $_POST["result_type_$result"] : 0
261  );
262  $resObj->setRangeMinTxt($_POST["range_min_$result"]);
263  $resObj->setRangeMaxTxt($_POST["range_max_$result"]);
264  $this->object->addResult($resObj);
265  $this->object->addResultUnits($resObj, $_POST["units_$result"]);
266  }
267  }
268  if ($checked == false) {
269  return 1;
270  } else {
271  $this->resetSavedPreviewSession();
272  return 0;
273  }
274  } else {
275  return 1;
276  }
277  }
278 
279  public function resetSavedPreviewSession()
280  {
281  global $DIC;
282  $ilUser = $DIC['ilUser'];
283  $user_id = $ilUser->getId();
284  $question_id = $this->object->getId();
285  require_once 'Modules/TestQuestionPool/classes/class.ilAssQuestionPreviewSession.php';
286  $ilAssQuestionPreviewSession = new ilAssQuestionPreviewSession($user_id, $question_id);
287  $ilAssQuestionPreviewSession->setParticipantsSolution(array());
288  }
289 
290  public function isSaveCommand()
291  {
292  return in_array($this->ctrl->getCmd(), array('saveFQ', 'saveEdit', 'saveReturnFQ'));
293  }
294 
300  public function editQuestion($checkonly = false)
301  {
302  $save = $this->isSaveCommand();
303 
304  $this->getQuestionTemplate();
305 
306  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
307  $form = new ilPropertyFormGUI();
308  $this->editForm = $form;
309 
310  $form->setFormAction($this->ctrl->getFormAction($this));
311  $form->setTitle($this->outQuestionType());
312  $form->setMultipart(false);
313  $form->setTableWidth('100%');
314  $form->setId('assformulaquestion');
315 
316  // title, author, description, question, working time (assessment mode)
318 
319  // Add info text
320  $question = $form->getItemByPostVar('question');
321  $question->setInfo($this->lng->txt('fq_question_desc'));
322 
323  $variables = $this->object->getVariables();
324  $categorized_units = $this->object->getUnitrepository()->getCategorizedUnits();
325  $result_units = $this->object->__get('resultunits');
326 
327  $unit_options = array();
328  $category_name = '';
329  $new_category = false;
330  foreach ((array) $categorized_units as $item) {
334  if ($item instanceof assFormulaQuestionUnitCategory) {
335  if ($category_name != $item->getDisplayString()) {
336  $new_category = true;
337  $category_name = $item->getDisplayString();
338  }
339  continue;
340  }
341  $unit_options[$item->getId()] = $item->getDisplayString() . ($new_category ? ' (' . $category_name . ')' : '');
342  $new_category = false;
343  }
344 
345  if (count($variables)) {
346  uasort($variables, function (assFormulaQuestionVariable $v1, assFormulaQuestionVariable $v2) {
347  $num_v1 = (int) substr($v1->getVariable(), 2);
348  $num_v2 = (int) substr($v2->getVariable(), 2);
349  if ($num_v1 > $num_v2) {
350  return 1;
351  } elseif ($num_v1 < $num_v2) {
352  return -1;
353  }
354 
355  return 0;
356  });
357 
358  foreach ($variables as $variable) {
362  $variable_header = new ilFormSectionHeaderGUI();
363  $variable_header->setTitle(sprintf($this->lng->txt('variable_x'), $variable->getVariable()));
364 
365  $range_min = new ilNumberInputGUI($this->lng->txt('range_min'), 'range_min_' . $variable->getVariable());
366  $range_min->allowDecimals(true);
367  $range_min->setSize(3);
368  $range_min->setRequired(true);
369  $range_min->setValue($variable->getRangeMin());
370 
371  $range_max = new ilNumberInputGUI($this->lng->txt('range_max'), 'range_max_' . $variable->getVariable());
372  $range_max->allowDecimals(true);
373  $range_max->setSize(3);
374  $range_max->setRequired(true);
375  $range_max->setValue($variable->getRangeMax());
376 
377  $units = new ilSelectInputGUI($this->lng->txt('unit'), 'unit_' . $variable->getVariable());
378  $units->setOptions(array(0 => $this->lng->txt('no_selection')) + $unit_options);
379  if (is_object($variable->getUnit())) {
380  $units->setValue($variable->getUnit()->getId());
381  }
382 
383  $precision = new ilNumberInputGUI($this->lng->txt('precision'), 'precision_' . $variable->getVariable());
384  $precision->setRequired(true);
385  $precision->setSize(3);
386  $precision->setMinValue(0);
387  $precision->setValue($variable->getPrecision());
388  $precision->setInfo($this->lng->txt('fq_precision_info'));
389 
390  $intprecision = new ilNumberInputGUI($this->lng->txt('intprecision'), 'intprecision_' . $variable->getVariable());
391  $intprecision->setSize(3);
392  $intprecision->setMinValue(1);
393  $intprecision->setValue($variable->getIntprecision());
394  $intprecision->setInfo($this->lng->txt('intprecision_info'));
395 
396  $form->addItem($variable_header);
397  $form->addItem($range_min);
398  $form->addItem($range_max);
399  $form->addItem($units);
400  $form->addItem($precision);
401  $form->addItem($intprecision);
402  }
403  }
404 
405  $results = $this->object->getResults();
406  if (count($results)) {
407  require_once 'Services/Form/classes/class.ilMultiSelectInputGUI.php';
408 
409  uasort($results, function (assFormulaQuestionResult $r1, assFormulaQuestionResult $r2) {
410  $num_r1 = (int) substr($r1->getResult(), 2);
411  $num_r2 = (int) substr($r2->getResult(), 2);
412  if ($num_r1 > $num_r2) {
413  return 1;
414  } elseif ($num_r1 < $num_r2) {
415  return -1;
416  }
417 
418  return 0;
419  });
420 
421  foreach ($results as $result) {
425  $result_header = new ilFormSectionHeaderGUI();
426  $result_header->setTitle(sprintf($this->lng->txt('result_x'), $result->getResult()));
427 
428  $formula = new ilTextInputGUI($this->lng->txt('formula'), 'formula_' . $result->getResult());
429  $formula->setInfo($this->lng->txt('fq_formula_desc'));
430  $formula->setRequired(true);
431  $formula->setSize(50);
432  $formula->setValue($result->getFormula());
433  $formula->setSuffix(' = ' . $result->getResult());
434 
435  if (
436  preg_match("/suggestrange_(.*)/", $this->ctrl->getCmd(), $matches) &&
437  strcmp($matches[1], $result->getResult()) == 0
438  ) {
439  // suggest a range for the result
440  if (strlen($result->substituteFormula($variables, $results))) {
441  $result->suggestRange($variables, $results);
442  }
443  }
444 
445  $range_min = new ilNumberInputGUI($this->lng->txt('range_min'), 'range_min_' . $result->getResult());
446  $range_min->allowDecimals(true);
447  $range_min->setSize(3);
448  $range_min->setRequired(true);
449  $range_min->setValue($result->getRangeMin());
450 
451  $range_max = new ilNumberInputGUI($this->lng->txt('range_max'), 'range_max_' . $result->getResult());
452  $range_max->allowDecimals(true);
453  $range_max->setSize(3);
454  $range_max->setRequired(true);
455  $range_max->setValue($result->getRangeMax());
456 
457  $matches = array();
458 
459  $precision = new ilNumberInputGUI($this->lng->txt('precision'), 'precision_' . $result->getResult());
460  $precision->setRequired(true);
461  $precision->setSize(3);
462  $precision->setMinValue(0);
463  $precision->setInfo($this->lng->txt('fq_precision_info'));
464  $precision->setValue($result->getPrecision());
465 
466  $tolerance = new ilNumberInputGUI($this->lng->txt('tolerance'), 'tolerance_' . $result->getResult());
467  $tolerance->setSize(3);
468  $tolerance->setMinValue(0);
469  $tolerance->setMaxValue(100);
470  $tolerance->allowDecimals(true);
471  $tolerance->setInfo($this->lng->txt('tolerance_info'));
472  $tolerance->setValue($result->getTolerance());
473 
474  $suggest_range_button = new ilCustomInputGUI('', '');
475  $suggest_range_button->setHtml('<input type="submit" class="btn btn-default" name="cmd[suggestrange_' . $result->getResult() . ']" value="' . $this->lng->txt("suggest_range") . '" />');
476 
477  $sel_result_units = new ilSelectInputGUI($this->lng->txt('unit'), 'unit_' . $result->getResult());
478  $sel_result_units->setOptions(array(0 => $this->lng->txt('no_selection')) + $unit_options);
479  $sel_result_units->setInfo($this->lng->txt('result_unit_info'));
480  if (is_object($result->getUnit())) {
481  $sel_result_units->setValue($result->getUnit()->getId());
482  }
483 
484  $mc_result_units = new ilMultiSelectInputGUI($this->lng->txt('result_units'), 'units_' . $result->getResult());
485  $mc_result_units->setOptions($unit_options);
486  $mc_result_units->setInfo($this->lng->txt('result_units_info'));
487  $selectedvalues = array();
488  foreach ($unit_options as $unit_id => $txt) {
489  if ($this->hasResultUnit($result, $unit_id, $result_units)) {
490  $selectedvalues[] = $unit_id;
491  }
492  }
493  $mc_result_units->setValue($selectedvalues);
494 
495  $result_type = new ilRadioGroupInputGUI($this->lng->txt('result_type_selection'), 'result_type_' . $result->getResult());
496  $result_type->setRequired(true);
497 
498  $no_type = new ilRadioOption($this->lng->txt('no_result_type'), 0);
499  $no_type->setInfo($this->lng->txt('fq_no_restriction_info'));
500 
501  $result_dec = new ilRadioOption($this->lng->txt('result_dec'), 1);
502  $result_dec->setInfo($this->lng->txt('result_dec_info'));
503 
504  $result_frac = new ilRadioOption($this->lng->txt('result_frac'), 2);
505  $result_frac->setInfo($this->lng->txt('result_frac_info'));
506 
507  $result_co_frac = new ilRadioOption($this->lng->txt('result_co_frac'), 3);
508  $result_co_frac->setInfo($this->lng->txt('result_co_frac_info'));
509 
510  $result_type->addOption($no_type);
511  $result_type->addOption($result_dec);
512  $result_type->addOption($result_frac);
513  $result_type->addOption($result_co_frac);
514  $result_type->setValue(strlen($result->getResultType()) ? $result->getResultType() : 0);
515 
516  $points = new ilNumberInputGUI($this->lng->txt('points'), 'points_' . $result->getResult());
517  $points->allowDecimals(true);
518  $points->setRequired(true);
519  $points->setSize(3);
520  $points->setMinValue(0);
521  $points->setValue(strlen($result->getPoints()) ? $result->getPoints() : 1);
522 
523  $rating_type = new ilCheckboxInputGUI($this->lng->txt('advanced_rating'), 'rating_advanced_' . $result->getResult());
524  $rating_type->setValue(1);
525  $rating_type->setInfo($this->lng->txt('advanced_rating_info'));
526 
527  if (!$save) {
528  $advanced_rating = $this->canUseAdvancedRating($result);
529  if (!$advanced_rating) {
530  $rating_type->setDisabled(true);
531  $rating_type->setChecked(false);
532  } else {
533  $rating_type->setChecked(strlen($result->getRatingSimple()) && $result->getRatingSimple() ? false : true);
534  }
535  }
536 
537  $sign = new ilNumberInputGUI($this->lng->txt('rating_sign'), 'rating_sign_' . $result->getResult());
538  $sign->setRequired(true);
539  $sign->setSize(3);
540  $sign->setMinValue(0);
541  $sign->setValue($result->getRatingSign());
542  $rating_type->addSubItem($sign);
543 
544  $value = new ilNumberInputGUI($this->lng->txt('rating_value'), 'rating_value_' . $result->getResult());
545  $value->setRequired(true);
546  $value->setSize(3);
547  $value->setMinValue(0);
548  $value->setValue($result->getRatingValue());
549  $rating_type->addSubItem($value);
550 
551  $unit = new ilNumberInputGUI($this->lng->txt('rating_unit'), 'rating_unit_' . $result->getResult());
552  $unit->setRequired(true);
553  $unit->setSize(3);
554  $unit->setMinValue(0);
555  $unit->setValue($result->getRatingUnit());
556  $rating_type->addSubItem($unit);
557 
558  $info_text = new ilNonEditableValueGUI($this->lng->txt('additional_rating_info'));
559  $rating_type->addSubItem($info_text);
560 
561  $form->addItem($result_header);
562  $form->addItem($formula);
563  $form->addItem($range_min);
564  $form->addItem($range_max);
565  $form->addItem($suggest_range_button);
566  $form->addItem($precision);
567  $form->addItem($tolerance);
568  $form->addItem($sel_result_units);
569  $form->addItem($mc_result_units);
570  $form->addItem($result_type);
571  $form->addItem($points);
572  $form->addItem($rating_type);
573  }
574 
575  $defined_result_vars = array();
576  $quest_vars = array();
577 
578  $defined_result_res = array();
579  $result_vars = array();
580 
581  foreach ($variables as $key => $object) {
582  $quest_vars[$key] = $key;
583  }
584 
585  foreach ($results as $key => $object) {
586  $result_vars[$key] = $key;
587  }
588 
589  foreach ($results as $tmp_result) {
593  $formula = $tmp_result->getFormula();
594 
595  preg_match_all("/([$][v][0-9]*)/", $formula, $form_vars);
596  preg_match_all("/([$][r][0-9]*)/", $formula, $form_res);
597  foreach ($form_vars[0] as $res_var) {
598  $defined_result_vars[$res_var] = $res_var;
599  }
600 
601  foreach ($form_res[0] as $res_res) {
602  $defined_result_res[$res_res] = $res_res;
603  }
604  }
605  }
606 
607  $result_has_undefined_vars = [];
608  $question_has_unused_vars = [];
609  $result_has_undefined_res = [];
610 
611  if (is_array($quest_vars) && count($quest_vars) > 0) {
612  $result_has_undefined_vars = array_diff($defined_result_vars, $quest_vars);
613  $question_has_unused_vars = array_diff($quest_vars, $defined_result_vars);
614  }
615 
616  if (is_array($result_vars) && count($result_vars) > 0) {
617  $result_has_undefined_res = array_diff($defined_result_res, $result_vars);
618  }
619  $error_message = '';
620 
621  if (count($result_has_undefined_vars) > 0 || count($question_has_unused_vars) > 0) {
622  if (count($result_has_undefined_vars) > 0) {
623  $error_message .= $this->lng->txt("res_contains_undef_var") . '<br>';
624  }
625  if (count($question_has_unused_vars) > 0) {
626  $error_message .= $this->lng->txt("que_contains_unused_var") . '<br>';
627  }
628 
629  $checked = false;
630  if ($save) {
631  ilUtil::sendFailure($error_message);
632  }
633  }
634 
635  if (is_array($result_has_undefined_res) && count($result_has_undefined_res) > 0) {
636  $error_message .= $this->lng->txt("res_contains_undef_res") . '<br>';
637  $checked = false;
638  }
639 
640  if ($save && !$checked) {
641  ilUtil::sendFailure($error_message);
642  }
643 
644  if ($this->object->getId()) {
645  $hidden = new ilHiddenInputGUI("", "ID");
646  $hidden->setValue($this->object->getId());
647  $form->addItem($hidden);
648  }
649 
651 
652  $form->addCommandButton('parseQuestion', $this->lng->txt("parseQuestion"));
653  $form->addCommandButton('saveReturnFQ', $this->lng->txt("save_return"));
654  $form->addCommandButton('saveFQ', $this->lng->txt("save"));
655 
656  $errors = $checked;
657 
658  if ($save) {
659  $found_vars = array();
660  $found_results = array();
661  foreach ((array) $_POST as $key => $value) {
662  if (preg_match("/^unit_(\\\$v\d+)$/", $key, $matches)) {
663  array_push($found_vars, $matches[1]);
664  }
665  if (preg_match("/^unit_(\\\$r\d+)$/", $key, $matches)) {
666  array_push($found_results, $matches[1]);
667  }
668  }
669 
670  $form->setValuesByPost();
671  $errors = !$form->checkInput();
672 
673  $custom_errors = false;
674  if (count($variables)) {
675  foreach ($variables as $variable) {
679  $min_range = $form->getItemByPostVar('range_min_' . $variable->getVariable());
680  $max_range = $form->getItemByPostVar('range_max_' . $variable->getVariable());
681  if ($min_range->getValue() > $max_range->getValue()) {
682  $min_range->setAlert($this->lng->txt('err_range'));
683  $max_range->setAlert($this->lng->txt('err_range'));
684  $custom_errors = true;
685  }
686  }
687  }
688 
689  if (count($results)) {
690  foreach ($results as $result) {
694  $min_range = $form->getItemByPostVar('range_min_' . $result->getResult());
695  $max_range = $form->getItemByPostVar('range_max_' . $result->getResult());
696  if ($min_range->getValue() > $max_range->getValue()) {
697  $min_range->setAlert($this->lng->txt('err_range'));
698  $max_range->setAlert($this->lng->txt('err_range'));
699  $custom_errors = true;
700  }
701 
702 
703  $formula = $form->getItemByPostVar('formula_' . $result->getResult());
704  if (strpos($formula->getValue(), $result->getResult()) !== false) {
705  $formula->setAlert($this->lng->txt('errRecursionInResult'));
706  $custom_errors = true;
707  }
708 
709  $result_unit = $form->getItemByPostVar('unit_' . $result->getResult());
710  $rating_advanced = $form->getItemByPostVar('rating_advanced_' . $result->getResult());
711  if (((int) $result_unit->getValue() <= 0) && $rating_advanced->getChecked()) {
712  unset($_POST['rating_advanced_' . $result->getResult()]);
713  $rating_advanced->setDisabled(true);
714  $rating_advanced->setChecked(false);
715  $rating_advanced->setAlert($this->lng->txt('err_rating_advanced_not_allowed'));
716  $custom_errors = true;
717  } elseif ($rating_advanced->getChecked()) {
718  $rating_sign = $form->getItemByPostVar('rating_sign_' . $result->getResult());
719  $rating_value = $form->getItemByPostVar('rating_value_' . $result->getResult());
720  $rating_unit = $form->getItemByPostVar('rating_unit_' . $result->getResult());
721 
722  $percentage = $rating_sign->getValue() + $rating_value->getValue() + $rating_unit->getValue();
723  if ($percentage != 100) {
724  $rating_advanced->setAlert($this->lng->txt('err_wrong_rating_advanced'));
725  $custom_errors = true;
726  }
727  }
728 
729  preg_match_all("/([$][v][0-9]*)/", $formula->getValue(), $form_vars);
730  $result_has_undefined_vars = array_diff($form_vars[0], (array) $found_vars);
731  if (count($result_has_undefined_vars)) {
732  $errors = true;
733  ilUtil::sendInfo($this->lng->txt('res_contains_undef_var'));
734  }
735  }
736  }
737 
738  if ($custom_errors && !$errors) {
739  $errors = true;
740  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
741  }
742  $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
743  if ($errors) {
744  $checkonly = false;
745  }
746  }
747 
748  if (!$checkonly) {
749  $this->tpl->setVariable('QUESTION_DATA', $form->getHTML());
750  }
751  return $errors;
752  }
753 
754  private function hasResultUnit($result, $unit_id, $resultunits)
755  {
756  if (array_key_exists($result->getResult(), $resultunits)) {
757  if (array_key_exists($unit_id, $resultunits[$result->getResult()])) {
758  return true;
759  }
760  }
761  return false;
762  }
763 
771  private function canUseAdvancedRating($result)
772  {
773  $resultunit = $result->getUnit();
774 
775  /*
776  * if there is a result-unit (unit selectbox) selected it is possible to use advanced rating
777  * if there is no result-unit selected it is NOT possible to use advanced rating, because there is no
778  * definition if the result-value or the unit-value should be the correct solution!!
779  *
780  */
781  if (is_object($resultunit)) {
782  return true;
783  } else {
784  return false;
785  }
786  }
787 
788  public function parseQuestion()
789  {
790  $this->writePostData();
791  $this->editQuestion();
792  }
793 
794  public function saveReturnFQ()
795  {
796  global $DIC;
797  $ilUser = $DIC['ilUser'];
798  $old_id = $_GET["q_id"];
799  $result = $this->writePostData();
800  if ($result == 0) {
801  $ilUser->setPref("tst_lastquestiontype", $this->object->getQuestionType());
802  $ilUser->writePref("tst_lastquestiontype", $this->object->getQuestionType());
803  $this->saveTaxonomyAssignments();
804  $this->object->saveToDb();
805  $originalexists = $this->object->_questionExistsInPool($this->object->original_id);
806  include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
807  if (($_GET["calling_test"] || (isset($_GET['calling_consumer']) && (int) $_GET['calling_consumer'])) && $originalexists && assQuestion::_isWriteable($this->object->original_id, $ilUser->getId())) {
808  $this->ctrl->redirect($this, "originalSyncForm");
809  return;
810  } elseif ($_GET["calling_test"]) {
811  require_once 'Modules/Test/classes/class.ilObjTest.php';
812  $test = new ilObjTest($_GET["calling_test"]);
813  #var_dump(assQuestion::_questionExistsInTest($this->object->getId(), $test->getTestId()));
814  $q_id = $this->object->getId();
815  if (!assQuestion::_questionExistsInTest($this->object->getId(), $test->getTestId())) {
816  global $DIC;
817  $tree = $DIC['tree'];
818  $ilDB = $DIC['ilDB'];
819  $ilPluginAdmin = $DIC['ilPluginAdmin'];
820 
821  include_once("./Modules/Test/classes/class.ilObjTest.php");
822  $_GET["ref_id"] = $_GET["calling_test"];
823  $test = new ilObjTest($_GET["calling_test"], true);
824 
825  require_once 'Modules/Test/classes/class.ilTestQuestionSetConfigFactory.php';
826  $testQuestionSetConfigFactory = new ilTestQuestionSetConfigFactory($tree, $ilDB, $ilPluginAdmin, $test);
827 
828  $new_id = $test->insertQuestion(
829  $testQuestionSetConfigFactory->getQuestionSetConfig(),
830  $this->object->getId()
831  );
832 
833  $q_id = $new_id;
834  if (isset($_REQUEST['prev_qid'])) {
835  $test->moveQuestionAfter($this->object->getId() + 1, $_REQUEST['prev_qid']);
836  }
837 
838  $this->ctrl->setParameter($this, 'q_id', $new_id);
839  $this->ctrl->setParameter($this, 'calling_test', $_GET['calling_test']);
840  #$this->ctrl->setParameter($this, 'test_ref_id', false);
841  }
842  ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
843  if ($_REQUEST['test_express_mode']) {
845  } else {
846  ilUtil::redirect("ilias.php?baseClass=ilObjTestGUI&cmd=questions&ref_id=" . $_GET["calling_test"]);
847  }
848  } else {
849  if ($this->object->getId() != $old_id) {
850  $this->callNewIdListeners($this->object->getId());
851  ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
852  $this->ctrl->redirectByClass("ilobjquestionpoolgui", "questions");
853  }
854  if (strcmp($_SESSION["info"], "") != 0) {
855  ilUtil::sendSuccess($_SESSION["info"] . "<br />" . $this->lng->txt("msg_obj_modified"), true);
856  } else {
857  ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
858  }
859  $this->ctrl->redirectByClass("ilobjquestionpoolgui", "questions");
860  }
861  } else {
862  $ilUser->setPref("tst_lastquestiontype", $this->object->getQuestionType());
863  $ilUser->writePref("tst_lastquestiontype", $this->object->getQuestionType());
864  $this->object->saveToDb();
865  $this->editQuestion();
866  }
867  }
868 
869  public function saveFQ()
870  {
871  $result = $this->writePostData();
872 
873  if ($result == 1) {
874  $this->editQuestion();
875  } else {
876  $this->saveTaxonomyAssignments();
877  $this->save();
878  }
879  }
883  public function checkInput()
884  {
885  if ((!$_POST["title"]) or (!$_POST["author"]) or (!$_POST["question"])) {
886  $this->addErrorMessage($this->lng->txt("fill_out_all_required_fields"));
887  return false;
888  }
889 
890 
891  return true;
892  }
893 
900  {
901  return true;
902  }
903 
904 
917  public function getSolutionOutput(
918  $active_id,
919  $pass = null,
920  $graphicalOutput = false,
921  $result_output = false,
922  $show_question_only = true,
923  $show_feedback = false,
924  $show_correct_solution = false,
925  $show_manual_scoring = false,
926  $show_question_text = true
927  ) {
928  // get the solution of the user for the active pass or from the last pass if allowed
929  $user_solution = array();
930  if (($active_id > 0) && (!$show_correct_solution)) {
931  $solutions = array();
932  include_once "./Modules/Test/classes/class.ilObjTest.php";
933  if (!ilObjTest::_getUsePreviousAnswers($active_id, true)) {
934  if (is_null($pass)) {
935  $pass = ilObjTest::_getPass($active_id);
936  }
937  }
938  $user_solution["active_id"] = $active_id;
939  $user_solution["pass"] = $pass;
940  $solutions = $this->object->getSolutionValues($active_id, $pass, !$this->getUseIntermediateSolution());
941  foreach ($solutions as $idx => $solution_value) {
942  if (preg_match("/^(\\\$v\\d+)$/", $solution_value["value1"], $matches)) {
943  $user_solution[$matches[1]] = $solution_value["value2"];
944  } elseif (preg_match("/^(\\\$r\\d+)$/", $solution_value["value1"], $matches)) {
945  if (!array_key_exists($matches[1], $user_solution)) {
946  $user_solution[$matches[1]] = array();
947  }
948  $user_solution[$matches[1]]["value"] = $solution_value["value2"];
949  } elseif (preg_match("/^(\\\$r\\d+)_unit$/", $solution_value["value1"], $matches)) {
950  if (!array_key_exists($matches[1], $user_solution)) {
951  $user_solution[$matches[1]] = array();
952  }
953  $user_solution[$matches[1]]["unit"] = $solution_value["value2"];
954  }
955  }
956  } elseif ($active_id) {
957  $solutions = null;
958  include_once "./Modules/Test/classes/class.ilObjTest.php";
959  if (!ilObjTest::_getUsePreviousAnswers($active_id, true)) {
960  if (is_null($pass)) {
961  $pass = ilObjTest::_getPass($active_id);
962  }
963  }
964  $user_solution = (array) $this->object->getBestSolution($this->object->getSolutionValues($active_id, $pass));
965  } elseif (is_object($this->getPreviewSession())) {
966  $solutionValues = array();
967 
968  $participantsSolution = $this->getPreviewSession()->getParticipantsSolution();
969  if (is_array($participantsSolution)) {
970  foreach ($participantsSolution as $val1 => $val2) {
971  $solutionValues[] = array('value1' => $val1, 'value2' => $val2);
972  }
973  }
974 
975  $user_solution = (array) $this->object->getBestSolution($solutionValues);
976  }
977 
978  $template = new ilTemplate("tpl.il_as_qpl_formulaquestion_output_solution.html", true, true, 'Modules/TestQuestionPool');
979  $questiontext = $this->object->substituteVariables($user_solution, $graphicalOutput, true, $result_output);
980 
981  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, true));
982  $questionoutput = $template->get();
983  $solutiontemplate = new ilTemplate("tpl.il_as_tst_solution_output.html", true, true, "Modules/TestQuestionPool");
984  $feedback = ($show_feedback) ? $this->getGenericFeedbackOutput($active_id, $pass) : "";
985  if (strlen($feedback)) {
986  $cssClass = (
987  $this->hasCorrectSolution($active_id, $pass) ?
989  );
990 
991  $solutiontemplate->setVariable("ILC_FB_CSS_CLASS", $cssClass);
992  $solutiontemplate->setVariable("FEEDBACK", $this->object->prepareTextareaOutput($feedback, true));
993  }
994  $solutiontemplate->setVariable("SOLUTION_OUTPUT", $questionoutput);
995 
996  $solutionoutput = $solutiontemplate->get();
997  if (!$show_question_only) {
998  // get page object output
999  $solutionoutput = $this->getILIASPage($solutionoutput);
1000  }
1001  return $solutionoutput;
1002  }
1003 
1004  public function getPreview($show_question_only = false, $showInlineFeedback = false)
1005  {
1006  $user_solution = array();
1007 
1008  if (is_object($this->getPreviewSession())) {
1009  $solutions = (array) $this->getPreviewSession()->getParticipantsSolution();
1010 
1011  foreach ($solutions as $val1 => $val2) {
1012  if (preg_match("/^(\\\$v\\d+)$/", $val1, $matches)) {
1013  $user_solution[$matches[1]] = $val2;
1014  } elseif (preg_match("/^(\\\$r\\d+)$/", $val1, $matches)) {
1015  if (!array_key_exists($matches[1], $user_solution)) {
1016  $user_solution[$matches[1]] = array();
1017  }
1018  $user_solution[$matches[1]]["value"] = $val2;
1019  } elseif (preg_match("/^(\\\$r\\d+)_unit$/", $val1, $matches)) {
1020  if (!array_key_exists($matches[1], $user_solution)) {
1021  $user_solution[$matches[1]] = array();
1022  }
1023  $user_solution[$matches[1]]["unit"] = $val2;
1024  }
1025 
1026  if (preg_match("/^(\\\$r\\d+)/", $val1, $matches) && $user_solution[$matches[1]]["result_type"] == 0) {
1027  $user_solution[$matches[1]]["result_type"] = assFormulaQuestionResult::getResultTypeByQstId($this->object->getId(), $val1);
1028  }
1029  }
1030  }
1031 
1032  if (!$this->object->hasRequiredVariableSolutionValues($user_solution)) {
1033  $user_solution = $this->object->getInitialVariableSolutionValues();
1034 
1035  if (is_object($this->getPreviewSession())) {
1036  $this->getPreviewSession()->setParticipantsSolution($user_solution);
1037  }
1038  }
1039 
1040  $template = new ilTemplate("tpl.il_as_qpl_formulaquestion_output.html", true, true, 'Modules/TestQuestionPool');
1041  if (is_object($this->getPreviewSession())) {
1042  $questiontext = $this->object->substituteVariables($user_solution, false, false, false);
1043  } else {
1044  $questiontext = $this->object->substituteVariables(array());
1045  }
1046  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, true));
1047  $questionoutput = $template->get();
1048  if (!$show_question_only) {
1049  // get page object output
1050  $questionoutput = $this->getILIASPage($questionoutput);
1051  }
1052  return $questionoutput;
1053  }
1054 
1055  // hey: prevPassSolutions - pass will be always available from now on
1056  public function getTestOutput($active_id, $pass, $is_postponed = false, $use_post_solutions = false, $show_feedback = false)
1057  // hey.
1058  {
1059  ilUtil::sendInfo($this->lng->txt('enter_valid_values'));
1060  // get the solution of the user for the active pass or from the last pass if allowed
1061  $user_solution = array();
1062  if ($active_id) {
1063  $solutions = (array) $this->object->getTestOutputSolutions($active_id, $pass);
1064 
1065  $actualPassIndex = null;
1066  if ($this->object->getTestPresentationConfig()->isSolutionInitiallyPrefilled()) {
1067  require_once 'Modules/Test/classes/class.ilObjTest.php';
1068  $actualPassIndex = ilObjTest::_getPass($active_id);
1069  }
1070 
1071  foreach ($solutions as $idx => $solution_value) {
1072  if (preg_match("/^(\\\$v\\d+)$/", $solution_value["value1"], $matches)) {
1073  if ($this->object->getTestPresentationConfig()->isSolutionInitiallyPrefilled()) {
1074  $this->object->saveCurrentSolution($active_id, $actualPassIndex, $matches[1], $solution_value["value2"], true);
1075  }
1076 
1077  $user_solution[$matches[1]] = $solution_value["value2"];
1078  } elseif (preg_match("/^(\\\$r\\d+)$/", $solution_value["value1"], $matches)) {
1079  if (!array_key_exists($matches[1], $user_solution)) {
1080  $user_solution[$matches[1]] = array();
1081  }
1082  $user_solution[$matches[1]]["value"] = $solution_value["value2"];
1083  } elseif (preg_match("/^(\\\$r\\d+)_unit$/", $solution_value["value1"], $matches)) {
1084  if (!array_key_exists($matches[1], $user_solution)) {
1085  $user_solution[$matches[1]] = array();
1086  }
1087  $user_solution[$matches[1]]["unit"] = $solution_value["value2"];
1088  }
1089 
1090  if (preg_match("/^(\\\$r\\d+)/", $solution_value["value1"], $matches) && $user_solution[$matches[1]]["result_type"] == 0) {
1091  $user_solution[$matches[1]]["result_type"] = assFormulaQuestionResult::getResultTypeByQstId($this->object->getId(), $solution_value["value1"]);
1092  }
1093  }
1094  }
1095 
1096  // fau: testNav - take question variables always from authorized solution because they are saved with this flag, even if an authorized solution is not saved
1097  $solutions = $this->object->getSolutionValues($active_id, $pass, true);
1098  foreach ($solutions as $idx => $solution_value) {
1099  if (preg_match("/^(\\\$v\\d+)$/", $solution_value["value1"], $matches)) {
1100  $user_solution[$matches[1]] = $solution_value["value2"];
1101  }
1102  }
1103  // fau.
1104 
1105  if (!$this->object->hasRequiredVariableSolutionValues($user_solution)) {
1106  foreach ($this->object->getInitialVariableSolutionValues() as $val1 => $val2) {
1107  $this->object->saveCurrentSolution($active_id, $pass, $val1, $val2, true);
1108  }
1109  }
1110 
1111  // generate the question output
1112  $template = new ilTemplate("tpl.il_as_qpl_formulaquestion_output.html", true, true, 'Modules/TestQuestionPool');
1113 
1114  $questiontext = $this->object->substituteVariables($user_solution);
1115 
1116  $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, true));
1117 
1118  $questionoutput = $template->get();
1119  $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
1120  return $pageoutput;
1121  }
1122 
1123  public function getSpecificFeedbackOutput($userSolution)
1124  {
1125  return '';
1126  }
1127 }
hasCorrectSolution($activeId, $passIndex)
addTab_QuestionPreview(ilTabsGUI $tabsGUI)
This class represents an option in a radio group.
setOptions($a_options)
Set Options.
addBasicQuestionFormProperties($form)
Add basic question form properties: assessment: title, author, description, question, working time.
static _getPass($active_id)
Retrieves the actual pass of a given user for a given test.
$_SESSION["AccountId"]
This class represents a selection list property in a property form.
$result
addTab_QuestionHints(ilTabsGUI $tabs)
adds the hints tab to ilTabsGUI
$template
This class represents a property form user interface.
Single choice question GUI representation The assFormulaQuestionGUI class encapsulates the GUI repres...
global $DIC
Definition: saml.php:7
addErrorMessage($errormessage)
$_GET["client_id"]
This class represents a section header in a property form.
suggestRange()
Suggest a range for a result public.
if(!array_key_exists('StateId', $_REQUEST)) $id
This class represents a checkbox property in a property form.
Class for single choice questions assFormulaQuestion is a class for single choice questions...
getPreview($show_question_only=false, $showInlineFeedback=false)
canUseAdvancedRating($result)
Check if advanced rating can be used for a result.
getSolutionOutput( $active_id, $pass=null, $graphicalOutput=false, $result_output=false, $show_question_only=true, $show_feedback=false, $show_correct_solution=false, $show_manual_scoring=false, $show_question_text=true)
Get the question solution output.
callNewIdListeners($a_new_id)
Call the new id listeners.
getQuestionTemplate()
get question template
setInfo($a_info)
Set Info.
populateTaxonomyFormSection(ilPropertyFormGUI $form)
getUseIntermediateSolution()
Get if intermediate solution should be used for solution output.
static getResultTypeByQstId($a_qst_id, $a_result)
static stripOnlySlashes($a_str)
strip slashes if magic qoutes is enabled
allowDecimals($a_value)
Toggle Decimals.
setInfo($a_info)
Set Information Text.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
setQuestionTabs()
Sets the ILIAS tabs for this question type Sets the ILIAS tabs for this question type public...
This class represents a hidden form property in a property form.
This class represents a multi selection list property in a property form.
This class represents a property in a property form.
static getReturnToPageLink($q_id=null)
if(isset($_POST['submit'])) $form
getILIASPage($html="")
Returns the ILIAS Page around a question.
This class represents a number property in a property form.
setValue($a_value)
Set Value.
special template class to simplify handling of ITX/PEAR
This class represents a text property in a property form.
getTestOutput($active_id, $pass, $is_postponed=false, $use_post_solutions=false, $show_feedback=false)
$ilUser
Definition: imgupload.php:18
addTab_QuestionFeedback(ilTabsGUI $tabs)
adds the feedback tab to ilTabsGUI
setOptions($a_options)
Set Options.
Basic GUI class for assessment questions.
$txt
Definition: error.php:11
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
__construct($id=-1)
assFormulaQuestionGUI constructor The constructor takes possible arguments an creates an instance of ...
static _questionExistsInTest($question_id, $test_id)
setSize($a_size)
Set Size.
This class represents a custom property in a property form.
$errors
Definition: index.php:6
This class represents a non editable value in a property form.
outQuestionPage($a_temp_var, $a_postponed=false, $active_id="", $html="", $inlineFeedbackEnabled=false)
output question page
$results
Definition: svg-scanner.php:47
addBackTab(ilTabsGUI $ilTabs)
global $ilDB
$url
getGenericFeedbackOutput($active_id, $pass)
Returns the answer specific feedback for the question.
static _getUsePreviousAnswers($active_id, $user_active_user_setting=false)
Returns if the previous results should be hidden for a learner.
static redirect($a_script)
save()
save question
supportsIntermediateSolutionOutput()
Question type specific support of intermediate solution output The function getSolutionOutput respect...
$key
Definition: croninfo.php:18
hasResultUnit($result, $unit_id, $resultunits)
$_POST["username"]
setRequired($a_required)
Set Required.
$test
Definition: Utf8Test.php:84
static _isWriteable($question_id, $user_id)
Returns true if the question is writeable by a certain user.