ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DatabaseGatewayImplementation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 
29 {
30  public function __construct(
31  protected \ilDBInterface $db
32  ) {
33  }
34 
35  public function create(GenericData $data): int
36  {
37  return $this->insert($data, false, false);
38  }
39 
40  public function createFromScratch(GenericData $data): int
41  {
42  return $this->insert($data, true, true);
43  }
44 
45  public function createWithNewPosition(GenericData $data): int
46  {
47  return $this->insert($data, true, false);
48  }
49 
50  public function insert(
51  GenericData $data,
52  bool $increment_position,
53  bool $generate_import_id
54  ): int {
55  $next_id = $this->db->nextId('adv_mdf_definition');
56 
57  $position = $increment_position ?
58  $this->getNextPositionInRecord($data->getRecordID()) :
59  $data->getPosition();
60 
61  $import_id = $generate_import_id ?
62  $this->generateUniqueImportId($next_id) :
63  $data->getImportID();
64 
65  $this->db->insert(
66  'adv_mdf_definition',
67  [
68  'field_id' => [\ilDBConstants::T_INTEGER, $next_id],
69  'field_type' => [\ilDBConstants::T_INTEGER, $data->type()->value],
70  'record_id' => [\ilDBConstants::T_INTEGER, $data->getRecordID()],
71  'import_id' => [\ilDBConstants::T_TEXT, $import_id],
72  'title' => [\ilDBConstants::T_TEXT, $data->getTitle()],
73  'description' => [\ilDBConstants::T_TEXT, $data->getDescription()],
74  'position' => [\ilDBConstants::T_INTEGER, $data->getPosition()],
75  'searchable' => [\ilDBConstants::T_INTEGER, $data->isSearchable()],
76  'required' => [\ilDBConstants::T_INTEGER, $data->isRequired()],
77  'field_values' => [\ilDBConstants::T_TEXT, serialize($data->getFieldValues())]
78  ]
79  );
80 
81  return $next_id;
82  }
83 
84  public function readByID(int $field_id): ?GenericData
85  {
86  $query = 'SELECT * FROM adv_mdf_definition WHERE field_id = ' .
87  $this->db->quote($field_id, \ilDBConstants::T_INTEGER);
88 
89  $res = $this->db->query($query);
90  if ($row = $this->db->fetchAssoc($res)) {
91  return $this->dataFromRow($row);
92  }
93  return null;
94  }
95 
99  public function readByIDs(int ...$field_ids): \Generator
100  {
101  if (empty($field_ids)) {
102  return;
103  }
104 
105  $query = 'SELECT * FROM adv_mdf_definition WHERE ' .
106  $this->db->in('field_id', $field_ids, false, \ilDBConstants::T_INTEGER);
107 
108  $res = $this->db->query($query);
109  $data = [];
110  while ($row = $this->db->fetchAssoc($res)) {
111  $data[$row['field_id']] = $this->dataFromRow($row);
112  }
113 
114  // Return data in the order the field IDs where passed.
115  foreach ($field_ids as $field_id) {
116  yield $data[$field_id];
117  }
118  }
119 
123  public function readByRecords(bool $only_searchable, int ...$record_ids): \Generator
124  {
125  if (empty($record_ids)) {
126  return;
127  }
128 
129  $query = 'SELECT * FROM adv_mdf_definition WHERE ' .
130  $this->db->in('record_id', $record_ids, false, \ilDBConstants::T_INTEGER);
131 
132  if ($only_searchable) {
133  $query .= ' AND searchable = 1';
134  }
135 
136  $query .= ' ORDER BY position';
137 
138  $res = $this->db->query($query);
139  while ($row = $this->db->fetchAssoc($res)) {
140  yield $this->dataFromRow($row);
141  }
142  }
143 
144  public function readByImportID(string $import_id): ?GenericData
145  {
146  $query = 'SELECT * FROM adv_mdf_definition WHERE import_id = ' .
147  $this->db->quote($import_id, \ilDBConstants::T_TEXT);
148 
149  $res = $this->db->query($query);
150  if ($row = $this->db->fetchAssoc($res)) {
151  return $this->dataFromRow($row);
152  }
153  return null;
154  }
155 
156  public function update(GenericData $data): void
157  {
158  if (!$data->isPersisted() || !$data->containsChanges()) {
159  return;
160  }
161 
162  $this->db->update(
163  'adv_mdf_definition',
164  [
165  'field_type' => [\ilDBConstants::T_INTEGER, $data->type()->value],
166  'record_id' => [\ilDBConstants::T_INTEGER, $data->getRecordID()],
167  'import_id' => [\ilDBConstants::T_TEXT, $data->getImportID()],
168  'title' => [\ilDBConstants::T_TEXT, $data->getTitle()],
169  'description' => [\ilDBConstants::T_TEXT, $data->getDescription()],
170  'position' => [\ilDBConstants::T_INTEGER, $data->getPosition()],
171  'searchable' => [\ilDBConstants::T_INTEGER, $data->isSearchable()],
172  'required' => [\ilDBConstants::T_INTEGER, $data->isRequired()],
173  'field_values' => [\ilDBConstants::T_TEXT, serialize($data->getFieldValues())]
174  ],
175  [
176  'field_id' => [\ilDBConstants::T_INTEGER, $data->id()]
177  ]
178  );
179  }
180 
181  public function delete(int ...$field_ids): void
182  {
183  if (empty($field_ids)) {
184  return;
185  }
186 
187  $query = 'DELETE FROM adv_mdf_definition WHERE ' .
188  $this->db->in('field_id', $field_ids, false, \ilDBConstants::T_INTEGER);
189 
190  $this->db->manipulate($query);
191  }
192 
193  protected function getNextPositionInRecord(int $record_id): int
194  {
195  $query = 'SELECT MAX(position) max_pos FROM adv_mdf_definition WHERE record_id = ' .
196  $this->db->quote($record_id, \ilDBConstants::T_INTEGER);
197 
198  $res = $this->db->query($query);
199  if ($row = $this->db->fetchAssoc($res)) {
200  return $row['max_pos'] + 1;
201  }
202  return 0;
203  }
204 
205  protected function generateUniqueImportId(int $field_id): string
206  {
207  return 'il_' . IL_INST_ID . '_adv_md_field_' . $field_id;
208  }
209 
210  protected function dataFromRow(array $row): GenericData
211  {
212  if (!isset($row['field_type']) || is_null($type = Type::tryFrom((int) $row['field_type']))) {
213  throw new Exception(
214  ($row['field_type'] ?? 'Null') . ' is invalid as field definition type'
215  );
216  }
217 
218  $field_values = unserialize((string) $row['field_values']);
219  if (!is_array($field_values)) {
220  $field_values = [];
221  }
222 
223  return new GenericDataImplementation(
224  $type,
225  (int) $row['record_id'],
226  (string) $row['import_id'],
227  (string) $row['title'],
228  (string) $row['description'],
229  (int) $row['position'],
230  (bool) $row['searchable'],
231  (bool) $row['required'],
232  $field_values,
233  (int) $row['field_id']
234  );
235  }
236 }
insert(GenericData $data, bool $increment_position, bool $generate_import_id)
$res
Definition: ltiservices.php:66
createFromScratch(GenericData $data)
Inserts the data, but replaces position and import ID: next position in the record, and a newly generated import ID is used.
const IL_INST_ID
Definition: constants.php:40
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
createWithNewPosition(GenericData $data)
Inserts the data, but replaces position by the next position in the record.