ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSessionAppointment.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once('./Services/Calendar/interfaces/interface.ilDatePeriod.php');
6 include_once('./Services/Calendar/classes/class.ilDate.php');
16 {
17  var $ilErr;
18  var $ilDB;
19  var $tree;
20  var $lng;
21 
22  protected $start = null;
23  protected $end = null;
24 
25  var $starting_time = null;
26  var $ending_time = null;
27 
28  function ilSessionAppointment($a_appointment_id = null)
29  {
30  global $ilErr,$ilDB,$lng,$tree;
31 
32  $this->ilErr =& $ilErr;
33  $this->db =& $ilDB;
34  $this->lng =& $lng;
35 
36  $this->appointment_id = $a_appointment_id;
37  $this->__read();
38  }
39 
47  public static function _lookupAppointment($a_obj_id)
48  {
49  global $ilDB;
50 
51  $query = "SELECT * FROM event_appointment ".
52  "WHERE event_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
53  $res = $ilDB->query($query);
54  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
55  {
56  $info['fullday'] = $row->fulltime;
57 
58  $date = new ilDateTime($row->e_start,IL_CAL_DATETIME,'UTC');
59  $info['start'] = $date->getUnixTime();
60  $date = new ilDateTime($row->e_end,IL_CAL_DATETIME,'UTC');
61  $info['end'] = $date->getUnixTime();
62 
63  return $info;
64  }
65  return array();
66  }
67 
76  public static function lookupNextSessionByCourse($a_ref_id)
77  {
78  global $tree,$ilDB;
79 
80 
81  $sessions = $tree->getChildsByType($a_ref_id,'sess');
82  $obj_ids = array();
83  foreach($sessions as $tree_data)
84  {
85  $obj_ids[] = $tree_data['obj_id'];
86  }
87  if(!count($obj_ids))
88  {
89  return false;
90  }
91 
92  // Try to read the next sessions within the next 24 hours
93  $now = new ilDate(time(),IL_CAL_UNIX);
94  $tomorrow = clone $now;
95  $tomorrow->increment(IL_CAL_DAY,2);
96 
97  $query = "SELECT event_id FROM event_appointment ".
98  "WHERE e_start > ".$ilDB->quote($now->get(IL_CAL_DATE,'timestamp')).' '.
99  "AND e_start < ".$ilDB->quote($tomorrow->get(IL_CAL_DATE,'timestamp')).' '.
100  "AND ".$ilDB->in('event_id',$obj_ids,false,'integer').' '.
101  "ORDER BY e_start ";
102 
103  $event_ids = array();
104 
105  $res = $ilDB->query($query);
106  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
107  {
108  $event_ids[] = $row->event_id;
109  }
110 
111  if(count($event_ids))
112  {
113  return $event_ids;
114  }
115 
116  // Alternativ: get next event.
117  $query = "SELECT event_id FROM event_appointment ".
118  "WHERE e_start > ".$ilDB->now()." ".
119  "AND ".$ilDB->in('event_id',$obj_ids,false,'integer')." ".
120  "ORDER BY e_start ";
121  $ilDB->setLimit(1);
122  $res = $ilDB->query($query);
123  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
124  {
125  $event_id = $row->event_id;
126  }
127  return isset($event_id) ? array($event_id) : array();
128  }
129 
138  public static function lookupLastSessionByCourse($a_ref_id)
139  {
140  global $tree,$ilDB;
141 
142  $sessions = $tree->getChildsByType($a_ref_id,'sess');
143  $obj_ids = array();
144  foreach($sessions as $tree_data)
145  {
146  $obj_ids[] = $tree_data['obj_id'];
147  }
148  if(!count($obj_ids))
149  {
150  return false;
151  }
152  $query = "SELECT event_id FROM event_appointment ".
153  "WHERE e_start < ".$ilDB->now()." ".
154  "AND ".$ilDB->in('event_id',$obj_ids,false,'integer')." ".
155  "ORDER BY e_start DESC ";
156  $ilDB->setLimit(1);
157  $res = $ilDB->query($query);
158  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
159  {
160  $event_id = $row->event_id;
161  }
162  return isset($event_id) ? $event_id : 0;
163  }
164 
165  // Interface methods
173  public function isFullday()
174  {
175  return $this->enabledFullTime();
176  }
177 
185  public function getStart()
186  {
187  return $this->start ? $this->start : $this->start = new ilDateTime(date('Y-m-d').' 08:00:00',IL_CAL_DATETIME);
188  }
189 
196  public function setStart($a_start)
197  {
198  $this->start = $a_start;
199  }
200 
207  public function getEnd()
208  {
209  return $this->end ? $this->end : $this->end = new ilDateTime(date('Y-m-d').' 16:00:00',IL_CAL_DATETIME);
210  }
211 
219  public function setEnd($a_end)
220  {
221  $this->end = $a_end;
222  }
223 
224  function setAppointmentId($a_appointment_id)
225  {
226  $this->appointment_id = $a_appointment_id;
227  }
228  function getAppointmentId()
229  {
230  return $this->appointment_id;
231  }
232 
233  function setSessionId($a_session_id)
234  {
235  $this->session_id = $a_session_id;
236  }
237  function getSessionId()
238  {
239  return $this->session_id;
240  }
241 
242  function setStartingTime($a_starting_time)
243  {
244  $this->starting_time = $a_starting_time;
245  $this->start = new ilDateTime($this->starting_time,IL_CAL_UNIX);
246 
247  }
248  function getStartingTime()
249  {
250  return isset($this->starting_time) ? $this->starting_time : mktime(8,0,0,date('n',time()),date('d',time()),date('Y',time()));
251  }
252 
253  function setEndingTime($a_ending_time)
254  {
255  $this->ending_time = $a_ending_time;
256  $this->end = new ilDateTime($this->ending_time,IL_CAL_UNIX);
257  }
258  function getEndingTime()
259  {
260  return isset($this->ending_time) ? $this->ending_time : mktime(16,0,0,date('n',time()),date('d',time()),date('Y',time()));
261  }
262 
263  function toggleFullTime($a_status)
264  {
265  $this->fulltime = $a_status;
266  }
267  function enabledFullTime()
268  {
269  return $this->fulltime;
270  }
271 
272  function formatTime()
273  {
275  }
276 
278  {
279  global $ilUser,$lng;
280 
281  $start = date($this->lng->txt('lang_timeformat_no_sec'),$start);
282  $end = date($this->lng->txt('lang_timeformat_no_sec'),$end);
283 
284  return $start.' - '. $end;
285  }
286 
287  public static function _appointmentToString($start,$end,$fulltime)
288  {
289  global $lng;
290 
291  if($fulltime)
292  {
295  #new ilDate($end,IL_CAL_UNIX)).' ('.$lng->txt('event_full_time_info').')';
296  new ilDate($end,IL_CAL_UNIX));
297  }
298  else
299  {
303  }
304  }
305 
307  {
309  }
310 
318  public function cloneObject($new_id)
319  {
320  $new_app = new ilSessionAppointment();
321  $new_app->setSessionId($new_id);
322  $new_app->setStartingTime($this->getStartingTime());
323  $new_app->setEndingTime($this->getEndingTime());
324  $new_app->toggleFullTime($this->isFullday());
325  $new_app->create();
326  return $new_app;
327  }
328 
329  function create()
330  {
331  global $ilDB;
332 
333  if(!$this->getSessionId())
334  {
335  return false;
336  }
337  $next_id = $ilDB->nextId('event_appointment');
338  $query = "INSERT INTO event_appointment (appointment_id,event_id,e_start,e_end,fulltime) ".
339  "VALUES( ".
340  $ilDB->quote($next_id,'integer').", ".
341  $ilDB->quote($this->getSessionId() ,'integer').", ".
342  $ilDB->quote($this->getStart()->get(IL_CAL_DATETIME,'','UTC') ,'timestamp').", ".
343  $ilDB->quote($this->getEnd()->get(IL_CAL_DATETIME,'','UTC') ,'timestamp').", ".
344  $ilDB->quote($this->enabledFullTime() ,'integer')." ".
345  ")";
346  $this->appointment_id = $next_id;
347  $res = $ilDB->manipulate($query);
348 
349  return true;
350  }
351 
352  function update()
353  {
354  global $ilDB;
355 
356  if(!$this->getSessionId())
357  {
358  return false;
359  }
360  $query = "UPDATE event_appointment ".
361  "SET event_id = ".$ilDB->quote($this->getSessionId() ,'integer').", ".
362  "e_start = ".$ilDB->quote($this->getStart()->get(IL_CAL_DATETIME,'','UTC') ,'timestamp').", ".
363  "e_end = ".$ilDB->quote($this->getEnd()->get(IL_CAL_DATETIME,'','UTC'), 'timestamp').", ".
364  "fulltime = ".$ilDB->quote($this->enabledFullTime() ,'integer')." ".
365  "WHERE appointment_id = ".$ilDB->quote($this->getAppointmentId() ,'integer')." ";
366  $res = $ilDB->manipulate($query);
367  return true;
368  }
369 
370  function delete()
371  {
373  }
374 
375  function _delete($a_appointment_id)
376  {
377  global $ilDB;
378 
379  $query = "DELETE FROM event_appointment ".
380  "WHERE appointment_id = ".$ilDB->quote($a_appointment_id ,'integer')." ";
381  $res = $ilDB->manipulate($query);
382 
383  return true;
384  }
385 
386  function _deleteBySession($a_event_id)
387  {
388  global $ilDB;
389 
390  $query = "DELETE FROM event_appointment ".
391  "WHERE event_id = ".$ilDB->quote($a_event_id ,'integer')." ";
392  $res = $ilDB->manipulate($query);
393 
394  return true;
395  }
396 
397  function _readAppointmentsBySession($a_event_id)
398  {
399  global $ilDB;
400 
401  $query = "SELECT * FROM event_appointment ".
402  "WHERE event_id = ".$ilDB->quote($a_event_id ,'integer')." ".
403  "ORDER BY starting_time";
404 
405  $res = $ilDB->query($query);
406  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
407  {
408  $appointments[] =& new ilSessionAppointment($row->appointment_id);
409  }
410  return is_array($appointments) ? $appointments : array();
411  }
412 
413  function validate()
414  {
415  if($this->starting_time > $this->ending_time)
416  {
417  $this->ilErr->appendMessage($this->lng->txt('event_etime_smaller_stime'));
418  return false;
419  }
420  return true;
421  }
422 
423  // PRIVATE
424  function __read()
425  {
426  global $ilDB;
427 
428  if(!$this->getAppointmentId())
429  {
430  return null;
431  }
432 
433  $query = "SELECT * FROM event_appointment ".
434  "WHERE appointment_id = ".$ilDB->quote($this->getAppointmentId() ,'integer')." ";
435  $res = $this->db->query($query);
436  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
437  {
438  $this->setSessionId($row->event_id);
439  $this->toggleFullTime($row->fulltime);
440 
441  if($this->isFullday())
442  {
443  $this->start = new ilDate($row->e_start,IL_CAL_DATETIME);
444  $this->end = new ilDate($row->e_end,IL_CAL_DATETIME);
445  }
446  else
447  {
448  $this->start = new ilDateTime($row->e_start,IL_CAL_DATETIME,'UTC');
449  $this->end = new ilDateTime($row->e_end,IL_CAL_DATETIME,'UTC');
450  }
451  $this->starting_time = $this->start->getUnixTime();
452  $this->ending_time = $this->end->getUnixTime();
453  }
454  return true;
455  }
456 
457 }
458 ?>