ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilNewsItem.php
Go to the documentation of this file.
1<?php
2
20
21const NEWS_NOTICE = 0;
22const NEWS_MESSAGE = 1;
23const NEWS_WARNING = 2;
24
25const NEWS_TEXT = "text";
26const NEWS_HTML = "html";
27const NEWS_AUDIO = "audio";
28const NEWS_USERS = "users";
29const NEWS_PUBLIC = "public";
30
43{
44 private int $mob_cnt_download = 0;
46 protected int $mob_cnt_play = 0;
47 protected ilDBInterface $db;
48 protected ilTree $tree;
51 protected ilObjUser $user;
52 protected ilLanguage $lng;
53 protected ilCtrl $ctrl;
54 protected int $id = 0;
55 protected string $title = "";
56 protected string $content = "";
57 protected bool $content_html = false;
58 protected int $context_obj_id = 0;
59 protected string $context_obj_type = "";
60 protected int $context_sub_obj_id = 0;
61 protected ?string $context_sub_obj_type = null;
62 protected string $content_type = "text";
63 protected string $creation_date = "";
64 protected string $update_date = "";
65 protected int $user_id = 0;
66 protected int $update_user_id = 0;
67 protected string $visibility = "users";
68 protected string $content_long = "";
69 protected int $priority = 1;
70 protected bool $content_is_lang_var = false;
71 protected int $mob_id = 0;
72 protected string $playtime = "";
73 private static int $privFeedId = 0;
74 private bool $limitation = false;
75 protected bool $content_text_is_lang_var = false;
77 protected ilLogger $log;
78
79 public function __construct(int $a_id = 0)
80 {
81 global $DIC;
82 $this->main_tpl = $DIC->ui()->mainTemplate();
83
84 $this->db = $DIC->database();
85 $this->tree = $DIC->repositoryTree();
86 $this->access = $DIC->access();
87 $this->obj_data_cache = $DIC["ilObjDataCache"];
88 $this->user = $DIC->user();
89 $this->lng = $DIC->language();
90 $this->ctrl = $DIC->ctrl();
91 if ($a_id > 0) {
92 $this->setId($a_id);
93 $this->read();
94 }
95 $this->limitation = true;
96 $this->log = $DIC->logger()->news();
97 $this->media_manager = $DIC->mediaObjects()->internal()->domain()->mediaObject();
98 }
99
100 public function setId(int $a_id): void
101 {
102 $this->id = $a_id;
103 }
104
105 public function getId(): int
106 {
107 return $this->id;
108 }
109
110 public function setTitle(string $a_title): void
111 {
112 $this->title = $a_title;
113 }
114
115 public function getTitle(): string
116 {
117 return $this->title;
118 }
119
120 public function setContent(string $a_content): void
121 {
122 $this->content = $a_content;
123 }
124
125 public function getContent(): string
126 {
127 return $this->content;
128 }
129
130 public function setContextObjId(int $a_context_obj_id): void
131 {
132 $this->context_obj_id = $a_context_obj_id;
133 }
134
135 public function getContextObjId(): int
136 {
138 }
139
140 public function setContextObjType(string $a_context_obj_type): void
141 {
142 $this->context_obj_type = $a_context_obj_type;
143 }
144
145 public function getContextObjType(): string
146 {
148 }
149
150 public function setContextSubObjId(int $a_context_sub_obj_id): void
151 {
152 $this->context_sub_obj_id = $a_context_sub_obj_id;
153 }
154
155 public function getContextSubObjId(): int
156 {
158 }
159
160 public function setContextSubObjType(?string $a_context_sub_obj_type): void
161 {
162 $this->context_sub_obj_type = $a_context_sub_obj_type;
163 }
164
165 public function getContextSubObjType(): ?string
166 {
168 }
169
170 public function setContentType(string $a_content_type = "text"): void
171 {
172 $this->content_type = $a_content_type;
173 }
174
175 public function getContentType(): string
176 {
177 return $this->content_type;
178 }
179
180 public function setCreationDate(string $a_creation_date): void
181 {
182 $this->creation_date = $a_creation_date;
183 }
184
185 public function getCreationDate(): string
186 {
188 }
189
190 public function setUpdateDate(string $a_update_date): void
191 {
192 $this->update_date = $a_update_date;
193 }
194
195 public function getUpdateDate(): string
196 {
197 return $this->update_date;
198 }
199
200 public function setUserId(int $a_user_id): void
201 {
202 $this->user_id = $a_user_id;
203 }
204
205 public function getUserId(): int
206 {
207 return $this->user_id;
208 }
209
210 public function setUpdateUserId(int $a_val): void
211 {
212 $this->update_user_id = $a_val;
213 }
214
215 public function getUpdateUserId(): int
216 {
218 }
219
223 public function setVisibility(
224 string $a_visibility = "users"
225 ): void {
226 $this->visibility = $a_visibility;
227 }
228
229 public function getVisibility(): string
230 {
231 return $this->visibility;
232 }
233
237 public function setContentLong(string $a_content_long): void
238 {
239 $this->content_long = $a_content_long;
240 }
241
242 public function getContentLong(): string
243 {
244 return $this->content_long;
245 }
246
247 public function setPriority(int $a_priority = 1): void
248 {
249 $this->priority = $a_priority;
250 }
251
252 public function getPriority(): int
253 {
254 return $this->priority;
255 }
256
257 public function setContentIsLangVar(
258 bool $a_content_is_lang_var = false
259 ): void {
260 $this->content_is_lang_var = $a_content_is_lang_var;
261 }
262
263 public function getContentIsLangVar(): bool
264 {
265 return $this->content_is_lang_var;
266 }
267
268 public function setMobId(int $a_mob_id): void
269 {
270 $this->mob_id = $a_mob_id;
271 }
272
273 public function getMobId(): int
274 {
275 return $this->mob_id;
276 }
277
281 public function setPlaytime(string $a_playtime): void
282 {
283 $this->playtime = $a_playtime;
284 }
285
286 public function getPlaytime(): string
287 {
288 return $this->playtime;
289 }
290
294 public function setLimitation(bool $a_limitation): void
295 {
296 $this->limitation = $a_limitation;
297 }
298
299 public function getLimitation(): bool
300 {
301 return $this->limitation;
302 }
303
304 public function setContentTextIsLangVar(bool $a_val = false): void
305 {
306 $this->content_text_is_lang_var = $a_val;
307 }
308
309 public function getContentTextIsLangVar(): bool
310 {
311 return $this->content_text_is_lang_var;
312 }
313
314 public function setMobPlayCounter(int $a_val): void
315 {
316 $this->mob_cnt_play = $a_val;
317 }
318
319 public function getMobPlayCounter(): int
320 {
321 return $this->mob_cnt_play;
322 }
323
324 public function setMobDownloadCounter(int $a_val): void
325 {
326 $this->mob_cnt_download = $a_val;
327 }
328
329 public function getMobDownloadCounter(): int
330 {
331 return $this->mob_cnt_download;
332 }
333
334 public function setContentHtml(bool $a_val): void
335 {
336 $this->content_html = $a_val;
337 }
338
339 public function getContentHtml(): bool
340 {
341 return $this->content_html;
342 }
343
348 public function read(): void
349 {
350 $ilDB = $this->db;
351
352 $query = "SELECT * FROM il_news_item WHERE id = " .
353 $ilDB->quote($this->getId(), "integer");
354 $set = $ilDB->query($query);
355 if ($rec = $ilDB->fetchAssoc($set)) {
356 $this->setTitle((string) $rec["title"]);
357 $this->setContent((string) $rec["content"]);
358 $this->setContextObjId((int) $rec["context_obj_id"]);
359 $this->setContextObjType($rec["context_obj_type"]);
360 $this->setContextSubObjId((int) $rec["context_sub_obj_id"]);
361 $this->setContextSubObjType((string) $rec["context_sub_obj_type"]);
362 $this->setContentType((string) $rec["content_type"]);
363 $this->setCreationDate((string) $rec["creation_date"]);
364 $this->setUpdateDate((string) $rec["update_date"]);
365 $this->setUserId((int) $rec["user_id"]);
366 $this->setUpdateUserId((int) $rec["update_user_id"]);
367 $this->setVisibility((string) $rec["visibility"]);
368 $this->setContentLong((string) $rec["content_long"]);
369 $this->setPriority((int) $rec["priority"]);
370 $this->setContentIsLangVar((bool) $rec["content_is_lang_var"]);
371 $this->setContentTextIsLangVar((bool) $rec["content_text_is_lang_var"]);
372 $this->setMobId((int) $rec["mob_id"]);
373 $this->setPlaytime((string) $rec["playtime"]);
374 $this->setMobPlayCounter((int) $rec["mob_cnt_play"]);
375 $this->setMobDownloadCounter((int) $rec["mob_cnt_download"]);
376 $this->setContentHtml((bool) $rec["content_html"]);
377 }
378 }
379
384 public function create(): void
385 {
386 $ilDB = $this->db;
387
388 // insert new record into db
389 $this->setId($ilDB->nextId("il_news_item"));
390 $ilDB->insert("il_news_item", [
391 "id" => ["integer", $this->getId()],
392 "title" => ["text", $this->getTitle()],
393 "content" => ["clob", $this->getContent()],
394 "content_html" => ["integer", (int) $this->getContentHtml()],
395 "context_obj_id" => ["integer", $this->getContextObjId()],
396 "context_obj_type" => ["text", $this->getContextObjType()],
397 "context_sub_obj_id" => ["integer", $this->getContextSubObjId()],
398 "context_sub_obj_type" => ["text", $this->getContextSubObjType()],
399 "content_type" => ["text", $this->getContentType()],
400 "creation_date" => ["timestamp", ilUtil::now()],
401 "update_date" => ["timestamp", ilUtil::now()],
402 "user_id" => ["integer", $this->getUserId()],
403 "update_user_id" => ["integer", $this->getUpdateUserId()],
404 "visibility" => ["text", $this->getVisibility()],
405 "content_long" => ["clob", $this->getContentLong()],
406 "priority" => ["integer", $this->getPriority()],
407 "content_is_lang_var" => ["integer", $this->getContentIsLangVar()],
408 "content_text_is_lang_var" => ["integer", (int) $this->getContentTextIsLangVar()],
409 "mob_id" => ["integer", $this->getMobId()],
410 "playtime" => ["text", $this->getPlaytime()]
411 ]);
412
413
414 $news_set = new ilSetting("news");
415 $max_items = $news_set->get("max_items");
416 if ($max_items <= 0) {
417 $max_items = 50;
418 }
419
420 // limit number of news
421 if ($this->getLimitation()) {
422 // Determine how many rows should be deleted
423 $query = "SELECT count(*) cnt " .
424 "FROM il_news_item " .
425 "WHERE " .
426 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
427 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
428 " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
429 " AND " . $ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true) . " ";
430
431 $set = $ilDB->query($query);
432 $rec = $ilDB->fetchAssoc($set);
433
434 // if we have more records than allowed, delete them
435 if (($rec["cnt"] > $max_items) && $this->getContextObjId() > 0) {
436 $query = "SELECT * " .
437 "FROM il_news_item " .
438 "WHERE " .
439 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
440 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
441 " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
442 " AND " . $ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true) .
443 " ORDER BY creation_date ASC";
444
445 $ilDB->setLimit($rec["cnt"] - $max_items, 0);
446 $del_set = $ilDB->query($query);
447 while ($del_item = $ilDB->fetchAssoc($del_set)) {
448 $del_news = new ilNewsItem((int) $del_item["id"]);
449 $del_news->delete();
450 }
451 }
452 }
453 }
454
461 public function update(bool $a_as_new = false): void
462 {
463 $ilDB = $this->db;
464
465 $fields = [
466 "title" => ["text", $this->getTitle()],
467 "content" => ["clob", $this->getContent()],
468 "content_html" => ["integer", (int) $this->getContentHtml()],
469 "context_obj_id" => ["integer", $this->getContextObjId()],
470 "context_obj_type" => ["text", $this->getContextObjType()],
471 "context_sub_obj_id" => ["integer", $this->getContextSubObjId()],
472 "context_sub_obj_type" => ["text", $this->getContextSubObjType()],
473 "content_type" => ["text", $this->getContentType()],
474 "user_id" => ["integer", $this->getUserId()],
475 "update_user_id" => ["integer", $this->getUpdateUserId()],
476 "visibility" => ["text", $this->getVisibility()],
477 "content_long" => ["clob", $this->getContentLong()],
478 "priority" => ["integer", $this->getPriority()],
479 "content_is_lang_var" => ["integer", $this->getContentIsLangVar()],
480 "content_text_is_lang_var" => ["integer", (int) $this->getContentTextIsLangVar()],
481 "mob_id" => ["integer", $this->getMobId()],
482 "mob_cnt_play" => ["integer", $this->getMobPlayCounter()],
483 "mob_cnt_download" => ["integer", $this->getMobDownloadCounter()],
484 "playtime" => ["text", $this->getPlaytime()]
485 ];
486
487 $now = ilUtil::now();
488 if ($a_as_new) {
489 $fields["creation_date"] = ["timestamp", $now];
490 }
491 $fields["update_date"] = ["timestamp", $now];
492
493 $ilDB->update("il_news_item", $fields, [
494 "id" => ["integer", $this->getId()]
495 ]);
496 }
497
498
503 public static function _getNewsItemsOfUser(
504 int $a_user_id,
505 bool $a_only_public = false,
506 bool $a_prevent_aggregation = false,
507 int $a_per = 0,
508 array &$a_cnt = [],
509 bool $no_auto_generated = false,
510 array $excluded = [],
511 int $a_limit = 0
512 ): array {
513 global $DIC;
514
515 $ilAccess = $DIC->access();
516 $fav_rep = new ilFavouritesDBRepository();
517
518 $news_item = new ilNewsItem();
519
520 $per = $a_per;
521
522 // this is currently not used
523 $ref_ids = [];
524
525 if (ilObjUser::_lookupPref($a_user_id, "pd_items_news") !== "n") {
526 // get all items of the personal desktop
527 $pd_items = $fav_rep->getFavouritesOfUser($a_user_id);
528 foreach ($pd_items as $item) {
529 if (!in_array($item["ref_id"], $ref_ids)) {
530 $ref_ids[] = (int) $item["ref_id"];
531 }
532 }
533
534 // get all memberships
535 $crs_mbs = ilParticipants::_getMembershipByType($a_user_id, ['crs']);
536 $grp_mbs = ilParticipants::_getMembershipByType($a_user_id, ['grp']);
537 $items = array_merge($crs_mbs, $grp_mbs);
538 foreach ($items as $i) {
539 $item_references = ilObject::_getAllReferences($i);
540 if (is_array($item_references) && count($item_references)) {
541 foreach ($item_references as $ref_id) {
542 if (!in_array($ref_id, $ref_ids)) {
543 $ref_ids[] = $ref_id;
544 }
545 }
546 }
547 }
548 }
549
550 $data = [];
551
552 foreach ($ref_ids as $ref_id) {
553 if (!$a_only_public) {
554 // this loop should not cost too much performance
555 $acc = $ilAccess->checkAccessOfUser($a_user_id, "read", "", $ref_id);
556
557 if (!$acc) {
558 continue;
559 }
560 }
561 if (self::getPrivateFeedId() > 0) {
562 global $DIC;
563
564 $rbacsystem = $DIC->rbac()->system();
565 $acc = $rbacsystem->checkAccessOfUser(self::getPrivateFeedId(), "read", $ref_id);
566
567 if (!$acc) {
568 continue;
569 }
570 }
571
573 $obj_type = ilObject::_lookupType($obj_id);
574 $news = $news_item->getNewsForRefId(
575 $ref_id,
576 $a_only_public,
577 false,
578 $per,
579 $a_prevent_aggregation,
580 false,
581 $no_auto_generated,
582 false,
583 $a_user_id,
584 0,
585 $excluded
586 );
587
588 // counter
589 if (!is_null($a_cnt)) {
590 $a_cnt[$ref_id] = count($news);
591 }
592
593 $data = self::mergeNews($data, $news);
594 }
595
596 $data = ilArrayUtil::sortArray($data, "creation_date", "desc", false, true);
597
598 if ($a_limit > 0) {
599 array_splice($data, $a_limit);
600 }
601 return $data;
602 }
603
612 public function getNewsForRefId(
613 int $a_ref_id,
614 bool $a_only_public = false,
615 bool $a_stopnesting = false,
616 $a_time_period = 0,
617 bool $a_prevent_aggregation = true,
618 bool $a_forum_group_sequences = false,
619 bool $a_no_auto_generated = false,
620 bool $a_ignore_date_filter = false,
621 ?int $a_user_id = null,
622 int $a_limit = 0,
623 array $a_excluded = []
624 ): array {
625 $obj_id = ilObject::_lookupObjId($a_ref_id);
626 $obj_type = ilObject::_lookupType($obj_id);
627
628 // get starting date
629 $starting_date = "";
630 if ($obj_type === "grp" || $obj_type === "crs") {
631 // see #31471, #30687, and ilMembershipNotification
633 $obj_id,
634 'cont_use_news',
635 '1'
636 ) || (
638 $obj_id,
639 'cont_show_news',
640 '1'
642 $obj_id,
643 'news_timeline'
644 )
645 )) {
646 return [];
647 }
648
649 $hide_news_per_date = ilBlockSetting::_lookup(
650 "news",
651 "hide_news_per_date",
652 0,
653 $obj_id
654 );
655 if ($hide_news_per_date && !$a_ignore_date_filter) {
656 $starting_date = ilBlockSetting::_lookup(
657 "news",
658 "hide_news_date",
659 0,
660 $obj_id
661 );
662 }
663 }
664
665 if ($obj_type === "cat" && !$a_stopnesting) {
666 $news = $this->getAggregatedChildNewsData(
667 $a_ref_id,
668 $a_only_public,
669 $a_time_period,
670 $a_prevent_aggregation,
671 $starting_date,
672 $a_no_auto_generated,
673 $a_excluded
674 );
675 } elseif (($obj_type === "grp" || $obj_type === "crs") &&
676 !$a_stopnesting) {
677 $news = $this->getAggregatedNewsData(
678 $a_ref_id,
679 $a_only_public,
680 $a_time_period,
681 $a_prevent_aggregation,
682 $starting_date,
683 $a_no_auto_generated,
684 $a_user_id,
685 $a_limit,
686 $a_excluded
687 );
688 } else {
689 $news_item = new ilNewsItem();
690 $news_item->setContextObjId($obj_id);
691 $news_item->setContextObjType($obj_type);
692 $news = $news_item->queryNewsForContext(
693 $a_only_public,
694 $a_time_period,
695 $starting_date,
696 $a_no_auto_generated,
697 false,
698 0,
699 $a_excluded
700 );
701 $unset = [];
702 foreach ($news as $k => $v) {
703 if (!$a_only_public || $v["visibility"] == NEWS_PUBLIC ||
704 ($v["priority"] == 0 &&
706 "news",
707 "public_notifications",
708 0,
709 $obj_id
710 ))) {
711 $news[$k]["ref_id"] = $a_ref_id;
712 } else {
713 $unset[] = $k;
714 }
715 }
716 foreach ($unset as $un) {
717 unset($news[$un]);
718 }
719 }
720
721 if (!$a_prevent_aggregation) {
722 $news = $this->aggregateForums($news);
723 } elseif ($a_forum_group_sequences) {
724 $news = $this->aggregateForums($news, true);
725 }
726
727 return $news;
728 }
729
735 public function getAggregatedNewsData(
736 int $a_ref_id,
737 bool $a_only_public = false,
738 $a_time_period = 0,
739 bool $a_prevent_aggregation = false,
740 string $a_starting_date = "",
741 bool $a_no_auto_generated = false,
742 ?int $a_user_id = null,
743 int $a_limit = 0,
744 array $a_exclude = []
745 ): array {
746 $tree = $this->tree;
747 $ilAccess = $this->access;
748 $ilObjDataCache = $this->obj_data_cache;
749
750 // get news of parent object
751 $data = [];
752
753 // get subtree
754 $cur_node = $tree->getNodeData($a_ref_id);
755
756 // do not check for lft (materialized path)
757 if ($cur_node) {
758 $nodes = $tree->getSubTree($cur_node, true);
759 } else {
760 $nodes = [];
761 }
762
763 // preload object data cache
764 $ref_ids = [];
765 $obj_ids = [];
766 $ref_id = [];
767 foreach ($nodes as $node) {
768 $ref_ids[] = (int) $node["child"];
769 $obj_ids[] = (int) $node["obj_id"];
770 }
771
772 $ilObjDataCache->preloadReferenceCache($ref_ids);
773 if (!$a_only_public) {
775 }
776
777 // no check, for which of the objects any news are available
778 $news_obj_ids = self::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
779 //$news_obj_ids = $obj_ids;
780
781 // get news for all subtree nodes
782 $contexts = [];
783 foreach ($nodes as $node) {
784 // only go on, if news are available
785 if (!in_array($node["obj_id"], $news_obj_ids)) {
786 continue;
787 }
788
789 if (!$a_only_public) {
790 if (!$a_user_id) {
791 $acc = $ilAccess->checkAccess("read", "", (int) $node["child"]);
792 } else {
793 $acc = $ilAccess->checkAccessOfUser(
794 $a_user_id,
795 "read",
796 "",
797 (int) $node["child"]
798 );
799 }
800 if (!$acc) {
801 continue;
802 }
803 }
804
805 $ref_id[$node["obj_id"]] = $node["child"];
806 $contexts[] = [
807 "obj_id" => $node["obj_id"],
808 "obj_type" => $node["type"]
809 ];
810 }
811
812 // sort and return
813 $news = $this->queryNewsForMultipleContexts(
814 $contexts,
815 $a_only_public,
816 $a_time_period,
817 $a_starting_date,
818 $a_no_auto_generated,
819 $a_user_id,
820 $a_limit,
821 $a_exclude
822 );
823
824 $to_del = [];
825 foreach ($news as $k => $v) {
826 $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
827 }
828
829 $data = self::mergeNews($data, $news);
830 $data = ilArrayUtil::sortArray($data, "creation_date", "desc", false, true);
831
832 if (!$a_prevent_aggregation) {
833 $data = $this->aggregateFiles($data, $a_ref_id);
834 }
835
836 return $data;
837 }
838
842 protected function aggregateForums(
843 array $news,
844 bool $a_group_posting_sequence = false
845 ): array {
846 $to_del = [];
847 $forums = [];
848 $last_aggregation_forum = 0;
849
850 // aggregate
851 foreach ($news as $k => $v) {
852 if ($a_group_posting_sequence && $last_aggregation_forum > 0 &&
853 $last_aggregation_forum != $v["context_obj_id"]) {
854 $forums[$last_aggregation_forum] = null;
855 }
856
857 if ($v["context_obj_type"] === "frm") {
858 if (!isset($forums[$v["context_obj_id"]])) {
859 // $forums[forum_id] = news_id;
860 $forums[$v["context_obj_id"]] = $k;
861 $last_aggregation_forum = $v["context_obj_id"];
862 } else {
863 $to_del[] = $k;
864 }
865
866 $news[$k]["no_context_title"] = true;
867
868 // aggregate every forum into it's "k" news
869 $news[$forums[$news[$k]["context_obj_id"]]]["aggregation"][$k]
870 = $news[$k];
871 $news[$k]["agg_ref_id"]
872 = $news[$k]["ref_id"];
873 $news[$k]["content"] = "";
874 $news[$k]["content_long"] = "";
875 }
876 }
877
878 // delete double entries
879 foreach ($to_del as $k) {
880 unset($news[$k]);
881 }
882 //var_dump($news[14]["aggregation"]);
883
884 return $news;
885 }
886
890 protected function aggregateFiles(
891 array $news,
892 int $a_ref_id
893 ): array {
894 $first_file = "";
895 $to_del = [];
896 foreach ($news as $k => $v) {
897 // aggregate file related news
898 if ($v["context_obj_type"] === "file") {
899 if ($first_file === "") {
900 $first_file = $k;
901 } else {
902 $to_del[] = $k;
903 }
904 $news[$first_file]["aggregation"][$k] = $v;
905 $news[$first_file]["agg_ref_id"] = $a_ref_id;
906 $news[$first_file]["ref_id"] = $a_ref_id;
907 }
908 }
909
910 foreach ($to_del as $v) {
911 unset($news[$v]);
912 }
913
914 return $news;
915 }
916
917
922 protected function getAggregatedChildNewsData(
923 int $a_ref_id,
924 bool $a_only_public = false,
925 int $a_time_period = 0,
926 bool $a_prevent_aggregation = false,
927 string $a_starting_date = "",
928 bool $a_no_auto_generated = false,
929 array $a_excluded = []
930 ): array {
931 $tree = $this->tree;
932 $ilAccess = $this->access;
933 $ref_id = [];
934 // get news of parent object
935 $data = $this->getNewsForRefId(
936 $a_ref_id,
937 $a_only_public,
938 true,
939 $a_time_period,
940 true,
941 false,
942 $a_no_auto_generated,
943 false,
944 null,
945 0,
946 $a_excluded
947 );
948 foreach ($data as $k => $v) {
949 $data[$k]["ref_id"] = $a_ref_id;
950 }
951
952 // get childs
953 $nodes = $tree->getChilds($a_ref_id);
954
955 // no check, for which of the objects any news are available
956 $obj_ids = [];
957 foreach ($nodes as $node) {
958 $obj_ids[] = $node["obj_id"];
959 }
960 $news_obj_ids = self::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
961 //$news_obj_ids = $obj_ids;
962
963 // get news for all subtree nodes
964 $contexts = [];
965 foreach ($nodes as $node) {
966 // only go on, if news are available
967 if (!in_array($node["obj_id"], $news_obj_ids)) {
968 continue;
969 }
970
971 if (!$a_only_public && !$ilAccess->checkAccess("read", "", (int) $node["child"])) {
972 continue;
973 }
974 $ref_id[$node["obj_id"]] = $node["child"];
975 $contexts[] = [
976 "obj_id" => $node["obj_id"],
977 "obj_type" => $node["type"]
978 ];
979 }
980
981 $news = $this->queryNewsForMultipleContexts(
982 $contexts,
983 $a_only_public,
984 $a_time_period,
985 $a_starting_date,
986 $a_no_auto_generated,
987 null,
988 0,
989 $a_excluded
990 );
991 foreach ($news as $k => $v) {
992 $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
993 }
994 $data = self::mergeNews($data, $news);
995
996 // sort and return
997 $data = ilArrayUtil::sortArray($data, "creation_date", "desc", false, true);
998
999 if (!$a_prevent_aggregation) {
1000 $data = $this->aggregateFiles($data, $a_ref_id);
1001 }
1002
1003 return $data;
1004 }
1005
1009 public function setContext(
1010 int $a_obj_id,
1011 string $a_obj_type,
1012 int $a_sub_obj_id = 0,
1013 string $a_sub_obj_type = ""
1014 ): void {
1015 $this->setContextObjId($a_obj_id);
1016 $this->setContextObjType($a_obj_type);
1017 $this->setContextSubObjId($a_sub_obj_id);
1018 $this->setContextSubObjType($a_sub_obj_type);
1019 }
1020
1025 protected static function handleTimePeriod($a_time_period): string
1026 {
1027 // time period is number of days
1028 if (is_numeric($a_time_period)) {
1029 if ($a_time_period > 0) {
1030 return date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
1031 }
1032 }
1033 // time period is datetime
1034 elseif (preg_match("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/", $a_time_period)) {
1035 return $a_time_period;
1036 }
1037 return "";
1038 }
1039
1045 public function queryNewsForContext(
1046 bool $a_for_rss_use = false,
1047 $a_time_period = 0,
1048 string $a_starting_date = "",
1049 bool $a_no_auto_generated = false,
1050 bool $a_oldest_first = false,
1051 int $a_limit = 0,
1052 array $a_exclude = []
1053 ): array {
1054 $ilDB = $this->db;
1055 $ilUser = $this->user;
1056
1057 $and = "";
1058 if ($a_time_period > 0) {
1059 $limit_ts = self::handleTimePeriod($a_time_period);
1060 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
1061 }
1062
1063 if ($a_starting_date !== "") {
1064 $and .= " AND creation_date > " . $ilDB->quote($a_starting_date, "timestamp") . " ";
1065 }
1066
1067 if ($a_no_auto_generated) {
1068 $and .= " AND priority = 1 AND content_type = " . $ilDB->quote("text", "text") . " ";
1069 }
1070
1071 // this is changed with 4.1 (news table for lm pages)
1072 if ($this->getContextSubObjId() > 0) {
1073 $and .= " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
1074 " AND context_sub_obj_type = " . $ilDB->quote($this->getContextSubObjType(), "text");
1075 }
1076
1077 if (count($a_exclude) > 0) {
1078 $and .= " AND " . $ilDB->in("id", $a_exclude, true, "integer") . " ";
1079 }
1080
1081 $ordering = ($a_oldest_first)
1082 ? " creation_date ASC, id ASC "
1083 : " creation_date DESC, id DESC ";
1084
1085 if ($a_for_rss_use && self::getPrivateFeedId() === 0) {
1086 $query = "SELECT * " .
1087 "FROM il_news_item " .
1088 " WHERE " .
1089 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
1090 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
1091 $and .
1092 " ORDER BY " . $ordering;
1093 } elseif (self::getPrivateFeedId() > 0) {
1094 $query = "SELECT il_news_item.* " .
1095 ", il_news_read.user_id user_read " .
1096 "FROM il_news_item LEFT JOIN il_news_read " .
1097 "ON il_news_item.id = il_news_read.news_id AND " .
1098 " il_news_read.user_id = " . $ilDB->quote(self::getPrivateFeedId(), "integer") .
1099 " WHERE " .
1100 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
1101 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
1102 $and .
1103 " ORDER BY " . $ordering;
1104 } else {
1105 $query = "SELECT il_news_item.* " .
1106 ", il_news_read.user_id as user_read " .
1107 "FROM il_news_item LEFT JOIN il_news_read " .
1108 "ON il_news_item.id = il_news_read.news_id AND " .
1109 " il_news_read.user_id = " . $ilDB->quote($ilUser->getId(), "integer") .
1110 " WHERE " .
1111 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
1112 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
1113 $and .
1114 " ORDER BY " . $ordering;
1115 }
1116 //echo $query;
1117 $set = $ilDB->query($query);
1118 $result = [];
1119 while ($rec = $ilDB->fetchAssoc($set)) {
1120 if ($a_limit > 0 && count($result) >= $a_limit) {
1121 continue;
1122 }
1123 if (!$a_for_rss_use || (self::getPrivateFeedId() > 0) || ($rec["visibility"] === NEWS_PUBLIC ||
1124 ((int) $rec["priority"] === 0 &&
1126 "news",
1127 "public_notifications",
1128 0,
1129 (int) $rec["context_obj_id"]
1130 )))) {
1131 $result[$rec["id"]] = $rec;
1132 }
1133 }
1134
1135 // do we get data for rss and may the time limit by an issue?
1136 // do a second query without time limit.
1137 // this is not very performant, but I do not have a better
1138 // idea. The keep_rss_min setting is currently (Jul 2012) only set
1139 // by mediacasts
1140 if ($a_time_period && $a_for_rss_use) {
1141 $keep_rss_min = ilBlockSetting::_lookup(
1142 "news",
1143 "keep_rss_min",
1144 0,
1145 $this->getContextObjId()
1146 );
1147 if ($keep_rss_min > 0) {
1148 return $this->queryNewsForContext(
1149 true,
1150 0,
1151 $a_starting_date,
1152 $a_no_auto_generated,
1153 $a_oldest_first,
1154 (int) $keep_rss_min,
1155 $a_exclude
1156 );
1157 }
1158 }
1159
1160 return $result;
1161 }
1162
1168 public static function queryNewsByIds(array $a_news_ids): array
1169 {
1170 global $DIC;
1171 $ilDB = $DIC->database();
1172 $news = [];
1173 $set = $ilDB->query("SELECT * FROM il_news_item " .
1174 " WHERE " . $ilDB->in("id", $a_news_ids, false, "integer"));
1175 while ($rec = $ilDB->fetchAssoc($set)) {
1176 $news[$rec["id"]] = $rec;
1177 }
1178 return $news;
1179 }
1180
1187 array $objects,
1188 $a_time_period = 1
1189 ): array {
1190 $ilDB = $this->db;
1191
1192 $all = [];
1193
1194 $this->log->debug("time period: " . $a_time_period);
1195 $limit_ts = self::handleTimePeriod($a_time_period);
1196
1197 // are there any news items for relevant objects and?
1198 $query = $ilDB->query($q = "SELECT id,context_obj_id,context_obj_type" .
1199 " FROM il_news_item" .
1200 " WHERE " . $ilDB->in("context_obj_id", array_keys($objects), false, "integer") .
1201 " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp"));
1202 $this->log->debug($q);
1203 while ($rec = $ilDB->fetchAssoc($query)) {
1204 if ($objects[$rec["context_obj_id"]]["type"] == $rec["context_obj_type"]) {
1205 $all[] = (int) $rec["id"];
1206 }
1207 }
1208
1209 return $all;
1210 }
1211
1217 array $a_contexts,
1218 bool $a_for_rss_use = false,
1219 $a_time_period = 0,
1220 string $a_starting_date = "",
1221 bool $a_no_auto_generated = false,
1222 ?int $a_user_id = null,
1223 int $a_limit = 0,
1224 array $a_exclude = []
1225 ): array {
1226 $ilDB = $this->db;
1227 $ilUser = $this->user;
1228
1229 $and = "";
1230 if ($a_time_period > 0) {
1231 $limit_ts = self::handleTimePeriod($a_time_period);
1232 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
1233 }
1234
1235 if ($a_starting_date !== "") {
1236 $and .= " AND creation_date > " . $ilDB->quote($a_starting_date, "timestamp") . " ";
1237 }
1238
1239 if ($a_no_auto_generated) {
1240 $and .= " AND priority = 1 AND content_type = " . $ilDB->quote("text", "text") . " ";
1241 }
1242
1243 if ($a_limit > 0) {
1244 $ilDB->setLimit($a_limit, 0);
1245 }
1246
1247 if (is_array($a_exclude) && count($a_exclude) > 0) {
1248 $and .= " AND " . $ilDB->in("id", $a_exclude, true, "integer") . " ";
1249 }
1250
1251 $ids = [];
1252 $type = [];
1253
1254 foreach ($a_contexts as $cont) {
1255 $ids[] = $cont["obj_id"];
1256 $type[$cont["obj_id"]] = $cont["obj_type"];
1257 }
1258
1259 if ($a_for_rss_use && self::getPrivateFeedId() === 0) {
1260 $query = "SELECT * " .
1261 "FROM il_news_item " .
1262 " WHERE " .
1263 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1264 $and .
1265 " ORDER BY creation_date DESC ";
1266 } elseif (self::getPrivateFeedId() > 0) {
1267 $query = "SELECT il_news_item.* " .
1268 ", il_news_read.user_id as user_read " .
1269 "FROM il_news_item LEFT JOIN il_news_read " .
1270 "ON il_news_item.id = il_news_read.news_id AND " .
1271 " il_news_read.user_id = " . $ilDB->quote(self::getPrivateFeedId(), "integer") .
1272 " WHERE " .
1273 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1274 $and .
1275 " ORDER BY creation_date DESC ";
1276 } else {
1277 if ($a_user_id) {
1278 $user_id = $a_user_id;
1279 } else {
1280 $user_id = $ilUser->getId();
1281 }
1282 $query = "SELECT il_news_item.* " .
1283 ", il_news_read.user_id as user_read " .
1284 "FROM il_news_item LEFT JOIN il_news_read " .
1285 "ON il_news_item.id = il_news_read.news_id AND " .
1286 " il_news_read.user_id = " . $ilDB->quote($user_id, "integer") .
1287 " WHERE " .
1288 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1289 $and .
1290 " ORDER BY creation_date DESC ";
1291 }
1292
1293 $set = $ilDB->query($query);
1294 $result = [];
1295 while ($rec = $ilDB->fetchAssoc($set)) {
1296 if ($type[$rec["context_obj_id"]] == $rec["context_obj_type"]) {
1297 if (!$a_for_rss_use || self::getPrivateFeedId() > 0 || ($rec["visibility"] === NEWS_PUBLIC ||
1298 ((int) $rec["priority"] === 0 &&
1300 "news",
1301 "public_notifications",
1302 0,
1303 (int) $rec["context_obj_id"]
1304 )))) {
1305 $result[$rec["id"]] = $rec;
1306 }
1307 }
1308 }
1309
1310 return $result;
1311 }
1312
1313
1318 public static function _setRead(
1319 int $a_user_id,
1320 int $a_news_id
1321 ): void {
1322 global $DIC;
1323
1324 $ilDB = $DIC->database();
1325 $ilAppEventHandler = $DIC["ilAppEventHandler"];
1326
1327 $ilDB->replace(
1328 "il_news_read",
1329 [
1330 "user_id" => ["integer", $a_user_id],
1331 "news_id" => ["integer", $a_news_id]
1332 ],
1333 []
1334 );
1335
1336 $ilAppEventHandler->raise(
1337 "components/ILIAS/News",
1338 "readNews",
1339 ["user_id" => $a_user_id, "news_ids" => [$a_news_id]]
1340 );
1341 }
1342
1347 public static function _setUnread(
1348 int $a_user_id,
1349 int $a_news_id
1350 ): void {
1351 global $DIC;
1352
1353 $ilDB = $DIC->database();
1354 $ilAppEventHandler = $DIC["ilAppEventHandler"];
1355
1356 $ilDB->manipulate("DELETE FROM il_news_read (user_id, news_id) VALUES (" .
1357 " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
1358 " AND news_id = " . $ilDB->quote($a_news_id, "integer"));
1359
1360 $ilAppEventHandler->raise(
1361 "components/ILIAS/News",
1362 "unreadNews",
1363 ["user_id" => $a_user_id, "news_ids" => [$a_news_id]]
1364 );
1365 }
1366
1371 public static function mergeNews(
1372 array $n1,
1373 array $n2
1374 ): array {
1375 foreach ($n2 as $id => $news) {
1376 $n1[$id] = $news;
1377 }
1378
1379 return $n1;
1380 }
1381
1386 public static function _getDefaultVisibilityForRefId(int $a_ref_id): string
1387 {
1388 global $DIC;
1389
1390 $tree = $DIC->repositoryTree();
1391
1392 $news_set = new ilSetting("news");
1393 $default_visibility = ($news_set->get("default_visibility") != "")
1394 ? $news_set->get("default_visibility")
1395 : "users";
1396
1397 if ($tree->isInTree($a_ref_id)) {
1398 $path = $tree->getPathFull($a_ref_id);
1399
1400 foreach ($path as $key => $row) {
1401 if (!in_array($row["type"], ["root", "cat", "crs", "fold", "grp"], true)) {
1402 continue;
1403 }
1404
1405 $visibility = ilBlockSetting::_lookup(
1406 "news",
1407 "default_visibility",
1408 0,
1409 (int) $row["obj_id"]
1410 );
1411
1412 if ($visibility != "") {
1413 $default_visibility = $visibility;
1414 }
1415 }
1416 }
1417
1418 return $default_visibility;
1419 }
1420
1421
1426 public function delete(): void
1427 {
1428 $ilDB = $this->db;
1429
1430 // delete il_news_read entries
1431 $ilDB->manipulate("DELETE FROM il_news_read " .
1432 " WHERE news_id = " . $ilDB->quote($this->getId(), "integer"));
1433
1434 // delete multimedia object
1435 $mob = $this->getMobId();
1436
1437 // delete
1438 $query = "DELETE FROM il_news_item" .
1439 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
1440 $ilDB->manipulate($query);
1441
1442 // delete mob after news, to have a "mob usage" of 0
1443 if ($mob > 0 && ilObject::_exists($mob)) {
1444 $mob = new ilObjMediaObject($mob);
1445 $mob->delete();
1446 }
1447 }
1448
1454 public static function getNewsOfContext(
1455 int $a_context_obj_id,
1456 string $a_context_obj_type,
1457 int $a_context_sub_obj_id = 0,
1458 string $a_context_sub_obj_type = ""
1459 ): array {
1460 global $DIC;
1461
1462 $ilDB = $DIC->database();
1463 $and = "";
1464
1465 if ($a_context_obj_id === 0 || $a_context_obj_type === "") {
1466 return [];
1467 }
1468
1469 if ($a_context_sub_obj_id > 0) {
1470 $and = " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1471 " AND context_sub_obj_type = " . $ilDB->quote($a_context_sub_obj_type, "text");
1472 }
1473
1474 // get news records
1475 $query = "SELECT id FROM il_news_item" .
1476 " WHERE context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1477 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1478 $and;
1479
1480 $news_set = $ilDB->query($query);
1481
1482 $news_arr = [];
1483 while ($news = $ilDB->fetchAssoc($news_set)) {
1484 $news_arr[] = new ilNewsItem((int) $news["id"]);
1485 }
1486 return $news_arr;
1487 }
1488
1493 public static function deleteNewsOfContext(
1494 int $a_context_obj_id,
1495 string $a_context_obj_type,
1496 int $a_context_sub_obj_id = 0,
1497 string $a_context_sub_obj_type = ""
1498 ): void {
1499 foreach (self::getNewsOfContext(
1500 $a_context_obj_id,
1501 $a_context_obj_type,
1502 $a_context_sub_obj_id,
1503 $a_context_sub_obj_type
1504 ) as $n) {
1505 $n->delete();
1506 }
1507 }
1508
1513 public static function _lookupTitle(int $a_news_id): string
1514 {
1515 global $DIC;
1516
1517 $ilDB = $DIC->database();
1518
1519 $query = "SELECT title FROM il_news_item WHERE id = " .
1520 $ilDB->quote($a_news_id, "integer");
1521 $set = $ilDB->query($query);
1522 $rec = $ilDB->fetchAssoc($set);
1523 return $rec["title"] ?? '';
1524 }
1525
1530 public static function _lookupVisibility(int $a_news_id): string
1531 {
1532 global $DIC;
1533
1534 $ilDB = $DIC->database();
1535
1536 $query = "SELECT visibility FROM il_news_item WHERE id = " .
1537 $ilDB->quote($a_news_id, "integer");
1538 $set = $ilDB->query($query);
1539 $rec = $ilDB->fetchAssoc($set);
1540
1541 return $rec["visibility"] ?? NEWS_USERS;
1542 }
1543
1548 public static function _lookupMobId(int $a_news_id): int
1549 {
1550 global $DIC;
1551
1552 $ilDB = $DIC->database();
1553
1554 $query = "SELECT mob_id FROM il_news_item WHERE id = " .
1555 $ilDB->quote($a_news_id, "integer");
1556 $set = $ilDB->query($query);
1557 $rec = $ilDB->fetchAssoc($set);
1558 return (int) ($rec["mob_id"] ?? 0);
1559 }
1560
1567 public static function filterObjIdsPerNews(
1568 array $a_obj_ids,
1569 $a_time_period = 0,
1570 string $a_starting_date = "",
1571 string $a_ending_date = '',
1572 bool $ignore_period = false
1573 ): array {
1574 global $DIC;
1575
1576 $ilDB = $DIC->database();
1577
1578 $and = "";
1579 if ($a_time_period > 0) {
1580 $limit_ts = self::handleTimePeriod($a_time_period);
1581 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
1582 }
1583
1584 if ($a_starting_date !== "") {
1585 $and .= " AND creation_date >= " . $ilDB->quote($a_starting_date, "timestamp");
1586 }
1587
1588 $query = "SELECT DISTINCT(context_obj_id) AS obj_id FROM il_news_item" .
1589 " WHERE " . $ilDB->in("context_obj_id", $a_obj_ids, false, "integer") . " " . $and;
1590 //" WHERE context_obj_id IN (".implode(ilUtil::quoteArray($a_obj_ids),",").")".$and;
1591
1592 $set = $ilDB->query($query);
1593 $objs = [];
1594 while ($rec = $ilDB->fetchAssoc($set)) {
1595 $objs[] = $rec["obj_id"];
1596 }
1597
1598 return $objs;
1599 }
1600
1604 public static function determineNewsTitleByNewsId(
1605 int $a_news_id,
1606 int $a_agg_ref_id = 0,
1607 array $a_aggregation = []
1608 ): string {
1609 global $DIC;
1610
1611 $ilDB = $DIC->database();
1612
1613 $query = "SELECT context_obj_type, content_is_lang_var, title FROM il_news_item WHERE id = " .
1614 $ilDB->quote($a_news_id, "integer");
1615 $set = $ilDB->query($query);
1616 $rec = $ilDB->fetchAssoc($set);
1617
1618 return self::determineNewsTitle(
1619 $rec["context_obj_type"],
1620 $rec["title"],
1621 $rec["content_is_lang_var"],
1622 $a_agg_ref_id,
1623 $a_aggregation
1624 );
1625 }
1626
1631 public static function determineNewsTitle(
1632 string $a_context_obj_type,
1633 string $a_title,
1634 bool $a_content_is_lang_var,
1635 int $a_agg_ref_id = 0,
1636 array $a_aggregation = [],
1637 ?ilLanguage $lng = null
1638 ): string {
1639 global $DIC;
1640
1641 if (is_null($lng)) {
1642 $lng = $DIC->language();
1643 }
1644 $obj_definition = $DIC["objDefinition"];
1645 $tit = "";
1646
1647 if ($a_agg_ref_id > 0) {
1648 $cnt = count($a_aggregation);
1649
1650 // forums
1651 if ($a_context_obj_type === "frm") {
1652 if ($cnt > 1) {
1653 return sprintf($lng->txt("news_x_postings"), $cnt);
1654 }
1655
1656 return $lng->txt("news_1_postings");
1657 }
1658
1659 // files
1660 $up_cnt = $cr_cnt = 0;
1661 foreach ($a_aggregation as $item) {
1662 if ($item["title"] === "file_updated") {
1663 $up_cnt++;
1664 } else {
1665 $cr_cnt++;
1666 }
1667 }
1668 $sep = "";
1669 if ($cr_cnt === 1) {
1670 $tit = $lng->txt("news_1_file_created");
1671 $sep = "<br />";
1672 } elseif ($cr_cnt > 1) {
1673 $tit = sprintf($lng->txt("news_x_files_created"), $cr_cnt);
1674 $sep = "<br />";
1675 }
1676 if ($up_cnt === 1) {
1677 $tit .= $sep . $lng->txt("news_1_file_updated");
1678 } elseif ($up_cnt > 1) {
1679 $tit .= $sep . sprintf($lng->txt("news_x_files_updated"), $up_cnt);
1680 }
1681 return $tit;
1682 }
1683
1684 if ($a_content_is_lang_var) {
1685 if ($obj_definition->isPlugin($a_context_obj_type)) {
1686 return ilObjectPlugin::lookupTxtById($a_context_obj_type, $a_title);
1687 }
1688 return $lng->txt($a_title);
1689 }
1690
1691 return $a_title;
1692 }
1693
1698 public static function determineNewsContent(
1699 string $a_context_obj_type,
1700 string $a_content,
1701 bool $a_is_lang_var,
1702 ?ilLanguage $lng = null
1703 ): string {
1704 global $DIC;
1705
1706 if (is_null($lng)) {
1707 $lng = $DIC->language();
1708 }
1709 $obj_definition = $DIC["objDefinition"];
1710
1711 if ($a_is_lang_var) {
1712 if ($obj_definition->isPlugin($a_context_obj_type)) {
1713 return ilObjectPlugin::lookupTxtById($a_context_obj_type, $a_content);
1714 }
1715 $lng->loadLanguageModule($a_context_obj_type);
1716 return $lng->txt($a_content);
1717 }
1718
1719 return $a_content;
1720 }
1721
1726 public static function getFirstNewsIdForContext(
1727 int $a_context_obj_id,
1728 string $a_context_obj_type,
1729 int $a_context_sub_obj_id = 0,
1730 string $a_context_sub_obj_type = ""
1731 ): int {
1732 global $DIC;
1733
1734 $ilDB = $DIC->database();
1735
1736 // Determine how many rows should be deleted
1737 $query = "SELECT id " .
1738 "FROM il_news_item " .
1739 "WHERE " .
1740 "context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1741 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1742 " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1743 " AND " . $ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true);
1744
1745 $set = $ilDB->query($query);
1746 $rec = $ilDB->fetchAssoc($set);
1747
1748 return (int) ($rec["id"] ?? 0);
1749 }
1750
1755 public static function getLastNewsIdForContext(
1756 int $a_context_obj_id,
1757 string $a_context_obj_type,
1758 int $a_context_sub_obj_id = 0,
1759 string $a_context_sub_obj_type = "",
1760 bool $a_only_today = false
1761 ): int {
1762 global $DIC;
1763
1764 $ilDB = $DIC->database();
1765
1766 // Determine how many rows should be deleted
1767 $query = "SELECT id, update_date " .
1768 "FROM il_news_item " .
1769 "WHERE " .
1770 "context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1771 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1772 " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1773 " AND " . $ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true) .
1774 " ORDER BY update_date DESC";
1775
1776 $ilDB->setLimit(1, 0);
1777 $set = $ilDB->query($query);
1778 $id = 0;
1779 if ($rec = $ilDB->fetchAssoc($set)) {
1780 $id = (int) $rec["id"];
1781 if ($a_only_today) {
1782 $now = ilUtil::now();
1783 if (strpos($rec["update_date"], substr($now, 0, 10)) !== 0) {
1784 $id = 0;
1785 }
1786 }
1787 }
1788
1789 return $id;
1790 }
1791
1792
1797 public static function _lookupMediaObjectUsages(int $a_mob_id): array
1798 {
1799 global $DIC;
1800
1801 $ilDB = $DIC->database();
1802
1803 $query = "SELECT id " .
1804 "FROM il_news_item " .
1805 "WHERE " .
1806 " mob_id = " . $ilDB->quote($a_mob_id, "integer");
1807
1808 $usages = [];
1809 $set = $ilDB->query($query);
1810 while ($rec = $ilDB->fetchAssoc($set)) {
1811 $usages[$rec["id"]] = ["type" => "news", "id" => $rec["id"]];
1812 }
1813
1814 return $usages;
1815 }
1816
1821 public static function _lookupContextObjId(int $a_news_id): int
1822 {
1823 global $DIC;
1824
1825 $ilDB = $DIC->database();
1826
1827 $query = "SELECT context_obj_id " .
1828 "FROM il_news_item " .
1829 "WHERE " .
1830 " id = " . $ilDB->quote($a_news_id, "integer");
1831 $set = $ilDB->query($query);
1832 $rec = $ilDB->fetchAssoc($set);
1833
1834 return $rec["context_obj_id"];
1835 }
1836
1840 public static function _lookupDefaultPDPeriod(): int
1841 {
1842 $news_set = new ilSetting("news");
1843 $per = $news_set->get("pd_period");
1844 if ((int) $per === 0) {
1845 $per = 30;
1846 }
1847
1848 return $per;
1849 }
1850
1854 public static function _lookupUserPDPeriod(int $a_user_id): int
1855 {
1856 $news_set = new ilSetting("news");
1857 $allow_shorter_periods = $news_set->get("allow_shorter_periods");
1858 $allow_longer_periods = $news_set->get("allow_longer_periods");
1859 $default_per = self::_lookupDefaultPDPeriod();
1860
1862 "pdnews",
1863 "news_pd_period",
1864 $a_user_id,
1865 0
1866 );
1867
1868 // news period information
1869 if ($per <= 0 ||
1870 (!$allow_shorter_periods && ($per < $default_per)) ||
1871 (!$allow_longer_periods && ($per > $default_per))
1872 ) {
1873 $per = $default_per;
1874 }
1875
1876 return (int) $per;
1877 }
1878
1882 public static function _lookupRSSPeriod(): int
1883 {
1884 $news_set = new ilSetting("news");
1885 $rss_period = $news_set->get("rss_period");
1886 if ((int) $rss_period === 0) { // default to two weeks
1887 $rss_period = 14;
1888 }
1889 return $rss_period;
1890 }
1891
1895 public static function setPrivateFeedId(int $a_userId): void
1896 {
1897 self::$privFeedId = $a_userId;
1898 }
1899
1903 public static function getPrivateFeedId(): int
1904 {
1905 return self::$privFeedId;
1906 }
1907
1912 public function deliverMobFile(
1913 string $a_purpose = "Standard",
1914 bool $a_increase_download_cnt = false
1915 ): bool {
1916 $mob = $this->getMobId();
1917 $mob = new ilObjMediaObject($mob);
1918 $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
1919
1920 // check purpose
1921 if (!$mob->hasPurposeItem($a_purpose)) {
1922 return false;
1923 }
1924
1925 $m_item = $mob->getMediaItem($a_purpose);
1926 if ($m_item->getLocationType() !== "Reference") {
1927 $this->media_manager->deliverEntry($mob->getId(), "/" . $m_item->getLocation());
1928 if ($a_increase_download_cnt) {
1929 $this->increaseDownloadCounter();
1930 }
1931 $this->main_tpl->setOnScreenMessage('failure', "File not found!", true);
1932 return false;
1933 }
1934
1935 if ($a_increase_download_cnt) {
1936 $this->increaseDownloadCounter();
1937 }
1938
1939 ilUtil::redirect($m_item->getLocation());
1940 return true;
1941 }
1942
1947 public function increaseDownloadCounter(): void
1948 {
1949 $ilDB = $this->db;
1950
1951 $cnt = $this->getMobDownloadCounter();
1952 $cnt++;
1953 $this->setMobDownloadCounter($cnt);
1954 $ilDB->manipulate(
1955 "UPDATE il_news_item SET " .
1956 " mob_cnt_download = " . $ilDB->quote($cnt, "integer") .
1957 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
1958 );
1959 }
1960
1966 public function increasePlayCounter(): void
1967 {
1968 $ilDB = $this->db;
1969
1970 $cnt = $this->getMobPlayCounter();
1971 $cnt++;
1972 $this->setMobPlayCounter($cnt);
1973 $ilDB->manipulate(
1974 "UPDATE il_news_item SET " .
1975 " mob_cnt_play = " . $ilDB->quote($cnt, "integer") .
1976 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
1977 );
1978 }
1979
1984 public static function prepareNewsDataFromCache(array $a_cres): array
1985 {
1986 global $DIC;
1987
1988 $ilDB = $DIC->database();
1989
1990 $data = $a_cres;
1991 $news_ids = array_keys($data);
1992 $set = $ilDB->query("SELECT id FROM il_news_item " .
1993 " WHERE " . $ilDB->in("id", $news_ids, false, "integer"));
1994 $existing_ids = [];
1995 while ($rec = $ilDB->fetchAssoc($set)) {
1996 $existing_ids[] = (int) $rec["id"];
1997 }
1998 //var_dump($existing_ids);
1999 $existing_news = [];
2000 foreach ($data as $k => $v) {
2001 if (in_array($k, $existing_ids)) {
2002 $existing_news[$k] = $v;
2003 }
2004 }
2005
2006 return $existing_news;
2007 }
2008}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const NEWS_AUDIO
const NEWS_TEXT
const NEWS_WARNING
const NEWS_USERS
const NEWS_HTML
const NEWS_MESSAGE
const NEWS_NOTICE
const NEWS_PUBLIC
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static _lookup(string $a_type, string $a_setting, int $a_user=0, int $a_block_id=0)
Lookup setting from database.
static _lookupContainerSetting(int $a_id, string $a_keyword, ?string $a_default_value=null)
Class ilCtrl provides processing control methods.
language handling
Component logger with individual log levels by component id.
A news item can be created by different sources.
static _setUnread(int $a_user_id, int $a_news_id)
Set item unread.
ilDBInterface $db
static _lookupUserPDPeriod(int $a_user_id)
getAggregatedChildNewsData(int $a_ref_id, bool $a_only_public=false, int $a_time_period=0, bool $a_prevent_aggregation=false, string $a_starting_date="", bool $a_no_auto_generated=false, array $a_excluded=[])
Get news aggregation for child objects (e.g.
setContentType(string $a_content_type="text")
static getFirstNewsIdForContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Get first new id of news set related to a certain context.
static int $privFeedId
string $context_obj_type
static getPrivateFeedId()
static _lookupRSSPeriod()
setId(int $a_id)
static _getNewsItemsOfUser(int $a_user_id, bool $a_only_public=false, bool $a_prevent_aggregation=false, int $a_per=0, array &$a_cnt=[], bool $no_auto_generated=false, array $excluded=[], int $a_limit=0)
Get all news items for a user.
getNewsForRefId(int $a_ref_id, bool $a_only_public=false, bool $a_stopnesting=false, $a_time_period=0, bool $a_prevent_aggregation=true, bool $a_forum_group_sequences=false, bool $a_no_auto_generated=false, bool $a_ignore_date_filter=false, ?int $a_user_id=null, int $a_limit=0, array $a_excluded=[])
Get News For Ref Id.
update(bool $a_as_new=false)
Update item in database.
setPriority(int $a_priority=1)
read()
Read item from database.
static handleTimePeriod($a_time_period)
Convert time period for DB-queries.
static getNewsOfContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Get all news of a context.
setUserId(int $a_user_id)
static mergeNews(array $n1, array $n2)
Merges two sets of news.
ilLanguage $lng
static _lookupMobId(int $a_news_id)
Lookup mob id.
setMobDownloadCounter(int $a_val)
string $update_date
setContextObjType(string $a_context_obj_type)
static determineNewsTitle(string $a_context_obj_type, string $a_title, bool $a_content_is_lang_var, int $a_agg_ref_id=0, array $a_aggregation=[], ?ilLanguage $lng=null)
Determine title for news item entry.
queryNewsForMultipleContexts(array $a_contexts, bool $a_for_rss_use=false, $a_time_period=0, string $a_starting_date="", bool $a_no_auto_generated=false, ?int $a_user_id=null, int $a_limit=0, array $a_exclude=[])
getAggregatedNewsData(int $a_ref_id, bool $a_only_public=false, $a_time_period=0, bool $a_prevent_aggregation=false, string $a_starting_date="", bool $a_no_auto_generated=false, ?int $a_user_id=null, int $a_limit=0, array $a_exclude=[])
Get news aggregation (e.g.
setContextSubObjId(int $a_context_sub_obj_id)
static prepareNewsDataFromCache(array $a_cres)
Prepare news data from cache.
setContentIsLangVar(bool $a_content_is_lang_var=false)
static filterObjIdsPerNews(array $a_obj_ids, $a_time_period=0, string $a_starting_date="", string $a_ending_date='', bool $ignore_period=false)
Checks whether news are available for.
__construct(int $a_id=0)
setContent(string $a_content)
setContentTextIsLangVar(bool $a_val=false)
increasePlayCounter()
Increase play counter.
static setPrivateFeedId(int $a_userId)
increaseDownloadCounter()
Increase download counter.
string $context_sub_obj_type
bool $content_is_lang_var
static deleteNewsOfContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="")
Delete all news of a context.
static _setRead(int $a_user_id, int $a_news_id)
Set item read.
aggregateForums(array $news, bool $a_group_posting_sequence=false)
aggregateFiles(array $news, int $a_ref_id)
setMobId(int $a_mob_id)
setContext(int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="")
Set context for news.
MediaObjectManager $media_manager
static _lookupMediaObjectUsages(int $a_mob_id)
Lookup media object usage(s)
string $creation_date
static _lookupVisibility(int $a_news_id)
Lookup News Visibility.
setContentHtml(bool $a_val)
static _lookupContextObjId(int $a_news_id)
Context Object ID.
ilObjectDataCache $obj_data_cache
setPlaytime(string $a_playtime)
queryNewsForContext(bool $a_for_rss_use=false, $a_time_period=0, string $a_starting_date="", bool $a_no_auto_generated=false, bool $a_oldest_first=false, int $a_limit=0, array $a_exclude=[])
Query news for a context.
ilObjUser $user
static _getDefaultVisibilityForRefId(int $a_ref_id)
Get default visibility for reference id.
static _lookupTitle(int $a_news_id)
Lookup News Title.
setMobPlayCounter(int $a_val)
static queryNewsByIds(array $a_news_ids)
Query news data by news ids.
static _lookupDefaultPDPeriod()
static determineNewsTitleByNewsId(int $a_news_id, int $a_agg_ref_id=0, array $a_aggregation=[])
Determine title for news item entry.
string $content_long
bool $content_text_is_lang_var
deliverMobFile(string $a_purpose="Standard", bool $a_increase_download_cnt=false)
Deliver mob file.
setLimitation(bool $a_limitation)
Set Limitation for number of items.
setVisibility(string $a_visibility="users")
string $content_type
checkNewsExistsForObjects(array $objects, $a_time_period=1)
setUpdateUserId(int $a_val)
ilAccessHandler $access
setContextSubObjType(?string $a_context_sub_obj_type)
setContextObjId(int $a_context_obj_id)
setTitle(string $a_title)
static getLastNewsIdForContext(int $a_context_obj_id, string $a_context_obj_type, int $a_context_sub_obj_id=0, string $a_context_sub_obj_type="", bool $a_only_today=false)
Get last news id of news set related to a certain context.
string $visibility
setContentLong(string $a_content_long)
setCreationDate(string $a_creation_date)
setUpdateDate(string $a_update_date)
static determineNewsContent(string $a_context_obj_type, string $a_content, bool $a_is_lang_var, ?ilLanguage $lng=null)
Determine new content.
ilGlobalTemplateInterface $main_tpl
static _getDirectory(int $a_mob_id)
Get absolute directory.
User class.
static _lookupPref(int $a_usr_id, string $a_keyword)
static preloadData(array $ref_ids)
Preload data to internal cache.
class ilObjectDataCache
static lookupTxtById(string $plugin_id, string $lang_var)
Class ilObject Basic functions for all objects.
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _lookupObjId(int $ref_id)
static _getMembershipByType(int $a_usr_id, array $a_type, bool $a_only_member_role=false)
get membership by type Get course or group membership
ILIAS Setting Class.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
isInTree(?int $a_node_id)
get all information of a node.
getPathFull(int $a_endnode_id, int $a_startnode_id=0)
get path from a given startnode to a given endnode if startnode is not given the rootnode is startnod...
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
getChilds(int $a_node_id, string $a_order="", string $a_direction="ASC")
get child nodes of given node
getSubTree(array $a_node, bool $a_with_data=true, array $a_type=[])
get all nodes in the subtree under specified node
static now()
Return current timestamp in Y-m-d H:i:s format.
static redirect(string $a_script)
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23