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