ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilChatroomXMLParser.php
Go to the documentation of this file.
1<?php
2
19declare(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_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
66 xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
67 }
68
72 public function handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs): void
73 {
74 switch ($a_name) {
75 case 'Body':
76 $this->timestamp = null;
77 $this->message = null;
78 break;
79
80 case 'Messages':
81 $this->in_messages = true;
82 break;
83 }
84 }
85
86 public function handlerEndTag(XMLParser $a_xml_parser, string $a_name): void
87 {
88 $this->cdata = trim($this->cdata);
89
90 switch ($a_name) {
91 case 'Title':
92 $this->chat->setTitle(ilUtil::stripSlashes($this->cdata));
93 break;
94
95 case 'Description':
96 $this->chat->setDescription(ilUtil::stripSlashes($this->cdata));
97 break;
98
99 case 'OnlineStatus':
100 $this->chat->setOfflineStatus(
101 ((int) $this->cdata) === 0
102 );
103 break;
104
105 case 'AllowAnonymousAccess':
106 $this->room->setSetting('allow_anonymous', (int) $this->cdata);
107 break;
108
109 case 'AllowCustomUsernames':
110 $this->room->setSetting('allow_custom_usernames', (int) $this->cdata);
111 break;
112
113 case 'EnableHistory':
114 $this->room->setSetting('enable_history', (int) $this->cdata);
115 break;
116
117 case 'DisplayPastMessages':
118 $this->room->setSetting('display_past_msgs', (int) $this->cdata);
119 break;
120
121 case 'AutoGeneratedUsernameSchema':
122 $this->room->setSetting('autogen_usernames', ilUtil::stripSlashes($this->cdata));
123 break;
124
125 case 'RoomId':
126 $this->exportRoomId = (int) $this->cdata;
127 break;
128
129 case 'CreatedTimestamp':
130 $this->timestamp = (int) $this->cdata;
131 break;
132
133 case 'Body':
134 $this->message = $this->cdata;
135 break;
136
137 case 'Message':
138 if ($this->isSameInstallation()) {
139 $message = json_decode($this->message, true, 512, JSON_THROW_ON_ERROR);
140 if (is_array($message)) {
141 $message['roomId'] = $this->room->getRoomId();
142 $message['timestamp'] = $this->timestamp;
143
144 $this->room->addHistoryEntry($message);
145 }
146 }
147 break;
148
149 case 'Messages':
150 $this->in_messages = false;
151 break;
152
153 case 'Chatroom':
154 // Set imported chats to offline
155 $this->chat->setOfflineStatus(true);
156 $this->chat->update();
157 $this->room->save();
158 break;
159 }
160
161 $this->cdata = '';
162 }
163
164 public function handlerCharacterData(XMLParser $a_xml_parser, string $a_data): void
165 {
166 if ($a_data !== "\n") {
167 $this->cdata .= preg_replace("/\t+/", ' ', $a_data);
168 }
169 }
170}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
handlerEndTag(XMLParser $a_xml_parser, string $a_name)
__construct(protected ilObjChatroom $chat, string $a_xml_data)
handlerBeginTag(XMLParser $a_xml_parser, string $a_name, array $a_attribs)
handlerCharacterData(XMLParser $a_xml_parser, string $a_data)
Class ilChatroom.
static byObjectId(int $object_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setXMLContent(string $a_xml_content)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
const IL_INST_ID
Definition: constants.php:40
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc