ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $this->validate($composite->nodes[0]);
44 $this->validate($composite->nodes[1]);
45 $this->validateSubTree($composite);
46 }
47
48 return;
49 }
50
51 private function validateSubTree(ilAssLacAbstractComposite $composite)
52 {
53 if ($composite->nodes[0] instanceof ilAssLacQuestionExpressionInterface &&
54 $composite->nodes[1] instanceof ilAssLacSolutionExpressionInterface
55 ) {
56 $question_expression = $composite->nodes[0];
57 $answer_expression = $composite->nodes[1];
58 $question_index = $composite->nodes[0]->getQuestionIndex();
59 $answer_index = null;
60 $question = $this->object_loader->getQuestion($question_index);
61
62 $this->checkQuestionExists($question, $question_index);
63 //$this->checkQuestionIsReachable($question, $question_index);
64
65 if ($this->isResultOfAnswerExpression($question_expression)) {
66 $answer_index = $question_expression->getAnswerIndex()-1;
67 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index);
68 }
69 if ($answer_expression instanceof ilAssLacNumberOfResultExpression && !($question instanceof assClozeTest)) {
70 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_expression->getNumericValue()-1);
71 }
72
73 $this->checkAnswerExpressionExist($question->getExpressionTypes(), $answer_expression, $question_index);
74 $this->checkOperatorExistForExpression($question->getOperators($answer_expression::$identifier), $answer_expression, $composite::$pattern);
75
76 if ($answer_expression instanceof ilAssLacOrderingResultExpression &&
77 ($question instanceof assOrderingHorizontal || $question instanceof assOrderingQuestion)
78 ) {
79 foreach ($answer_expression->getOrdering() as $order) {
80 $count = 0;
81 foreach ($answer_expression->getOrdering() as $element) {
82 if ($element == $order) {
83 $count++;
84 }
85 }
86 if ($count > 1) {
87 throw new ilAssLacDuplicateElement($order);
88 }
89
90 $this->checkIfAnswerIndexOfQuestionExists($question, $question_index, $order-1);
91 }
92 }
93 if ($question instanceof assClozeTest) {
94 $this->validateClozeTest($answer_index, $question, $answer_expression, $question_index);
95 } elseif (
96 $answer_expression instanceof ilAssLacPercentageResultExpression &&
97 $this->isResultOfAnswerExpression($question_expression) &&
98 !($question instanceof assFormulaQuestion)
99 ) {
100 throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index . "[" . ($answer_index+1) . "]");
101 }
102 } elseif (
103 ($composite->nodes[0] instanceof ilAssLacAbstractOperation &&
104 $composite->nodes[1] instanceof ilAssLacExpressionInterface) ||
105 ($composite->nodes[0] instanceof ilAssLacExpressionInterface &&
106 $composite->nodes[1] instanceof ilAssLacAbstractOperation) ||
107 ($composite->nodes[0] instanceof ilAssLacSolutionExpressionInterface)
108 ) {
109 throw new ilAssLacUnableToParseCondition("");
110 }
111 }
112
121 private function validateClozeTest($answer_index, $question, $answer_expression, $question_index)
122 {
123 if ($answer_index !== null) {
124 $options = $question->getAvailableAnswerOptions($answer_index);
125 $found = false;
126 switch ($options->getType()) {
127 case 0: // text
128 if (
129 $answer_expression instanceof ilAssLacStringResultExpression
130 ) {
131 $found = true;
132 }
133
134 break;
135 case 1: // select
136
137 if ($answer_expression instanceof ilAssLacStringResultExpression) {
138 foreach ($options->getItems($this->getNonShuffler()) as $item) {
139 if ($item->getAnswertext() == $answer_expression->getText()) {
140 $found = true;
141 }
142 }
143 } elseif ($answer_expression instanceof ilAssLacNumberOfResultExpression) {
144 foreach ($options->getItems($question->getShuffler()) as $item) {
145 if ($item->getOrder() == $answer_expression->getNumericValue()-1) {
146 $found = true;
147 }
148 }
149 }
150 break;
151 case 2: // numeric
152 if ($answer_expression instanceof ilAssLacNumericResultExpression) {
153 $found = true;
154 }
155 break;
156 }
157
158 if ($answer_expression instanceof ilAssLacEmptyAnswerExpression) {
159 $found = true;
160 }
161 if (!$found && !($answer_expression instanceof ilAssLacPercentageResultExpression)) {
162 throw new ilAssLacAnswerValueNotExist($question_index, $answer_expression->getValue(), $answer_index+1);
163 }
164 }
165 }
166
174 private function checkIfAnswerIndexOfQuestionExists($question, $question_index, $answer_index)
175 {
176 $answer_options = $question->getAvailableAnswerOptions($answer_index);
177 if ($answer_options == null) {
178 throw new ilAssLacAnswerIndexNotExist($question_index, $answer_index+1);
179 }
180 }
181
188 private function checkQuestionExists($question, $index)
189 {
190 if ($question == null) {
192 }
193 }
194
200 private function isResultOfAnswerExpression($expression)
201 {
202 if ($expression instanceof ilAssLacResultOfAnswerOfQuestionExpression) {
203 return true;
204 }
205
206 if ($expression instanceof ilAssLacResultOfAnswerOfCurrentQuestionExpression) {
207 return true;
208 }
209
210 return false;
211 }
212
220 private function checkAnswerExpressionExist($expressions, $answer_expression, $question_index)
221 {
222 if (!in_array($answer_expression::$identifier, $expressions)) {
223 throw new ilAssLacExpressionNotSupportedByQuestion($answer_expression->getValue(), $question_index);
224 }
225 }
226
234 private function checkOperatorExistForExpression($operators, $answer_expression, $pattern)
235 {
236 if (!in_array($pattern, $operators)) {
237 throw new ilAssLacOperatorNotSupportedByExpression($answer_expression->getValue(), $pattern);
238 }
239 }
240
241 protected function getNonShuffler()
242 {
243 require_once 'Services/Randomization/classes/class.ilArrayElementOrderKeeper.php';
244 return new ilArrayElementOrderKeeper();
245 }
246}
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
An exception for terminatinating execution or to throw for unit testing.
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~.
$index
Definition: metadata.php:60