ILIAS  release_8 Revision v8.24
class.ilAssQuestionHintList.php
Go to the documentation of this file.
1<?php
2
27class ilAssQuestionHintList implements Iterator
28{
35 private $questionHints = array();
36
43 public function current()
44 {
45 return current($this->questionHints);
46 }
47
54 public function rewind()
55 {
56 return reset($this->questionHints);
57 }
58
65 public function next()
66 {
67 return next($this->questionHints);
68 }
69
76 public function key()
77 {
78 return key($this->questionHints);
79 }
80
87 public function valid(): bool
88 {
89 return key($this->questionHints) !== null;
90 }
91
97 public function __construct()
98 {
99 }
100
107 public function addHint(ilAssQuestionHint $questionHint): void
108 {
109 $this->questionHints[] = $questionHint;
110 }
111
119 public function getHint($hintId): ilAssQuestionHint
120 {
121 foreach ($this as $questionHint) {
122 /* @var $questionHint ilAssQuestionHint */
123
124 if ($questionHint->getId() == $hintId) {
125 return $questionHint;
126 }
127 }
128
129 require_once 'Modules/TestQuestionPool/exceptions/class.ilTestQuestionPoolException.php';
130 throw new ilTestQuestionPoolException("hint with id $hintId does not exist in this list");
131 }
132
141 public function hintExists($hintId): bool
142 {
143 foreach ($this as $questionHint) {
144 /* @var $questionHint ilAssQuestionHint */
145
146 if ($questionHint->getId() == $hintId) {
147 return true;
148 }
149 }
150
151 return false;
152 }
153
163 public function reIndex(): void
164 {
165 $counter = 0;
166
167 foreach ($this as $questionHint) {
168 /* @var $questionHint ilAssQuestionHint */
169
170 $questionHint->setIndex(++$counter);
171 $questionHint->save();
172 }
173 }
174
184 public static function duplicateListForQuestion($originalQuestionId, $duplicateQuestionId): array
185 {
186 $hintIds = array();
187
188 $questionHintList = self::getListByQuestionId($originalQuestionId);
189
190 foreach ($questionHintList as $questionHint) {
191 /* @var $questionHint ilAssQuestionHint */
192
193 $originalHintId = $questionHint->getId();
194
195 $questionHint->setId(null);
196 $questionHint->setQuestionId($duplicateQuestionId);
197
198 $questionHint->save();
199
200 $duplicateHintId = $questionHint->getId();
201
202 $hintIds[$originalHintId] = $duplicateHintId;
203 }
204
205 return $hintIds;
206 }
207
215 public function getTableData(): array
216 {
217 $tableData = array();
218
219 foreach ($this as $questionHint) {
220 /* @var $questionHint ilAssQuestionHint */
221
222 $tableData[] = array(
223 'hint_id' => $questionHint->getId(),
224 'hint_index' => $questionHint->getIndex(),
225 'hint_points' => $questionHint->getPoints(),
226 'hint_text' => $questionHint->getText()
227 );
228 }
229
230 return $tableData;
231 }
232
242 public static function getListByQuestionId($questionId): ilAssQuestionHintList
243 {
244 global $DIC;
245 $ilDB = $DIC['ilDB'];
246
247 $query = "
248 SELECT qht_hint_id,
249 qht_question_fi,
250 qht_hint_index,
251 qht_hint_points,
252 qht_hint_text
253
254 FROM qpl_hints
255
256 WHERE qht_question_fi = %s
257
258 ORDER BY qht_hint_index ASC
259 ";
260
261 $res = $ilDB->queryF(
262 $query,
263 array('integer'),
264 array((int) $questionId)
265 );
266
267 $questionHintList = new self();
268
269 while ($row = $ilDB->fetchAssoc($res)) {
270 $questionHint = new ilAssQuestionHint();
271
272 ilAssQuestionHint::assignDbRow($questionHint, $row);
273
274 $questionHintList->addHint($questionHint);
275 }
276
277 return $questionHintList;
278 }
279
289 public static function getListByHintIds($hintIds): ilAssQuestionHintList
290 {
291 global $DIC;
292 $ilDB = $DIC['ilDB'];
293
294 $qht_hint_id__IN__hintIds = $ilDB->in('qht_hint_id', (array) $hintIds, false, 'integer');
295
296 $query = "
297 SELECT qht_hint_id,
298 qht_question_fi,
299 qht_hint_index,
300 qht_hint_points,
301 qht_hint_text
302
303 FROM qpl_hints
304
305 WHERE $qht_hint_id__IN__hintIds
306
307 ORDER BY qht_hint_index ASC
308 ";
309
310 $res = $ilDB->query($query);
311
312 $questionHintList = new self();
313
314 while ($row = $ilDB->fetchAssoc($res)) {
315 $questionHint = new ilAssQuestionHint();
316
317 ilAssQuestionHint::assignDbRow($questionHint, $row);
318
319 $questionHintList->addHint($questionHint);
320 }
321
322 return $questionHintList;
323 }
324
336 public static function getNextIndexByQuestionId($questionId): int
337 {
338 global $DIC;
339 $ilDB = $DIC['ilDB'];
340
341 $query = "
342 SELECT 1 + COALESCE( MAX(qht_hint_index), 0 ) next_index
343
344 FROM qpl_hints
345
346 WHERE qht_question_fi = %s
347 ";
348
349 $res = $ilDB->queryF(
350 $query,
351 array('integer'),
352 array((int) $questionId)
353 );
354
355 $row = $ilDB->fetchAssoc($res);
356
357 return $row['next_index'];
358 }
359
366 public static function deleteHintsByQuestionIds($questionIds)
367 {
368 global $DIC;
369 $ilDB = $DIC['ilDB'];
370
371 $__qht_question_fi__IN__questionIds = $ilDB->in('qht_question_fi', $questionIds, false, 'integer');
372
373 $query = "
374 DELETE FROM qpl_hints
375 WHERE $__qht_question_fi__IN__questionIds
376 ";
377
378 return $ilDB->manipulate($query);
379 }
380}
rewind()
iterator interface method
getHint($hintId)
returns the question hint object relating to the passed hint id
key()
iterator interface method
current()
iterator interface method
valid()
iterator interface method
static deleteHintsByQuestionIds($questionIds)
Deletes all question hints relating to questions included in given question ids.
static getNextIndexByQuestionId($questionId)
determines the next index to be used for a new hint that is to be added to the list of existing hints...
hintExists($hintId)
checks wether a question hint object relating to the passed id exists or not
getTableData()
returns an array with data of the hints in this list that is adopted to be used as table gui data
reIndex()
re-indexes the list's hints sequentially by current order (starting with index "1")
next()
iterator interface method
addHint(ilAssQuestionHint $questionHint)
adds a question hint object to the current list instance
static duplicateListForQuestion($originalQuestionId, $duplicateQuestionId)
duplicates a hint list from given original question id to given duplicate question id and returns an ...
static getListByHintIds($hintIds)
instantiates a question hint list for the passed hint ids
static getListByQuestionId($questionId)
instantiates a question hint list for the passed question id
static assignDbRow(self $questionHint, $hintDbRow)
assigns the field elements of passed hint db row array to the corresponding hint object properties of...
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query