ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ReservationDBRepository.php
Go to the documentation of this file.
1<?php
2
20
23
30{
31 protected \ilDBInterface $db;
32 protected ?array $preloaded_by_context_list = null;
33
34 public function __construct(
36 ?array $preload_context_obj_ids = null
37 ) {
38 $this->db = $db;
39 if (is_array($preload_context_obj_ids)) {
40 $this->preloadByContextIds($preload_context_obj_ids);
41 }
42 }
43
48 public function getForId(int $id): array
49 {
50 $il_db_statement = $this->db->query(
51 "SELECT * FROM booking_reservation WHERE booking_reservation_id = {$this->db->quote($id, \ilDBConstants::T_INTEGER)}"
52 );
53 return $this->db->fetchAssoc($il_db_statement) ?? [];
54 }
55
59 public function create(
60 int $user_id,
61 int $assigner_id,
62 int $object_id,
63 int $context_obj_id,
64 int $from,
65 int $to,
66 int $status,
67 int $group_id,
68 string $message = ""
69 ): int {
70 $ilDB = $this->db;
71
72 $id = $ilDB->nextId('booking_reservation');
73 $ilDB->manipulate('INSERT INTO booking_reservation' .
74 ' (booking_reservation_id,user_id,assigner_id,object_id,context_obj_id,date_from,date_to,status,group_id,message)' .
75 ' VALUES (' . $ilDB->quote($id, 'integer') .
76 ',' . $ilDB->quote($user_id, 'integer') .
77 ',' . $ilDB->quote($assigner_id, 'integer') .
78 ',' . $ilDB->quote($object_id, 'integer') .
79 ',' . $ilDB->quote($context_obj_id, 'integer') .
80 ',' . $ilDB->quote($from, 'integer') .
81 ',' . $ilDB->quote($to, 'integer') .
82 ',' . $ilDB->quote($status, 'integer') .
83 ',' . $ilDB->quote($group_id, 'integer') .
84 ',' . $ilDB->quote($message, 'text') . ')');
85 return $id;
86 }
87
91 public function update(
92 int $id,
93 int $user_id,
94 int $assigner_id,
95 int $object_id,
96 int $context_obj_id,
97 int $from,
98 int $to,
99 int $status,
100 int $group_id,
101 string $message = ""
102 ): int {
103 $ilDB = $this->db;
104 return $ilDB->manipulate('UPDATE booking_reservation' .
105 ' SET object_id = ' . $ilDB->quote($object_id, 'text') .
106 ', user_id = ' . $ilDB->quote($user_id, 'integer') .
107 ', assigner_id = ' . $ilDB->quote($assigner_id, 'integer') .
108 ', date_from = ' . $ilDB->quote($from, 'integer') .
109 ', date_to = ' . $ilDB->quote($to, 'integer') .
110 ', status = ' . $ilDB->quote($status, 'integer') .
111 ', group_id = ' . $ilDB->quote($group_id, 'integer') .
112 ', context_obj_id = ' . $ilDB->quote($context_obj_id, 'integer') .
113 ', message = ' . $ilDB->quote($message, 'text') .
114 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
115 }
116
117 public function delete(int $id): void
118 {
119 $ilDB = $this->db;
120
121 if ($id) {
122 $ilDB->manipulate('DELETE FROM booking_reservation' .
123 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
124 }
125 }
126
130 public function getNewGroupId(): int
131 {
132 return $this->db->nextId('booking_reservation_group');
133 }
134
135
140 public function getNumberOfReservations(
141 array $ids,
142 int $from,
143 int $to,
144 bool $only_not_over_yet = false
145 ): array {
146 $date = $only_not_over_yet
147 ? " AND date_to > {$this->db->quote(time(), ilDBConstants::T_INTEGER)}"
148 : '';
149
150 $set = $this->db->query(
151 'SELECT count(*) cnt, object_id FROM booking_reservation'
152 . " WHERE {$this->db->in('object_id', $ids, '', ilDBConstants::T_INTEGER)}{$date}"
153 . " AND (status IS NULL OR status <> {$this->db->quote(ilBookingReservation::STATUS_CANCELLED, ilDBConstants::T_INTEGER)})"
154 . " AND date_from = {$this->db->quote($from, ilDBConstants::T_INTEGER)} AND date_to = {$this->db->quote($to, ilDBConstants::T_INTEGER)}"
155 . ' GROUP BY object_id'
156 );
157
158 $res = [];
159 while ($row = $this->db->fetchAssoc($set)) {
160 $res[$row['object_id']] = $row;
161 }
162 return $res;
163 }
164
168 public function getListByDate(
169 bool $a_has_schedule,
170 ?array $a_object_ids = null,
171 ?array $filter = null,
172 ?array $a_pool_ids = null
173 ): array {
174 $ilDB = $this->db;
175
176 $res = array();
177
178 $sql = 'SELECT r.*, o.title, o.pool_id, o.post_text, o.post_file' .
179 ' FROM booking_reservation r' .
180 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
181
182 $where = [];
183 if ($a_pool_ids !== null) {
184 $where = array($ilDB->in('pool_id', $a_pool_ids, '', 'integer'));
185 }
186
187 if ($a_object_ids !== null) {
188 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
189 }
190
191 if (isset($filter['context_obj_ids']) && count($filter['context_obj_ids']) > 0) {
192 $where = array($ilDB->in('context_obj_id', $filter['context_obj_ids'], '', 'integer'));
193 }
194
195 if ($filter['status'] ?? false) {
196 if ($filter['status'] > 0) {
197 $where[] = 'status = ' . $ilDB->quote($filter['status'], 'integer');
198 } else {
199 $where[] = '(status != ' . $ilDB->quote(-$filter['status'], 'integer') .
200 ' OR status IS NULL)';
201 }
202 }
203 if (isset($filter['title']) && is_string($filter['title'])) {
204 $where[] = '(' . $ilDB->like('title', 'text', '%' . $filter['title'] . '%') .
205 ' OR ' . $ilDB->like('description', 'text', '%' . $filter['title'] . '%') . ')';
206 }
207 if ($a_has_schedule) {
208 if (isset($filter['from']) && (int) $filter['from'] > 0) {
209 $where[] = 'date_from >= ' . $ilDB->quote($filter['from'], 'integer');
210 }
211 if (isset($filter['to']) && (int) $filter['to'] > 0) {
212 $where[] = 'date_to <= ' . $ilDB->quote($filter['to'], 'integer');
213 }
214 if (!isset($filter['past']) || !$filter['past']) {
215 $where[] = 'date_to > ' . $ilDB->quote(time(), 'integer');
216 }
217 }
218 if (isset($filter['user_id']) && is_numeric($filter['user_id'])) { // #16584
219 $where[] = 'user_id = ' . $ilDB->quote($filter['user_id'], 'integer');
220 }
221 if (count($where) > 0) {
222 $sql .= ' WHERE ' . implode(' AND ', $where);
223 }
224
225 if ($a_has_schedule) {
226 $sql .= ' ORDER BY date_from DESC';
227 } else {
228 // #16155 - could be cancelled and re-booked
229 $sql .= ' ORDER BY status';
230 }
231 $set = $ilDB->query($sql);
232 while ($row = $ilDB->fetchAssoc($set)) {
233 $obj_id = $row["object_id"];
234 $user_id = $row["user_id"];
235
236 if ($a_has_schedule) {
237 $slot = $row["date_from"] . "_" . $row["date_to"];
238 $idx = $obj_id . "_" . $user_id . "_" . $slot;
239 } else {
240 $idx = $obj_id . "_" . $user_id;
241 }
242 $idx .= "_" . $row["context_obj_id"];
243
244 if ($a_has_schedule && ($filter["slot"] ?? false)) {
245 $slot_idx = date("w", $row["date_from"]) . "_" . date("H:i", $row["date_from"]) .
246 "-" . date("H:i", $row["date_to"] + 1);
247 if ($filter["slot"] != $slot_idx) {
248 continue;
249 }
250 }
251
252 if (!isset($res[$idx])) {
254
255 $res[$idx] = array(
256 "object_id" => $obj_id
257 ,"title" => $row["title"]
258 ,"post_text" => $row["post_text"]
259 ,"post_file" => $row["post_file"]
260 ,"pool_id" => $row["pool_id"]
261 ,"context_obj_id" => (int) $row["context_obj_id"]
262 ,"user_id" => $user_id
263 ,"message" => (string) $row["message"]
264 ,"counter" => 1
265 ,"user_name" => $uname["lastname"] . ", " . $uname["firstname"] // #17862
266 ,"login" => $uname["login"]
267 );
268
269 if ($a_has_schedule) {
270 $res[$idx]["booking_reservation_id"] = $idx;
271 $res[$idx]["date"] = date("Y-m-d", $row["date_from"]);
272 $res[$idx]["slot"] = date("H:i", $row["date_from"]) . " - " .
273 date("H:i", $row["date_to"] + 1);
274 $res[$idx]["week"] = date("W", $row["date_from"]);
275 $res[$idx]["weekday"] = date("w", $row["date_from"]);
276 $res[$idx]["can_be_cancelled"] = ($row["status"] != \ilBookingReservation::STATUS_CANCELLED &&
277 $row["date_from"] > time());
278 $res[$idx]["_sortdate"] = $row["date_from"];
279
280 // this currently means: has any cancelled reservations (it is not grouped by this info)
281 $res[$idx]["status"] = $row["status"];
282 } else {
283 $res[$idx]["booking_reservation_id"] = $row["booking_reservation_id"];
284 $res[$idx]["status"] = $row["status"];
285 $res[$idx]["can_be_cancelled"] = ($row["status"] != \ilBookingReservation::STATUS_CANCELLED);
286 }
287 } else {
288 $res[$idx]["counter"]++;
289 }
290 }
291
292 return $res;
293 }
294
298
303 protected function preloadByContextIds(
304 array $context_obj_ids
305 ): void {
306 $filter = ["context_obj_ids" => ($context_obj_ids)];
307 $filter['past'] = true;
308 $filter['status'] = -\ilBookingReservation::STATUS_CANCELLED;
309 $list = $this->getListByDate(true, null, $filter);
310 $list = \ilArrayUtil::sortArray($list, "slot", "asc", true);
311 $list = \ilArrayUtil::stableSortArray($list, "date", "asc", true);
312 $list = \ilArrayUtil::stableSortArray($list, "object_id", "asc", true);
313 $this->preloaded_by_context_list = \ilArrayUtil::stableSortArray($list, "pool_id", "asc", true);
314 }
315
321 int $context_obj_id
322 ): array {
323 if (!is_array($this->preloaded_by_context_list)) {
324 throw new \ilBookingReservationException("Repo not initilialized.");
325 }
326 return array_filter($this->preloaded_by_context_list, static function ($row) use ($context_obj_id) {
327 return ($row["context_obj_id"] == $context_obj_id);
328 });
329 }
330
331 public function getReservationIdsByBookingObjectId(int $booking_object_id): array
332 {
333 $set = $this->db->queryF(
334 "SELECT booking_reservation_id FROM booking_reservation " .
335 " WHERE object_id = %s ",
336 ["integer"],
337 [$booking_object_id]
338 );
339 $ret = [];
340 while ($row = $this->db->fetchAssoc($set)) {
341 $ret[] = (int) $row['booking_reservation_id'];
342 }
343
344 return $ret;
345 }
346}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Repo class for reservations Acts on tables booking_reservation (rw), booking_reservation_group (rw) a...
getListByDate(bool $a_has_schedule, ?array $a_object_ids=null, ?array $filter=null, ?array $a_pool_ids=null)
List all reservations by date.
preloadByContextIds(array $context_obj_ids)
Preload reservation information for context obj ids.
create(int $user_id, int $assigner_id, int $object_id, int $context_obj_id, int $from, int $to, int $status, int $group_id, string $message="")
getNumberOfReservations(array $ids, int $from, int $to, bool $only_not_over_yet=false)
Get number of uncancelled reservations in time frame.
getCachedContextObjBookingInfo(int $context_obj_id)
Get context object properties info.
update(int $id, int $user_id, int $assigner_id, int $object_id, int $context_obj_id, int $from, int $to, int $status, int $group_id, string $message="")
__construct(\ilDBInterface $db, ?array $preload_context_obj_ids=null)
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static stableSortArray(array $array, string $a_array_sortby, string $a_array_sortorder="asc", bool $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilDBConstants.
static _lookupName(int $a_user_id)
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($_POST as $key=> $value) $res
if(!file_exists('../ilias.ini.php'))