1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
6require_once("./Modules/LearningModule/classes/class.ilLMObjectGUI.php");
7require_once("./Modules/LearningModule/classes/class.ilLMObject.php");
8
9/**
10* Class ilStructureObjectGUI
11*
12* User Interface for Structure Objects Editing
13*
14* @author Alex Killing <alex.killing@gmx.de>
15* @version $Id$
16*
17* @ilCtrl_Calls ilStructureObjectGUI: ilConditionHandlerGUI, ilObjectMetaDataGUI
18*
19* @ingroup ModulesIliasLearningModule
20*/
21class ilStructureObjectGUI extends ilLMObjectGUI
22{
23    /**
24     * @var ilObjUser
25     */
26    protected $user;
27
28    /**
29     * @var ilErrorHandling
30     */
31    protected $error;
32
33    /**
34     * @var ilTabsGUI
35     */
36    protected $tabs;
37
38    /**
39     * @var Logger
40     */
41    protected $log;
42
43    public $obj;	// structure object
44    public $tree;
45
46    /**
47    * Constructor
48    * @access	public
49    */
50    public function __construct(&$a_content_obj, &$a_tree)
51    {
52        global $DIC;
53
54        $this->tree = $DIC->repositoryTree();
55        $this->user = $DIC->user();
56        $this->ctrl = $DIC->ctrl();
57        $this->lng = $DIC->language();
58        $this->error = $DIC["ilErr"];
59        $this->tabs = $DIC->tabs();
60        $this->log = $DIC["ilLog"];
61        $this->tpl = $DIC["tpl"];
62        parent::__construct($a_content_obj);
63        $this->tree = $a_tree;
64    }
65
66    /**
67    * set structure object
68    *
69    * @param	object		$a_st_object	structure object
70    */
71    public function setStructureObject(&$a_st_object)
72    {
73        $this->obj = $a_st_object;
74    }
75
76
77    /**
78    * this function is called by condition handler gui interface
79    */
80    public function getType()
81    {
82        return "st";
83    }
84
85    /**
86    * execute command
87    */
88    public function executeCommand()
89    {
90        //echo "<br>:cmd:".$this->ctrl->getCmd().":cmdClass:".$this->ctrl->getCmdClass().":";
91        $next_class = $this->ctrl->getNextClass($this);
92        $cmd = $this->ctrl->getCmd();
93
94        switch ($next_class) {
95            case 'ilobjectmetadatagui':
96
97                $this->setTabs();
98
99                include_once 'Services/Object/classes/class.ilObjectMetaDataGUI.php';
100                $md_gui = new ilObjectMetaDataGUI($this->content_object, $this->obj->getType(), $this->obj->getId());
101                $md_gui->addMDObserver($this->obj, 'MDUpdateListener', 'General');
102                $md_gui->addMDObserver($this->obj, 'MDUpdateListener', 'Educational'); // #9510
103                $this->ctrl->forwardCommand($md_gui);
104                break;
105
106            case "ilconditionhandlergui":
107        $ilTabs = $this->tabs;
108                include_once './Services/Conditions/classes/class.ilConditionHandlerGUI.php';
109
110                $this->setTabs();
111                $this->initConditionHandlerInterface();
112                $this->ctrl->forwardCommand($this->condHI);
113                $ilTabs->setTabActive('preconditions');
114                break;
115
116            default:
117                if ($cmd == 'listConditions') {
118                    $this->setTabs();
119                    $this->initConditionHandlerInterface();
120                    $this->condHI->executeCommand();
121                } elseif (($cmd == "create") && ($_POST["new_type"] == "pg")) {
122                    $this->setTabs();
123                    $pg_gui = new ilLMPageObjectGUI($this->content_object);
124                    $pg_gui->executeCommand();
125                } else {
126                    $this->$cmd();
127                }
128                break;
129        }
130    }
131
132
133    /**
134    * create new page or chapter in chapter
135    */
136    public function create()
137    {
138        if ($_GET["obj_id"] != "") {
139            $this->setTabs();
140        }
141        parent::create();
142    }
143
144    public function edit()
145    {
146        $this->view();
147    }
148
149    /*
150    * display pages of structure object
151    */
152    public function view()
153    {
154        $tree = $this->tree;
155        $ilUser = $this->user;
156        $ilCtrl = $this->ctrl;
157        $lng = $this->lng;
158
159        $this->showHierarchy();
160    }
161
162
163    /**
164    * Show subhiearchy of pages and subchapters
165    */
166    public function showHierarchy()
167    {
168        $lng = $this->lng;
169        $ilCtrl = $this->ctrl;
170
171        $this->setTabs();
172
173        $ilCtrl->setParameter($this, "backcmd", "showHierarchy");
174
175        include_once("./Modules/LearningModule/classes/class.ilChapterHierarchyFormGUI.php");
176        $form_gui = new ilChapterHierarchyFormGUI($this->content_object->getType(), $_GET["transl"]);
177        $form_gui->setFormAction($ilCtrl->getFormAction($this));
178        $form_gui->setTitle($this->obj->getTitle());
179        $form_gui->setIcon(ilUtil::getImagePath("icon_st.svg"));
180        $form_gui->setTree($this->tree);
181        $form_gui->setCurrentTopNodeId($this->obj->getId());
182        $form_gui->addMultiCommand($lng->txt("delete"), "delete");
183        $form_gui->addMultiCommand($lng->txt("cut"), "cutItems");
184        $form_gui->addMultiCommand($lng->txt("copy"), "copyItems");
185        $form_gui->addMultiCommand($lng->txt("cont_de_activate"), "activatePages");
186        if ($this->content_object->getLayoutPerPage()) {
187            $form_gui->addMultiCommand($lng->txt("cont_set_layout"), "setPageLayout");
188        }
189        $form_gui->setDragIcon(ilUtil::getImagePath("icon_pg.svg"));
190        $form_gui->addCommand($lng->txt("cont_save_all_titles"), "saveAllTitles");
191        $form_gui->addHelpItem($lng->txt("cont_chapters_after_pages"));
192        $up_gui = "ilobjlearningmodulegui";
193        $ilCtrl->setParameterByClass($up_gui, "active_node", $this->obj->getId());
194        $ilCtrl->setParameterByClass($up_gui, "active_node", "");
195
196        $ctpl = new ilTemplate("tpl.chap_and_pages.html", true, true, "Modules/LearningModule");
197        $ctpl->setVariable("HIERARCHY_FORM", $form_gui->getHTML());
198        $ilCtrl->setParameter($this, "obj_id", $_GET["obj_id"]);
199
200        include_once("./Modules/LearningModule/classes/class.ilObjContentObjectGUI.php");
201        $ml_head = ilObjContentObjectGUI::getMultiLangHeader($this->content_object->getId(), $this);
202
203        $this->tpl->setContent($ml_head . $ctpl->get());
204    }
205
206    /**
207    * Copy items to clipboard, then cut them from the current tree
208    */
209    public function cutItems($a_return = "view")
210    {
211        $ilCtrl = $this->ctrl;
212        $lng = $this->lng;
213
214        $items = ilUtil::stripSlashesArray($_POST["id"]);
215        if (!is_array($items)) {
216            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
217            $ilCtrl->redirect($this, "showHierarchy");
218        }
219
220        $todel = array();			// delete IDs < 0 (needed for non-js editing)
221        foreach ($items as $k => $item) {
222            if ($item < 0) {
223                $todel[] = $k;
224            }
225        }
226        foreach ($todel as $k) {
227            unset($items[$k]);
228        }
229
230        if (!ilLMObject::uniqueTypesCheck($items)) {
231            ilUtil::sendFailure($lng->txt("cont_choose_pages_or_chapters_only"), true);
232            $ilCtrl->redirect($this, "showHierarchy");
233        }
234
235        ilLMObject::clipboardCut($this->content_object->getId(), $items);
236        ilEditClipboard::setAction("cut");
237        //ilUtil::sendInfo($this->lng->txt("msg_cut_clipboard"), true);
238        ilUtil::sendInfo($lng->txt("cont_selected_items_have_been_cut"), true);
239
240        $ilCtrl->redirect($this, $a_return);
241    }
242
243    /**
244    * Copy items to clipboard
245    */
246    public function copyItems($a_return = "view")
247    {
248        $ilCtrl = $this->ctrl;
249        $lng = $this->lng;
250
251        $items = ilUtil::stripSlashesArray($_POST["id"]);
252        if (!is_array($items)) {
253            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
254            $ilCtrl->redirect($this, "showHierarchy");
255        }
256
257        $todel = array();				// delete IDs < 0 (needed for non-js editing)
258        foreach ($items as $k => $item) {
259            if ($item < 0) {
260                $todel[] = $k;
261            }
262        }
263        foreach ($todel as $k) {
264            unset($items[$k]);
265        }
266
267        if (!ilLMObject::uniqueTypesCheck($items)) {
268            ilUtil::sendFailure($lng->txt("cont_choose_pages_or_chapters_only"), true);
269            $ilCtrl->redirect($this, "showHierarchy");
270        }
271
272        ilLMObject::clipboardCopy($this->content_object->getId(), $items);
273        ilEditClipboard::setAction("copy");
274
275        ilUtil::sendInfo($lng->txt("cont_selected_items_have_been_copied"), true);
276        $ilCtrl->redirect($this, $a_return);
277    }
278
279    /**
280    * Save all titles of chapters/pages
281    */
282    public function saveAllTitles()
283    {
284        $ilCtrl = $this->ctrl;
285
286        ilLMObject::saveTitles($this->content_object, ilUtil::stripSlashesArray($_POST["title"]), $_GET["transl"]);
287
288        ilUtil::sendSuccess($this->lng->txt("lm_save_titles"), true);
289        $ilCtrl->redirect($this, "showHierarchy");
290    }
291
292    /*
293    * display subchapters of structure object
294    */
295    public function subchap()
296    {
297        $tree = $this->tree;
298        $ilCtrl = $this->ctrl;
299        $lng = $this->lng;
300        $ilUser = $this->user;
301
302        $this->setTabs();
303
304        $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.structure_edit.html", "Modules/LearningModule");
305        $num = 0;
306
307        $this->tpl->setCurrentBlock("form");
308        $this->ctrl->setParameter($this, "backcmd", "subchap");
309        $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
310        $this->tpl->setVariable("HEADER_TEXT", $this->lng->txt("cont_subchapters"));
311        $this->tpl->setVariable("CHECKBOX_TOP", IL_FIRST_NODE);
312
313        $cnt = 0;
314        $childs = $this->tree->getChilds($this->obj->getId());
315        foreach ($childs as $child) {
316            if ($child["type"] != "st") {
317                continue;
318            }
319            $this->tpl->setCurrentBlock("table_row");
320            // color changing
321            $css_row = ilUtil::switchColor($cnt++, "tblrow1", "tblrow2");
322
323            // checkbox
324            $this->tpl->setVariable("CHECKBOX_ID", $child["obj_id"]);
325            $this->tpl->setVariable("CSS_ROW", $css_row);
326            $this->tpl->setVariable("IMG_OBJ", ilUtil::getImagePath("icon_st.svg"));
327
328            // type
329            $this->ctrl->setParameterByClass("ilStructureObjectGUI", "obj_id", $child["obj_id"]);
330            $link = $this->ctrl->getLinkTargetByClass("ilStructureObjectGUI", "view");
331            $this->tpl->setVariable("LINK_TARGET", $link);
332
333            // title
334            $this->tpl->setVariable(
335                "TEXT_CONTENT",
336                ilStructureObject::_getPresentationTitle(
337                    $child["obj_id"],
338                    IL_CHAPTER_TITLE,
339                    $this->content_object->isActiveNumbering()
340                )
341            );
342
343            $this->tpl->parseCurrentBlock();
344        }
345        if ($cnt == 0) {
346            $this->tpl->setCurrentBlock("notfound");
347            $this->tpl->setVariable("NUM_COLS", 3);
348            $this->tpl->setVariable("TXT_OBJECT_NOT_FOUND", $this->lng->txt("obj_not_found"));
349            $this->tpl->parseCurrentBlock();
350        }
351        //else
352        //{
353        // SHOW VALID ACTIONS
354        $this->tpl->setVariable("NUM_COLS", 3);
355        $acts = array("delete" => "delete", "cutChapter" => "cut",
356                "copyChapter" => "copyChapter");
357        if ($ilUser->clipboardHasObjectsOfType("st")) {
358            $acts["pasteChapter"] = "pasteChapter";
359        }
360        $this->showActions($acts);
361        //}
362
363        // SHOW POSSIBLE SUB OBJECTS
364        $this->tpl->setVariable("NUM_COLS", 3);
365        //$this->showPossibleSubObjects("st");
366        $subobj = array("st");
367        $opts = ilUtil::formSelect(12, "new_type", $subobj);
368        //$this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.svg"));
369        $this->tpl->setCurrentBlock("add_object");
370        $this->tpl->setVariable("SELECT_OBJTYPE", $opts);
371        //$this->tpl->setVariable("FORMACTION_OBJ_ADD", "adm_object.php?cmd=create&ref_id=".$_GET["ref_id"]);
372        $this->tpl->setVariable("BTN_NAME", "create");
373        $this->tpl->setVariable("TXT_ADD", $this->lng->txt("insert"));
374        $this->tpl->parseCurrentBlock();
375
376        //$this->tpl->setVariable("NUM_COLS", 2);
377        //$this->showPossibleSubObjects("st");
378
379        $this->tpl->setCurrentBlock("form");
380        $this->tpl->parseCurrentBlock();
381
382        $ilCtrl->setParameter($this, "obj_id", $_GET["obj_id"]);
383    }
384
385    /**
386    * output a cell in object list
387    */
388    public function add_cell($val, $link = "")
389    {
390        if (!empty($link)) {
391            $this->tpl->setCurrentBlock("begin_link");
392            $this->tpl->setVariable("LINK_TARGET", $link);
393            $this->tpl->parseCurrentBlock();
394            $this->tpl->touchBlock("end_link");
395        }
396
397        $this->tpl->setCurrentBlock("text");
398        $this->tpl->setVariable("TEXT_CONTENT", $val);
399        $this->tpl->parseCurrentBlock();
400        $this->tpl->setCurrentBlock("table_cell");
401        $this->tpl->parseCurrentBlock();
402    }
403
404
405    /**
406    * save new chapter
407    */
408    public function save()
409    {
410        $this->obj = new ilStructureObject($this->content_object);
411
412        $this->obj->setType("st");
413        $this->obj->setTitle(ilUtil::stripSlashes($_POST["Fobject"]["title"]));
414        $this->obj->setDescription(ilUtil::stripSlashes($_POST["Fobject"]["desc"]));
415        $this->obj->setLMId($this->content_object->getId());
416        $this->obj->create();
417
418        $this->putInTree();
419
420        // check the tree
421        $this->checkTree();
422
423        if (!empty($_GET["obj_id"])) {
424            $this->ctrl->redirect($this, "subchap");
425        }
426    }
427
428    /**
429    * put chapter into tree
430    */
431    public function putInTree()
432    {
433        //echo "st:putInTree";
434        // chapters should be behind pages in the tree
435        // so if target is first node, the target is substituted with
436        // the last child of type pg
437        if ($_GET["target"] == IL_FIRST_NODE) {
438            $tree = new ilTree($this->content_object->getId());
439            $tree->setTableNames('lm_tree', 'lm_data');
440            $tree->setTreeTablePK("lm_id");
441
442            // determine parent node id
443            $parent_id = (!empty($_GET["obj_id"]))
444                ? $_GET["obj_id"]
445                : $tree->getRootId();
446            // determine last child of type pg
447            $childs = $tree->getChildsByType($parent_id, "pg");
448            if (count($childs) != 0) {
449                $_GET["target"] = $childs[count($childs) - 1]["obj_id"];
450            }
451        }
452        if (empty($_GET["target"])) {
453            $_GET["target"] = IL_LAST_NODE;
454        }
455
456        parent::putInTree();
457    }
458
459    /**
460    * cut page
461    */
462    public function cutPage()
463    {
464        $this->cutItems();
465    }
466
467    /**
468    * copy page
469    */
470    public function copyPage()
471    {
472        $this->copyItems();
473    }
474
475    /**
476    * paste page
477    */
478    public function pastePage()
479    {
480        $ilUser = $this->user;
481        $ilErr = $this->error;
482
483        if (!$ilUser->clipboardHasObjectsOfType("pg")) {
484            $ilErr->raiseError($this->lng->txt("no_page_in_clipboard"), $ilErr->MESSAGE);
485        }
486
487        return $this->insertPageClip();
488    }
489
490
491    /**
492    * Cut chapter(s)
493    */
494    public function cutChapter()
495    {
496        $this->cutItems("subchap");
497    }
498
499    /**
500    * copy a single chapter (selection)
501    */
502    public function copyChapter()
503    {
504        $this->copyItems("subchap");
505    }
506
507    /**
508    * paste chapter
509    */
510    public function pasteChapter()
511    {
512        $ilUser = $this->user;
513
514        return $this->insertChapterClip(false, "subchap");
515    }
516
517    /**
518    * activates or deactivates pages
519    */
520    public function activatePages()
521    {
522        $lng = $this->lng;
523
524        include_once("./Modules/LearningModule/classes/class.ilLMPage.php");
525        if (is_array($_POST["id"])) {
526            $act_items = array();
527            // get all "top" ids, i.e. remove ids, that have a selected parent
528            foreach ($_POST["id"] as $id) {
529                $path = $this->tree->getPathId($id);
530                $take = true;
531                foreach ($path as $path_id) {
532                    if ($path_id != $id && in_array($path_id, $_POST["id"])) {
533                        $take = false;
534                    }
535                }
536                if ($take) {
537                    $act_items[] = $id;
538                }
539            }
540
541
542            foreach ($act_items as $id) {
543                $childs = $this->tree->getChilds($id);
544                foreach ($childs as $child) {
545                    if (ilLMObject::_lookupType($child["child"]) == "pg") {
546                        $act = ilLMPage::_lookupActive(
547                            $child["child"],
548                            $this->content_object->getType()
549                        );
550                        ilLMPage::_writeActive(
551                            $child["child"],
552                            $this->content_object->getType(),
553                            !$act
554                        );
555                    }
556                }
557                if (ilLMObject::_lookupType($id) == "pg") {
558                    $act = ilLMPage::_lookupActive(
559                        $id,
560                        $this->content_object->getType()
561                    );
562                    ilLMPage::_writeActive(
563                        $id,
564                        $this->content_object->getType(),
565                        !$act
566                    );
567                }
568            }
569        } else {
570            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
571        }
572
573        $this->ctrl->redirect($this, "view");
574    }
575
576    //
577    // Condition handling stuff
578    //
579
580    public function initConditionHandlerInterface()
581    {
582        include_once("./Services/Conditions/classes/class.ilConditionHandlerGUI.php");
583
584        $this->condHI = new ilConditionHandlerGUI($this);
585        $this->condHI->setBackButtons(array());
586        $this->condHI->setAutomaticValidation(false);
587        $this->condHI->setTargetType("st");
588        $this->condHI->setTargetRefId($this->content_object->getRefId());
589        $this->condHI->setTargetId($this->obj->getId());
590        $this->condHI->setTargetTitle($this->obj->getTitle());
591    }
592
593
594    /**
595    * cancel creation of new page or chapter
596    */
597    public function cancel()
598    {
599        if ($_GET["obj_id"] != 0) {
600            if ($_GET["new_type"] == "pg") {
601                $this->ctrl->redirect($this, "view");
602            } else {
603                $this->ctrl->redirect($this, "subchap");
604            }
605        }
606    }
607
608
609    /**
610    * output tabs
611    */
612    public function setTabs()
613    {
614        $ilTabs = $this->tabs;
615        $ilUser = $this->user;
616        $lng = $this->lng;
617
618        // subelements
619        $ilTabs->addTarget(
620            "cont_pages_and_subchapters",
621            $this->ctrl->getLinkTarget($this, 'showHierarchy'),
622            array("view", "showHierarchy"),
623            get_class($this)
624        );
625
626        // preconditions
627        $ilTabs->addTarget(
628            "preconditions",
629            $this->ctrl->getLinkTarget($this, 'listConditions'),
630            "listConditions",
631            get_class($this)
632        );
633
634        // metadata
635        include_once "Services/Object/classes/class.ilObjectMetaDataGUI.php";
636        $mdgui = new ilObjectMetaDataGUI($this->content_object, $this->obj->getType(), $this->obj->getId());
637        $mdtab = $mdgui->getTab();
638        if ($mdtab) {
639            $ilTabs->addTarget(
640                "meta_data",
641                $mdtab,
642                "",
643                "ilmdeditorgui"
644            );
645        }
646
647        $this->tpl->setTitleIcon(ilUtil::getImagePath("icon_st.svg"));
648        $this->tpl->setTitle(
649            $this->lng->txt($this->obj->getType()) . ": " . $this->obj->getTitle()
650        );
651
652        // presentation view
653        $ilTabs->addNonTabbedLink(
654            "pres_mode",
655            $lng->txt("cont_presentation_view"),
656            ILIAS_HTTP_PATH . "/goto.php?target=st_" . $this->obj->getId(),
657            "_top"
658        );
659    }
660
661    /**
662    * redirect script
663    *
664    * @param	string		$a_target
665    */
666    public static function _goto($a_target, $a_target_ref_id = "")
667    {
668        global $DIC;
669
670        $rbacsystem = $DIC->rbac()->system();
671        $ilErr = $DIC["ilErr"];
672        $lng = $DIC->language();
673        $ilAccess = $DIC->access();
674
675        // determine learning object
676        $lm_id = ilLMObject::_lookupContObjID($a_target);
677
678        // get all references
679        $ref_ids = ilObject::_getAllReferences($lm_id);
680
681        // always try passed ref id first
682        if (in_array($a_target_ref_id, $ref_ids)) {
683            $ref_ids = array_merge(array($a_target_ref_id), $ref_ids);
684        }
685
686        // check read permissions
687        foreach ($ref_ids as $ref_id) {
688            // Permission check
689            if ($ilAccess->checkAccess("read", "", $ref_id)) {
690                // don't redirect anymore, just set parameters
691                // (goto.php includes  "ilias.php")
692                $_GET["baseClass"] = "ilLMPresentationGUI";
693                $_GET["obj_id"] = $a_target;
694                $_GET["ref_id"] = $ref_id;
695                include_once("ilias.php");
696                exit;
697                ;
698            }
699        }
700
701        if ($ilAccess->checkAccess("read", "", ROOT_FOLDER_ID)) {
702            ilUtil::sendFailure(sprintf(
703                $lng->txt("msg_no_perm_read_item"),
704                ilObject::_lookupTitle($lm_id)
705            ), true);
706            include_once("./Services/Object/classes/class.ilObjectGUI.php");
707            ilObjectGUI::_gotoRepositoryRoot();
708        }
709
710        $ilErr->raiseError($lng->txt("msg_no_perm_read_lm"), $ilErr->FATAL);
711    }
712
713    /**
714    * Insert (multiple) chapters at node
715    */
716    public function insertChapter($a_as_sub = false)
717    {
718        $ilCtrl = $this->ctrl;
719        $lng = $this->lng;
720
721        include_once("./Modules/LearningModule/classes/class.ilChapterHierarchyFormGUI.php");
722
723        $num = ilChapterHierarchyFormGUI::getPostMulti();
724        $node_id = ilChapterHierarchyFormGUI::getPostNodeId();
725
726        if ($a_as_sub) {		// as subchapter
727            if (!ilChapterHierarchyFormGUI::getPostFirstChild()) {	// insert under parent
728                $parent_id = $node_id;
729                $target = "";
730            } else {													// we shouldnt end up here
731                $ilCtrl->redirect($this, "showHierarchy");
732                return;
733            }
734        } else {				// as chapter
735            if (!ilChapterHierarchyFormGUI::getPostFirstChild()) {	// insert after node id
736                $parent_id = $this->tree->getParentId($node_id);
737                $target = $node_id;
738            } else {													// insert as first child
739                $parent_id = $node_id;
740                $target = IL_FIRST_NODE;
741            }
742        }
743        for ($i = 1; $i <= $num; $i++) {
744            $chap = new ilStructureObject($this->content_object);
745            $chap->setType("st");
746            $chap->setTitle($lng->txt("cont_new_chap"));
747            $chap->setLMId($this->content_object->getId());
748            $chap->create();
749            ilLMObject::putInTree($chap, $parent_id, $target);
750        }
751
752        $ilCtrl->redirect($this, "view");
753    }
754
755    /**
756    * Insert (multiple) subchapters at node
757    */
758    public function insertSubchapter()
759    {
760        $ilCtrl = $this->ctrl;
761
762        $this->insertChapter(true);
763    }
764
765    /**
766    * Insert Chapter from clipboard
767    */
768    public function insertChapterClip($a_as_sub = false, $a_return = "view")
769    {
770        $ilUser = $this->user;
771        $ilCtrl = $this->ctrl;
772        $ilLog = $this->log;
773
774        $ilLog->write("Insert Chapter From Clipboard");
775
776        include_once("./Modules/LearningModule/classes/class.ilChapterHierarchyFormGUI.php");
777
778        $node_id = ilChapterHierarchyFormGUI::getPostNodeId();
779        $first_child = ilChapterHierarchyFormGUI::getPostFirstChild();
780
781        if ($a_as_sub) {		// as subchapter
782            if (!$first_child) {	// insert under parent
783                $parent_id = $node_id;
784                $target = "";
785            } else {													// we shouldnt end up here
786                $ilCtrl->redirect($this, "showHierarchy");
787                return;
788            }
789        } else {	// as chapter
790            if (!$first_child) {	// insert after node id
791                $parent_id = $this->tree->getParentId($node_id);
792                $target = $node_id;
793            } else {													// insert as first child
794                $parent_id = $node_id;
795                $target = IL_FIRST_NODE;
796
797                // do not move a chapter in front of a page
798                $childs = $this->tree->getChildsByType($parent_id, "pg");
799                if (count($childs) != 0) {
800                    $target = $childs[count($childs) - 1]["obj_id"];
801                }
802            }
803        }
804
805        // copy and paste
806        $chapters = $ilUser->getClipboardObjects("st", true);
807        $copied_nodes = array();
808
809        foreach ($chapters as $chap) {
810            $ilLog->write("Call pasteTree, Target LM: " . $this->content_object->getId() . ", Chapter ID: " . $chap["id"]
811                . ", Parent ID: " . $parent_id . ", Target: " . $target);
812            $cid = ilLMObject::pasteTree(
813                $this->content_object,
814                $chap["id"],
815                $parent_id,
816                $target,
817                $chap["insert_time"],
818                $copied_nodes,
819                (ilEditClipboard::getAction() == "copy")
820            );
821            $target = $cid;
822        }
823        ilLMObject::updateInternalLinks($copied_nodes);
824
825        if (ilEditClipboard::getAction() == "cut") {
826            $ilUser->clipboardDeleteObjectsOfType("pg");
827            $ilUser->clipboardDeleteObjectsOfType("st");
828            ilEditClipboard::clear();
829        }
830
831        $this->content_object->checkTree();
832        $ilCtrl->redirect($this, $a_return);
833    }
834
835    /**
836    * Insert Chapter from clipboard
837    */
838    public function insertSubchapterClip()
839    {
840        $this->insertChapterClip(true);
841    }
842
843    /**
844    * Insert (multiple) pages at node
845    */
846    public function insertPage()
847    {
848        $ilCtrl = $this->ctrl;
849        $lng = $this->lng;
850
851        include_once("./Modules/LearningModule/classes/class.ilChapterHierarchyFormGUI.php");
852
853        $num = ilChapterHierarchyFormGUI::getPostMulti();
854        $node_id = ilChapterHierarchyFormGUI::getPostNodeId();
855
856        if (!ilChapterHierarchyFormGUI::getPostFirstChild()) {	// insert after node id
857            $parent_id = $this->tree->getParentId($node_id);
858            $target = $node_id;
859        } else {													// insert as first child
860            $parent_id = $node_id;
861            $target = IL_FIRST_NODE;
862        }
863
864        for ($i = 1; $i <= $num; $i++) {
865            $page = new ilLMPageObject($this->content_object);
866            $page->setType("pg");
867            $page->setTitle($lng->txt("cont_new_page"));
868            $page->setLMId($this->content_object->getId());
869            $page->create();
870            ilLMObject::putInTree($page, $parent_id, $target);
871        }
872
873        $ilCtrl->redirect($this, "showHierarchy");
874    }
875
876    /**
877    * Insert pages from clipboard
878    */
879    public function insertPageClip()
880    {
881        $ilCtrl = $this->ctrl;
882        $ilUser = $this->user;
883
884        include_once("./Modules/LearningModule/classes/class.ilChapterHierarchyFormGUI.php");
885
886        $node_id = ilChapterHierarchyFormGUI::getPostNodeId();
887        $first_child = ilChapterHierarchyFormGUI::getPostFirstChild();
888
889        if (!$first_child) {	// insert after node id
890            $parent_id = $this->tree->getParentId($node_id);
891            $target = $node_id;
892        } else {													// insert as first child
893            $parent_id = $node_id;
894            $target = IL_FIRST_NODE;
895        }
896
897        // cut and paste
898        $pages = $ilUser->getClipboardObjects("pg");
899        $copied_nodes = array();
900        foreach ($pages as $pg) {
901            $cid = ilLMObject::pasteTree(
902                $this->content_object,
903                $pg["id"],
904                $parent_id,
905                $target,
906                $pg["insert_time"],
907                $copied_nodes,
908                (ilEditClipboard::getAction() == "copy")
909            );
910            $target = $cid;
911        }
912        ilLMObject::updateInternalLinks($copied_nodes);
913
914        if (ilEditClipboard::getAction() == "cut") {
915            $ilUser->clipboardDeleteObjectsOfType("pg");
916            $ilUser->clipboardDeleteObjectsOfType("st");
917            ilEditClipboard::clear();
918        }
919
920        $ilCtrl->redirect($this, "view");
921    }
922
923
924    /**
925    * Perform drag and drop action
926    */
927    public function proceedDragDrop()
928    {
929        $ilCtrl = $this->ctrl;
930
931        //echo "-".$_POST["il_hform_source_id"]."-".$_POST["il_hform_target_id"]."-".$_POST["il_hform_fc"]."-";
932        $this->content_object->executeDragDrop(
933            $_POST["il_hform_source_id"],
934            $_POST["il_hform_target_id"],
935            $_POST["il_hform_fc"],
936            $_POST["il_hform_as_subitem"]
937        );
938        $ilCtrl->redirect($this, "showHierarchy");
939    }
940
941    ////
942    //// Pages layout
943    ////
944
945    /**
946     * Set layout for multipl pages
947     */
948    public function setPageLayout()
949    {
950        $tpl = $this->tpl;
951        $ilCtrl = $this->ctrl;
952        $lng = $this->lng;
953
954        if (!is_array($_POST["id"])) {
955            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
956            $ilCtrl->redirect($this, "showHierarchy");
957        }
958
959        $this->initSetPageLayoutForm();
960
961        $tpl->setContent($this->form->getHTML());
962    }
963
964    /**
965     * Init set page layout form.
966     */
967    public function initSetPageLayoutForm()
968    {
969        $lng = $this->lng;
970        $ilCtrl = $this->ctrl;
971
972        include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
973        $this->form = new ilPropertyFormGUI();
974
975        if (is_array($_POST["id"])) {
976            foreach ($_POST["id"] as $id) {
977                $hi = new ilHiddenInputGUI("id[]");
978                $hi->setValue($id);
979                $this->form->addItem($hi);
980            }
981        }
982        $layout = ilObjContentObjectGUI::getLayoutOption(
983            $lng->txt("cont_layout"),
984            "layout",
985            $this->content_object->getLayout()
986        );
987
988        $this->form->addItem($layout);
989
990        $this->form->addCommandButton("savePageLayout", $lng->txt("save"));
991        $this->form->addCommandButton("showHierarchy", $lng->txt("cancel"));
992
993        $this->form->setTitle($lng->txt("cont_set_layout"));
994        $this->form->setFormAction($ilCtrl->getFormAction($this));
995    }
996
997    /**
998     * Save page layout
999     */
1000    public function savePageLayout()
1001    {
1002        $lng = $this->lng;
1003        $ilCtrl = $this->ctrl;
1004
1005        foreach ($_POST["id"] as $id) {
1006            $id = ilUtil::stripSlashes($id);
1007            ilLMPageObject::writeLayout(
1008                ilUtil::stripSlashes($id),
1009                ilUtil::stripSlashes($_POST["layout"]),
1010                $this->content_object
1011            );
1012        }
1013        ilUtil::sendSuccess($lng->txt("msg_obj_modified"), true);
1014        $ilCtrl->redirect($this, "showHierarchy");
1015    }
1016
1017    /**
1018     * Edit master language
1019     *
1020     * @param
1021     * @return
1022     */
1023    public function editMasterLanguage()
1024    {
1025        $ilCtrl = $this->ctrl;
1026
1027        $ilCtrl->setParameter($this, "transl", "");
1028        $ilCtrl->redirect($this, "showHierarchy");
1029    }
1030
1031    /**
1032     * Switch to language
1033     *
1034     * @param
1035     * @return
1036     */
1037    public function switchToLanguage()
1038    {
1039        $ilCtrl = $this->ctrl;
1040
1041        $ilCtrl->setParameter($this, "transl", $_GET["totransl"]);
1042        $ilCtrl->redirect($this, "showHierarchy");
1043    }
1044}
1045