ILIAS  release_8 Revision v8.24
class.ilFavouritesDBRepository.php
Go to the documentation of this file.
1<?php
2
23{
25 public static array $is_desktop_item = [];
26 protected ilDBInterface $db;
27 protected ilTree $tree;
28
29 public function __construct(
30 ilDBInterface $db = null,
31 ilTree $tree = null
32 ) {
33 global $DIC;
34
35 $this->db = (is_null($db))
36 ? $DIC->database()
37 : $db;
38 $this->tree = (is_null($tree))
39 ? $DIC->repositoryTree()
40 : $tree;
41 }
42
43
44 // Add favourite
45 public function add(int $user_id, int $ref_id): void
46 {
47 $db = $this->db;
48
50
51 $item_set = $db->queryF(
52 "SELECT * FROM desktop_item WHERE " .
53 "item_id = %s AND type = %s AND user_id = %s",
54 ["integer", "text", "integer"],
55 [$ref_id, $type, $user_id]
56 );
57
58 // only insert if item is not already on desktop
59 if (!$db->fetchAssoc($item_set)) {
61 "INSERT INTO desktop_item (item_id, type, user_id, parameters) VALUES " .
62 " (%s,%s,%s,%s)",
63 ["integer", "text", "integer", "text"],
64 [$ref_id, $type, $user_id, ""]
65 );
66 }
67 }
68
69 // Remove favourite
70 public function remove(int $user_id, int $ref_id): void
71 {
72 $db = $this->db;
73
75 "DELETE FROM desktop_item WHERE " .
76 " item_id = %s AND user_id = %s",
77 ["integer", "integer"],
78 [$ref_id, $user_id]
79 );
80 }
81
82
89 public function getFavouritesOfUser(int $user_id, ?array $a_types = null): array
90 {
93
94 if (is_null($a_types)) {
95 $item_set = $ilDB->queryF("SELECT obj.obj_id, obj.description, oref.ref_id, obj.title, obj.type " .
96 " FROM desktop_item it, object_reference oref " .
97 ", object_data obj" .
98 " WHERE " .
99 "it.item_id = oref.ref_id AND " .
100 "oref.obj_id = obj.obj_id AND " .
101 "it.user_id = %s", ["integer"], [$user_id]);
102 $items = $all_parent_path = [];
103 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
104 if ($item_rec["type"] !== "rolf" &&
105 $item_rec["type"] !== "itgr" &&
106 $tree->isInTree((int) $item_rec["ref_id"])) { // due to bug 11508
107 $parent_ref = $tree->getParentId((int) $item_rec["ref_id"]);
108
109 if (!isset($all_parent_path[$parent_ref])) {
110 if ($parent_ref > 0) { // workaround for #0023176
111 $node = $tree->getNodeData($parent_ref);
112 $all_parent_path[$parent_ref] = $node["title"];
113 } else {
114 $all_parent_path[$parent_ref] = "";
115 }
116 }
117
118 $parent_path = $all_parent_path[$parent_ref];
119
120 $title = ilObject::_lookupTitle($item_rec["obj_id"]);
121 $desc = ilObject::_lookupDescription($item_rec["obj_id"]);
122 $items[$parent_path . $title . $item_rec["ref_id"]] =
123 [
124 "ref_id" => (int) $item_rec["ref_id"],
125 "obj_id" => (int) $item_rec["obj_id"],
126 "type" => $item_rec["type"],
127 "title" => $title,
128 "description" => $desc,
129 "parent_ref" => (int) $parent_ref
130 ];
131 }
132 }
133 } else {
134 $items = [];
135 foreach ($a_types as $a_type) {
136 if ($a_type === "itgr") {
137 continue;
138 }
139 $item_set = $ilDB->queryF(
140 "SELECT obj.obj_id, obj.description, oref.ref_id, obj.title FROM desktop_item it, object_reference oref " .
141 ", object_data obj WHERE " .
142 "it.item_id = oref.ref_id AND " .
143 "oref.obj_id = obj.obj_id AND " .
144 "it.type = %s AND " .
145 "it.user_id = %s " .
146 "ORDER BY title",
147 ["text", "integer"],
148 [$a_type, $user_id]
149 );
150
151 while ($item_rec = $ilDB->fetchAssoc($item_set)) {
152 $title = ilObject::_lookupTitle($item_rec["obj_id"]);
153 $desc = ilObject::_lookupDescription($item_rec["obj_id"]);
154 $items[$title . $a_type . $item_rec["ref_id"]] =
155 [
156 "ref_id" => (int) $item_rec["ref_id"],
157 "obj_id" => (int) $item_rec["obj_id"],
158 "type" => $a_type,
159 "title" => $title,
160 "description" => $desc
161 ];
162 }
163 }
164 }
165 ksort($items);
166 return $items;
167 }
168
169 // check whether an item is on the users desktop or not
170 public function ifIsFavourite(int $user_id, int $ref_id): bool
171 {
172 $db = $this->db;
173
174 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
175 $item_set = $db->queryF(
176 "SELECT item_id FROM desktop_item WHERE " .
177 "item_id = %s AND user_id = %s",
178 ["integer", "integer"],
179 [$ref_id, $user_id]
180 );
181
182 if ($db->fetchAssoc($item_set)) {
183 self::$is_desktop_item[$user_id . ":" . $ref_id] = true;
184 } else {
185 self::$is_desktop_item[$user_id . ":" . $ref_id] = false;
186 }
187 }
188 return self::$is_desktop_item[$user_id . ":" . $ref_id];
189 }
190
191 // Load favourites data
192 public function loadData(int $user_id, array $ref_ids): void
193 {
194 $db = $this->db;
195 if (!is_array($ref_ids)) {
196 return;
197 }
198
199 $load_ref_ids = [];
200 foreach ($ref_ids as $ref_id) {
201 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
202 $load_ref_ids[] = $ref_id;
203 }
204 }
205
206 if (count($load_ref_ids) > 0) {
207 $item_set = $db->query("SELECT item_id FROM desktop_item WHERE " .
208 $db->in("item_id", $load_ref_ids, false, "integer") .
209 " AND user_id = " . $db->quote($user_id, "integer"));
210 while ($r = $db->fetchAssoc($item_set)) {
211 self::$is_desktop_item[$user_id . ":" . $r["item_id"]] = true;
212 }
213 foreach ($load_ref_ids as $ref_id) {
214 if (!isset(self::$is_desktop_item[$user_id . ":" . $ref_id])) {
215 self::$is_desktop_item[$user_id . ":" . $ref_id] = false;
216 }
217 }
218 }
219 }
220
221 // Remove favourite entries of a repository item
222 public function removeFavouritesOfRefId(int $ref_id): void
223 {
224 $db = $this->db;
225
227 "DELETE FROM desktop_item WHERE " .
228 " item_id = %s",
229 ["integer"],
230 [$ref_id]
231 );
232 }
233
234 // Remove favourite entries of a user
235 public function removeFavouritesOfUser(int $user_id): void
236 {
237 $db = $this->db;
238
240 "DELETE FROM desktop_item WHERE " .
241 " user_id = %s",
242 ["integer"],
243 [$user_id]
244 );
245 }
246}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ifIsFavourite(int $user_id, int $ref_id)
getFavouritesOfUser(int $user_id, ?array $a_types=null)
Get all desktop items of user and specified type.
__construct(ilDBInterface $db=null, ilTree $tree=null)
loadData(int $user_id, array $ref_ids)
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
static _lookupDescription(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isInTree(?int $a_node_id)
get all information of a node.
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
getParentId(int $a_node_id)
get parent id of given node
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
quote($value, string $type)
manipulateF(string $query, array $types, array $values)
query(string $query)
Run a (read-only) Query on the database.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
in(string $field, array $values, bool $negate=false, string $type="")
$ref_id
Definition: ltiauth.php:67
$type