ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilFavouritesDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
27  public static array $is_desktop_item = [];
28  protected ilDBInterface $db;
29  protected ilTree $tree;
30 
31  public function __construct(
32  ?ilDBInterface $db = null,
33  ?ilTree $tree = null
34  ) {
35  global $DIC;
36 
37  $this->db = (is_null($db))
38  ? $DIC->database()
39  : $db;
40  $this->tree = (is_null($tree))
41  ? $DIC->repositoryTree()
42  : $tree;
43  }
44 
45  // Add favourite
46  public function add(int $user_id, int $ref_id): void
47  {
48  $db = $this->db;
49 
50  $type = ilObject::_lookupType($ref_id, true);
51 
52  $item_set = $db->queryF(
53  'SELECT * FROM desktop_item WHERE ' .
54  'item_id = %s AND type = %s AND user_id = %s',
55  ['integer', 'text', 'integer'],
56  [$ref_id, $type, $user_id]
57  );
58 
59  // only insert if item is not already on desktop
60  if (!$db->fetchAssoc($item_set)) {
61  $db->manipulateF(
62  'INSERT INTO desktop_item (item_id, type, user_id, parameters) VALUES ' .
63  ' (%s,%s,%s,%s)',
64  ['integer', 'text', 'integer', 'text'],
65  [$ref_id, $type, $user_id, '']
66  );
67  }
68  }
69 
70  // Remove favourite
71  public function remove(int $user_id, int $ref_id): void
72  {
73  $db = $this->db;
74 
75  $db->manipulateF(
76  'DELETE FROM desktop_item WHERE ' .
77  ' item_id = %s AND user_id = %s',
78  ['integer', 'integer'],
79  [$ref_id, $user_id]
80  );
81  }
82 
89  public function getFavouritesOfUser(int $user_id, ?array $a_types = null): array
90  {
91  $tree = $this->tree;
92  $ilDB = $this->db;
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 
167  return $items;
168  }
169 
170  // check whether an item is on the users desktop or not
171  public function ifIsFavourite(int $user_id, int $ref_id): bool
172  {
173  $db = $this->db;
174  $user_identification = $user_id . ':' . $ref_id;
175  if (!isset(self::$is_desktop_item[$user_identification])) {
176  $item_set = $db->queryF(
177  'SELECT item_id FROM desktop_item WHERE ' .
178  'item_id = %s AND user_id = %s',
179  ['integer', 'integer'],
180  [$ref_id, $user_id]
181  );
182 
183  if ($db->fetchAssoc($item_set)) {
184  self::$is_desktop_item[$user_identification] = true;
185  } else {
186  self::$is_desktop_item[$user_identification] = false;
187  }
188  }
189 
190  return self::$is_desktop_item[$user_identification];
191  }
192 
193  // Load favourites data
194  public function loadData(int $user_id, array $ref_ids): void
195  {
196  $db = $this->db;
197  if (!is_array($ref_ids)) {
198  return;
199  }
200 
201  $load_ref_ids = [];
202  foreach ($ref_ids as $ref_id) {
203  $user_identification = $user_id . ':' . $ref_id;
204  if (!isset(self::$is_desktop_item[$user_identification])) {
205  $load_ref_ids[] = $ref_id;
206  }
207  }
208 
209  if (count($load_ref_ids) > 0) {
210  $item_set = $db->query('SELECT item_id FROM desktop_item WHERE ' .
211  $db->in('item_id', $load_ref_ids, false, 'integer') .
212  ' AND user_id = ' . $db->quote($user_id, 'integer'));
213  while ($r = $db->fetchAssoc($item_set)) {
214  self::$is_desktop_item[$user_id . ':' . $r['item_id']] = true;
215  }
216  foreach ($load_ref_ids as $ref_id) {
217  $user_identification = $user_id . ':' . $ref_id;
218  if (!isset(self::$is_desktop_item[$user_identification])) {
219  self::$is_desktop_item[$user_id . ':' . $ref_id] = false;
220  }
221  }
222  }
223  }
224 
225  // Remove favourite entries of a repository item
226  public function removeFavouritesOfRefId(int $ref_id): void
227  {
228  $db = $this->db;
229 
230  $db->manipulateF(
231  'DELETE FROM desktop_item WHERE ' .
232  ' item_id = %s',
233  ['integer'],
234  [$ref_id]
235  );
236  }
237 
238  // Remove favourite entries of a user
239  public function removeFavouritesOfUser(int $user_id): void
240  {
241  $db = $this->db;
242 
243  $db->manipulateF(
244  'DELETE FROM desktop_item WHERE ' .
245  ' user_id = %s',
246  ['integer'],
247  [$user_id]
248  );
249  }
250 }
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
manipulateF(string $query, array $types, array $values)
fetchAssoc(ilDBStatement $statement)
isInTree(?int $a_node_id)
get all information of a node.
quote($value, string $type)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
static _lookupTitle(int $obj_id)
global $DIC
Definition: shib_login.php:26
ifIsFavourite(int $user_id, int $ref_id)
query(string $query)
Run a (read-only) Query on the database.
getParentId(int $a_node_id)
get parent id of given node
static _lookupDescription(int $obj_id)
queryF(string $query, array $types, array $values)
in(string $field, array $values, bool $negate=false, string $type="")
loadData(int $user_id, array $ref_ids)
__construct(?ilDBInterface $db=null, ?ilTree $tree=null)
static _lookupType(int $id, bool $reference=false)
getFavouritesOfUser(int $user_id, ?array $a_types=null)
Get all desktop items of user and specified type.
$r