1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2009 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24/**
25* class for editing lm menu
26*
27* @author Sascha Hofmann <saschahofmann@gmx.de>
28* @version $Id$
29*
30* @ingroup ModulesIliasLearningModule
31*/
32class ilLMMenuEditor
33{
34    protected $active = "n";
35
36    /**
37     * @var ilDB
38     */
39    protected $db;
40
41    public function __construct()
42    {
43        global $DIC;
44
45        $ilDB = $DIC->database();
46
47        $this->db = $ilDB;
48        $this->link_type = "extern";
49        $this->link_ref_id = null;
50    }
51
52    public function setObjId($a_obj_id)
53    {
54        $this->lm_id = $a_obj_id;
55    }
56
57    public function getObjId()
58    {
59        return $this->lm_id;
60    }
61
62    public function setEntryId($a_id)
63    {
64        $this->entry_id = $a_id;
65    }
66
67    public function getEntryId()
68    {
69        return $this->entry_id;
70    }
71
72    public function setLinkType($a_link_type)
73    {
74        $this->link_type = $a_link_type;
75    }
76
77    public function getLinkType()
78    {
79        return $this->link_type;
80    }
81
82    public function setTitle($a_title)
83    {
84        $this->title = $a_title;
85    }
86
87    public function getTitle()
88    {
89        return $this->title;
90    }
91
92    public function setTarget($a_target)
93    {
94        $this->target = $a_target;
95    }
96
97    public function getTarget()
98    {
99        return $this->target;
100    }
101
102    public function setLinkRefId($a_link_ref_id)
103    {
104        $this->link_ref_id = $a_link_ref_id;
105    }
106
107    public function getLinkRefId()
108    {
109        return $this->link_ref_id;
110    }
111
112    /**
113     * Set active
114     *
115     * @param string $a_val
116     */
117    public function setActive($a_val)
118    {
119        $this->active = $a_val;
120    }
121
122    /**
123     * Get active
124     *
125     * @return string
126     */
127    public function getActive()
128    {
129        return $this->active;
130    }
131
132
133    public function create()
134    {
135        $ilDB = $this->db;
136
137        $id = $ilDB->nextId("lm_menu");
138        $q = "INSERT INTO lm_menu (id, lm_id,link_type,title,target,link_ref_id, active) " .
139             "VALUES " .
140             "(" .
141             $ilDB->quote($id, "integer") . "," .
142             $ilDB->quote((int) $this->getObjId(), "integer") . "," .
143             $ilDB->quote($this->getLinkType(), "text") . "," .
144             $ilDB->quote($this->getTitle(), "text") . "," .
145             $ilDB->quote($this->getTarget(), "text") . "," .
146             $ilDB->quote((int) $this->getLinkRefId(), "integer") . "," .
147             $ilDB->quote($this->getActive(), "text") .
148            ")";
149        $r = $ilDB->manipulate($q);
150
151        $this->entry_id = $id;
152
153        return true;
154    }
155
156    public function getMenuEntries($a_only_active = false)
157    {
158        $ilDB = $this->db;
159
160        $entries = array();
161
162        if ($a_only_active === true) {
163            $and = " AND active = " . $ilDB->quote("y", "text");
164        }
165
166        $q = "SELECT * FROM lm_menu " .
167             "WHERE lm_id = " . $ilDB->quote($this->lm_id, "integer") .
168             $and;
169
170        $r = $ilDB->query($q);
171
172        while ($row = $ilDB->fetchObject($r)) {
173            $entries[] = array('id' => $row->id,
174                               'title' => $row->title,
175                               'link' => $row->target,
176                               'type' => $row->link_type,
177                               'ref_id' => $row->link_ref_id,
178                               'active' => $row->active
179                               );
180        }
181
182        return $entries;
183    }
184
185    /**
186     * delete menu entry
187     *
188     */
189    public function delete($a_id)
190    {
191        $ilDB = $this->db;
192
193        if (!$a_id) {
194            return false;
195        }
196
197        $q = "DELETE FROM lm_menu WHERE id = " .
198            $ilDB->quote($a_id, "integer");
199        $ilDB->manipulate($q);
200
201        return true;
202    }
203
204    /**
205     * update menu entry
206     *
207     */
208    public function update()
209    {
210        $ilDB = $this->db;
211
212        $q = "UPDATE lm_menu SET " .
213            " link_type = " . $ilDB->quote($this->getLinkType(), "text") . "," .
214            " title = " . $ilDB->quote($this->getTitle(), "text") . "," .
215            " target = " . $ilDB->quote($this->getTarget(), "text") . "," .
216            " link_ref_id = " . $ilDB->quote((int) $this->getLinkRefId(), "integer") .
217            " WHERE id = " . $ilDB->quote($this->getEntryId(), "integer");
218        $r = $ilDB->manipulate($q);
219
220        return true;
221    }
222
223    public function readEntry($a_id)
224    {
225        $ilDB = $this->db;
226
227        if (!$a_id) {
228            return false;
229        }
230
231        $q = "SELECT * FROM lm_menu WHERE id = " .
232            $ilDB->quote($a_id, "integer");
233        $r = $ilDB->query($q);
234
235        $row = $ilDB->fetchObject($r);
236
237        $this->setTitle($row->title);
238        $this->setTarget($row->target);
239        $this->setLinkType($row->link_type);
240        $this->setLinkRefId($row->link_ref_id);
241        $this->setEntryid($a_id);
242        $this->setActive($row->active);
243    }
244
245    /**
246     * update active status of all menu entries of lm
247     * @param	array	entry ids
248     *
249     */
250    public function updateActiveStatus($a_entries)
251    {
252        $ilDB = $this->db;
253
254        // update active status
255        $q = "UPDATE lm_menu SET " .
256             "active = CASE " .
257             "WHEN " . $ilDB->in("id", $a_entries, false, "integer") . " " .
258             "THEN " . $ilDB->quote("y", "text") . " " .
259             "ELSE " . $ilDB->quote("n", "text") . " " .
260             "END " .
261             "WHERE lm_id = " . $ilDB->quote($this->lm_id, "integer");
262
263        $ilDB->manipulate($q);
264    }
265
266    /**
267     * Fix ref ids on import
268     *
269     * @param int $new_lm_id
270     * @param array $ref_mapping
271     */
272    public static function fixImportMenuItems(int $new_lm_id, array $ref_mapping)
273    {
274        global $DIC;
275
276        $db = $DIC->database();
277
278        $set = $db->queryF(
279            "SELECT * FROM lm_menu " .
280            " WHERE lm_id = %s ",
281            array("integer"),
282            array($new_lm_id)
283        );
284        while ($rec = $db->fetchAssoc($set)) {
285            // ... only check internal links
286            if ($rec["link_type"] == "intern") {
287                $link = explode("_", $rec["link_ref_id"]);
288                $ref_id = (int) $link[count($link) - 1];
289                $new_ref_id = $ref_mapping[$ref_id];
290                // if ref id has been imported, update it
291                if ($new_ref_id > 0) {
292                    $new_target = str_replace((string) $ref_id, (string) $new_ref_id, $rec["target"]);
293                    $db->update("lm_menu", array(
294                            "link_ref_id" => array("integer", $new_ref_id),
295                            "target" => array("text", $new_target)
296                        ), array(	// where
297                            "id" => array("integer", $rec["id"])
298                        ));
299                } else {	// if not, delete the menu item
300                    $db->manipulateF(
301                        "DELETE FROM lm_menu WHERE " .
302                        " id = %s",
303                        array("integer"),
304                        array($rec["id"])
305                    );
306                }
307            }
308        }
309    }
310
311    /**
312     * Write status for entry id
313     *
314     * @param $entry_id
315     * @param $active
316     */
317    public static function writeActive($entry_id, $active)
318    {
319        global $DIC;
320
321        $db = $DIC->database();
322
323        $db->update("lm_menu", array(
324                "active" => array("text", ($active ? "y" : "n"))
325            ), array(	// where
326                "id" => array("", $entry_id)
327            ));
328    }
329}
330