ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilFavouritesDBRepository.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
12
16 public static $is_desktop_item = [];
17
21 public function __construct(\ilDBInterface $db = null, ilTree $tree = null)
22 {
23 global $DIC;
24
25 $this->db = (is_null($db))
26 ? $DIC->database()
27 : $db;
28 $this->tree = (is_null($tree))
29 ? $DIC->repositoryTree()
30 : $tree;
31 }
32
33
39 public function add(int $user_id, int $ref_id)
40 {
41 $db = $this->db;
42
43 $type = ilObject::_lookupType($ref_id, true);
44
45 $item_set = $db->queryF(
46 "SELECT * FROM desktop_item WHERE " .
47 "item_id = %s AND type = %s AND user_id = %s",
48 ["integer", "text", "integer"],
49 [$ref_id, $type, $user_id]
50 );
51
52 // only insert if item is not already on desktop
53 if (!$db->fetchAssoc($item_set)) {
54 $db->manipulateF(
55 "INSERT INTO desktop_item (item_id, type, user_id, parameters) VALUES " .
56 " (%s,%s,%s,%s)",
57 array("integer", "text", "integer", "text"),
58 array($ref_id,$type,$user_id,"")
59 );
60 }
61 }
62
69 public function remove(int $user_id, int $ref_id)
70 {
71 $db = $this->db;
72
73 $db->manipulateF(
74 "DELETE FROM desktop_item WHERE " .
75 " item_id = %s AND user_id = %s",
76 array("integer", "integer"),
77 array($ref_id, $user_id)
78 );
79 }
80
81
91 public function getFavouritesOfUser(int $user_id, array $a_types = null) : array
92 {
93 $tree = $this->tree;
94 $ilDB = $this->db;
95
96 if (is_null($a_types)) {
97 $item_set = $ilDB->queryF("SELECT obj.obj_id, obj.description, oref.ref_id, obj.title, obj.type " .
98 " FROM desktop_item it, object_reference oref " .
99 ", object_data obj" .
100 " WHERE " .
101 "it.item_id = oref.ref_id AND " .
102 "oref.obj_id = obj.obj_id AND " .
103 "it.user_id = %s", array("integer"), array($user_id));
104 $items = $all_parent_path = array();
105 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
106 if ($tree->isInTree($item_rec["ref_id"])
107 && $item_rec["type"] != "rolf"
108 && $item_rec["type"] != "itgr") { // due to bug 11508
109 $parent_ref = $tree->getParentId($item_rec["ref_id"]);
110
111 if (!isset($all_parent_path[$parent_ref])) {
112 if ($parent_ref > 0) { // workaround for #0023176
113 $node = $tree->getNodeData($parent_ref);
114 $all_parent_path[$parent_ref] = $node["title"];
115 } else {
116 $all_parent_path[$parent_ref] = "";
117 }
118 }
119
120 $parent_path = $all_parent_path[$parent_ref];
121
122 $title = ilObject::_lookupTitle($item_rec["obj_id"]);
123 $desc = ilObject::_lookupDescription($item_rec["obj_id"]);
124 $items[$parent_path . $title . $item_rec["ref_id"]] =
125 array("ref_id" => $item_rec["ref_id"],
126 "obj_id" => $item_rec["obj_id"],
127 "type" => $item_rec["type"],
128 "title" => $title,
129 "description" => $desc,
130 "parent_ref" => $parent_ref);
131 }
132 }
133 ksort($items);
134 } else {
135 $items = array();
136 foreach ($a_types as $a_type) {
137 if ($a_type == "itgr") {
138 continue;
139 }
140 $item_set = $ilDB->queryF(
141 "SELECT obj.obj_id, obj.description, oref.ref_id, obj.title FROM desktop_item it, object_reference oref " .
142 ", object_data obj WHERE " .
143 "it.item_id = oref.ref_id AND " .
144 "oref.obj_id = obj.obj_id AND " .
145 "it.type = %s AND " .
146 "it.user_id = %s " .
147 "ORDER BY title",
148 array("text", "integer"),
149 array($a_type, $user_id)
150 );
151
152 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
153 $title = ilObject::_lookupTitle($item_rec["obj_id"]);
154 $desc = ilObject::_lookupDescription($item_rec["obj_id"]);
155 $items[$title . $a_type . $item_rec["ref_id"]] =
156 array("ref_id" => $item_rec["ref_id"],
157 "obj_id" => $item_rec["obj_id"], "type" => $a_type,
158 "title" => $title, "description" => $desc);
159 }
160 }
161 ksort($items);
162 }
163 return $items;
164 }
165
172 public function ifIsFavourite($user_id, $ref_id)
173 {
174 $db = $this->db;
175
176 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
177 $item_set = $db->queryF(
178 "SELECT item_id FROM desktop_item WHERE " .
179 "item_id = %s AND user_id = %s",
180 array("integer", "integer"),
181 array($ref_id, $user_id)
182 );
183
184 if ($db->fetchAssoc($item_set)) {
185 self::$is_desktop_item[$user_id . ":" . $ref_id] = true;
186 } else {
187 self::$is_desktop_item[$user_id . ":" . $ref_id] = false;
188 }
189 }
190 return self::$is_desktop_item[$user_id . ":" . $ref_id];
191 }
192
198 public function loadData(int $user_id, array $ref_ids)
199 {
200 $db = $this->db;
201 if (!is_array($ref_ids)) {
202 return;
203 }
204
205 $load_ref_ids = [];
206 foreach ($ref_ids as $ref_id) {
207 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
208 $load_ref_ids[] = $ref_id;
209 }
210 }
211
212 if (count($load_ref_ids) > 0) {
213 $item_set = $db->query("SELECT item_id FROM desktop_item WHERE " .
214 $db->in("item_id", $load_ref_ids, false, "integer") .
215 " AND user_id = " . $db->quote($user_id, "integer"));
216 while ($r = $db->fetchAssoc($item_set)) {
217 self::$is_desktop_item[$user_id . ":" . $r["item_id"]] = true;
218 }
219 foreach ($load_ref_ids as $ref_id) {
220 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
221 self::$is_desktop_item[$user_id . ":" . $ref_id] = false;
222 }
223 }
224 }
225 }
226
232 public function removeFavouritesOfRefId(int $ref_id)
233 {
234 $db = $this->db;
235
236 $db->manipulateF(
237 "DELETE FROM desktop_item WHERE " .
238 " item_id = %s",
239 ["integer"],
240 [$ref_id]
241 );
242 }
243
249 public function removeFavouritesOfUser(int $user_id)
250 {
251 $db = $this->db;
252
253 $db->manipulateF(
254 "DELETE FROM desktop_item WHERE " .
255 " user_id = %s",
256 ["integer"],
257 [$user_id]
258 );
259 }
260}
An exception for terminatinating execution or to throw for unit testing.
ifIsFavourite($user_id, $ref_id)
check wether an item is on the users desktop or not
add(int $user_id, int $ref_id)
Add favourite.
getFavouritesOfUser(int $user_id, array $a_types=null)
Get all desktop items of user and specified type.
__construct(\ilDBInterface $db=null, ilTree $tree=null)
Constructor.
removeFavouritesOfRefId(int $ref_id)
Remove favourite entries of a repository item.
loadData(int $user_id, array $ref_ids)
Load favourites data.
removeFavouritesOfUser(int $user_id)
Remove favourite entries of a user.
static _lookupTitle($a_id)
lookup object title
static _lookupDescription($a_id)
lookup object description
static _lookupType($a_id, $a_reference=false)
lookup object type
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
Interface ilDBInterface.
$type
global $ilDB
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46