ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ParagraphCommandActionHandler.php
Go to the documentation of this file.
1<?php
2
20
23
27class ParagraphCommandActionHandler implements Server\CommandActionHandler
28{
29 protected \ilPCParagraph $content_obj;
30 protected \ILIAS\DI\UIServices $ui;
31 protected \ilLanguage $lng;
32 protected \ilPageObjectGUI $page_gui;
33 protected \ilObjUser $user;
35 protected Server\UIWrapper $ui_wrapper;
36
38 {
39 global $DIC;
40
41 $this->ui = $DIC->ui();
42 $this->lng = $DIC->language();
43 $this->page_gui = $page_gui;
44 $this->user = $DIC->user();
45 $this->response_factory = new ParagraphResponseFactory();
46 $this->ui_wrapper = new Server\UIWrapper($this->ui, $this->lng);
47 }
48
49 public function handle(array $query, array $body): Server\Response
50 {
51 switch ($body["action"]) {
52 case "insert":
53 return $this->insertCommand($body);
54
55 case "update":
56 return $this->updateCommand($body);
57
58 case "update.auto":
59 return $this->autoUpdateCommand($body);
60
61 case "insert.auto":
62 return $this->autoInsertCommand($body);
63
64 case "split":
65 return $this->split($body);
66
67 case "cmd.sec.class":
68 return $this->sectionClassCommand($body);
69
70 case "cmd.merge.previous":
71 return $this->mergePrevious($body);
72
73 case "cmd.cancel":
74 return $this->cancelCommand($body);
75
76 case "delete":
77 return $this->deleteCommand($body);
78
79 default:
80 throw new Exception("Unknown action " . $body["action"]);
81 }
82 }
83
84 protected function insertCommand(
85 array $body,
86 bool $auto = false
87 ): Server\Response {
88 $updated = $this->insertParagraph($body["data"]["pcid"], $body["data"]["after_pcid"], $body["data"]["content"], $body["data"]["characteristic"], $body["data"]["fromPlaceholder"]);
89
90 return $this->response_factory->getResponseObject($this->page_gui, $updated, $body["data"]["pcid"]);
91 }
92
99 protected function insertParagraph(
100 string $pcid,
101 string $after_pcid,
102 string $content,
103 string $characteristic,
104 bool $from_placeholder = false
105 ) {
106 $page = $this->page_gui->getPageObject();
107
108 $pcid = ":" . $pcid;
109 $insert_id = $this->getFullIdForPCId($page, $after_pcid);
110 $content = $this->getContentForSaving($pcid, $content, $characteristic);
111
112 $this->content_obj = new \ilPCParagraph($page);
113 return $this->content_obj->saveJS(
114 $page,
115 $content,
116 \ilUtil::stripSlashes($characteristic),
117 \ilUtil::stripSlashes($pcid),
118 $insert_id,
119 $from_placeholder
120 );
121 }
122
123 protected function autoInsertCommand(
124 array $body
125 ): Server\Response {
126 return $this->insertCommand($body, true);
127 }
128
129 protected function updateCommand(
130 array $body,
131 bool $auto = false
132 ): Server\Response {
133 $updated = $this->updateParagraph($body["data"]["pcid"], $body["data"]["content"], $body["data"]["characteristic"]);
134 return $this->response_factory->getResponseObject($this->page_gui, $updated, $body["data"]["pcid"]);
135 }
136
143 protected function updateParagraph(
144 string $pcid,
145 string $content,
146 string $characteristic
147 ) {
148 $page = $this->page_gui->getPageObject();
149
150 $pcid = $this->getFullIdForPCId($page, $pcid);
151 $content = $this->getContentForSaving($pcid, $content, $characteristic);
152 $this->content_obj = new \ilPCParagraph($page);
153 return $this->content_obj->saveJS(
154 $page,
155 $content,
156 \ilUtil::stripSlashes($characteristic),
158 );
159 }
160
161 protected function autoUpdateCommand(array $body): Server\Response
162 {
163 return $this->updateCommand($body, true);
164 }
165
166 protected function split(
167 array $body,
168 bool $auto = false
169 ): Server\Response {
170 $page = $this->page_gui->getPageObject();
171
172 $pcid = ":" . $body["data"]["pcid"];
173 $insert_id = "";
174 if ($body["data"]["insert_mode"]) {
175 $insert_id = $this->getFullIdForPCId($page, $body["data"]["after_pcid"]);
176 }
177
178 $content = $this->getContentForSaving($pcid, $body["data"]["text"], $body["data"]["characteristic"]);
179
180 $content_obj = new \ilPCParagraph($page);
181 $updated = $content_obj->saveJS(
182 $page,
183 $content,
184 \ilUtil::stripSlashes($body["data"]["characteristic"]),
185 \ilUtil::stripSlashes($pcid),
186 $insert_id,
187 $body["data"]["fromPlaceholder"] ?? false
188 );
189 $current_after_id = $body["data"]["pcid"];
190 $all_pc_ids[] = $current_after_id;
191
192 foreach ($body["data"]["new_paragraphs"] as $p) {
193 if ($updated === true) {
194 $page->addHierIDs();
195 $insert_id = $this->getFullIdForPCId($page, $current_after_id);
196 $content = $this->getContentForSaving($p["pcid"], $p["model"]["text"], $p["model"]["characteristic"]);
197 $content_obj = new \ilPCParagraph($page);
198 $updated = $content_obj->saveJS(
199 $page,
200 $content,
201 \ilUtil::stripSlashes($p["model"]["characteristic"]),
202 ":" . \ilUtil::stripSlashes($p["pcid"]),
203 $insert_id
204 );
205 $all_pc_ids[] = $p["pcid"];
206 $current_after_id = $p["pcid"];
207 }
208 }
209
210 return $this->response_factory->getResponseObjectMulti($this->page_gui, $updated, $all_pc_ids);
211 }
212
216 protected function getFullIdForPCId(
217 \ilPageObject $page,
218 string $pc_id
219 ): string {
220 $id = "pg:";
221 if (!in_array($pc_id, ["", "pg"])) {
222 $hier_ids = $page->getHierIdsForPCIds([$pc_id]);
223 $id = ($hier_ids[$pc_id] ?? "") . ":" . $pc_id;
224 }
225 return $id;
226 }
227
228 protected function getContentForSaving(
229 string $pcid,
230 string $content,
231 string $characteristic
232 ): string {
233 $content = str_replace("&nbsp;", " ", $content);
234 return "<div id='" .
235 $pcid . "' class='ilc_text_block_" .
236 $characteristic . "'>" . $content . "</div>";
237 }
238
245 protected function sectionClassCommand(array $body): Server\Response
246 {
247 $insert_mode = $body["data"]["insert_mode"];
248 $after_pcid = $body["data"]["after_pcid"];
249 $pcid = $body["data"]["pcid"];
250 $content = $body["data"]["text"];
251 $characteristic = $body["data"]["characteristic"];
252 $old_section_characteristic = $body["data"]["old_section_characteristic"];
253 $new_section_characteristic = $body["data"]["new_section_characteristic"];
254
255 // first insert/update the current paragraph
256 if (!$insert_mode) {
257 $updated = $this->updateParagraph($pcid, $content, $characteristic);
258 } else {
259 $updated = $this->insertParagraph($pcid, $after_pcid, $content, $characteristic);
260 }
261
262
264 if ($updated) {
265 $page = $this->page_gui->getPageObject();
266 $page->addHierIDs();
267 $parent = $page->getParentContentObjectForPcId($pcid);
268
269 // case 1: parent section exists and new characteristic is not empty
270 if (!is_null($parent) && $parent->getType() == "sec" && $new_section_characteristic != "") {
271 $parent->setCharacteristic($new_section_characteristic);
272 $updated = $page->update();
273 }
274 // case 2: move from none to section
275 elseif ((is_null($parent) || $parent->getType() != "sec") && $old_section_characteristic == "" && $new_section_characteristic != "") {
276 $sec = new \ilPCSection($page);
277 $hier_ids = $page->getHierIdsForPCIds([$pcid]);
278 $sec->create($page, $hier_ids[$pcid], $pcid);
279 $sec->setCharacteristic($new_section_characteristic);
280 $sec_pcid = $page->generatePCId();
281 $sec->writePCId($sec_pcid);
282 $updated = $page->update();
283 $page->addHierIDs();
284 $par = $page->getContentObjectForPcId($pcid);
285 $sec = $page->getContentObjectForPcId($sec_pcid);
286 // note: we want the pcid of the Section itself here
287 $sec_node_pc_id = $sec->getDomNode()->firstChild->getAttribute("PCID");
288 $hier_ids = $page->getHierIdsForPCIds([$sec_node_pc_id]);
289 $node = $par->getDomNode();
290 $node->parentNode->removeChild($node);
291 $page->insertContentNode($node, $hier_ids[$sec_node_pc_id], IL_INSERT_CHILD, $sec_node_pc_id);
292 $updated = $page->update();
293 } // case 3: move from section to none
294 elseif ((!is_null($parent) && $parent->getType() == "sec") && $old_section_characteristic != "" && $new_section_characteristic == "") {
295 // note: we want the pcid of the PageContent element of the Section here
296 $sec_node_pc_id = $parent->getDomNode()->getAttribute("PCID");
297 $sec_node_hier_id = $page->getHierIdForPCId($sec_node_pc_id);
298 // all kids of the section
299 $childs_reverse = array_reverse(iterator_to_array($parent->getDomNode()->firstChild->childNodes));
300 foreach ($childs_reverse as $child) {
301 // unlink kid
302 $child->parentNode->removeChild($child);
303 // insert after section
304 $page->insertContentNode($child, $sec_node_hier_id, IL_INSERT_AFTER, $sec_node_pc_id, true);
305 }
306 // unlink section
307 $node = $parent->getDomNode();
308 $node->parentNode->removeChild($node);
309 $updated = $page->update();
310 }
311 }
312 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
313 }
314
318 protected function mergePrevious(array $body): Server\Response
319 {
320 $page = $this->page_gui->getPageObject();
321
322 $updated = $this->updateParagraph(
323 $body["data"]["previousPcid"],
324 $body["data"]["newPreviousContent"],
325 $body["data"]["previousCharacteristic"]
326 );
327
328 $page->addHierIDs();
329 $hier_id = $page->getHierIdForPcId($body["data"]["pcid"]);
330 $updated = $page->deleteContents(
331 [$hier_id],
332 true,
333 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
334 );
335 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
336 }
337
344 protected function cancelCommand(array $body): Server\Response
345 {
346 $remove_section_for_pcid = $body["data"]["removeSectionFromPcid"];
347 $par_text = $body["data"]["paragraphText"];
348 $par_characteristic = $body["data"]["paragraphCharacteristic"];
349
350 $page = $this->page_gui->getPageObject();
351 $page->addHierIDs();
352 $paragraph = $page->getContentObjectForPcId($remove_section_for_pcid);
353 $parent = $page->getParentContentObjectForPcId($remove_section_for_pcid);
354 $parent_pc_id = $parent->getPCId();
355
356 $updated = true;
357
358 // case 1: parent section exists and new characteristic is not empty
359 if ($parent->getType() == "sec") {
360 $updated = $this->updateParagraph($remove_section_for_pcid, $par_text, $par_characteristic);
361
362 if ($updated) {
363 $page->addHierIDs();
364 $page->moveContentAfter($paragraph->getHierId(), $parent->getHierId());
365 $updated = $page->update();
366 }
367
368 if ($updated) {
369 $page->addHierIDs();
370 $hid = $page->getHierIdForPcId($parent_pc_id);
371 $updated = $page->deleteContents(
372 [$hid],
373 true,
374 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
375 );
376 }
377
378 if ($updated) {
379 $updated = $page->update();
380 }
381 }
382 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
383 }
384
388 protected function deleteCommand(array $body): Server\Response
389 {
390 $pcids = [$body["data"]["pcid"]];
391
392 $page = $this->page_gui->getPageObject();
393
394 $hids = array_map(
395 function ($pcid) {
396 return $this->getIdForPCId($pcid);
397 },
398 $pcids
399 );
400
401 $updated = $page->deleteContents(
402 $hids,
403 true,
404 $this->page_gui->getPageConfig()->getEnableSelfAssessment()
405 );
406
407 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
408 }
409
410 protected function getIdForPCId(string $pcid): string
411 {
412 $page = $this->page_gui->getPageObject();
413 $id = "pg:";
414 if (!in_array($pcid, ["", "pg"])) {
415 $hier_ids = $page->getHierIdsForPCIds([$pcid]);
416 $id = $hier_ids[$pcid] . ":" . $pcid;
417 }
418 return $id;
419 }
420}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Page editor json server.
insertParagraph(string $pcid, string $after_pcid, string $content, string $characteristic, bool $from_placeholder=false)
Insert paragraph.
updateParagraph(string $pcid, string $content, string $characteristic)
Update paragraph.
getFullIdForPCId(\ilPageObject $page, string $pc_id)
Get full id for pc id.
getContentForSaving(string $pcid, string $content, string $characteristic)
return true
const IL_INSERT_CHILD
const IL_INSERT_AFTER
Class ilPageObjectGUI.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
insertContentNode(DOMNode $a_cont_node, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="")
insert a content node before/after a sibling or as first child of a parent
getContentObjectForPcId(string $pcid)
Get content object for pc id.
update(bool $a_validate=true, bool $a_no_history=false)
update complete page content in db (dom xml content is used)
deleteContents(array $a_hids, bool $a_update=true, bool $a_self_ass=false, bool $move_operation=false)
Delete multiple content objects.
getHierIdForPcId(string $pcid)
getParentContentObjectForPcId(string $pcid)
Get parent content object for pc id.
moveContentAfter(string $a_source, string $a_target, string $a_spcid="", string $a_tpcid="")
move content object from position $a_source before position $a_target (both hierarchical content ids)
addHierIDs()
Add hierarchical ID (e.g.
getHierIdsForPCIds(array $a_pc_ids)
Get hier ids for a set of pc ids.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26