ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBookingReservation.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  protected $id; // int
15  protected $object_id; // int
16  protected $user_id; // int
17  protected $from; // timestamp
18  protected $to; // timestamp
19  protected $status; // status
20 
21  const STATUS_IN_USE = 2;
22  const STATUS_CANCELLED = 5;
23 
31  function __construct($a_id = NULL)
32  {
33  $this->id = (int)$a_id;
34  $this->read();
35  }
36 
41  function getId()
42  {
43  return $this->id;
44  }
45 
50  function setObjectId($a_object_id)
51  {
52  $this->object_id = $a_object_id;
53  }
54 
59  function getObjectId()
60  {
61  return $this->object_id;
62  }
63 
68  function setUserId($a_user_id)
69  {
70  $this->user_id = (int)$a_user_id;
71  }
72 
77  function getUserId()
78  {
79  return $this->user_id;
80  }
81 
86  function setFrom($a_from)
87  {
88  $this->from = (int)$a_from;
89  }
90 
95  function getFrom()
96  {
97  return $this->from;
98  }
99 
104  function setTo($a_to)
105  {
106  $this->to = (int)$a_to;
107  }
108 
113  function getTo()
114  {
115  return $this->to;
116  }
117 
122  function setStatus($a_status)
123  {
124  if($a_status === NULL)
125  {
126  $this->status = NULL;
127  }
128  if($this->isValidStatus((int)$a_status))
129  {
130  $this->status = (int)$a_status;
131  }
132  }
133 
138  function getStatus()
139  {
140  return $this->status;
141  }
142 
148  static function isValidStatus($a_status)
149  {
150  if(in_array($a_status, array(self::STATUS_IN_USE, self::STATUS_CANCELLED)))
151  {
152  return true;
153  }
154  return false;
155  }
156 
160  protected function read()
161  {
162  global $ilDB;
163 
164  if($this->id)
165  {
166  $set = $ilDB->query('SELECT object_id,user_id,date_from,date_to,status'.
167  ' FROM booking_reservation'.
168  ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
169  $row = $ilDB->fetchAssoc($set);
170  $this->setUserId($row['user_id']);
171  $this->setObjectId($row['object_id']);
172  $this->setFrom($row['date_from']);
173  $this->setTo($row['date_to']);
174  $this->setStatus($row['status']);
175  }
176  }
177 
182  function save()
183  {
184  global $ilDB;
185 
186  if($this->id)
187  {
188  return false;
189  }
190 
191  $this->id = $ilDB->nextId('booking_reservation');
192 
193  return $ilDB->manipulate('INSERT INTO booking_reservation'.
194  ' (booking_reservation_id,user_id,object_id,date_from,date_to,status)'.
195  ' VALUES ('.$ilDB->quote($this->id, 'integer').','.$ilDB->quote($this->getUserId(), 'integer').
196  ','.$ilDB->quote($this->getObjectId(), 'integer').','.$ilDB->quote($this->getFrom(), 'integer').
197  ','.$ilDB->quote($this->getTo(), 'integer').','.$ilDB->quote($this->getStatus(), 'integer').')');
198  }
199 
204  function update()
205  {
206  global $ilDB;
207 
208  if(!$this->id)
209  {
210  return false;
211  }
212 
213  // there can only be 1
214  if($this->getStatus() == self::STATUS_IN_USE)
215  {
216  $ilDB->manipulate('UPDATE booking_reservation'.
217  ' SET status = '.$ilDB->quote(NULL, 'integer').
218  ' WHERE object_id = '.$ilDB->quote($this->getObjectId(), 'integer').
219  ' AND status = '.$ilDB->quote(self::STATUS_IN_USE, 'integer'));
220  }
221 
222  return $ilDB->manipulate('UPDATE booking_reservation'.
223  ' SET object_id = '.$ilDB->quote($this->getObjectId(), 'text').
224  ', user_id = '.$ilDB->quote($this->getUserId(), 'integer').
225  ', date_from = '.$ilDB->quote($this->getFrom(), 'integer').
226  ', date_to = '.$ilDB->quote($this->getTo(), 'integer').
227  ', status = '.$ilDB->quote($this->getStatus(), 'integer').
228  ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
229  }
230 
235  function delete()
236  {
237  global $ilDB;
238 
239  if($this->id)
240  {
241  return $ilDB->manipulate('DELETE FROM booking_reservation'.
242  ' WHERE booking_reservation_id = '.$ilDB->quote($this->id, 'integer'));
243  }
244  }
245 
254  static function getAvailableObject(array $a_ids, $a_from, $a_to, $a_return_single = true)
255  {
256  global $ilDB;
257 
258  $nr_map = ilBookingObject::getNrOfItemsForObjects($a_ids);
259 
260  $from = $ilDB->quote($a_from, 'integer');
261  $to = $ilDB->quote($a_to, 'integer');
262 
263  $set = $ilDB->query('SELECT count(*) cnt, object_id'.
264  ' FROM booking_reservation'.
265  ' WHERE '.$ilDB->in('object_id', $a_ids, '', 'integer').
266  ' AND (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').')'.
267  ' AND ((date_from <= '.$from.' AND date_to >= '.$from.')'.
268  ' OR (date_from <= '.$to.' AND date_to >= '.$to.')'.
269  ' OR (date_from >= '.$from.' AND date_to <= '.$to.'))'.
270  ' GROUP BY object_id');
271  $blocked = array();
272  while($row = $ilDB->fetchAssoc($set))
273  {
274  if($row['cnt'] >= $nr_map[$row['object_id']])
275  {
276  $blocked[] = $row['object_id'];
277  }
278  }
279  $available = array_diff($a_ids, $blocked);
280  if(sizeof($available))
281  {
282  if($a_return_single)
283  {
284  return array_shift($available);
285  }
286  else
287  {
288  return $available;
289  }
290  }
291  }
292 
293  static function isObjectAvailableNoSchedule($a_obj_id)
294  {
295  global $ilDB;
296 
297  $all = ilBookingObject::getNrOfItemsForObjects(array($a_obj_id));
298  $all = (int)$all[$a_obj_id];
299 
300  $set = $ilDB->query('SELECT COUNT(*) cnt'.
301  ' FROM booking_reservation r'.
302  ' JOIN booking_object o ON (o.booking_object_id = r.object_id)'.
303  ' WHERE (status IS NULL OR status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').')'.
304  ' AND r.object_id = '.$ilDB->quote($a_obj_id, 'integer'));
305  $cnt = $ilDB->fetchAssoc($set);
306  $cnt = (int)$cnt['cnt'];
307 
308  return (bool)($all-$cnt); // #11864
309  }
310 
316  static function getCurrentOrUpcomingReservation($a_object_id)
317  {
318  global $ilDB;
319 
320  $now = $ilDB->quote(time(), 'integer');
321 
322  $ilDB->setLimit(1);
323  $set = $ilDB->query('SELECT user_id, status, date_from, date_to'.
324  ' FROM booking_reservation'.
325  ' WHERE ((date_from <= '.$now.' AND date_to >= '.$now.')'.
326  ' OR date_from > '.$now.')'.
327  ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').
328  ' OR STATUS IS NULL) AND object_id = '.$ilDB->quote($a_object_id, 'integer').
329  ' ORDER BY date_from');
330  $row = $ilDB->fetchAssoc($set);
331  return $row;
332  }
333 
334  static function getObjectReservationForUser($a_object_id, $a_user_id)
335  {
336  global $ilDB;
337 
338  $set = $ilDB->query('SELECT booking_reservation_id FROM booking_reservation'.
339  ' WHERE user_id = '.$ilDB->quote($a_user_id, 'integer').
340  ' AND object_id = '.$ilDB->quote($a_object_id, 'integer').
341  ' AND (status <> '.$ilDB->quote(self::STATUS_CANCELLED, 'integer').
342  ' OR STATUS IS NULL)');
343  $row = $ilDB->fetchAssoc($set);
344  return $row['booking_reservation_id'];
345  }
346 
355  static function getList($a_object_ids, $a_limit = 10, $a_offset = 0, array $filter)
356  {
357  global $ilDB;
358 
359  $sql = 'SELECT r.*,o.title'.
360  ' FROM booking_reservation r'.
361  ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
362 
363  $count_sql = 'SELECT COUNT(*) AS counter'.
364  ' FROM booking_reservation r'.
365  ' JOIN booking_object o ON (o.booking_object_id = r.object_id)';
366 
367  $where = array($ilDB->in('r.object_id', $a_object_ids, '', 'integer'));
368  if($filter['status'])
369  {
370  if($filter['status'] > 0)
371  {
372  $where[] = 'status = '.$ilDB->quote($filter['status'], 'integer');
373  }
374  else
375  {
376  $where[] = '(status != '.$ilDB->quote(-$filter['status'], 'integer').
377  ' OR status IS NULL)';
378  }
379  }
380  if($filter['from'])
381  {
382  $where[] = 'date_from >= '.$ilDB->quote($filter['from'], 'integer');
383  }
384  if($filter['to'])
385  {
386  $where[] = 'date_to <= '.$ilDB->quote($filter['to'], 'integer');
387  }
388  if(sizeof($where))
389  {
390  $sql .= ' WHERE '.implode(' AND ', $where);
391  $count_sql .= ' WHERE '.implode(' AND ', $where);
392  }
393 
394  $set = $ilDB->query($count_sql);
395  $row = $ilDB->fetchAssoc($set);
396  $counter = $row['counter'];
397 
398  $sql .= ' ORDER BY date_from DESC, booking_reservation_id DESC';
399 
400  $ilDB->setLimit($a_limit, $a_offset);
401  $set = $ilDB->query($sql);
402  $res = array();
403  while($row = $ilDB->fetchAssoc($set))
404  {
405  $res[] = $row;
406  }
407 
408  return array('data'=>$res, 'counter'=>$counter);
409  }
410 
417  static function changeStatus(array $a_ids, $a_status)
418  {
419  global $ilDB;
420 
421  if(self::isValidStatus($a_status))
422  {
423  return $ilDB->manipulate('UPDATE booking_reservation'.
424  ' SET status = '.$ilDB->quote($a_status, 'integer').
425  ' WHERE '.$ilDB->in('booking_reservation_id', $a_ids, '', 'integer'));
426 
427  }
428  }
429 
430  function getCalendarEntry()
431  {
432  global $ilDB;
433 
434  include_once 'Services/Calendar/classes/class.ilCalendarCategory.php';
435 
436  $set = $ilDB->query("SELECT ce.cal_id FROM cal_entries ce".
437  " JOIN cal_cat_assignments cca ON ce.cal_id = cca.cal_id".
438  " JOIN cal_categories cc ON cca.cat_id = cc.cat_id".
439  " JOIN booking_reservation br ON ce.context_id = br.booking_reservation_id".
440  " WHERE cc.obj_id = ".$ilDB->quote($this->getUserId(),'integer').
441  " AND br.user_id = ".$ilDB->quote($this->getUserId(),'integer').
442  " AND cc.type = ".$ilDB->quote(ilCalendarCategory::TYPE_BOOK,'integer').
443  " AND ce.context_id = ".$ilDB->quote($this->getId(), 'integer'));
444  $row = $ilDB->fetchAssoc($set);
445  return $row["cal_id"];
446  }
447 }
448 
449 ?>