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