ILIAS  release_8 Revision v8.24
class.ilBookingReservationDBRepository.php
Go to the documentation of this file.
1<?php
2
25{
26 protected ilDBInterface $db;
27 protected ?array $preloaded_by_context_list = null;
28
33 public function __construct(
35 ?array $preload_context_obj_ids = null
36 ) {
37 if (is_array($preload_context_obj_ids)) {
38 $this->preloadByContextIds($preload_context_obj_ids);
39 }
40 $this->db = $db;
41 }
42
47 public function getForId(int $id): array
48 {
50 $set = $ilDB->query('SELECT *' .
51 ' FROM booking_reservation' .
52 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
53 return $ilDB->fetchAssoc($set);
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 ): int {
69 $ilDB = $this->db;
70
71 $id = $ilDB->nextId('booking_reservation');
72 $ilDB->manipulate('INSERT INTO booking_reservation' .
73 ' (booking_reservation_id,user_id,assigner_id,object_id,context_obj_id,date_from,date_to,status,group_id)' .
74 ' VALUES (' . $ilDB->quote($id, 'integer') .
75 ',' . $ilDB->quote($user_id, 'integer') .
76 ',' . $ilDB->quote($assigner_id, 'integer') .
77 ',' . $ilDB->quote($object_id, 'integer') .
78 ',' . $ilDB->quote($context_obj_id, 'integer') .
79 ',' . $ilDB->quote($from, 'integer') .
80 ',' . $ilDB->quote($to, 'integer') .
81 ',' . $ilDB->quote($status, 'integer') .
82 ',' . $ilDB->quote($group_id, 'integer') . ')');
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 ): int {
100 $ilDB = $this->db;
101 return $ilDB->manipulate('UPDATE booking_reservation' .
102 ' SET object_id = ' . $ilDB->quote($object_id, 'text') .
103 ', user_id = ' . $ilDB->quote($user_id, 'integer') .
104 ', assigner_id = ' . $ilDB->quote($assigner_id, 'integer') .
105 ', date_from = ' . $ilDB->quote($from, 'integer') .
106 ', date_to = ' . $ilDB->quote($to, 'integer') .
107 ', status = ' . $ilDB->quote($status, 'integer') .
108 ', group_id = ' . $ilDB->quote($group_id, 'integer') .
109 ', context_obj_id = ' . $ilDB->quote($context_obj_id, 'integer') .
110 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
111 }
112
113 public function delete(int $id): void
114 {
115 $ilDB = $this->db;
116
117 if ($id) {
118 $ilDB->manipulate('DELETE FROM booking_reservation' .
119 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
120 }
121 }
122
126 public function getNewGroupId(): int
127 {
128 return $this->db->nextId('booking_reservation_group');
129 }
130
131
136 public function getNumberOfReservations(
137 array $ids,
138 int $from,
139 int $to,
140 bool $only_not_over_yet = false
141 ): array {
142 $ilDB = $this->db;
143
144 $from = $ilDB->quote($from, 'integer');
145 $to = $ilDB->quote($to, 'integer');
146
147 $date = $only_not_over_yet
148 ? ' AND date_to > ' . $ilDB->quote(time(), "integer")
149 : "";
150
151 $set = $ilDB->query('SELECT count(*) cnt, object_id' .
152 ' FROM booking_reservation' .
153 ' WHERE ' . $ilDB->in('object_id', $ids, '', 'integer') . $date .
154 ' AND (status IS NULL OR status <> ' . $ilDB->quote(
156 'integer'
157 ) . ')' .
158 ' AND date_from <= ' . $to . ' AND date_to >= ' . $from .
159 ' GROUP BY object_id');
160 $res = [];
161 while ($row = $ilDB->fetchAssoc($set)) {
162 $res[$row["object_id"]] = $row;
163 }
164 return $res;
165 }
166
170 public function getListByDate(
171 bool $a_has_schedule,
172 array $a_object_ids = null,
173 array $filter = null,
174 array $a_pool_ids = null
175 ): array {
176 $ilDB = $this->db;
177
178 $res = array();
179
180 $sql = 'SELECT r.*, o.title, o.pool_id' .
181 ' FROM booking_reservation r' .
182 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
183
184 $where = [];
185 if ($a_pool_ids !== null) {
186 $where = array($ilDB->in('pool_id', $a_pool_ids, '', 'integer'));
187 }
188
189 if ($a_object_ids !== null) {
190 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
191 }
192
193 if (isset($filter['context_obj_ids']) && count($filter['context_obj_ids']) > 0) {
194 $where = array($ilDB->in('context_obj_id', $filter['context_obj_ids'], '', 'integer'));
195 }
196
197 if ($filter['status'] ?? false) {
198 if ($filter['status'] > 0) {
199 $where[] = 'status = ' . $ilDB->quote($filter['status'], 'integer');
200 } else {
201 $where[] = '(status != ' . $ilDB->quote(-$filter['status'], 'integer') .
202 ' OR status IS NULL)';
203 }
204 }
205 if (isset($filter['title']) && is_string($filter['title'])) {
206 $where[] = '(' . $ilDB->like('title', 'text', '%' . $filter['title'] . '%') .
207 ' OR ' . $ilDB->like('description', 'text', '%' . $filter['title'] . '%') . ')';
208 }
209 if ($a_has_schedule) {
210 if (isset($filter['from']) && (int) $filter['from'] > 0) {
211 $where[] = 'date_from >= ' . $ilDB->quote($filter['from'], 'integer');
212 }
213 if (isset($filter['to']) && (int) $filter['to'] > 0) {
214 $where[] = 'date_to <= ' . $ilDB->quote($filter['to'], 'integer');
215 }
216 if (!isset($filter['past']) || !$filter['past']) {
217 $where[] = 'date_to > ' . $ilDB->quote(time(), 'integer');
218 }
219 }
220 if (isset($filter['user_id']) && is_numeric($filter['user_id'])) { // #16584
221 $where[] = 'user_id = ' . $ilDB->quote($filter['user_id'], 'integer');
222 }
223 if (count($where) > 0) {
224 $sql .= ' WHERE ' . implode(' AND ', $where);
225 }
226
227 if ($a_has_schedule) {
228 $sql .= ' ORDER BY date_from DESC';
229 } else {
230 // #16155 - could be cancelled and re-booked
231 $sql .= ' ORDER BY status';
232 }
233 $set = $ilDB->query($sql);
234 while ($row = $ilDB->fetchAssoc($set)) {
235 $obj_id = $row["object_id"];
236 $user_id = $row["user_id"];
237
238 if ($a_has_schedule) {
239 $slot = $row["date_from"] . "_" . $row["date_to"];
240 $idx = $obj_id . "_" . $user_id . "_" . $slot;
241 } else {
242 $idx = $obj_id . "_" . $user_id;
243 }
244 $idx .= "_" . $row["context_obj_id"];
245
246 if ($a_has_schedule && ($filter["slot"] ?? false)) {
247 $slot_idx = date("w", $row["date_from"]) . "_" . date("H:i", $row["date_from"]) .
248 "-" . date("H:i", $row["date_to"] + 1);
249 if ($filter["slot"] != $slot_idx) {
250 continue;
251 }
252 }
253
254 if (!isset($res[$idx])) {
255 $uname = ilObjUser::_lookupName($user_id);
256
257 $res[$idx] = array(
258 "object_id" => $obj_id
259 ,"title" => $row["title"]
260 ,"pool_id" => $row["pool_id"]
261 ,"context_obj_id" => (int) $row["context_obj_id"]
262 ,"user_id" => $user_id
263 ,"counter" => 1
264 ,"user_name" => $uname["lastname"] . ", " . $uname["firstname"] // #17862
265 ,"login" => $uname["login"]
266 );
267
268 if ($a_has_schedule) {
269 $res[$idx]["booking_reservation_id"] = $idx;
270 $res[$idx]["date"] = date("Y-m-d", $row["date_from"]);
271 $res[$idx]["slot"] = date("H:i", $row["date_from"]) . " - " .
272 date("H:i", $row["date_to"] + 1);
273 $res[$idx]["week"] = date("W", $row["date_from"]);
274 $res[$idx]["weekday"] = date("w", $row["date_from"]);
275 $res[$idx]["can_be_cancelled"] = ($row["status"] != ilBookingReservation::STATUS_CANCELLED &&
276 $row["date_from"] > time());
277 $res[$idx]["_sortdate"] = $row["date_from"];
278
279 // this currently means: has any cancelled reservations (it is not grouped by this info)
280 $res[$idx]["status"] = $row["status"];
281 } else {
282 $res[$idx]["booking_reservation_id"] = $row["booking_reservation_id"];
283 $res[$idx]["status"] = $row["status"];
284 $res[$idx]["can_be_cancelled"] = ($row["status"] != ilBookingReservation::STATUS_CANCELLED);
285 }
286 } else {
287 $res[$idx]["counter"]++;
288 }
289 }
290
291 return $res;
292 }
293
297
302 protected function preloadByContextIds(
303 array $context_obj_ids
304 ): void {
305 $filter = ["context_obj_ids" => ($context_obj_ids)];
306 $filter['past'] = true;
307 $filter['status'] = -ilBookingReservation::STATUS_CANCELLED;
309 $repo = $f->getRepo();
310 $list = $repo->getListByDate(true, null, $filter);
311 $list = ilArrayUtil::sortArray($list, "slot", "asc", true);
312 $list = ilArrayUtil::stableSortArray($list, "date", "asc", true);
313 $list = ilArrayUtil::stableSortArray($list, "object_id", "asc", true);
314 $this->preloaded_by_context_list = ilArrayUtil::stableSortArray($list, "pool_id", "asc", true);
315 }
316
322 int $context_obj_id
323 ): array {
324 if (!is_array($this->preloaded_by_context_list)) {
325 throw new ilBookingReservationException("Repo not initilialized.");
326 }
327 return array_filter($this->preloaded_by_context_list, static function ($row) use ($context_obj_id) {
328 return ($row["context_obj_id"] == $context_obj_id);
329 });
330 }
331
332 public function getReservationIdsByBookingObjectId(int $booking_object_id): array
333 {
334 $set = $this->db->queryF(
335 "SELECT booking_reservation_id FROM booking_reservation " .
336 " WHERE object_id = %s ",
337 ["integer"],
338 [$booking_object_id]
339 );
340 $ret = [];
341 while ($row = $this->db->fetchAssoc($set)) {
342 $ret[] = (int) $row['booking_reservation_id'];
343 }
344
345 return $ret;
346 }
347}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilDBInterface $db, ?array $preload_context_obj_ids=null)
Do not call this constructor directly, use ilBookingReservationDBRepositoryFactory instead.
getListByDate(bool $a_has_schedule, array $a_object_ids=null, array $filter=null, array $a_pool_ids=null)
List all reservations by date.
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)
getNumberOfReservations(array $ids, int $from, int $to, bool $only_not_over_yet=false)
Get number of uncancelled reservations in time frame.
create(int $user_id, int $assigner_id, int $object_id, int $context_obj_id, int $from, int $to, int $status, int $group_id)
getCachedContextObjBookingInfo(int $context_obj_id)
Get context object properties info.
preloadByContextIds(array $context_obj_ids)
Preload reservation information for context obj ids.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupName(int $a_user_id)
lookup user name
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
Interface ilDBInterface.
$res
Definition: ltiservices.php:69