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