ILIAS  release_8 Revision v8.24
class.ilSessionAppointment.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
31{
33 protected ilDBInterface $db;
34 protected ilTree $tree;
35 protected ilLanguage $lng;
36 protected ?ilDateTime $start = null;
37 protected ?ilDateTime $end = null;
38 protected int $starting_time = 0;
39 protected int $ending_time = 0;
40 protected bool $fulltime = false;
41 protected int $appointment_id = 0;
42 protected int $session_id = 0;
43
44 public function __construct(int $a_appointment_id = 0)
45 {
46 global $DIC;
47
48 $this->ilErr = $DIC['ilErr'];
49 $this->db = $DIC->database();
50 $this->tree = $DIC->repositoryTree();
51 $this->lng = $DIC->language();
52
53 $this->appointment_id = $a_appointment_id;
54 $this->__read();
55 }
56
57 public static function _lookupAppointment(int $a_obj_id): array
58 {
59 global $DIC;
60
61 $ilDB = $DIC->database();
62
63 $query = "SELECT * FROM event_appointment " .
64 "WHERE event_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
65 $res = $ilDB->query($query);
66 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
67 $info['fullday'] = $row->fulltime;
68
69 $date = new ilDateTime($row->e_start, IL_CAL_DATETIME, 'UTC');
70 $info['start'] = $date->getUnixTime();
71 $date = new ilDateTime($row->e_end, IL_CAL_DATETIME, 'UTC');
72 $info['end'] = $date->getUnixTime();
73
74 return $info;
75 }
76 return [];
77 }
78
82 public static function lookupNextSessionByCourse(int $a_ref_id)
83 {
84 global $DIC;
85
86 $tree = $DIC->repositoryTree();
87 $ilDB = $DIC->database();
88
89
90 $sessions = $tree->getChildsByType($a_ref_id, 'sess');
91 $obj_ids = [];
92 foreach ($sessions as $tree_data) {
93 $obj_ids[] = $tree_data['obj_id'];
94 }
95 if (!count($obj_ids)) {
96 return false;
97 }
98
99 // Try to read the next sessions within the next 24 hours
100 $now = new ilDate(time(), IL_CAL_UNIX);
101 $tomorrow = clone $now;
102 $tomorrow->increment(IL_CAL_DAY, 2);
103
104 $query = "SELECT event_id FROM event_appointment " .
105 "WHERE e_start > " . $ilDB->quote($now->get(IL_CAL_DATE), 'timestamp') . ' ' .
106 "AND e_start < " . $ilDB->quote($tomorrow->get(IL_CAL_DATE), 'timestamp') . ' ' .
107 "AND " . $ilDB->in('event_id', $obj_ids, false, 'integer') . ' ' .
108 "ORDER BY e_start ";
109
110 $event_ids = [];
111
112 $res = $ilDB->query($query);
113 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
114 $event_ids[] = $row->event_id;
115 }
116
117 if (count($event_ids)) {
118 return $event_ids;
119 }
120
121 // Alternativ: get next event.
122 $query = "SELECT event_id FROM event_appointment " .
123 "WHERE e_start > " . $ilDB->now() . " " .
124 "AND " . $ilDB->in('event_id', $obj_ids, false, 'integer') . " " .
125 "ORDER BY e_start ";
126 $ilDB->setLimit(1, 0);
127 $res = $ilDB->query($query);
128 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
129 $event_id = $row->event_id;
130 }
131 return isset($event_id) ? [$event_id] : [];
132 }
133
137 public static function lookupLastSessionByCourse(int $a_ref_id)
138 {
139 global $DIC;
140
141 $tree = $DIC->repositoryTree();
142 $ilDB = $DIC->database();
143
144 $sessions = $tree->getChildsByType($a_ref_id, 'sess');
145 $obj_ids = [];
146 foreach ($sessions as $tree_data) {
147 $obj_ids[] = $tree_data['obj_id'];
148 }
149 if (!count($obj_ids)) {
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, 0);
157 $res = $ilDB->query($query);
158 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
159 $event_id = (int) $row->event_id;
160 }
161 return $event_id ?? 0;
162 }
163
164 public function isFullday(): bool
165 {
166 return $this->enabledFullTime();
167 }
168
169 public function getStart(): ?ilDateTime
170 {
171 return $this->start ?: $this->start = new ilDateTime(date('Y-m-d') . ' 08:00:00', IL_CAL_DATETIME);
172 }
173
174 public function setStart(ilDateTime $a_start): void
175 {
176 $this->start = $a_start;
177 }
178
179 public function getEnd(): ?ilDateTime
180 {
181 return $this->end ?: $this->end = new ilDateTime(date('Y-m-d') . ' 16:00:00', IL_CAL_DATETIME);
182 }
183
184 public function setEnd(ilDateTime $a_end): void
185 {
186 $this->end = $a_end;
187 }
188
189 public function setAppointmentId(int $a_appointment_id): void
190 {
191 $this->appointment_id = $a_appointment_id;
192 }
193
194 public function getAppointmentId(): int
195 {
197 }
198
199 public function setSessionId(int $a_session_id): void
200 {
201 $this->session_id = $a_session_id;
202 }
203 public function getSessionId(): int
204 {
205 return $this->session_id;
206 }
207
208 public function setStartingTime(int $a_starting_time): void
209 {
210 $this->starting_time = $a_starting_time;
211 $this->start = new ilDateTime($this->starting_time, IL_CAL_UNIX);
212 }
213
214 public function getStartingTime(): int
215 {
216 return $this->starting_time ?? mktime(8, 0, 0, (int) date('n', time()), (int) date('j', time()), (int) date('Y', time()));
217 }
218
219 public function setEndingTime(int $a_ending_time): void
220 {
221 $this->ending_time = $a_ending_time;
222 $this->end = new ilDateTime($this->ending_time, IL_CAL_UNIX);
223 }
224 public function getEndingTime(): int
225 {
226 return $this->ending_time ?? mktime(16, 0, 0, (int) date('n', time()), (int) date('j', time()), (int) date('Y', time()));
227 }
228
229 public function toggleFullTime(bool $a_status): void
230 {
231 $this->fulltime = $a_status;
232 }
233 public function enabledFullTime(): bool
234 {
235 return $this->fulltime;
236 }
237
238 public function formatTime(): string
239 {
240 return $this->timeToString($this->getStartingTime(), $this->getEndingTime());
241 }
242
243 public function timeToString(int $start, int $end): string
244 {
246
247 $start = date($lng->txt('lang_timeformat_no_sec'), $start);
248 $end = date($lng->txt('lang_timeformat_no_sec'), $end);
249
250 return $start . ' - ' . $end;
251 }
252
253 public static function _appointmentToString(int $start, int $end, bool $fulltime): string
254 {
255 global $DIC;
256
257 $lng = $DIC->language();
258
259 if ($fulltime) {
262 #new ilDate($end,IL_CAL_UNIX)).' ('.$lng->txt('event_full_time_info').')';
264 );
265 } else {
269 );
270 }
271 }
272
273 public function appointmentToString(): string
274 {
275 return self::_appointmentToString($this->getStartingTime(), $this->getEndingTime(), $this->isFullday());
276 }
277
278 public function cloneObject(int $new_id): self
279 {
280 $new_app = new ilSessionAppointment();
281 $new_app->setSessionId($new_id);
282 $new_app->setStartingTime($this->getStartingTime());
283 $new_app->setEndingTime($this->getEndingTime());
284 $new_app->toggleFullTime($this->enabledFullTime());
285 $new_app->create();
286
287 return $new_app;
288 }
289
290 public function create(): bool
291 {
293
294 if (!$this->getSessionId()) {
295 return false;
296 }
297 $next_id = $ilDB->nextId('event_appointment');
298 $query = "INSERT INTO event_appointment (appointment_id,event_id,e_start,e_end,fulltime) " .
299 "VALUES( " .
300 $ilDB->quote($next_id, 'integer') . ", " .
301 $ilDB->quote($this->getSessionId(), 'integer') . ", " .
302 $ilDB->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
303 $ilDB->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
304 $ilDB->quote((int) $this->enabledFullTime(), 'integer') . " " .
305 ")";
306 $this->appointment_id = $next_id;
307 $res = $ilDB->manipulate($query);
308
309 return true;
310 }
311
312 public function update(): bool
313 {
315
316 if (!$this->getSessionId()) {
317 return false;
318 }
319 $query = "UPDATE event_appointment " .
320 "SET event_id = " . $ilDB->quote($this->getSessionId(), 'integer') . ", " .
321 "e_start = " . $ilDB->quote($this->getStart()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
322 "e_end = " . $ilDB->quote($this->getEnd()->get(IL_CAL_DATETIME, '', 'UTC'), 'timestamp') . ", " .
323 "fulltime = " . $ilDB->quote((int) $this->enabledFullTime(), 'integer') . " " .
324 "WHERE appointment_id = " . $ilDB->quote($this->getAppointmentId(), 'integer') . " ";
325 $res = $ilDB->manipulate($query);
326
327 return true;
328 }
329
330 public function delete(): bool
331 {
332 return self::_delete($this->getAppointmentId());
333 }
334
335 public static function _delete(int $a_appointment_id): bool
336 {
337 global $DIC;
338
339 $ilDB = $DIC->database();
340
341 $query = "DELETE FROM event_appointment " .
342 "WHERE appointment_id = " . $ilDB->quote($a_appointment_id, 'integer') . " ";
343 $res = $ilDB->manipulate($query);
344
345 return true;
346 }
347
348 public static function _deleteBySession(int $a_event_id): bool
349 {
350 global $DIC;
351
352 $ilDB = $DIC->database();
353
354 $query = "DELETE FROM event_appointment " .
355 "WHERE event_id = " . $ilDB->quote($a_event_id, 'integer') . " ";
356 $res = $ilDB->manipulate($query);
357
358 return true;
359 }
360
361 public static function _readAppointmentsBySession(int $a_event_id): array
362 {
363 global $DIC;
364
365 $ilDB = $DIC->database();
366
367 $query = "SELECT * FROM event_appointment " .
368 "WHERE event_id = " . $ilDB->quote($a_event_id, 'integer') . " " .
369 "ORDER BY starting_time";
370
371 $res = $ilDB->query($query);
372 $appointments = [];
373 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
374 $appointments[] = new ilSessionAppointment((int) $row->appointment_id);
375 }
376 return $appointments;
377 }
378
379 public function validate(): bool
380 {
381 if ($this->starting_time > $this->ending_time) {
382 $this->ilErr->appendMessage($this->lng->txt('event_etime_smaller_stime'));
383 return false;
384 }
385 return true;
386 }
387
388 protected function __read(): ?bool
389 {
391
392 if (!$this->getAppointmentId()) {
393 return null;
394 }
395
396 $query = "SELECT * FROM event_appointment " .
397 "WHERE appointment_id = " . $ilDB->quote($this->getAppointmentId(), 'integer') . " ";
398 $res = $this->db->query($query);
399 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
400 $this->setSessionId((int) $row->event_id);
401 $this->toggleFullTime((bool) $row->fulltime);
402
403 if ($this->isFullday()) {
404 $this->start = new ilDate($row->e_start, IL_CAL_DATETIME);
405 $this->end = new ilDate($row->e_end, IL_CAL_DATETIME);
406 } else {
407 $this->start = new ilDateTime($row->e_start, IL_CAL_DATETIME, 'UTC');
408 $this->end = new ilDateTime($row->e_end, IL_CAL_DATETIME, 'UTC');
409 }
410 $this->starting_time = (int) $this->start->getUnixTime();
411 $this->ending_time = (int) $this->end->getUnixTime();
412 }
413 return true;
414 }
415}
const IL_CAL_DATE
const IL_CAL_UNIX
const IL_CAL_DATETIME
const IL_CAL_DAY
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
Class for single dates.
Error Handling & global info handling uses PEAR error class.
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getStart()
Get start of date period.
timeToString(int $start, int $end)
static lookupNextSessionByCourse(int $a_ref_id)
static _deleteBySession(int $a_event_id)
isFullday()
is event a fullday period
static _lookupAppointment(int $a_obj_id)
__construct(int $a_appointment_id=0)
setStartingTime(int $a_starting_time)
static _readAppointmentsBySession(int $a_event_id)
static lookupLastSessionByCourse(int $a_ref_id)
setAppointmentId(int $a_appointment_id)
static _appointmentToString(int $start, int $end, bool $fulltime)
static _delete(int $a_appointment_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getChildsByType(int $a_node_id, string $a_type)
get child nodes of given node by object type
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$query