1<?php
2
3/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * LM Chapter
7 *
8 * @author Alex Killing <alex.killing@gmx.de>
9 * @version $Id\$
10 * @ingroup
11 */
12class ilLMChapterImportForm
13{
14    /**
15     * @var ilObjUser
16     */
17    protected $user;
18
19    /**
20     * @var ilLanguage
21     */
22    protected $lng;
23
24    protected $tree = null;
25    protected $node_id = null;
26    protected $confirm = false;
27    protected $perform = false;
28
29    /**
30     * Constructor
31     *
32     * @param
33     * @return
34     */
35    public function __construct($a_slm, $a_node_id, $a_first_child, $a_confirm = false)
36    {
37        global $DIC;
38
39        $this->user = $DIC->user();
40        $this->lng = $DIC->language();
41        include_once("./Modules/Scorm2004/classes/class.ilSCORM2004Tree.php");
42        $this->slm = $a_slm;
43        $this->tree = new ilSCORM2004Tree($this->slm->getId());
44        $this->node_id = $a_node_id;
45        $this->node = $this->tree->getNodeData($this->node_id);
46        $this->current_depth = $this->node["depth"];
47        if ($a_first_child) {
48            $this->current_depth++;
49            $this->current_parent = $this->node;
50        } else {
51            $this->current_parent = $this->tree->getNodeData($this->node["parent"]);
52        }
53        if ($this->current_parent["child"] == $this->tree->readRootId()) {
54            $this->current_parent["title"] = ilObject::_lookupTitle($this->slm->getId());
55            $this->current_parent["type"] = "sahs";
56        }
57        $this->first_child = $a_first_child;
58        $this->confirm = $a_confirm;
59        $this->correct = true;
60    }
61
62    /**
63     * Is correct?
64     *
65     * @param
66     * @return
67     */
68    public function isCorrect()
69    {
70        return $this->correct;
71    }
72
73    public function performInserts()
74    {
75        // act like in confirm mode
76        $this->confirm = true;
77        $this->perform = true;
78        $this->processHierarchy();
79    }
80
81    public function processHierarchy()
82    {
83        $ilUser = $this->user;
84
85        $target = ($this->first_child)
86            ? IL_FIRST_NODE
87            : $this->node_id;
88
89        // get chapters
90        $chapters = $ilUser->getClipboardObjects("st", true);
91
92        if ($this->perform) {
93            $this->current_parent["insert_id"] = $this->current_parent["child"];
94            $this->target[$this->current_parent["insert_id"]] = $target;
95        }
96        foreach ($chapters as $chap) {
97            $chap["parent"] = $this->current_parent;
98            $this->addNode($chap, $this->current_parent, $this->current_depth);
99        }
100    }
101
102    /**
103     * Get html
104     *
105     * @param
106     * @return
107     */
108    public function getHTML()
109    {
110        $ilUser = $this->user;
111
112        include_once("./Services/UIComponent/NestedList/classes/class.ilNestedList.php");
113        $this->list = new ilNestedList();
114        $this->list->setListClass("noStyle");
115
116        $this->list->addListNode(
117            ilUtil::img(ilUtil::getImagePath("icon_" . $this->current_parent["type"] . ".svg")) . " " .
118            $this->current_parent["title"] . " " .
119            "",
120            $this->current_parent["id"],
121            0
122        );
123
124        $this->processHierarchy();
125
126        return $this->list->getHTML();
127    }
128
129    /**
130     * Add node
131     *
132     * @param
133     * @return
134     */
135    public function addNode($a_node, $a_parent, $a_depth)
136    {
137        $ilUser = $this->user;
138        $lng = $this->lng;
139
140        //$tpl = new ilTemplate("tpl.lm_chap_import_node.html", true, true, "Modules/Scorm2004");
141        $lng->loadLanguageModule("content");
142
143        $options = array(
144            "" => $lng->txt("cont_assign_to_parent"),
145            "chap" => $lng->txt("obj_chap"),
146            "sco" => $lng->txt("obj_sco"),
147            "ass" => $lng->txt("obj_ass")
148        );
149
150        if (!$this->confirm) {
151            $sel = ($a_node["type"] != "pg")
152                ? $sel = ilUtil::formSelect($_POST["node"][$a_node["id"]], "node[" . $a_node["id"] . "]", $options, false, true)
153                : "";
154            $img = ilUtil::img(ilUtil::getImagePath("icon_" . $a_node["type"] . ".svg"));
155        } else {
156            // if scos/assets are nested in scos/assets, put the tree one level up
157            //echo "<br>-".$a_node["title"]."-".$_POST["node"][$a_node["id"]]."-".$_POST["node"][$a_parent["id"]]."-";
158            if (in_array($_POST["node"][$a_node["id"]], array("sco", "ass")) && in_array($_POST["node"][$a_parent["id"]], array("sco", "ass"))) {
159                //echo "j";
160                //var_dump($a_parent);
161                //		echo "<br>getting-from-".$a_parent["title"]."-".$a_parent["id"]."-".$a_target."-";
162                $a_parent = $a_parent["parent"];
163                $a_depth--;
164            }
165
166            $sel = ($a_node["type"] != "pg")
167                ? $sel = "<strong>" . $lng->txt("obj_" . $_POST["node"][$a_node["id"]]) . "</strong>"
168                : "";
169            $ctype = ($a_node["type"] != "pg")
170                ? $_POST["node"][$a_node["id"]]
171                : "pg";
172
173            $parent_type = $a_parent["type"];
174            if (isset($_POST["node"][$a_parent["id"]])) {
175                $parent_type = $_POST["node"][$a_parent["id"]];
176            }
177            //echo "<br>-".$a_node["title"]."-".$parent_type."-";
178            $img = ilUtil::img(ilUtil::getImagePath("icon_" . $ctype . ".svg"));
179
180            $error = "";
181            switch ($a_depth) {
182                case 0:
183                case 1:
184                    $this->correct = false;
185                    break;
186
187                case 2:
188                    if (!in_array($ctype, array("", "chap", "sco", "ass"))) {
189                        $error = '<span class="alert">' . $lng->txt("cont_type_not_allowed") . ": " . $lng->txt("obj_" . $ctype) . "</span>";
190                        if ($_POST["node"][$a_node["id"]] != "" || $a_node["type"] == "pg") {
191                            $this->correct = false;
192                        }
193                    }
194                    break;
195
196                default:
197                    if ($parent_type == "chap" && !in_array($ctype, array("sco", "ass")) ||
198                        ($parent_type != "chap" && !in_array($ctype, array("pg")))) {
199                        $error = '<span class="alert">' . $lng->txt("cont_type_not_allowed") . ": " . $lng->txt("obj_" . $ctype) . "</span>";
200                        if ($_POST["node"][$a_node["id"]] != "" || $a_node["type"] == "pg") {
201                            $this->correct = false;
202                        }
203                    }
204                    break;
205            }
206        }
207
208        // if node should be inserted, increase depth
209        if (!$this->confirm || $_POST["node"][$a_node["id"]] != "" || $a_node["type"] == "pg") {
210            if (!$this->perform) {
211                $this->list->addListNode(
212                    $img . " " .
213                    $a_node["title"] . " " .
214                    $sel . " " . $error,
215                    $a_node["id"],
216                    $a_parent["id"]
217                );
218            } else {
219                if ($a_parent["insert_id"] > 0) {
220                    $target = $this->target[$a_parent["insert_id"]];
221
222                    // create new node of ctype, put it under parent (respect after node from hierarchy)
223                    switch ($ctype) {
224                        case "chap":
225                            include_once("./Modules/Scorm2004/classes/class.ilSCORM2004Chapter.php");
226                            $chap = new ilSCORM2004Chapter($this->slm);
227                            $chap->setTitle($a_node["title"]);
228                            $chap->setSLMId($this->slm->getId());
229                            $chap->create();
230                            ilSCORM2004Node::putInTree($chap, $a_parent["insert_id"], $target);
231                            $a_node["insert_id"] = $chap->getId();
232                            break;
233
234                        case "ass":
235                            include_once("./Modules/Scorm2004/classes/class.ilSCORM2004Asset.php");
236                            $ass = new ilSCORM2004Asset($this->slm);
237                            $ass->setTitle($a_node["title"]);
238                            $ass->setSLMId($this->slm->getId());
239                            $ass->create();
240                            ilSCORM2004Node::putInTree($ass, $a_parent["insert_id"], $target);
241                            $a_node["insert_id"] = $ass->getId();
242                            break;
243
244                        case "sco":
245                            include_once("./Modules/Scorm2004/classes/class.ilSCORM2004Sco.php");
246                            $sco = new ilSCORM2004Sco($this->slm);
247                            $sco->setTitle($a_node["title"]);
248                            $sco->setSLMId($this->slm->getId());
249                            $sco->create();
250                            ilSCORM2004Node::putInTree($sco, $a_parent["insert_id"], $target);
251                            $a_node["insert_id"] = $sco->getId();
252                            break;
253
254                        case "pg":
255                            include_once("./Modules/Scorm2004/classes/class.ilSCORM2004Node.php");
256                            $copied_nodes = array();
257                            $a_node["insert_id"] = ilSCORM2004Node::pasteTree(
258                                $this->slm,
259                                $a_node["id"],
260                                $a_parent["insert_id"],
261                                $target,
262                                $a_node["insert_time"],
263                                $copied_nodes,
264                                true,
265                                true,
266                                "lm"
267                            );
268                            break;
269                    }
270
271                    $this->target[$a_parent["insert_id"]] = $a_node["insert_id"];
272
273                    // set $a_node["insert_id"] to new id
274                }
275            }
276
277            $a_depth++;
278            $parent = $a_node;
279        } else {
280            $parent = $a_parent;
281        }
282
283        $childs = $ilUser->getClipboardChilds($a_node["id"], $a_node["insert_time"]);
284        foreach ($childs as $c) {
285            $c["parent"] = $a_node;
286            //		$c["parent_target"] = rand(1,100);
287            //		echo "<br>setting-".$c["parent_target"]."-into-".$c["id"]."-".$c["title"]."-";
288            $this->addNode($c, $parent, $a_depth);
289        }
290    }
291}
292