ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAssQuestionHintList.php
Go to the documentation of this file.
1 <?php
2 
29 {
31  private array $questionHints = [];
32 
33  public function current(): ilAssQuestionHint
34  {
35  return current($this->questionHints);
36  }
37 
38  public function rewind(): void
39  {
40  reset($this->questionHints);
41  }
42 
43  public function next(): void
44  {
45  next($this->questionHints);
46  }
47 
48  public function key(): int
49  {
50  return key($this->questionHints);
51  }
52 
53  public function valid(): bool
54  {
55  return key($this->questionHints) !== null;
56  }
57 
58  public function __construct()
59  {
60  }
61 
62  public function addHint(ilAssQuestionHint $questionHint): void
63  {
64  $this->questionHints[] = $questionHint;
65  }
66 
70  public function getHint($hintId): ilAssQuestionHint
71  {
72  foreach ($this as $questionHint) {
73  /* @var $questionHint ilAssQuestionHint */
74 
75  if ($questionHint->getId() == $hintId) {
76  return $questionHint;
77  }
78  }
79 
80  throw new ilTestQuestionPoolException("hint with id $hintId does not exist in this list");
81  }
82 
86  public function hintExists($hintId): bool
87  {
88  foreach ($this as $questionHint) {
89  /* @var ilAssQuestionHint $questionHint */
90  if ($questionHint->getId() == $hintId) {
91  return true;
92  }
93  }
94 
95  return false;
96  }
97 
104  public function reIndex(): void
105  {
106  $counter = 0;
107 
108  foreach ($this as $questionHint) {
109  /* @var $questionHint ilAssQuestionHint */
110 
111  $questionHint->setIndex(++$counter);
112  $questionHint->save();
113  }
114  }
115 
124  public static function duplicateListForQuestion($originalQuestionId, $duplicateQuestionId): array
125  {
126  $hintIds = [];
127 
128  $questionHintList = self::getListByQuestionId($originalQuestionId);
129 
130  foreach ($questionHintList as $questionHint) {
131  /* @var $questionHint ilAssQuestionHint */
132 
133  $originalHintId = $questionHint->getId();
134 
135  $questionHint->setId(0);
136  $questionHint->setQuestionId($duplicateQuestionId);
137 
138  $questionHint->save();
139 
140  $duplicateHintId = $questionHint->getId();
141 
142  $hintIds[$originalHintId] = $duplicateHintId;
143  }
144 
145  return $hintIds;
146  }
147 
153  public function getTableData(): array
154  {
155  $tableData = [];
156 
157  foreach ($this as $questionHint) {
158  /* @var $questionHint ilAssQuestionHint */
159 
160  $tableData[] = [
161  'hint_id' => $questionHint->getId(),
162  'hint_index' => $questionHint->getIndex(),
163  'hint_points' => $questionHint->getPoints(),
164  'hint_text' => $questionHint->getText()
165  ];
166  }
167 
168  return $tableData;
169  }
170 
175  public static function getListByQuestionId($questionId): ilAssQuestionHintList
176  {
177  global $DIC;
178  $ilDB = $DIC->database();
179 
180  $query = "
181  SELECT qht_hint_id,
182  qht_question_fi,
183  qht_hint_index,
184  qht_hint_points,
185  qht_hint_text
186 
187  FROM qpl_hints
188 
189  WHERE qht_question_fi = %s
190 
191  ORDER BY qht_hint_index ASC
192  ";
193 
194  $res = $ilDB->queryF(
195  $query,
196  ['integer'],
197  [(int) $questionId]
198  );
199 
200  $questionHintList = new self();
201 
202  while ($row = $ilDB->fetchAssoc($res)) {
203  $questionHint = new ilAssQuestionHint();
204 
205  ilAssQuestionHint::assignDbRow($questionHint, $row);
206 
207  $questionHintList->addHint($questionHint);
208  }
209 
210  return $questionHintList;
211  }
212 
217  public static function getListByHintIds($hintIds): ilAssQuestionHintList
218  {
219  global $DIC;
220  $ilDB = $DIC->database();
221 
222  $qht_hint_id__IN__hintIds = $ilDB->in('qht_hint_id', (array) $hintIds, false, 'integer');
223 
224  $query = "
225  SELECT qht_hint_id,
226  qht_question_fi,
227  qht_hint_index,
228  qht_hint_points,
229  qht_hint_text
230 
231  FROM qpl_hints
232 
233  WHERE $qht_hint_id__IN__hintIds
234 
235  ORDER BY qht_hint_index ASC
236  ";
237 
238  $res = $ilDB->query($query);
239 
240  $questionHintList = new self();
241 
242  while ($row = $ilDB->fetchAssoc($res)) {
243  $questionHint = new ilAssQuestionHint();
244 
245  ilAssQuestionHint::assignDbRow($questionHint, $row);
246 
247  $questionHintList->addHint($questionHint);
248  }
249 
250  return $questionHintList;
251  }
252 
259  public static function getNextIndexByQuestionId($questionId): int
260  {
261  global $DIC;
262  $ilDB = $DIC->database();
263 
264  $query = "
265  SELECT 1 + COALESCE( MAX(qht_hint_index), 0 ) next_index
266 
267  FROM qpl_hints
268 
269  WHERE qht_question_fi = %s
270  ";
271 
272  $res = $ilDB->queryF(
273  $query,
274  ['integer'],
275  [(int) $questionId]
276  );
277  $row = $ilDB->fetchAssoc($res);
278 
279  return is_array($row) ? (int) $row['next_index'] : 1;
280  }
281 
286  public static function deleteHintsByQuestionIds(array $questionIds): int
287  {
288  global $DIC;
289  $ilDB = $DIC->database();
290 
291  $__qht_question_fi__IN__questionIds = $ilDB->in('qht_question_fi', $questionIds, false, 'integer');
292 
293  $query = "
294  DELETE FROM qpl_hints
295  WHERE $__qht_question_fi__IN__questionIds
296  ";
297 
298  return $ilDB->manipulate($query);
299  }
300 }
static getListByQuestionId($questionId)
instantiates a question hint list for the passed question id
$res
Definition: ltiservices.php:66
static deleteHintsByQuestionIds(array $questionIds)
Deletes all question hints relating to questions included in given question ids.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getListByHintIds($hintIds)
instantiates a question hint list for the passed hint 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...
global $DIC
Definition: shib_login.php:22
static duplicateListForQuestion($originalQuestionId, $duplicateQuestionId)
duplicates a hint list from given original question id to given duplicate question id and returns an ...
reIndex()
re-indexes the list&#39;s hints sequentially by current order (starting with index "1") ATTENTION: it als...
static assignDbRow(self $questionHint, $hintDbRow)
assigns the field elements of passed hint db row array to the corresponding hint object properties of...
addHint(ilAssQuestionHint $questionHint)
getTableData()
returns an array with data of the hints in this list that is adopted to be used as table gui data ...