ILIAS  release_8 Revision v8.24
class.ilTestRandomQuestionSetQuestionCollection.php
Go to the documentation of this file.
1<?php
2
25// hey: fixRandomTestBuildable - iterator interface for collection
27 Iterator
28// hey.
29{
30 private $questions = array();
31
32 public function setQuestions($questions)
33 {
34 $this->questions = $questions;
35 }
36
37 public function getQuestions(): array
38 {
39 return $this->questions;
40 }
41
43 {
44 $this->questions[] = $question;
45 }
46
50 public function current()
51 {
52 return current($this->questions);
53 }
54
58 public function next()
59 {
60 return next($this->questions);
61 }
62
63 public function key(): string
64 {
65 return key($this->questions);
66 }
67
68 public function valid(): bool
69 {
70 return key($this->questions) !== null;
71 }
72
76 public function rewind()
77 {
78 return reset($this->questions);
79 }
80 // hey.
81
82 public function isGreaterThan($amount): bool
83 {
84 return count($this->questions) > $amount;
85 }
86
87 public function isSmallerThan($amount): bool
88 {
89 return count($this->questions) < $amount;
90 }
91
95 public function getMissingCount($requiredAmount): int
96 {
97 // hey: fixRandomTestBuildable - fix returning missing count instead of difference (neg values!)
98 $difference = $requiredAmount - count($this->questions);
99 $missingCount = $difference < 0 ? 0 : $difference;
100 return $missingCount;
101 // hey.
102 }
103
104 public function shuffleQuestions()
105 {
106 shuffle($this->questions);
107 }
108
109 public function mergeQuestionCollection(self $questionCollection)
110 {
111 $this->questions = array_merge($this->questions, $questionCollection->getQuestions());
112 }
113
115 {
116 $uniqueQuestions = array();
117
118 foreach ($this->getQuestions() as $question) {
119 /* @var ilTestRandomQuestionSetQuestion $question */
120
121 if (!isset($uniqueQuestions[$question->getQuestionId()])) {
122 $uniqueQuestions[$question->getQuestionId()] = $question;
123 }
124 }
125
126 $uniqueQuestionCollection = new self();
127 $uniqueQuestionCollection->setQuestions($uniqueQuestions);
128
129 return $uniqueQuestionCollection;
130 }
131
133 {
134 // hey: fixRandomTestBuildable - comment for refactoring
144 // hey.
145
146 $questionIds = array_flip($questionCollection->getInvolvedQuestionIds());
147
148 $relativeComplementCollection = new self();
149
150 foreach ($this->getQuestions() as $question) {
151 if (!isset($questionIds[$question->getQuestionId()])) {
152 $relativeComplementCollection->addQuestion($question);
153 }
154 }
155
156 return $relativeComplementCollection;
157 }
158
160 {
161 $questionIds = array_flip($questionCollection->getInvolvedQuestionIds());
162
163 $intersectionCollection = new self();
164
165 foreach ($this->getQuestions() as $question) {
166 if (!isset($questionIds[$question->getQuestionId()])) {
167 continue;
168 }
169
170 $intersectionCollection->addQuestion($question);
171 }
172
173 return $intersectionCollection;
174 }
175
176 public function getQuestionAmount(): int
177 {
178 return count($this->getQuestions());
179 }
180 // hey.
181
182 public function getInvolvedQuestionIds(): array
183 {
184 $questionIds = array();
185
186 foreach ($this->getQuestions() as $question) {
187 $questionIds[] = $question->getQuestionId();
188 }
189
190 return $questionIds;
191 }
192
194 {
195 $randomKeys = $this->getRandomArrayKeys($this->questions, $requiredAmount);
196
197 $randomQuestionCollection = new self();
198
199 foreach ($randomKeys as $randomKey) {
200 $randomQuestionCollection->addQuestion($this->questions[$randomKey]);
201 }
202
203 return $randomQuestionCollection;
204 }
205
206 private function getRandomArrayKeys($array, $numKeys)
207 {
208 if ($numKeys < 1) {
209 return array();
210 }
211
212 if ($numKeys > 1) {
213 return array_rand($array, $numKeys);
214 }
215
216 return array( array_rand($array, $numKeys) );
217 }
218}