ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilAssLacCompositeValidator.php
Go to the documentation of this file.
1 <?php
2 
19 use ILIAS\Refinery\Random\Group as RandomGroup;
20 
29 {
35  protected $object_loader;
36 
37  private RandomGroup $randomGroup;
38 
42  public function __construct($object_loader)
43  {
44  global $DIC;
45 
46  $this->object_loader = $object_loader;
47  $this->randomGroup = $DIC->refinery()->random();
48  }
49 
50  public function validate(ilAssLacAbstractComposite $composite): void
51  {
52  if (count($composite->nodes) > 0) {
53  $this->validate($composite->nodes[0]);
54  $this->validate($composite->nodes[1]);
55  $this->validateSubTree($composite);
56  }
57 
58  return;
59  }
60 
61  private function validateSubTree(ilAssLacAbstractComposite $composite): void
62  {
63  if ($composite->nodes[0] instanceof ilAssLacQuestionExpressionInterface &&
64  $composite->nodes[1] instanceof ilAssLacSolutionExpressionInterface
65  ) {
66  $question_expression = $composite->nodes[0];
67  $answer_expression = $composite->nodes[1];
68  $question_index = $composite->nodes[0]->getQuestionIndex();
69  $answer_index = null;
70  $question = $this->object_loader->getQuestion($question_index);
71 
72  $this->checkQuestionExists($question, $question_index);
73  //$this->checkQuestionIsReachable($question, $question_index);
74 
75  if ($this->isResultOfAnswerExpression($question_expression)) {
76  $answer_index = $question_expression->getAnswerIndex() - 1;
77  // @PHP8-CR I suspect this cluster of typizations is broken in some way. I still leave these remarks "intact"
78  // to assist a more thorough analysis.
79  $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index);
80  }
81  if ($answer_expression instanceof ilAssLacNumberOfResultExpression && !($question instanceof assClozeTest)) {
82  // @PHP8-CR I suspect this cluster of typizations is broken in some way. I still leave these remarks "intact"
83  // to assist a more thorough analysis.
84  $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_expression->getNumericValue() - 1);
85  }
86 
87  $this->checkAnswerExpressionExist($question->getExpressionTypes(), $answer_expression, $question_index);
88  $this->checkOperatorExistForExpression($question->getOperators($answer_expression::$identifier), $answer_expression, $composite::$pattern);
89 
90  if ($answer_expression instanceof ilAssLacOrderingResultExpression &&
91  ($question instanceof assOrderingHorizontal || $question instanceof assOrderingQuestion)
92  ) {
93  foreach ($answer_expression->getOrdering() as $order) {
94  $count = 0;
95  foreach ($answer_expression->getOrdering() as $element) {
96  if ($element == $order) {
97  $count++;
98  }
99  }
100  if ($count > 1) {
101  throw new ilAssLacDuplicateElement($order);
102  }
103 
104  $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $order - 1);
105  }
106  }
107  if ($question instanceof assClozeTest) {
108  $this->validateClozeTest($answer_index, $question, $answer_expression, $question_index);
109  } elseif (
110  $answer_expression instanceof ilAssLacPercentageResultExpression &&
111  // @PHP8-CR I suspect this cluster of typizations is broken in some way. I still leave these remarks "intact"
112  // to assist a more thorough analysis.
113  $this->isResultOfAnswerExpression($question_expression) &&
114  !($question instanceof assFormulaQuestion)
115  ) {
116  throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index . "[" . ($answer_index + 1) . "]");
117  }
118  } elseif (
119  ($composite->nodes[0] instanceof ilAssLacAbstractOperation &&
120  $composite->nodes[1] instanceof ilAssLacExpressionInterface) ||
121  ($composite->nodes[0] instanceof ilAssLacExpressionInterface &&
122  $composite->nodes[1] instanceof ilAssLacAbstractOperation) ||
123  ($composite->nodes[0] instanceof ilAssLacSolutionExpressionInterface)
124  ) {
125  throw new ilAssLacUnableToParseCondition("");
126  }
127  }
128 
137  private function validateClozeTest($answer_index, $question, $answer_expression, $question_index): void
138  {
139  if ($answer_index !== null) {
140  $options = $question->getAvailableAnswerOptions($answer_index);
141  $found = false;
142  switch ($options->getType()) {
143  case 0: // text
144  if (
145  $answer_expression instanceof ilAssLacStringResultExpression
146  ) {
147  $found = true;
148  }
149 
150  break;
151  case 1: // select
152 
153  if ($answer_expression instanceof ilAssLacStringResultExpression) {
154  foreach ($options->getItems($this->getNonShuffler()) as $item) {
155  if ($item->getAnswertext() == $answer_expression->getText()) {
156  $found = true;
157  }
158  }
159  } elseif ($answer_expression instanceof ilAssLacNumberOfResultExpression) {
160  foreach ($options->getItems($question->getShuffler()) as $item) {
161  if ($item->getOrder() == $answer_expression->getNumericValue() - 1) {
162  $found = true;
163  }
164  }
165  }
166  break;
167  case 2: // numeric
168  if ($answer_expression instanceof ilAssLacNumericResultExpression) {
169  $found = true;
170  }
171  break;
172  }
173 
174  if ($answer_expression instanceof ilAssLacEmptyAnswerExpression) {
175  $found = true;
176  }
177  if (!$found && !($answer_expression instanceof ilAssLacPercentageResultExpression)) {
178  throw new ilAssLacAnswerValueNotExist($question_index, $answer_expression->getValue(), $answer_index + 1);
179  }
180  }
181  }
182 
190  private function checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index): void
191  {
192  $answer_options = $question->getAvailableAnswerOptions($answer_index);
193  if ($answer_options == null) {
194  throw new ilAssLacAnswerIndexNotExist($question_index, $answer_index + 1);
195  }
196  }
197 
204  private function checkQuestionExists($question, $index): void
205  {
206  if ($question == null) {
207  throw new ilAssLacQuestionNotExist($index);
208  }
209  }
210 
216  private function isResultOfAnswerExpression($expression): bool
217  {
218  // @PHP8-CR I suspect this cluster of typizations is broken in some way. I still leave these remarks "intact"
219  // to assist a more thorough analysis.
220  if ($expression instanceof ilAssLacResultOfAnswerOfQuestionExpression) {
221  return true;
222  }
223 
224  if ($expression instanceof ilAssLacResultOfAnswerOfCurrentQuestionExpression) {
225  return true;
226  }
227 
228  return false;
229  }
230 
238  private function checkAnswerExpressionExist($expressions, $answer_expression, $question_index): void
239  {
240  if (!in_array($answer_expression::$identifier, $expressions)) {
241  throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index);
242  }
243  }
244 
252  private function checkOperatorExistForExpression($operators, $answer_expression, $pattern): void
253  {
254  if (!in_array($pattern, $operators)) {
255  throw new ilAssLacOperatorNotSupportedByExpression($answer_expression->getValue(), $pattern);
256  }
257  }
258 
259  protected function getNonShuffler(): \ILIAS\Refinery\Transformation
260  {
261  return $this->randomGroup->dontShuffle();
262  }
263 }
checkOperatorExistForExpression($operators, $answer_expression, $pattern)
validate(ilAssLacAbstractComposite $composite)
Class for horizontal ordering questions.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
validateClozeTest($answer_index, $question, $answer_expression, $question_index)
Class for cloze tests.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class for single choice questions assFormulaQuestion is a class for single choice questions...
validateSubTree(ilAssLacAbstractComposite $composite)
checkAnswerExpressionExist($expressions, $answer_expression, $question_index)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class for ordering questions.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...