1<?php
2
3/* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
6/**
7* Navigation History of Repository Items
8*
9* @author Alex Killing <alex.killing@gmx.de>
10* @version $Id$
11*/
12class ilNavigationHistory
13{
14    /**
15     * @var ilObjUser
16     */
17    protected $user;
18
19    /**
20     * @var ilDB
21     */
22    protected $db;
23
24    /**
25     * @var ilTree
26     */
27    protected $tree;
28
29    /**
30     * @var ilObjectDefinition
31     */
32    protected $obj_definition;
33
34    /**
35     * @var ilPluginAdmin
36     */
37    protected $plugin_admin;
38
39
40    private $items;
41
42    /**
43    * Constructor.
44    *
45    * @param	int	$a_id
46    */
47    public function __construct()
48    {
49        global $DIC;
50
51        $this->user = $DIC->user();
52        $this->db = $DIC->database();
53        $this->tree = $DIC->repositoryTree();
54        $this->obj_definition = $DIC["objDefinition"];
55        $this->plugin_admin = $DIC["ilPluginAdmin"];
56        $this->items = array();
57        $items = null;
58        if (isset($_SESSION["il_nav_history"])) {
59            $items = unserialize($_SESSION["il_nav_history"]);
60        }
61        if (is_array($items)) {
62            $this->items = $items;
63        }
64    }
65
66    /**
67    * Add an item to the stack. If ref_id is already used,
68    * the item is moved to the top.
69    */
70    public function addItem(
71        $a_ref_id,
72        $a_link,
73        $a_type,
74        $a_title = "",
75        $a_sub_obj_id = "",
76        $a_goto_link = ""
77    ) {
78        $ilUser = $this->user;
79        $ilDB = $this->db;
80
81        // never store?
82        if ($ilUser->prefs["store_last_visited"] == 2) {
83            return;
84        }
85
86        $a_sub_obj_id = $a_sub_obj_id . "";
87
88        if ($a_title == "" && $a_ref_id > 0) {
89            $obj_id = ilObject::_lookupObjId($a_ref_id);
90            if (ilObject::_exists($obj_id)) {
91                $a_title = ilObject::_lookupTitle($obj_id);
92            }
93        }
94
95        $id = $a_ref_id . ":" . $a_sub_obj_id;
96
97        $new_items[$id] = array("id" => $id,"ref_id" => $a_ref_id, "link" => $a_link, "title" => $a_title,
98            "type" => $a_type, "sub_obj_id" => $a_sub_obj_id, "goto_link" => $a_goto_link);
99
100        $cnt = 1;
101        foreach ($this->items as $key => $item) {
102            if ($item["id"] != $id && $cnt <= 10) {
103                $new_items[$item["id"]] = $item;
104                $cnt++;
105            }
106        }
107
108        // put items in session
109        $this->items = $new_items;
110
111        $items = serialize($this->items);
112        $_SESSION["il_nav_history"] = $items;
113        //var_dump($this->getItems());
114
115
116        // only store in session?
117        if ($ilUser->prefs["store_last_visited"] == 1) {
118            return;
119        }
120
121
122        // update entries in db
123        $ilDB->update(
124            "usr_data",
125            array(
126                    "last_visited" => array("clob", serialize($this->getItems()))),
127            array(
128                "usr_id" => array("integer", $ilUser->getId()))
129        );
130    }
131
132    /**
133    * Get navigation item stack.
134    */
135    public function getItems()
136    {
137        $tree = $this->tree;
138        $ilDB = $this->db;
139        $ilUser = $this->user;
140        $objDefinition = $this->obj_definition;
141        $ilPluginAdmin = $this->plugin_admin;
142
143        $items = array();
144
145        foreach ($this->items as $it) {
146            if ($tree->isInTree($it["ref_id"]) &&
147                (
148                    !$objDefinition->isPluginTypeName($it["type"]) ||
149                $ilPluginAdmin->isActive(
150                    IL_COMP_SERVICE,
151                    "Repository",
152                    "robj",
153                    ilPlugin::lookupNameForId(IL_COMP_SERVICE, "Repository", "robj", $it["type"])
154                )
155                )) {
156                $items[$it["ref_id"] . ":" . $it["sub_obj_id"]] = $it;
157            }
158        }
159        // less than 10? -> get items from db
160        if (count($items) < 10 && $ilUser->getId() != ANONYMOUS_USER_ID) {
161            $set = $ilDB->query(
162                "SELECT last_visited FROM usr_data " .
163                " WHERE usr_id = " . $ilDB->quote($ilUser->getId(), "integer")
164            );
165            $rec = $ilDB->fetchAssoc($set);
166            $db_entries = unserialize($rec["last_visited"]);
167            $cnt = count($items);
168            if (is_array($db_entries)) {
169                foreach ($db_entries as $rec) {
170                    include_once("./Services/Link/classes/class.ilLink.php");
171
172                    if ($cnt <= 10 && !isset($items[$rec["ref_id"] . ":" . $rec["sub_obj_id"]])) {
173                        if ($tree->isInTree($rec["ref_id"]) &&
174                            (
175                                !$objDefinition->isPluginTypeName($rec["type"]) ||
176                            $ilPluginAdmin->isActive(
177                                IL_COMP_SERVICE,
178                                "Repository",
179                                "robj",
180                                ilPlugin::lookupNameForId(IL_COMP_SERVICE, "Repository", "robj", $rec["type"])
181                            )
182                            )) {
183                            $link = ($rec["goto_link"] != "")
184                                ? $rec["goto_link"]
185                                : ilLink::_getLink($rec["ref_id"]);
186                            if ($rec["sub_obj_id"] != "") {
187                                $title = $rec["title"];
188                            } else {
189                                $title = ilObject::_lookupTitle(ilObject::_lookupObjId($rec["ref_id"]));
190                            }
191                            $items[$rec["ref_id"] . ":" . $rec["sub_obj_id"]] = array("id" => $rec["ref_id"] . ":" . $rec["sub_obj_id"],
192                                "ref_id" => $rec["ref_id"], "link" => $link, "title" => $title,
193                                "type" => $rec["type"], "sub_obj_id" => $rec["sub_obj_id"], "goto_link" => $rec["goto_link"]);
194                            $cnt++;
195                        }
196                    }
197                }
198            }
199        }
200        //var_dump($items);
201        return $items;
202    }
203
204    /**
205     * Delete DB entries
206     *
207     * @param
208     * @return
209     */
210    public function deleteDBEntries()
211    {
212        $ilUser = $this->user;
213        $ilDB = $this->db;
214
215        // update entries in db
216        $ilDB->update(
217            "usr_data",
218            array(
219                    "last_visited" => array("clob", serialize(array()))),
220            array(
221                "usr_id" => array("integer", $ilUser->getId()))
222        );
223    }
224
225    /**
226     * Delete session entries
227     *
228     * @param
229     * @return
230     */
231    public function deleteSessionEntries()
232    {
233        $_SESSION["il_nav_history"] = serialize(array());
234    }
235}
236