ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
ilAssLacCompositeValidator.php
Go to the documentation of this file.
1<?php
2
3require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Expressions/ilAssLacResultOfAnswerOfQuestionExpression.php";
4require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacExpressionNotSupportedByQuestion.php";
5require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacQuestionNotExist.php";
6require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacOperatorNotSupportedByExpression.php";
7require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacUnsupportedExpression.php";
8require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacUnsupportedOperation.php";
9require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacAnswerIndexNotExist.php";
10require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacQuestionNotReachable.php";
11require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacAnswerValueNotExist.php";
12require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacUnableToParseCondition.php";
13require_once "Modules/TestQuestionPool/classes/questions/LogicalAnswerCompare/Exception/ilAssLacDuplicateElement.php";
14
23{
24
30 protected $object_loader;
31
35 public function __construct($object_loader)
36 {
37 $this->object_loader = $object_loader;
38 }
39
40 public function validate(ilAssLacAbstractComposite $composite)
41 {
42 if(count($composite->nodes) > 0)
43 {
44 $this->validate($composite->nodes[0]);
45 $this->validate($composite->nodes[1]);
46 $this->validateSubTree($composite);
47 }
48
49 return;
50 }
51
52 private function validateSubTree(ilAssLacAbstractComposite $composite)
53 {
54 if($composite->nodes[0] instanceof ilAssLacQuestionExpressionInterface &&
55 $composite->nodes[1] instanceof ilAssLacSolutionExpressionInterface
56 ){
57 $question_expression = $composite->nodes[0];
58 $answer_expression = $composite->nodes[1];
59 $question_index = $composite->nodes[0]->getQuestionIndex();
60 $answer_index = null;
61 $question = $this->object_loader->getQuestion($question_index);
62
63 $this->checkQuestionExists($question, $question_index);
64 //$this->checkQuestionIsReachable($question, $question_index);
65
66 if($this->isResultOfAnswerExpression($question_expression))
67 {
68 $answer_index = $question_expression->getAnswerIndex()-1;
69 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index);
70 }
71 if($answer_expression instanceof ilAssLacNumberOfResultExpression && !($question instanceof assClozeTest))
72 {
73 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_expression->getNumericValue()-1);
74 }
75
76 $this->checkAnswerExpressionExist($question->getExpressionTypes(), $answer_expression, $question_index);
77 $this->checkOperatorExistForExpression($question->getOperators($answer_expression::$identifier), $answer_expression, $composite::$pattern);
78
79 if($answer_expression instanceof ilAssLacOrderingResultExpression &&
80 ($question instanceof assOrderingHorizontal || $question instanceof assOrderingQuestion )
81 )
82 {
83 foreach($answer_expression->getOrdering() as $order)
84 {
85 $count = 0;
86 foreach($answer_expression->getOrdering() as $element)
87 {
88 if($element == $order)
89 {
90 $count++;
91 }
92 }
93 if($count > 1)
94 {
95 throw new ilAssLacDuplicateElement($order);
96 }
97
98 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $order-1);
99 }
100 }
101 if($question instanceof assClozeTest)
102 {
103 $this->validateClozeTest($answer_index, $question, $answer_expression, $question_index);
104 }
105 elseif(
106 $answer_expression instanceof ilAssLacPercentageResultExpression &&
107 $this->isResultOfAnswerExpression($question_expression) &&
108 !($question instanceof assFormulaQuestion)
109 )
110 {
111 throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index . "[". ($answer_index+1) . "]");
112 }
113 }
114 elseif(
115 ($composite->nodes[0] instanceof ilAssLacAbstractOperation &&
116 $composite->nodes[1] instanceof ilAssLacExpressionInterface) ||
117 ($composite->nodes[0] instanceof ilAssLacExpressionInterface &&
118 $composite->nodes[1] instanceof ilAssLacAbstractOperation) ||
119 ($composite->nodes[0] instanceof ilAssLacSolutionExpressionInterface)
120 )
121 {
122 throw new ilAssLacUnableToParseCondition("");
123 }
124 }
125
134 private function validateClozeTest($answer_index, $question, $answer_expression, $question_index)
135 {
136 if($answer_index !== null)
137 {
138 $options = $question->getAvailableAnswerOptions($answer_index);
139 $found = false;
140 switch($options->getType())
141 {
142 case 0: // text
143 if(
144 $answer_expression instanceof ilAssLacStringResultExpression
145 )
146 {
147 $found = true;
148 }
149
150 break;
151 case 1: // select
152
153 if($answer_expression instanceof ilAssLacStringResultExpression)
154 {
155 foreach($options->getItems($this->getNonShuffler()) as $item)
156 {
157 if($item->getAnswertext() == $answer_expression->getText())
158 {
159 $found = true;
160 }
161 }
162 }
163 elseif($answer_expression instanceof ilAssLacNumberOfResultExpression)
164 {
165
166 foreach($options->getItems($question->getShuffler()) as $item)
167 {
168 if($item->getOrder() == $answer_expression->getNumericValue()-1)
169 {
170 $found = true;
171 }
172 }
173 }
174 break;
175 case 2: // numeric
176 if($answer_expression instanceof ilAssLacNumericResultExpression)
177 {
178 $found = true;
179 }
180 break;
181 }
182
183 if($answer_expression instanceof ilAssLacEmptyAnswerExpression)
184 {
185 $found = true;
186 }
187 if(!$found && !($answer_expression instanceof ilAssLacPercentageResultExpression))
188 {
189 throw new ilAssLacAnswerValueNotExist($question_index, $answer_expression->getValue(), $answer_index+1);
190 }
191
192 }
193 }
194
202 private function checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index)
203 {
204 $answer_options = $question->getAvailableAnswerOptions($answer_index);
205 if($answer_options == null)
206 {
207 throw new ilAssLacAnswerIndexNotExist($question_index, $answer_index+1);
208 }
209 }
210
217 private function checkQuestionExists($question, $index)
218 {
219 if($question == null)
220 {
221 throw new ilAssLacQuestionNotExist($index);
222 }
223 }
224
230 private function isResultOfAnswerExpression($expression)
231 {
232 if( $expression instanceof ilAssLacResultOfAnswerOfQuestionExpression )
233 {
234 return true;
235 }
236
237 if( $expression instanceof ilAssLacResultOfAnswerOfCurrentQuestionExpression )
238 {
239 return true;
240 }
241
242 return false;
243 }
244
252 private function checkAnswerExpressionExist($expressions, $answer_expression, $question_index)
253 {
254 if(!in_array($answer_expression::$identifier, $expressions))
255 {
256 throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index);
257 }
258 }
259
267 private function checkOperatorExistForExpression($operators, $answer_expression, $pattern)
268 {
269 if(!in_array($pattern, $operators))
270 {
271 throw new ilAssLacOperatorNotSupportedByExpression($answer_expression->getValue(),$pattern);
272 }
273 }
274
275 protected function getNonShuffler()
276 {
277 require_once 'Services/Randomization/classes/class.ilArrayElementOrderKeeper.php';
278 return new ilArrayElementOrderKeeper();
279 }
280}
281
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.
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)
Class NumberOfResultExpression fot the expression +n+.
Class NumericResultExpression for the expression #n#.
Class OrderingResultExpression for the expression $a,..,n,m$.
Class PercentageResultExpression for the expression n%.
Class ResultOfAnswerOfCurrentQuestion for the expression R[m].
Class ResultOfAnswerOfQuestion for the expression Qn[m].
Class StringResultExpression for the expression ~TEXT~.
if(!is_array($argv)) $options