ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCalendarCategory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const LTYPE_LOCAL = 1;
29 public const LTYPE_REMOTE = 2;
30
31 private static ?array $instances = [];
32
33 public const DEFAULT_COLOR = '#04427e';
34
35 public const TYPE_UNDEFINED = 0;
36 public const TYPE_USR = 1; // user
37 public const TYPE_OBJ = 2; // object
38 public const TYPE_GLOBAL = 3; // global
39 public const TYPE_CH = 4; // consultation hours
40 public const TYPE_BOOK = 5; // booking manager
41
42 protected static array $SORTED_TYPES = array(
43 0 => self::TYPE_GLOBAL,
44 1 => self::TYPE_USR,
45 2 => self::TYPE_CH,
46 3 => self::TYPE_BOOK,
47 4 => self::TYPE_OBJ
48 );
49
50 protected int $cat_id = 0;
51 protected string $color = '';
52 protected int $type = self::TYPE_USR;
53 protected int $obj_id = 0;
54 protected string $obj_type = '';
55 protected string $title = '';
56
58 protected string $remote_url = '';
59 protected string $remote_user = '';
60 protected string $remote_pass = '';
61 protected ?ilDateTime $remote_sync = null;
62
63 protected ilDBInterface $db;
64
65 public function __construct(int $a_cat_id = 0)
66 {
67 global $DIC;
68
69 $this->db = $DIC->database();
70 $this->cat_id = $a_cat_id;
71
72 $this->read();
73 }
74
78 public static function _getInstanceByObjId(int $a_obj_id): ?ilCalendarCategory
79 {
80 global $DIC;
81
82 $ilDB = $DIC['ilDB'];
83 $query = "SELECT cat_id FROM cal_categories " .
84 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " " .
85 "AND type = " . $ilDB->quote(self::TYPE_OBJ, 'integer');
86 $res = $ilDB->query($query);
87 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
88 return new ilCalendarCategory((int) $row->cat_id);
89 }
90 return null;
91 }
92
93 public static function getInstanceByCategoryId(int $a_cat_id): ilCalendarCategory
94 {
95 if (!isset(self::$instances[$a_cat_id])) {
96 return self::$instances[$a_cat_id] = new ilCalendarCategory($a_cat_id);
97 }
98 return self::$instances[$a_cat_id];
99 }
100
104 public static function lookupCategorySortIndex(int $a_type_id): int
105 {
106 return (int) array_search($a_type_id, self::$SORTED_TYPES);
107 }
108
113 public static function lookupAppointments(int $a_category_id): array
114 {
115 global $DIC;
116
117 $ilDB = $DIC['ilDB'];
118 $query = "SELECT * FROM cal_cat_assignments " .
119 'WHERE cat_id = ' . $ilDB->quote($a_category_id, 'integer');
120 $res = $ilDB->query($query);
121 $apps = [];
122 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
123 $apps[] = (int) $row->cal_id;
124 }
125 return $apps;
126 }
127
128 public function getCategoryID(): int
129 {
130 return $this->cat_id;
131 }
132
133 public function setTitle(string $a_title): void
134 {
135 $this->title = $a_title;
136 }
137
138 public function getTitle(): string
139 {
140 return $this->title;
141 }
142
143 public function setColor(string $a_color): void
144 {
145 $this->color = $a_color;
146 }
147
148 public function getColor(): string
149 {
150 return $this->color;
151 }
152
153 public function setType(int $a_type): void
154 {
155 $this->type = $a_type;
156 }
157
158 public function getType(): int
159 {
160 return $this->type;
161 }
162
163 public function setObjId(int $a_obj_id): void
164 {
165 $this->obj_id = $a_obj_id;
166 }
167
168 public function getObjId(): int
169 {
170 return $this->obj_id;
171 }
172
173 public function getObjType(): string
174 {
175 return $this->obj_type;
176 }
177
178 public function getLocationType(): int
179 {
180 return $this->location;
181 }
182
183 public function setLocationType(int $a_type): void
184 {
185 $this->location = $a_type;
186 }
187
188 public function setRemoteUrl(string $a_url): void
189 {
190 $this->remote_url = $a_url;
191 }
192
193 public function getRemoteUrl(): string
194 {
195 return $this->remote_url;
196 }
197
198 public function setRemoteUser(string $a_user): void
199 {
200 $this->remote_user = $a_user;
201 }
202
203 public function getRemoteUser(): string
204 {
205 return $this->remote_user;
206 }
207
208 public function setRemotePass(string $a_pass): void
209 {
210 $this->remote_pass = $a_pass;
211 }
212
213 public function getRemotePass(): string
214 {
215 return $this->remote_pass;
216 }
217
221 public function setRemoteSyncLastExecution(ilDateTime $dt): void
222 {
223 $this->remote_sync = $dt;
224 }
225
230 {
231 if ($this->remote_sync instanceof ilDateTime) {
232 return $this->remote_sync;
233 }
234 return new ilDateTime();
235 }
236
237 public function add(): int
238 {
239 $next_id = $this->db->nextId('cal_categories');
240 $query = "INSERT INTO cal_categories (cat_id,obj_id,color,type,title,loc_type,remote_url,remote_user,remote_pass,remote_sync) " .
241 "VALUES ( " .
242 $this->db->quote($next_id, 'integer') . ", " .
243 $this->db->quote($this->getObjId(), 'integer') . ", " .
244 $this->db->quote($this->getColor(), 'text') . ", " .
245 $this->db->quote($this->getType(), 'integer') . ", " .
246 /*
247 * The title needs to be truncated to fit into the table column. This is a pretty
248 * brute force method for doing so, but right now I can't find a better place for it.
249 */
250 $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
251 $this->db->quote($this->getLocationType(), 'integer') . ', ' .
252 $this->db->quote($this->getRemoteUrl(), 'text') . ', ' .
253 $this->db->quote($this->getRemoteUser(), 'text') . ', ' .
254 $this->db->quote($this->getRemotePass(), 'text') . ', ' .
255 $this->db->quote(
257 'timestamp'
258 ) . ' ' .
259 ")";
260
261 $this->db->manipulate($query);
262
263 $this->cat_id = $next_id;
264 return $this->cat_id;
265 }
266
267 public function update(): void
268 {
269 $query = "UPDATE cal_categories " .
270 "SET obj_id = " . $this->db->quote($this->getObjId(), 'integer') . ", " .
271 "color = " . $this->db->quote($this->getColor(), 'text') . ", " .
272 "type = " . $this->db->quote($this->getType(), 'integer') . ", " .
273 /*
274 * The title needs to be truncated to fit into the table column. This is a pretty
275 * brute force method for doing so, but right now I can't find a better place for it.
276 */
277 "title = " . $this->db->quote(mb_substr($this->getTitle(), 0, 128), 'text') . ", " .
278 "loc_type = " . $this->db->quote($this->getLocationType(), 'integer') . ', ' .
279 "remote_url = " . $this->db->quote($this->getRemoteUrl(), 'text') . ', ' .
280 "remote_user = " . $this->db->quote($this->getRemoteUser(), 'text') . ', ' .
281 "remote_pass = " . $this->db->quote($this->getRemotePass(), 'text') . ', ' .
282 'remote_sync = ' . $this->db->quote($this->getRemoteSyncLastExecution()->get(
284 '',
286 ), 'timestamp') . ' ' .
287 "WHERE cat_id = " . $this->db->quote($this->cat_id, 'integer') . " ";
288 $res = $this->db->manipulate($query);
289 }
290
291 public function delete(): void
292 {
293 $query = "DELETE FROM cal_categories " .
294 "WHERE cat_id = " . $this->db->quote($this->cat_id, 'integer') . " ";
295 $res = $this->db->manipulate($query);
296
298
299 foreach (ilCalendarCategoryAssignments::_getAssignedAppointments(array($this->cat_id)) as $app_id) {
301 }
303 }
304
305 public function validate(): bool
306 {
308 return false;
309 }
310 if (strlen($this->getTitle()) && strlen($this->getColor()) && $this->getType()) {
311 return true;
312 }
313 return false;
314 }
315
316 private function read(): void
317 {
318 if (!$this->cat_id) {
319 return;
320 }
321
322 $query = "SELECT * FROM cal_categories " .
323 "WHERE cat_id = " . $this->db->quote($this->getCategoryID(), 'integer') . " ";
324 $res = $this->db->query($query);
325 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
326 $this->cat_id = (int) $row->cat_id;
327 $this->obj_id = (int) $row->obj_id;
328 $this->type = (int) $row->type;
329 $this->color = (string) $row->color;
330 $this->title = (string) $row->title;
331 $this->location = (int) $row->loc_type;
332 $this->remote_url = (string) $row->remote_url;
333 $this->remote_user = (string) $row->remote_user;
334 $this->remote_pass = (string) $row->remote_pass;
335
336 if ($row->remote_sync) {
337 $this->remote_sync = new ilDateTime((string) $row->remote_sync, IL_CAL_DATETIME, 'UTC');
338 } else {
339 $this->remote_sync = new ilDateTime();
340 }
341 }
342 if ($this->getType() == self::TYPE_OBJ) {
343 $this->title = ilObject::_lookupTitle($this->getObjId());
344 $this->obj_type = ilObject::_lookupType($this->getObjId());
345 }
346 }
347}
const IL_CAL_DATETIME
static _deleteByCategoryId(int $a_cat_id)
Delete assignments by category id @access public.
static _getAssignedAppointments(array $a_cat_id)
Get assigned apointments.
Stores calendar categories.
setRemoteSyncLastExecution(ilDateTime $dt)
Set remote sync last execution.
static lookupCategorySortIndex(int $a_type_id)
Lookup sort index of calendar type.
static lookupAppointments(int $a_category_id)
get all assigned appointment ids
static _getInstanceByObjId(int $a_obj_id)
get instance by obj_id
getRemoteSyncLastExecution()
Get last execution date of remote sync.
static getInstanceByCategoryId(int $a_cat_id)
static _delete(int $a_entry_id)
static _deleteCategories(int $a_cat_id)
@classDescription Date and time handling
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26