ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilChatroomInstaller.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
34 public static function install(): void
35 {
39 global $DIC;
40
41 $ilDB = $DIC->database();
42
43 if (!$ilDB->tableExists('chatroom_settings')) {
44 $fields = [
45 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
46 'object_id' => ['type' => 'integer', 'length' => 4, 'notnull' => false, 'default' => 0],
47 'room_type' => ['type' => 'text', 'length' => 20, 'notnull' => true],
48 'allow_anonymous' => ['type' => 'integer', 'length' => 1, 'notnull' => false, 'default' => 0],
49 'allow_custom_usernames' => ['type' => 'integer', 'length' => 1, 'notnull' => false, 'default' => 0],
50 'enable_history' => ['type' => 'integer', 'length' => 1, 'notnull' => false, 'default' => 0],
51 'autogen_usernames' => ['type' => 'text', 'length' => 50, 'notnull' => false, 'default' => 'Anonymous #'],
52 'allow_private_rooms' => ['type' => 'integer', 'length' => 1, 'notnull' => false, 'default' => 0],
53 ];
54
55 $ilDB->createTable('chatroom_settings', $fields);
56 $ilDB->addPrimaryKey('chatroom_settings', ['room_id']);
57 $ilDB->createSequence('chatroom_settings');
58 }
59
60 if (!$ilDB->tableExists('chatroom_users')) {
61 $fields = [
62 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
63 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
64 'userdata' => ['type' => 'text', 'length' => 4000, 'notnull' => true],
65 'connected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
66 ];
67 $ilDB->createTable('chatroom_users', $fields);
68 $ilDB->addPrimaryKey('chatroom_users', ['room_id', 'user_id']);
69 }
70
71 if (!$ilDB->tableExists('chatroom_sessions')) {
72 $fields = [
73 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
74 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
75 'userdata' => ['type' => 'text', 'length' => 4000, 'notnull' => true],
76 'connected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
77 'disconnected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
78 ];
79 $ilDB->createTable('chatroom_sessions', $fields);
80 }
81
82 if (!$ilDB->tableExists('chatroom_history')) {
83 $fields = [
84 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
85 'message' => ['type' => 'text', 'length' => 4000, 'notnull' => true],
86 'timestamp' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
87 ];
88 $ilDB->createTable('chatroom_history', $fields);
89 }
90
91 if (!$ilDB->tableExists('chatroom_bans')) {
92 $fields = [
93 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
94 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
95 'timestamp' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
96 'remark' => ['type' => 'text', 'length' => 1000, 'notnull' => false],
97 ];
98 $ilDB->createTable('chatroom_bans', $fields);
99 }
100
101 if (!$ilDB->tableExists('chatroom_admconfig')) {
102 $fields = [
103 'instance_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
104 'server_settings' => ['type' => 'text', 'length' => 2000, 'notnull' => true],
105 'default_config' => ['type' => 'integer', 'length' => 1, 'notnull' => true, 'default' => 0],
106 ];
107 $ilDB->createTable('chatroom_admconfig', $fields);
108 $ilDB->addPrimaryKey('chatroom_admconfig', ['instance_id']);
109 $ilDB->createSequence('chatroom_admconfig');
110 }
111
112 if (!$ilDB->tableExists('chatroom_prooms')) {
113 $fields = [
114 'proom_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
115 'parent_id' => ['type' => 'text', 'length' => 2000, 'notnull' => true],
116 'title' => ['type' => 'text', 'length' => 200, 'notnull' => true, 'default' => 0],
117 'owner' => ['type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0],
118 'created' => ['type' => 'integer', 'length' => 4, 'notnull' => true, 'default' => 0],
119 'closed' => ['type' => 'integer', 'length' => 4, 'notnull' => false, 'default' => 0],
120 ];
121 $ilDB->createTable('chatroom_prooms', $fields);
122 $ilDB->addPrimaryKey('chatroom_prooms', ['proom_id']);
123 $ilDB->createSequence('chatroom_prooms');
124 }
125
126 if (!$ilDB->tableExists('chatroom_psessions')) {
127 $fields = [
128 'proom_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
129 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
130 'connected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
131 'disconnected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
132 ];
133 $ilDB->createTable('chatroom_psessions', $fields);
134 }
135
136 if (!$ilDB->tableExists('chatroom_uploads')) {
137 $fields = [
138 'upload_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
139 'room_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
140 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
141 'filename' => ['type' => 'text', 'length' => 200, 'notnull' => true],
142 'filetype' => ['type' => 'text', 'length' => 200, 'notnull' => true],
143 'timestamp' => ['type' => 'integer', 'length' => 4, 'notnull' => true]
144 ];
145 $ilDB->createTable('chatroom_uploads', $fields);
146 $ilDB->addPrimaryKey('chatroom_uploads', ['upload_id']);
147 $ilDB->createSequence('chatroom_uploads');
148 }
149
150 if (!$ilDB->tableColumnExists('chatroom_prooms', 'is_public')) {
151 $ilDB->addTableColumn('chatroom_prooms', 'is_public', ['type' => 'integer', 'default' => 1, 'length' => 1]);
152 }
153
154 if (!$ilDB->tableExists('chatroom_psessions')) {
155 $fields = [
156 'proom_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
157 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
158 'connected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
159 'disconnected' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
160 ];
161 $ilDB->createTable('chatroom_psessions', $fields);
162 }
163
164 if (!$ilDB->tableExists('chatroom_proomaccess')) {
165 $fields = [
166 'proom_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
167 'user_id' => ['type' => 'integer', 'length' => 4, 'notnull' => true],
168 ];
169 $ilDB->createTable('chatroom_proomaccess', $fields);
170 }
171
172 if (!$ilDB->tableColumnExists('chatroom_admconfig', 'client_settings')) {
173 $ilDB->addTableColumn(
174 "chatroom_admconfig",
175 "client_settings",
176 [
177 "type" => "text",
178 "length" => 1000,
179 "notnull" => true
180 ]
181 );
182 }
183
184 self::registerObject();
185 self::registerAdminObject();
186 self::removeOldChatEntries();
187 self::convertChatObjects();
188
189 $notificationSettings = new ilSetting('notifications');
190 $notificationSettings->set('enable_osd', '1');
191 }
192
197 public static function registerObject(): void
198 {
199 global $DIC;
200
202 $ilDB = $DIC->database();
203
204 $typ_id = null;
205
206 $query = 'SELECT obj_id FROM object_data ' .
207 'WHERE type = ' . $ilDB->quote('typ', 'text') . ' ' .
208 'AND title = ' . $ilDB->quote('chtr', 'text');
209 if (!($object_definition_row = $ilDB->fetchAssoc($ilDB->query($query)))) {
210 $typ_id = $ilDB->nextId('object_data');
211 $ilDB->insert(
212 'object_data',
213 [
214 'obj_id' => ['integer', $typ_id],
215 'type' => ['text', 'typ'],
216 'title' => ['text', 'chtr'],
217 'description' => ['text', 'Chatroom Object'],
218 'owner' => ['integer', -1],
219 'create_date' => ['timestamp', date('Y-m-d H:i:s')],
220 'last_update' => ['timestamp', date('Y-m-d H:i:s')]
221 ]
222 );
223
224 // REGISTER RBAC OPERATIONS FOR OBJECT TYPE
225 // 1: edit_permissions, 2: visible, 3: read, 4:write
226 foreach ([1, 2, 3, 4] as $ops_id) {
227 $query = "INSERT INTO rbac_ta (typ_id, ops_id) VALUES ( " .
228 $ilDB->quote($typ_id, 'integer') . "," . $ilDB->quote($ops_id, 'integer') . ")";
229 $ilDB->manipulate($query);
230 }
231 }
232
233 if ($moderatePermissionId = self::getModeratorPermissionId()) {
234 if (!$typ_id) {
235 $typ_id = (int) $object_definition_row['obj_id'];
236 }
237
238 if ($typ_id) {
239 $ilDB->manipulateF(
240 'DELETE FROM rbac_ta WHERE typ_id = %s AND ops_id = %s',
241 ['integer', 'integer'],
242 [$typ_id, $moderatePermissionId]
243 );
244
245 $ilDB->insert(
246 'rbac_ta',
247 [
248 'typ_id' => ['integer', $typ_id],
249 'ops_id' => ['integer', $moderatePermissionId],
250 ]
251 );
252 }
253 }
254 }
255
256 private static function getModeratorPermissionId(): int
257 {
258 global $DIC;
259
261 $ilDB = $DIC->database();
262
263 $rset = $ilDB->queryF(
264 'SELECT ops_id FROM rbac_operations WHERE operation = %s',
265 ['text'],
266 ['moderate']
267 );
268 if ($row = $ilDB->fetchAssoc($rset)) {
269 return (int) $row['ops_id'];
270 }
271
272 return 0;
273 }
274
279 public static function registerAdminObject(): void
280 {
281 global $DIC;
282
284 $ilDB = $DIC->database();
285
286 $query = 'SELECT * FROM object_data WHERE type = ' . $ilDB->quote('chta', 'text');
287 if (!$ilDB->fetchAssoc($ilDB->query($query))) {
288 $obj_id = $ilDB->nextId('object_data');
289 $ilDB->insert(
290 'object_data',
291 [
292 'obj_id' => ['integer', $obj_id],
293 'type' => ['text', 'chta'],
294 'title' => ['text', 'Chatroom Admin'],
295 'description' => ['text', 'Chatroom General Settings'],
296 'owner' => ['integer', -1],
297 'create_date' => ['timestamp', date('Y-m-d H:i:s')],
298 'last_update' => ['timestamp', date('Y-m-d H:i:s')]
299 ]
300 );
301
302 $ref_id = $ilDB->nextId('object_reference');
303 $query = "
304 INSERT INTO object_reference (ref_id, obj_id) VALUES(" .
305 $ilDB->quote($ref_id, 'integer') . ", " . $ilDB->quote($obj_id, 'integer') . ")";
306 $ilDB->manipulate($query);
307
308 $tree = new ilTree(ROOT_FOLDER_ID);
309 $tree->insertNode($ref_id, SYSTEM_FOLDER_ID);
310 }
311 }
312
313 public static function removeOldChatEntries(): void
314 {
315 global $DIC;
316
318 $ilDB = $DIC->database();
319
320 $res = $ilDB->queryF(
321 'SELECT object_data.obj_id, ref_id, lft, rgt
322 FROM object_data
323 INNER JOIN object_reference ON object_reference.obj_id = object_data.obj_id
324 INNER JOIN tree ON child = ref_id
325 WHERE type = %s',
326 ['text'],
327 ['chac']
328 );
329
330 $data = $ilDB->fetchAssoc($res);
331 if ($data) {
332 $res = $ilDB->queryF(
333 'SELECT * FROM tree
334 INNER JOIN object_reference ON ref_id = child
335 INNER JOIN object_data ON object_data.obj_id = object_reference.obj_id
336 WHERE lft BETWEEN %s AND %s',
337 ['integer', 'integer'],
338 [$data['lft'], $data['rgt']]
339 );
340 while ($row = $ilDB->fetchAssoc($res)) {
341 $ilDB->manipulate(
342 'DELETE FROM object_data WHERE obj_id = ' . $ilDB->quote($row['obj_id'], 'integer')
343 );
344
345 $ilDB->manipulate(
346 'DELETE FROM object_reference WHERE ref_id = ' . $ilDB->quote($row['ref_id'], 'integer')
347 );
348
349 $ilDB->manipulate('DELETE FROM tree WHERE child = ' . $ilDB->quote($row['ref_id'], 'integer'));
350 }
351 }
352
353 $ilDB->manipulateF('DELETE FROM object_data WHERE type = %s AND title = %s', ['text', 'text'], ['typ', 'chat']);
354 $ilDB->manipulateF('DELETE FROM object_data WHERE type = %s AND title = %s', ['text', 'text'], ['typ', 'chac']);
355 }
356
360 public static function convertChatObjects(): void
361 {
362 global $DIC;
363
365 $ilDB = $DIC->database();
366
367 $res = $ilDB->queryF(
368 'SELECT obj_id FROM object_data WHERE type = %s',
369 ['text'],
370 ['chat']
371 );
372
373 $obj_ids = [];
374
375 while ($row = $ilDB->fetchAssoc($res)) {
376 $obj_ids[] = (int) $row['obj_id'];
377 }
378
379 $ilDB->manipulateF(
380 'UPDATE object_data SET type = %s WHERE type = %s',
381 ['text', 'text'],
382 ['chtr', 'chat']
383 );
384
386 }
387
392 public static function setChatroomSettings(array $obj_ids): void
393 {
394 foreach ($obj_ids as $obj_id) {
395 $room = new ilChatroom();
396 $room->saveSettings([
397 'object_id' => $obj_id,
398 'autogen_usernames' => 'Autogen #',
399 'room_type' => 'repository'
400 ]);
401 }
402 }
403
404 public static function createDefaultPublicRoom(bool $force = false): void
405 {
406 global $DIC;
407
409 $ilDB = $DIC->database();
410
411 if ($force) {
412 $query = 'DELETE FROM chatroom_settings WHERE room_type = ' . $ilDB->quote('default', 'text');
413 $ilDB->manipulate($query);
414 $create = true;
415 } else {
416 $query = 'SELECT * FROM chatroom_settings WHERE room_type = ' . $ilDB->quote('default', 'text');
417 $rset = $ilDB->query($query);
418 $create = !$ilDB->fetchAssoc($rset);
419 }
420 if ($create) {
421 $query = "
422 SELECT object_data.obj_id, object_reference.ref_id
423 FROM object_data
424 INNER JOIN object_reference ON object_reference.obj_id = object_data.obj_id
425 WHERE type = " . $ilDB->quote('chta', 'text');
426 $rset = $ilDB->query($query);
427 $row = $ilDB->fetchAssoc($rset);
428 $chatfolder_ref_id = (int) $row['ref_id'];
429
430 $newObj = new ilObjChatroom();
431
432 $newObj->setType('chtr');
433 $newObj->setTitle('Public Chat');
434 $newObj->setDescription('');
435 $newObj->create(); // true for upload
436 $newObj->createReference();
437 $newObj->putInTree($chatfolder_ref_id);
438 $newObj->setPermissions($chatfolder_ref_id);
439
440 $obj_id = $newObj->getId();
441 $ref_id = $newObj->getRefId();
442
443 $id = $ilDB->nextId('chatroom_settings');
444 $ilDB->insert(
445 'chatroom_settings',
446 [
447 'room_id' => ['integer', $id],
448 'object_id' => ['integer', $obj_id],
449 'room_type' => ['text', 'default'],
450 'allow_anonymous' => ['integer', 0],
451 'allow_custom_usernames' => ['integer', 0],
452 'enable_history' => ['integer', 0],
453 'autogen_usernames' => ['text', 'Anonymous #'],
454 'allow_private_rooms' => ['integer', 1],
455 ]
456 );
457
458 $settings = new ilSetting('chatroom');
459 $settings->set('public_room_ref', (string) $ref_id);
460 }
461 }
462
463 public static function createMissinRoomSettingsForConvertedObjects(): void
464 {
465 global $DIC;
466
468 $ilDB = $DIC->database();
469
470 $res = $ilDB->queryF(
471 'SELECT obj_id FROM object_data LEFT JOIN chatroom_settings ON object_id = obj_id ' .
472 'WHERE type = %s AND room_id IS NULL',
473 ['text'],
474 ['chtr']
475 );
476
477 $roomsToFix = [];
478 while ($row = $ilDB->fetchAssoc($res)) {
479 $roomsToFix[] = (int) $row['obj_id'];
480 }
481
482 self::setChatroomSettings($roomsToFix);
483 }
484
485 public static function ensureCorrectPublicChatroomTreeLocation(int $ref_id): void
486 {
487 global $DIC;
488
490 $tree = $DIC->repositoryTree();
492 $ilDB = $DIC->database();
494 $rbacadmin = $DIC->rbac()->admin();
495
496 $ilDB->setLimit(1, 0);
497 $query = '
498 SELECT object_data.obj_id, object_reference.ref_id
499 FROM object_data
500 INNER JOIN object_reference ON object_reference.obj_id = object_data.obj_id
501 WHERE type = ' . $ilDB->quote('chta', 'text');
502 $rset = $ilDB->query($query);
503 $row = $ilDB->fetchAssoc($rset);
504 $chatfolder_ref_id = (int) $row['ref_id'];
505 $pid = (int) $tree->getParentId($ref_id);
506
507 if (
508 $chatfolder_ref_id &&
509 $pid !== $chatfolder_ref_id &&
510 !$tree->isDeleted($chatfolder_ref_id)
511 ) {
512 $tree->moveTree($ref_id, $chatfolder_ref_id);
513 $rbacadmin->adjustMovedObjectPermissions($ref_id, $pid);
515 }
516 }
517}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class ilChatroomInstaller.
static setChatroomSettings(array $obj_ids)
Sets autogen_usernames default option for chatrooms.
Class ilChatroom.
static _adjustMovedObjectConditions(int $a_ref_id)
In the moment it is not allowed to create preconditions on objects that are located outside of a cour...
ILIAS Setting Class.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const SYSTEM_FOLDER_ID
Definition: constants.php:35
const ROOT_FOLDER_ID
Definition: constants.php:32
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26