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