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