ILIAS  release_8 Revision v8.24
class.ilAssQuestionHint.php
Go to the documentation of this file.
1<?php
18require_once 'Modules/TestQuestionPool/exceptions/class.ilTestQuestionPoolException.php';
19
29{
30 public const PAGE_OBJECT_TYPE = 'qht';
31
38 private $id = null;
39
46 private $questionId = null;
47
55 private $index = null;
56
64 private $points = null;
65
72 private $text = null;
73
79 public function __construct()
80 {
81 }
82
89 public function getId(): ?int
90 {
91 return $this->id;
92 }
93
100 public function setId($id): void
101 {
102 $this->id = (int) $id;
103 }
104
111 public function getQuestionId(): ?int
112 {
113 return $this->questionId;
114 }
115
122 public function setQuestionId($questionId): void
123 {
124 $this->questionId = (int) $questionId;
125 }
126
133 public function getIndex(): ?int
134 {
135 return $this->index;
136 }
137
144 public function setIndex($index): void
145 {
146 $this->index = (int) $index;
147 }
148
155 public function getPoints(): ?float
156 {
157 return $this->points;
158 }
159
166 public function setPoints($points): void
167 {
168 $this->points = abs((float) $points);
169 }
170
177 public function getText(): ?string
178 {
179 return $this->text;
180 }
181
188 public function setText($text): void
189 {
190 if ($text !== null) {
191 $this->text = $this->getHtmlQuestionContentPurifier()->purify($text);
192 }
193 }
194
204 public function load($id): bool
205 {
206 global $DIC;
207 $ilDB = $DIC['ilDB'];
208
209 $query = "
210 SELECT qht_hint_id,
211 qht_question_fi,
212 qht_hint_index,
213 qht_hint_points,
214 qht_hint_text
215
216 FROM qpl_hints
217
218 WHERE qht_hint_id = %s
219 ";
220
221 $res = $ilDB->queryF(
222 $query,
223 array('integer'),
224 array((int) $id)
225 );
226
227 while ($row = $ilDB->fetchAssoc($res)) {
228 self::assignDbRow($this, $row);
229
230 return true;
231 }
232
233 return false;
234 }
235
245 public function save(): bool
246 {
247 if ($this->getId()) {
248 return $this->update();
249 } else {
250 return $this->insert();
251 }
252 }
253
262 private function update(): bool
263 {
264 global $DIC;
265 $ilDB = $DIC['ilDB'];
266
267 return $ilDB->update(
268 'qpl_hints',
269 array(
270 'qht_question_fi' => array('integer', $this->getQuestionId()),
271 'qht_hint_index' => array('integer', $this->getIndex()),
272 'qht_hint_points' => array('float', $this->getPoints()),
273 'qht_hint_text' => array('clob', $this->getText())
274 ),
275 array(
276 'qht_hint_id' => array('integer', $this->getId())
277 )
278 );
279 }
280
289 private function insert(): bool
290 {
291 global $DIC;
292 $ilDB = $DIC['ilDB'];
293
294 $this->setId($ilDB->nextId('qpl_hints'));
295
296 return $ilDB->insert('qpl_hints', array(
297 'qht_hint_id' => array('integer', $this->getId()),
298 'qht_question_fi' => array('integer', $this->getQuestionId()),
299 'qht_hint_index' => array('integer', $this->getIndex()),
300 'qht_hint_points' => array('float', $this->getPoints()),
301 'qht_hint_text' => array('clob', $this->getText())
302 ));
303 }
304
311 public function delete(): int
312 {
313 return self::deleteById($this->getId());
314 }
315
325 public static function assignDbRow(self $questionHint, $hintDbRow): void
326 {
327 foreach ($hintDbRow as $field => $value) {
328 switch ($field) {
329 case 'qht_hint_id': $questionHint->setId($value); break;
330 case 'qht_question_fi': $questionHint->setQuestionId($value); break;
331 case 'qht_hint_index': $questionHint->setIndex($value); break;
332 case 'qht_hint_points': $questionHint->setPoints($value); break;
333 case 'qht_hint_text': $questionHint->setText($value); break;
334
335 default: throw new ilTestQuestionPoolException("invalid db field identifier ($field) given!");
336 }
337 }
338 }
339
350 public static function deleteById($hintId): int
351 {
352 global $DIC;
353 $ilDB = $DIC['ilDB'];
354
355 $query = "
356 DELETE FROM qpl_hints
357 WHERE qht_hint_id = %s
358 ";
359
360 return $ilDB->manipulateF(
361 $query,
362 array('integer'),
363 array($hintId)
364 );
365 }
366
376 public static function getInstanceById($hintId): ilAssQuestionHint
377 {
378 $questionHint = new self();
379 $questionHint->load($hintId);
380 return $questionHint;
381 }
382
383 public function getPageObjectType(): string
384 {
386 }
387
388 public static function getHintIndexLabel(ilLanguage $lng, $hintIndex): string
389 {
390 return sprintf($lng->txt('tst_question_hints_index_column_label'), $hintIndex);
391 }
392
394 {
395 return ilHtmlPurifierFactory::getInstanceByType('qpl_usersolution');
396 }
397}
getPoints()
returns the points to ground-off for this hint
save()
saves the current hint object state to database.
static deleteById($hintId)
deletes the persisted hint object in database by deleting the hint dataset identified by hint id
static assignDbRow(self $questionHint, $hintDbRow)
assigns the field elements of passed hint db row array to the corresponding hint object properties of...
update()
persists the current object state to database by updating an existing dataset identified by hint id
getText()
returns the hint text
insert()
persists the current object state to database by inserting a new dataset with a new hint id fetched f...
getQuestionId()
returns the question id the hint currently relates to
setId($id)
sets the passed hint id
static getInstanceById($hintId)
creates a hint object instance, loads the persisted hint dataset identified by passed hint id from da...
load($id)
loads the hint dataset with passed id from database and assigns it the to this hint object instance
getIndex()
returns the ordering index of hint
setQuestionId($questionId)
sets the passed question id so hint relates to it
setText($text)
sets the passed hint text
setPoints($points)
sets the passed points to ground-off for this hint
setIndex($index)
sets the passed hint ordering index
getId()
returns the hint id
static getHintIndexLabel(ilLanguage $lng, $hintIndex)
static getInstanceByType(string $type)
language handling
global $DIC
Definition: feed.php:28
$res
Definition: ltiservices.php:69
$query
$lng