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