ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilChatroomXMLParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  protected ilChatroom $room;
24  protected string $cdata = '';
25  protected bool $in_messages = false;
26  protected ?string $import_install_id = null;
27  protected ?int $exportRoomId = 0;
28  protected ?int $timestamp = 0;
29  protected ?string $message = '';
30  protected ?string $title = '';
31 
32  public function __construct(protected ilObjChatroom $chat, string $a_xml_data)
33  {
35 
36  $room = ilChatroom::byObjectId($this->chat->getId());
37  if ($room !== null) {
38  $this->room = $room;
39  } else {
40  $this->room = new ilChatroom();
41  $this->room->setSetting('object_id', $this->chat->getId());
42  $this->room->save();
43  }
44 
45  $this->setXMLContent('<?xml version="1.0" encoding="utf-8"?>' . $a_xml_data);
46  }
47 
48  public function setImportInstallId(?string $id): void
49  {
50  $this->import_install_id = $id;
51  }
52 
53  public function getImportInstallId(): ?string
54  {
56  }
57 
58  private function isSameInstallation(): bool
59  {
60  return defined('IL_INST_ID') && IL_INST_ID > 0 && $this->getImportInstallId() == IL_INST_ID;
61  }
62 
63  public function setHandlers($a_xml_parser): void
64  {
65  xml_set_object($a_xml_parser, $this);
66  xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
67  xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
68  }
69 
73  public function handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs): void
74  {
75  switch ($a_name) {
76  case 'Messages':
77  $this->in_messages = true;
78  break;
79  }
80  }
81 
82  public function handlerEndTag(XMLParser $a_xml_parser, string $a_name): void
83  {
84  $this->cdata = trim($this->cdata);
85 
86  switch ($a_name) {
87  case 'Title':
88  $this->chat->setTitle(ilUtil::stripSlashes($this->cdata));
89  break;
90 
91  case 'Description':
92  $this->chat->setDescription(ilUtil::stripSlashes($this->cdata));
93  break;
94 
95  case 'OnlineStatus':
96  $this->chat->setOfflineStatus(
97  ((int) $this->cdata) === 0
98  );
99  break;
100 
101  case 'AllowAnonymousAccess':
102  $this->room->setSetting('allow_anonymous', (int) $this->cdata);
103  break;
104 
105  case 'AllowCustomUsernames':
106  $this->room->setSetting('allow_custom_usernames', (int) $this->cdata);
107  break;
108 
109  case 'EnableHistory':
110  $this->room->setSetting('enable_history', (int) $this->cdata);
111  break;
112 
113  case 'DisplayPastMessages':
114  $this->room->setSetting('display_past_msgs', (int) $this->cdata);
115  break;
116 
117  case 'AutoGeneratedUsernameSchema':
118  $this->room->setSetting('autogen_usernames', ilUtil::stripSlashes($this->cdata));
119  break;
120 
121  case 'RoomId':
122  $this->exportRoomId = (int) $this->cdata;
123  break;
124 
125  case 'CreatedTimestamp':
126  $this->timestamp = (int) $this->cdata;
127  break;
128 
129  case 'Body':
130  $this->message = $this->cdata;
131  break;
132 
133  case 'Message':
134  break;
135 
136  case 'Messages':
137  if ($this->isSameInstallation()) {
138  $message = json_decode($this->message, true, 512, JSON_THROW_ON_ERROR);
139  if (is_array($message)) {
140  $message['roomId'] = $this->room->getRoomId();
141  $message['timestamp'] = $this->timestamp;
142 
143  $this->room->addHistoryEntry($message);
144  }
145  }
146 
147  $this->timestamp = 0;
148  $this->in_messages = false;
149  break;
150 
151  case 'Chatroom':
152  // Set imported chats to offline
153  $this->chat->setOfflineStatus(true);
154  $this->chat->update();
155  $this->room->save();
156  break;
157  }
158 
159  $this->cdata = '';
160  }
161 
162  public function handlerCharacterData(XMLParser $a_xml_parser, string $a_data): void
163  {
164  if ($a_data !== "\n") {
165  $this->cdata .= preg_replace("/\t+/", ' ', $a_data);
166  }
167  }
168 }
const IL_INST_ID
Definition: constants.php:40
handlerCharacterData(XMLParser $a_xml_parser, string $a_data)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
__construct(protected ilObjChatroom $chat, string $a_xml_data)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerEndTag(XMLParser $a_xml_parser, string $a_name)
__construct(VocabulariesInterface $vocabularies)
Class ilChatroom.
handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs)
static byObjectId(int $object_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setXMLContent(string $a_xml_content)