ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 'Messages':
76 $this->in_messages = true;
77 break;
78 }
79 }
80
81 public function handlerEndTag(XMLParser $a_xml_parser, string $a_name): void
82 {
83 $this->cdata = trim($this->cdata);
84
85 switch ($a_name) {
86 case 'Title':
87 $this->chat->setTitle(ilUtil::stripSlashes($this->cdata));
88 break;
89
90 case 'Description':
91 $this->chat->setDescription(ilUtil::stripSlashes($this->cdata));
92 break;
93
94 case 'OnlineStatus':
95 $this->chat->setOfflineStatus(
96 ((int) $this->cdata) === 0
97 );
98 break;
99
100 case 'AllowAnonymousAccess':
101 $this->room->setSetting('allow_anonymous', (int) $this->cdata);
102 break;
103
104 case 'AllowCustomUsernames':
105 $this->room->setSetting('allow_custom_usernames', (int) $this->cdata);
106 break;
107
108 case 'EnableHistory':
109 $this->room->setSetting('enable_history', (int) $this->cdata);
110 break;
111
112 case 'DisplayPastMessages':
113 $this->room->setSetting('display_past_msgs', (int) $this->cdata);
114 break;
115
116 case 'AutoGeneratedUsernameSchema':
117 $this->room->setSetting('autogen_usernames', ilUtil::stripSlashes($this->cdata));
118 break;
119
120 case 'RoomId':
121 $this->exportRoomId = (int) $this->cdata;
122 break;
123
124 case 'CreatedTimestamp':
125 $this->timestamp = (int) $this->cdata;
126 break;
127
128 case 'Body':
129 $this->message = $this->cdata;
130 break;
131
132 case 'Message':
133 break;
134
135 case 'Messages':
136 if ($this->isSameInstallation()) {
137 $message = json_decode($this->message, true, 512, JSON_THROW_ON_ERROR);
138 if (is_array($message)) {
139 $message['roomId'] = $this->room->getRoomId();
140 $message['timestamp'] = $this->timestamp;
141
142 $this->room->addHistoryEntry($message);
143 }
144 }
145
146 $this->timestamp = 0;
147 $this->in_messages = false;
148 break;
149
150 case 'Chatroom':
151 // Set imported chats to offline
152 $this->chat->setOfflineStatus(true);
153 $this->chat->update();
154 $this->room->save();
155 break;
156 }
157
158 $this->cdata = '';
159 }
160
161 public function handlerCharacterData(XMLParser $a_xml_parser, string $a_data): void
162 {
163 if ($a_data !== "\n") {
164 $this->cdata .= preg_replace("/\t+/", ' ', $a_data);
165 }
166 }
167}
$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