ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
assOrderingQuestionDatabaseRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
27 {
28  public const TABLE_NAME_BASE = 'qpl_questions';
29  public const TABLE_NAME_QUESTIONS = 'qpl_qst_ordering';
30  public const TABLE_NAME_ANSWERS = 'qpl_a_ordering';
31 
32  protected \ilDBInterface $db;
33 
34  public function __construct(\ilDBInterface $db)
35  {
36  $this->db = $db;
37  }
38 
39  public function getOrderingList(int $question_id): \ilAssOrderingElementList
40  {
41  return $this->buildOrderingList($question_id);
42  }
43 
44  public function updateOrderingList(\ilAssOrderingElementList $list): void
45  {
46  $atom_query = $this->db->buildAtomQuery();
47  $atom_query->addTableLock(self::TABLE_NAME_ANSWERS);
48  $atom_query->addTableLock(self::TABLE_NAME_ANSWERS . '_seq');
49 
50  $atom_query->addQueryCallable(
51  function (\ilDBInterface $db) use ($list) {
52  $this->deleteOrderingElements($list->getQuestionId());
53  foreach ($list->getElements() as $order_element) {
54  $this->insertOrderingElement($order_element, $list->getQuestionId());
55  }
56  }
57  );
58  $atom_query->run();
59  }
60 
61  protected function buildOrderingList(
62  int $question_id
64  $elements = $this->getOrderingElementsForList($question_id);
65  return new \ilAssOrderingElementList($question_id, $elements);
66  }
67 
71  protected function getOrderingElementsForList(int $question_id): array
72  {
73  $query = 'SELECT' . PHP_EOL
74  . 'answer_id, answertext, solution_key, random_id, depth, position' . PHP_EOL
75  . 'FROM ' . self::TABLE_NAME_ANSWERS . PHP_EOL
76  . 'WHERE question_fi=' . $question_id . PHP_EOL
77  . 'ORDER BY position ASC';
78 
79  $elements = [];
80  $res = $this->db->query($query);
81  while ($row = $this->db->fetchAssoc($res)) {
82  $elements[] = $this->buildOrderingElement(
83  (int) $row['answer_id'],
84  (int) $row['random_id'],
85  (int) $row['solution_key'],
86  (int) $row['position'],
87  (int) $row['depth'],
88  (string) $row['answertext']
89  );
90  }
91  return $elements;
92  }
93 
94  protected function deleteOrderingElements(int $question_id): void
95  {
96  $query = 'DELETE FROM ' . self::TABLE_NAME_ANSWERS . PHP_EOL
97  . 'WHERE question_fi = ' . $question_id;
98  $this->db->manipulate($query);
99  }
100 
101  protected function insertOrderingElement(\ilAssOrderingElement $order_element, int $question_id): void
102  {
103  $next_id = $this->db->nextId(self::TABLE_NAME_ANSWERS);
104  $values = array(
105  'answer_id' => ['integer', $next_id],
106  'question_fi' => ['integer', $question_id],
107  'answertext' => ['text', $order_element->getContent()],
108  'solution_key' => ['integer', $order_element->getSolutionIdentifier()],
109  'random_id' => ['integer', $order_element->getRandomIdentifier()],
110  'position' => ['integer', $order_element->getPosition()],
111  'depth' => ['integer', $order_element->getIndentation()],
112  'tstamp' => ['integer', $this->getTime()]
113  );
114  $this->db->insert(self::TABLE_NAME_ANSWERS, $values);
115  }
116 
117  protected function getTime()
118  {
119  return time();
120  }
121 
122  protected function buildOrderingElement(
123  int $answer_id,
124  int $random_identifier,
125  int $solution_identifier,
126  int $position,
127  int $indentation,
128  string $content
130  return (new \ilAssOrderingElement($answer_id))
131  ->withRandomIdentifier($random_identifier)
132  ->withSolutionIdentifier($solution_identifier)
133  ->withPosition($position)
134  ->withIndentation($indentation)
135  ->withContent($content);
136  }
137 }
$res
Definition: ltiservices.php:69
repository for assOrderingQuestion (the answer elements within, at least...)
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...
insertOrderingElement(\ilAssOrderingElement $order_element, int $question_id)
$query