ILIAS  release_7 Revision v7.30-3-g800a261c036
assOrderingQuestionDatabaseRepository.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
9{
10 const TABLE_NAME_BASE = 'qpl_questions';
11 const TABLE_NAME_QUESTIONS = 'qpl_qst_ordering';
12 const TABLE_NAME_ANSWERS = 'qpl_a_ordering';
13
14 protected $db;
15
16 public function __construct(\ilDBInterface $db)
17 {
18 $this->db = $db;
19 }
20
21 public function getOrderingList(int $question_id) : \ilAssOrderingElementList
22 {
23 return $this->buildOrderingList($question_id);
24 }
25
26 public function updateOrderingList(\ilAssOrderingElementList $list) : void
27 {
28 $atom_query = $this->db->buildAtomQuery();
29 $atom_query->addTableLock(self::TABLE_NAME_ANSWERS);
30 $atom_query->addTableLock(self::TABLE_NAME_ANSWERS . '_seq');
31
32 $atom_query->addQueryCallable(
33 function (\ilDBInterface $db) use ($list) {
35 foreach ($list->getElements() as $order_element) {
36 $this->insertOrderingElement($order_element, $list->getQuestionId());
37 }
38 }
39 );
40 $atom_query->run();
41 }
42
43 protected function buildOrderingList(
44 int $question_id
46 $elements = $this->getOrderingElementsForList($question_id);
47 return new \ilAssOrderingElementList($question_id, $elements);
48 }
49
53 protected function getOrderingElementsForList(int $question_id) : array
54 {
55 $query = 'SELECT' . PHP_EOL
56 . 'answer_id, answertext, solution_key, random_id, depth, position' . PHP_EOL
57 . 'FROM ' . self::TABLE_NAME_ANSWERS . PHP_EOL
58 . 'WHERE question_fi=' . $question_id . PHP_EOL
59 . 'ORDER BY position ASC';
60
61 $elements = [];
62 $res = $this->db->query($query);
63 while ($row = $this->db->fetchAssoc($res)) {
64 $elements[] = $this->buildOrderingElement(
65 (int) $row['answer_id'],
66 (int) $row['random_id'],
67 (int) $row['solution_key'],
68 (int) $row['position'],
69 (int) $row['depth'],
70 (string) $row['answertext']
71 );
72 }
73 return $elements;
74 }
75
76 protected function deleteOrderingElements(int $question_id) : void
77 {
78 $query = 'DELETE FROM ' . self::TABLE_NAME_ANSWERS . PHP_EOL
79 . 'WHERE question_fi = ' . $question_id;
80 $this->db->manipulate($query);
81 }
82
83 protected function insertOrderingElement(\ilAssOrderingElement $order_element, int $question_id) : void
84 {
85 $next_id = $this->db->nextId(self::TABLE_NAME_ANSWERS);
86 $values = array(
87 'answer_id' => ['integer', $next_id],
88 'question_fi' => ['integer', $question_id],
89 'answertext' => ['text', $order_element->getContent()],
90 'solution_key' => ['integer', $order_element->getSolutionIdentifier()],
91 'random_id' => ['integer', $order_element->getRandomIdentifier()],
92 'position' => ['integer', $order_element->getPosition()],
93 'depth' => ['integer', $order_element->getIndentation()],
94 'tstamp' => ['integer', $this->getTime()]
95 );
96 $this->db->insert(self::TABLE_NAME_ANSWERS, $values);
97 }
98
99 protected function getTime()
100 {
101 return time();
102 }
103
104 protected function buildOrderingElement(
105 int $answer_id,
106 int $random_identifier,
107 int $solution_identifier,
108 int $position,
109 int $indentation,
110 string $content
112 return (new \ilAssOrderingElement($answer_id))
113 ->withRandomIdentifier($random_identifier)
114 ->withSolutionIdentifier($solution_identifier)
115 ->withPosition($position)
116 ->withIndentation($indentation)
117 ->withContent($content);
118 }
119}
An exception for terminatinating execution or to throw for unit testing.
repository for assOrderingQuestion (the answer elements within, at least...)
insertOrderingElement(\ilAssOrderingElement $order_element, int $question_id)
buildOrderingElement(int $answer_id, int $random_identifier, int $solution_identifier, int $position, int $indentation, string $content)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
foreach($_POST as $key=> $value) $res