ILIAS  release_8 Revision v8.24
class.NoteDBRepository.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Notes;
22
27{
28 protected \ilDBInterface $db;
30
31 public function __construct(
34 ) {
35 $this->db = $db;
36 $this->data = $data;
37 }
38
39 public function createNote(
40 Note $note
41 ): Note {
42 $db = $this->db;
43
44 $id = $db->nextId("note");
45 $context = $note->getContext();
46 $db->insert("note", array(
47 "id" => array("integer", $id),
48 "rep_obj_id" => array("integer", $context->getObjId()),
49 "obj_id" => array("integer", $context->getSubObjId()),
50 "obj_type" => array("text", $context->getType()),
51 "news_id" => array("integer", $context->getNewsId()),
52 "type" => array("integer", $note->getType()),
53 "author" => array("integer", $note->getAuthor()),
54 "note_text" => array("clob", $note->getText()),
55 "creation_date" => array("timestamp", $note->getCreationDate()),
56 "no_repository" => array("integer", (int) !$context->getInRepository())
57 ));
58 return $this->getById($id);
59 }
60
61 public function deleteNote(int $id): void
62 {
63 $db = $this->db;
64 $q = "DELETE FROM note WHERE id = " .
65 $db->quote($id, "integer");
66 $db->manipulate($q);
67 }
68
69 public function updateNoteText(
70 int $id,
71 string $text
72 ): void {
73 $db = $this->db;
74
75 $update_date = \ilUtil::now();
76 $db->update("note", array(
77 "note_text" => array("clob", $text),
78 "update_date" => array("timestamp", $update_date),
79 ), array(
80 "id" => array("integer", $id)
81 ));
82 }
83
88 public function getById(
89 int $id
90 ): Note {
91 $db = $this->db;
92
93 $set = $db->queryF(
94 "SELECT * FROM note " .
95 " WHERE id = %s ",
96 ["integer"],
97 [$id]
98 );
99 if ($rec = $db->fetchAssoc($set)) {
100 return $this->getNoteFromRecord($rec);
101 }
102 throw new NoteNotFoundException("Note with ID $id not found.");
103 }
104
105 protected function getNoteFromRecord(array $rec): Note
106 {
107 return $this->data->note(
108 (int) $rec["id"],
109 $this->data->context(
110 (int) $rec["rep_obj_id"],
111 (int) $rec["obj_id"],
112 $rec["obj_type"],
113 (int) $rec["news_id"],
114 !$rec["no_repository"]
115 ),
116 (string) $rec["note_text"],
117 (int) $rec["author"],
118 (int) $rec["type"],
119 $rec["creation_date"],
120 $rec["update_date"]
121 );
122 }
123
128 protected function getQuery(
130 int $type = Note::PRIVATE,
131 bool $incl_sub = false,
132 int $author = 0,
133 bool $ascending = false,
134 bool $count = false,
135 string $since = "",
136 array $obj_ids = [],
137 string $search_text = ""
138 ): string {
139 $db = $this->db;
140
141 $author_where = ($author > 0)
142 ? " AND author = " . $db->quote($author, "integer")
143 : "";
144
145 $sub_where = ($context)
146 ? " rep_obj_id = " . $db->quote($context->getObjId(), "integer")
147 : " " . $db->in("rep_obj_id", $obj_ids, false, "integer");
148
149 $sub_where .= ($context && !$incl_sub)
150 ? " AND note.obj_id = " . $db->quote($context->getSubObjId(), "integer") .
151 " AND note.obj_type = " . $db->quote($context->getType(), "text")
152 : "";
153
154 if ($since !== "") {
155 $sub_where .= " AND creation_date > " . $db->quote($since, "timestamp");
156 }
157
158 $news_where = "";
159 if ($context) {
160 $news_where =
161 " AND news_id = " . $db->quote($context->getNewsId(), "integer");
162
163 $sub_where .= " AND no_repository = " . $db->quote(!$context->getInRepository(), "integer");
164 }
165
166 // search text
167 $join = "";
168 if ($search_text !== "") {
169 $sub_where .= " AND (" . $db->like("note_text", "text", "%" . $search_text . "%");
170 $join = " JOIN usr_data ud ON (author = ud.usr_id)";
171 $join .= " LEFT JOIN object_data od ON (rep_obj_id = od.obj_id)";
172 $sub_where .= " OR " . $db->like("ud.lastname", "text", "%" . $search_text . "%");
173 $sub_where .= " OR " . $db->like("ud.firstname", "text", "%" . $search_text . "%");
174 $sub_where .= " OR " . $db->like("ud.login", "text", "%" . $search_text . "%");
175 $sub_where .= " OR " . $db->like("od.title", "text", "%" . $search_text . "%");
176 $sub_where .= ")";
177 }
178
179 $fields = $count ? "count(*) cnt" : "note.*";
180 $query = "SELECT $fields FROM note $join WHERE " .
181 $sub_where .
182 " AND note.type = " . $db->quote($type, "integer") .
183 $author_where .
184 $news_where .
185 " ORDER BY creation_date ";
186 $query .= ($ascending) ? "ASC" : "DESC";
187 return $query;
188 }
189
194 public function getNotesForContext(
196 int $type = Note::PRIVATE,
197 bool $incl_sub = false,
198 int $author = 0,
199 bool $ascending = false,
200 string $since = "",
201 string $search_text = ""
202 ): array {
203 $db = $this->db;
204
205 $query = $this->getQuery(
206 $context,
207 $type,
208 $incl_sub,
209 $author,
210 $ascending,
211 false,
212 $since,
213 [],
214 $search_text
215 );
216
217 $set = $db->query($query);
218 $notes = [];
219 while ($note_rec = $db->fetchAssoc($set)) {
220 $notes[] = $this->getNoteFromRecord($note_rec);
221 }
222 return $notes;
223 }
224
229 public function getNotesForObjIds(
230 array $obj_ids,
231 int $type = Note::PRIVATE,
232 bool $incl_sub = false,
233 int $author = 0,
234 bool $ascending = false,
235 string $since = "",
236 string $search_text = ""
237 ): array {
238 $db = $this->db;
239
240 $query = $this->getQuery(
241 null,
242 $type,
243 $incl_sub,
244 $author,
245 $ascending,
246 false,
247 $since,
248 $obj_ids,
249 $search_text
250 );
251
252 $set = $db->query($query);
253 $notes = [];
254 while ($note_rec = $db->fetchAssoc($set)) {
255 $notes[] = $this->getNoteFromRecord($note_rec);
256 }
257 return $notes;
258 }
259
260 public function getNrOfNotesForContext(
262 int $type = Note::PRIVATE,
263 bool $incl_sub = false,
264 int $author = 0
265 ): int {
266 $db = $this->db;
267
268 $query = $this->getQuery(
269 $context,
270 $type,
271 $incl_sub,
272 $author,
273 false,
274 true
275 );
276
277 $set = $db->query($query);
278 $rec = $db->fetchAssoc($set);
279 return (int) $rec["cnt"];
280 }
281
285 public function getRelatedObjIdsOfUser(
286 int $user_id,
287 int $type
288 ): array {
289 $db = $this->db;
290
291 $q = "SELECT DISTINCT rep_obj_id FROM note WHERE " .
292 " type = " . $db->quote($type, "integer") .
293 " AND author = " . $db->quote($user_id, "integer") .
294 " AND (no_repository IS NULL OR no_repository < " . $db->quote(1, "integer") . ")";
295
296 $set = $db->query($q);
297 $ids = [];
298 while ($rec = $db->fetchAssoc($set)) {
299 $ids[] = (int) $rec["rep_obj_id"];
300 }
301 return $ids;
302 }
303
308 public function filterObjectsWithNotes(array $obj_ids, int $type): array
309 {
310 $db = $this->db;
311
312 $q = "SELECT DISTINCT rep_obj_id FROM note WHERE " .
313 $db->in("rep_obj_id", $obj_ids, false, "integer") .
314 " AND type = " . $db->quote($type, "integer") .
315 " AND (no_repository IS NULL OR no_repository < " . $db->quote(1, "integer") . ")";
316
317 $set = $db->query($q);
318 $ids = [];
319 while ($rec = $db->fetchAssoc($set)) {
320 $ids[] = (int) $rec["rep_obj_id"];
321 }
322 return $ids;
323 }
324
328 public function getUserCount(
329 int $obj_id,
330 int $sub_obj_id,
331 string $obj_type
332 ): int {
333 $db = $this->db;
334
335 $set = $db->queryF(
336 "SELECT count(DISTINCT author) cnt FROM note WHERE " .
337 "rep_obj_id = %s AND obj_id = %s AND obj_type = %s",
338 array("integer", "integer", "text"),
339 array($obj_id, $sub_obj_id, $obj_type)
340 );
341 $rec = $db->fetchAssoc($set);
342 return (int) $rec["cnt"];
343 }
344
351 array $obj_ids,
352 int $user_id,
353 bool $no_sub_objs = false
354 ): array {
355 $db = $this->db;
356
357 $q = "SELECT count(id) c, rep_obj_id, type FROM note WHERE " .
358 " ((type = " . $db->quote(Note::PRIVATE, "integer") . " AND " .
359 "author = " . $db->quote($user_id, "integer") . ") OR " .
360 " type = " . $db->quote(Note::PUBLIC, "integer") . ") AND " .
361 $db->in("rep_obj_id", $obj_ids, false, "integer");
362
363 if ($no_sub_objs) {
364 $q .= " AND obj_id = " . $db->quote(0, "integer");
365 }
366
367 $q .= " GROUP BY rep_obj_id, type ";
368
369 $cnt = array();
370 $set = $db->query($q);
371 while ($rec = $db->fetchAssoc($set)) {
372 $cnt[$rec["rep_obj_id"]][$rec["type"]] = $rec["c"];
373 }
374 return $cnt;
375 }
376}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Repository internal data service.
getNrOfNotesForContext(Context $context, int $type=Note::PRIVATE, bool $incl_sub=false, int $author=0)
countNotesAndCommentsMultipleObjects(array $obj_ids, int $user_id, bool $no_sub_objs=false)
Get all notes related to multiple repository objects.
filterObjectsWithNotes(array $obj_ids, int $type)
__construct(InternalDataService $data, \ilDBInterface $db)
getRelatedObjIdsOfUser(int $user_id, int $type)
getNotesForObjIds(array $obj_ids, int $type=Note::PRIVATE, bool $incl_sub=false, int $author=0, bool $ascending=false, string $since="", string $search_text="")
Get all notes related to a specific object.
updateNoteText(int $id, string $text)
getUserCount(int $obj_id, int $sub_obj_id, string $obj_type)
How many users have attached a note/comment to a given object?
getNotesForContext(Context $context, int $type=Note::PRIVATE, bool $incl_sub=false, int $author=0, bool $ascending=false, string $since="", string $search_text="")
Get all notes related to a specific object.
getQuery(?Context $context, int $type=Note::PRIVATE, bool $incl_sub=false, int $author=0, bool $ascending=false, bool $count=false, string $since="", array $obj_ids=[], string $search_text="")
Get query.
static now()
Return current timestamp in Y-m-d H:i:s format.
Interface ilDBInterface.
insert(string $table_name, array $values)
nextId(string $table_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
$type
$context
Definition: webdav.php:29