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