ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBookingReservationDBRepository.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
14{
18 protected $db;
19
24
31 public function __construct(ilDBInterface $db, $preload_context_obj_ids = null)
32 {
33 if (is_array($preload_context_obj_ids)) {
34 $this->preloadByContextIds($preload_context_obj_ids);
35 }
36 $this->db = $db;
37 }
38
45 public function getForId(int $id)
46 {
48 $set = $ilDB->query('SELECT *' .
49 ' FROM booking_reservation' .
50 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
51 $row = $ilDB->fetchAssoc($set);
52 return $row;
53 }
54
68 public function create(
69 int $user_id,
70 int $assigner_id,
71 int $object_id,
72 $context_obj_id,
73 int $from,
74 int $to,
75 $status,
76 $group_id
77 ) {
79
80 $id = $ilDB->nextId('booking_reservation');
81 $ilDB->manipulate('INSERT INTO booking_reservation' .
82 ' (booking_reservation_id,user_id,assigner_id,object_id,context_obj_id,date_from,date_to,status,group_id)' .
83 ' VALUES (' . $ilDB->quote($id, 'integer') .
84 ',' . $ilDB->quote($user_id, 'integer') .
85 ',' . $ilDB->quote($assigner_id, 'integer') .
86 ',' . $ilDB->quote($object_id, 'integer') .
87 ',' . $ilDB->quote($context_obj_id, 'integer') .
88 ',' . $ilDB->quote($from, 'integer') .
89 ',' . $ilDB->quote($to, 'integer') .
90 ',' . $ilDB->quote($status, 'integer') .
91 ',' . $ilDB->quote($group_id, 'integer') . ')');
92 return $id;
93 }
94
109 public function update(
110 int $id,
111 int $user_id,
112 int $assigner_id,
113 int $object_id,
114 $context_obj_id,
115 int $from,
116 int $to,
117 $status,
118 $group_id
119 ) {
121 return $ilDB->manipulate('UPDATE booking_reservation' .
122 ' SET object_id = ' . $ilDB->quote($object_id, 'text') .
123 ', user_id = ' . $ilDB->quote($user_id, 'integer') .
124 ', assigner_id = ' . $ilDB->quote($assigner_id, 'integer') .
125 ', date_from = ' . $ilDB->quote($from, 'integer') .
126 ', date_to = ' . $ilDB->quote($to, 'integer') .
127 ', status = ' . $ilDB->quote($status, 'integer') .
128 ', group_id = ' . $ilDB->quote($group_id, 'integer') .
129 ', context_obj_id = ' . $ilDB->quote($context_obj_id, 'integer') .
130 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
131 }
132
138 public function delete(int $id)
139 {
141
142 if ($id) {
143 $ilDB->manipulate('DELETE FROM booking_reservation' .
144 ' WHERE booking_reservation_id = ' . $ilDB->quote($id, 'integer'));
145 }
146 }
147
152 public function getNewGroupId()
153 {
155
156 return $ilDB->nextId('booking_reservation_group');
157 }
158
159
168 public function getNumberOfReservations(array $ids, int $from, int $to, $only_not_over_yet = false)
169 {
171
172 $from = $ilDB->quote($from, 'integer');
173 $to = $ilDB->quote($to, 'integer');
174
175 $date = $only_not_over_yet
176 ? ' AND date_to > ' . $ilDB->quote(time(), "integer")
177 : "";
178
179 $set = $ilDB->query('SELECT count(*) cnt, object_id' .
180 ' FROM booking_reservation' .
181 ' WHERE ' . $ilDB->in('object_id', $ids, '', 'integer') . $date .
182 ' AND (status IS NULL OR status <> ' . $ilDB->quote(
184 'integer'
185 ) . ')' .
186 ' AND date_from <= ' . $to . ' AND date_to >= ' . $from .
187 ' GROUP BY object_id');
188 $res = [];
189 while ($row = $ilDB->fetchAssoc($set)) {
190 $res[$row["object_id"]] = $row;
191 }
192 return $res;
193 }
194
203 public function getListByDate(
204 $a_has_schedule,
205 array $a_object_ids = null,
206 array $filter = null,
207 array $a_pool_ids = null
208 ) {
210
211 $res = array();
212
213 $sql = 'SELECT r.*, o.title, o.pool_id' .
214 ' FROM booking_reservation r' .
215 ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
216
217 if ($a_pool_ids !== null) {
218 $where = array($ilDB->in('pool_id', $a_pool_ids, '', 'integer'));
219 }
220
221 if ($a_object_ids !== null) {
222 $where = array($ilDB->in('object_id', $a_object_ids, '', 'integer'));
223 }
224
225 if (is_array($filter['context_obj_ids']) && count($filter['context_obj_ids']) > 0) {
226 $where = array($ilDB->in('context_obj_id', $filter['context_obj_ids'], '', 'integer'));
227 }
228
229 if ($filter['status']) {
230 if ($filter['status'] > 0) {
231 $where[] = 'status = ' . $ilDB->quote($filter['status'], 'integer');
232 } else {
233 $where[] = '(status != ' . $ilDB->quote(-$filter['status'], 'integer') .
234 ' OR status IS NULL)';
235 }
236 }
237 if ($filter['title']) {
238 $where[] = '(' . $ilDB->like('title', 'text', '%' . $filter['title'] . '%') .
239 ' OR ' . $ilDB->like('description', 'text', '%' . $filter['title'] . '%') . ')';
240 }
241 if ($a_has_schedule) {
242 if ($filter['from']) {
243 $where[] = 'date_from >= ' . $ilDB->quote($filter['from'], 'integer');
244 }
245 if ($filter['to']) {
246 $where[] = 'date_to <= ' . $ilDB->quote($filter['to'], 'integer');
247 }
248 if (!$filter['past']) {
249 $where[] = 'date_to > ' . $ilDB->quote(time(), 'integer');
250 }
251 }
252 if ($filter['user_id']) { // #16584
253 $where[] = 'user_id = ' . $ilDB->quote($filter['user_id'], 'integer');
254 }
255 /*
256 if($a_group_id)
257 {
258 $where[] = 'group_id = '.$ilDB->quote(substr($a_group_id, 1), 'integer');
259 }
260 */
261 if (sizeof($where)) {
262 $sql .= ' WHERE ' . implode(' AND ', $where);
263 }
264
265 if ($a_has_schedule) {
266 $sql .= ' ORDER BY date_from DESC';
267 } else {
268 // #16155 - could be cancelled and re-booked
269 $sql .= ' ORDER BY status';
270 }
271
272 $set = $ilDB->query($sql);
273 while ($row = $ilDB->fetchAssoc($set)) {
274 $obj_id = $row["object_id"];
275 $user_id = $row["user_id"];
276
277 if ($a_has_schedule) {
278 $slot = $row["date_from"] . "_" . $row["date_to"];
279 $idx = $obj_id . "_" . $user_id . "_" . $slot;
280 } else {
281 $idx = $obj_id . "_" . $user_id;
282 }
283 $idx .= "_" . $row["context_obj_id"];
284
285 if ($a_has_schedule && $filter["slot"]) {
286 $slot_idx = date("w", $row["date_from"]) . "_" . date("H:i", $row["date_from"]) .
287 "-" . date("H:i", $row["date_to"] + 1);
288 if ($filter["slot"] != $slot_idx) {
289 continue;
290 }
291 }
292
293 if (!isset($res[$idx])) {
294 $uname = ilObjUser::_lookupName($user_id);
295
296 $res[$idx] = array(
297 "object_id" => $obj_id
298 ,"title" => $row["title"]
299 ,"pool_id" => $row["pool_id"]
300 ,"context_obj_id" => (int) $row["context_obj_id"]
301 ,"user_id" => $user_id
302 ,"counter" => 1
303 ,"user_name" => $uname["lastname"] . ", " . $uname["firstname"] // #17862
304 );
305
306 if ($a_has_schedule) {
307 $res[$idx]["booking_reservation_id"] = $idx;
308 $res[$idx]["date"] = date("Y-m-d", $row["date_from"]);
309 $res[$idx]["slot"] = date("H:i", $row["date_from"]) . " - " .
310 date("H:i", $row["date_to"] + 1);
311 $res[$idx]["week"] = date("W", $row["date_from"]);
312 $res[$idx]["weekday"] = date("w", $row["date_from"]);
313 $res[$idx]["can_be_cancelled"] = ($row["status"] != ilBookingReservation::STATUS_CANCELLED &&
314 $row["date_from"] > time());
315 $res[$idx]["_sortdate"] = $row["date_from"];
316
317 // this currently means: has any cancelled reservations (it is not grouped by this info)
318 $res[$idx]["status"] = $row["status"];
319 } else {
320 $res[$idx]["booking_reservation_id"] = $row["booking_reservation_id"];
321 $res[$idx]["status"] = $row["status"];
322 $res[$idx]["can_be_cancelled"] = ($row["status"] != ilBookingReservation::STATUS_CANCELLED);
323 }
324 } else {
325 $res[$idx]["counter"]++;
326 }
327 }
328
329 return $res;
330 }
331
335
341 protected function preloadByContextIds($context_obj_ids)
342 {
343 $filter = ["context_obj_ids" => ($context_obj_ids)];
344 $filter['past'] = true;
345 $filter['status'] = -ilBookingReservation::STATUS_CANCELLED;
347 $repo = $f->getRepo();
348 $list = $repo->getListByDate(true, null, $filter);
349 $list = ilUtil::sortArray($list, "slot", "asc", true);
350 $list = ilUtil::stableSortArray($list, "date", "asc", true);
351 $list = ilUtil::stableSortArray($list, "object_id", "asc", true);
352 $this->preloaded_by_context_list = ilUtil::stableSortArray($list, "pool_id", "asc", true);
353 }
354
362 public function getCachedContextObjBookingInfo($context_obj_id)
363 {
364 if (!is_array($this->preloaded_by_context_list)) {
365 throw new ilBookingReservationException("Repo not initilialized.");
366 }
367 return array_filter($this->preloaded_by_context_list, function ($row) use ($context_obj_id) {
368 return ($row["context_obj_id"] == $context_obj_id);
369 });
370 }
371}
An exception for terminatinating execution or to throw for unit testing.
getCachedContextObjBookingInfo($context_obj_id)
Get context object properties info.
update(int $id, int $user_id, int $assigner_id, int $object_id, $context_obj_id, int $from, int $to, $status, $group_id)
Update.
create(int $user_id, int $assigner_id, int $object_id, $context_obj_id, int $from, int $to, $status, $group_id)
Insert.
preloadByContextIds($context_obj_ids)
Preload reservation information for context obj ids.
__construct(ilDBInterface $db, $preload_context_obj_ids=null)
Constructor.
getNumberOfReservations(array $ids, int $from, int $to, $only_not_over_yet=false)
Get number of uncancelled reservations in time frame.
getListByDate( $a_has_schedule, array $a_object_ids=null, array $filter=null, array $a_pool_ids=null)
List all reservations by date.
static _lookupName($a_user_id)
lookup user name
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
static stableSortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
Interface ilDBInterface.
foreach($_POST as $key=> $value) $res
global $ilDB