ILIAS  trunk Revision v12.0_alpha-399-g579a087ced2
class.ilNewsItem.php
Go to the documentation of this file.
1<?php
2
21
22const NEWS_NOTICE = 0;
23const NEWS_MESSAGE = 1;
24const NEWS_WARNING = 2;
25
26const NEWS_TEXT = "text";
27const NEWS_HTML = "html";
28const NEWS_AUDIO = "audio";
29const NEWS_USERS = "users";
30const NEWS_PUBLIC = "public";
31
44{
45 private int $mob_cnt_download = 0;
47 protected int $mob_cnt_play = 0;
48 protected ilDBInterface $db;
49 protected ilTree $tree;
52 protected ilObjUser $user;
53 protected ilLanguage $lng;
54 protected ilCtrl $ctrl;
55 protected int $id = 0;
56 protected string $title = "";
57 protected string $content = "";
58 protected bool $content_html = false;
59 protected int $context_obj_id = 0;
60 protected string $context_obj_type = "";
61 protected int $context_sub_obj_id = 0;
62 protected ?string $context_sub_obj_type = null;
63 protected string $content_type = "text";
64 protected string $creation_date = "";
65 protected string $update_date = "";
66 protected int $user_id = 0;
67 protected int $update_user_id = 0;
68 protected string $visibility = "users";
69 protected string $content_long = "";
70 protected int $priority = 1;
71 protected bool $content_is_lang_var = false;
72 protected int $mob_id = 0;
73 protected string $playtime = "";
74 private static int $privFeedId = 0;
75 private bool $limitation = false;
76 protected bool $content_text_is_lang_var = false;
78 protected ilLogger $log;
79
80 public function __construct(int $a_id = 0)
81 {
82 global $DIC;
83 $this->main_tpl = $DIC->ui()->mainTemplate();
84
85 $this->db = $DIC->database();
86 $this->tree = $DIC->repositoryTree();
87 $this->access = $DIC->access();
88 $this->obj_data_cache = $DIC["ilObjDataCache"];
89 $this->user = $DIC->user();
90 $this->lng = $DIC->language();
91 $this->ctrl = $DIC->ctrl();
92 if ($a_id > 0) {
93 $this->setId($a_id);
94 $this->read();
95 }
96 $this->limitation = true;
97 $this->log = $DIC->logger()->news();
98 $this->media_manager = $DIC->mediaObjects()->internal()->domain()->mediaObject();
99 }
100
101 public function setId(int $a_id): void
102 {
103 $this->id = $a_id;
104 }
105
106 public function getId(): int
107 {
108 return $this->id;
109 }
110
111 public function setTitle(string $a_title): void
112 {
113 $this->title = $a_title;
114 }
115
116 public function getTitle(): string
117 {
118 return $this->title;
119 }
120
121 public function setContent(string $a_content): void
122 {
123 $this->content = $a_content;
124 }
125
126 public function getContent(): string
127 {
128 return $this->content;
129 }
130
131 public function setContextObjId(int $a_context_obj_id): void
132 {
133 $this->context_obj_id = $a_context_obj_id;
134 }
135
136 public function getContextObjId(): int
137 {
139 }
140
141 public function setContextObjType(string $a_context_obj_type): void
142 {
143 $this->context_obj_type = $a_context_obj_type;
144 }
145
146 public function getContextObjType(): string
147 {
149 }
150
151 public function setContextSubObjId(int $a_context_sub_obj_id): void
152 {
153 $this->context_sub_obj_id = $a_context_sub_obj_id;
154 }
155
156 public function getContextSubObjId(): int
157 {
159 }
160
161 public function setContextSubObjType(?string $a_context_sub_obj_type): void
162 {
163 $this->context_sub_obj_type = $a_context_sub_obj_type;
164 }
165
166 public function getContextSubObjType(): ?string
167 {
169 }
170
171 public function setContentType(string $a_content_type = "text"): void
172 {
173 $this->content_type = $a_content_type;
174 }
175
176 public function getContentType(): string
177 {
178 return $this->content_type;
179 }
180
181 public function setCreationDate(string $a_creation_date): void
182 {
183 $this->creation_date = $a_creation_date;
184 }
185
186 public function getCreationDate(): string
187 {
189 }
190
191 public function setUpdateDate(string $a_update_date): void
192 {
193 $this->update_date = $a_update_date;
194 }
195
196 public function getUpdateDate(): string
197 {
198 return $this->update_date;
199 }
200
201 public function setUserId(int $a_user_id): void
202 {
203 $this->user_id = $a_user_id;
204 }
205
206 public function getUserId(): int
207 {
208 return $this->user_id;
209 }
210
211 public function setUpdateUserId(int $a_val): void
212 {
213 $this->update_user_id = $a_val;
214 }
215
216 public function getUpdateUserId(): int
217 {
219 }
220
224 public function setVisibility(
225 string $a_visibility = "users"
226 ): void {
227 $this->visibility = $a_visibility;
228 }
229
230 public function getVisibility(): string
231 {
232 return $this->visibility;
233 }
234
238 public function setContentLong(string $a_content_long): void
239 {
240 $this->content_long = $a_content_long;
241 }
242
243 public function getContentLong(): string
244 {
245 return $this->content_long;
246 }
247
248 public function setPriority(int $a_priority = 1): void
249 {
250 $this->priority = $a_priority;
251 }
252
253 public function getPriority(): int
254 {
255 return $this->priority;
256 }
257
258 public function setContentIsLangVar(
259 bool $a_content_is_lang_var = false
260 ): void {
261 $this->content_is_lang_var = $a_content_is_lang_var;
262 }
263
264 public function getContentIsLangVar(): bool
265 {
266 return $this->content_is_lang_var;
267 }
268
269 public function setMobId(int $a_mob_id): void
270 {
271 $this->mob_id = $a_mob_id;
272 }
273
274 public function getMobId(): int
275 {
276 return $this->mob_id;
277 }
278
282 public function setPlaytime(string $a_playtime): void
283 {
284 $this->playtime = $a_playtime;
285 }
286
287 public function getPlaytime(): string
288 {
289 return $this->playtime;
290 }
291
295 public function setLimitation(bool $a_limitation): void
296 {
297 $this->limitation = $a_limitation;
298 }
299
300 public function getLimitation(): bool
301 {
302 return $this->limitation;
303 }
304
305 public function setContentTextIsLangVar(bool $a_val = false): void
306 {
307 $this->content_text_is_lang_var = $a_val;
308 }
309
310 public function getContentTextIsLangVar(): bool
311 {
312 return $this->content_text_is_lang_var;
313 }
314
315 public function setMobPlayCounter(int $a_val): void
316 {
317 $this->mob_cnt_play = $a_val;
318 }
319
320 public function getMobPlayCounter(): int
321 {
322 return $this->mob_cnt_play;
323 }
324
325 public function setMobDownloadCounter(int $a_val): void
326 {
327 $this->mob_cnt_download = $a_val;
328 }
329
330 public function getMobDownloadCounter(): int
331 {
332 return $this->mob_cnt_download;
333 }
334
335 public function setContentHtml(bool $a_val): void
336 {
337 $this->content_html = $a_val;
338 }
339
340 public function getContentHtml(): bool
341 {
342 return $this->content_html;
343 }
344
349 public function read(): void
350 {
351 $ilDB = $this->db;
352
353 $query = "SELECT * FROM il_news_item WHERE id = " .
354 $ilDB->quote($this->getId(), "integer");
355 $set = $ilDB->query($query);
356 if ($rec = $ilDB->fetchAssoc($set)) {
357 $this->setTitle((string) $rec["title"]);
358 $this->setContent((string) $rec["content"]);
359 $this->setContextObjId((int) $rec["context_obj_id"]);
360 $this->setContextObjType($rec["context_obj_type"]);
361 $this->setContextSubObjId((int) $rec["context_sub_obj_id"]);
362 $this->setContextSubObjType((string) $rec["context_sub_obj_type"]);
363 $this->setContentType((string) $rec["content_type"]);
364 $this->setCreationDate((string) $rec["creation_date"]);
365 $this->setUpdateDate((string) $rec["update_date"]);
366 $this->setUserId((int) $rec["user_id"]);
367 $this->setUpdateUserId((int) $rec["update_user_id"]);
368 $this->setVisibility((string) $rec["visibility"]);
369 $this->setContentLong((string) $rec["content_long"]);
370 $this->setPriority((int) $rec["priority"]);
371 $this->setContentIsLangVar((bool) $rec["content_is_lang_var"]);
372 $this->setContentTextIsLangVar((bool) $rec["content_text_is_lang_var"]);
373 $this->setMobId((int) $rec["mob_id"]);
374 $this->setPlaytime((string) $rec["playtime"]);
375 $this->setMobPlayCounter((int) $rec["mob_cnt_play"]);
376 $this->setMobDownloadCounter((int) $rec["mob_cnt_download"]);
377 $this->setContentHtml((bool) $rec["content_html"]);
378 }
379 }
380
385 public function create(): void
386 {
387 $ilDB = $this->db;
388
389 // insert new record into db
390 $this->setId($ilDB->nextId("il_news_item"));
391 $ilDB->insert("il_news_item", [
392 "id" => ["integer", $this->getId()],
393 "title" => ["text", $this->getTitle()],
394 "content" => ["clob", $this->getContent()],
395 "content_html" => ["integer", (int) $this->getContentHtml()],
396 "context_obj_id" => ["integer", $this->getContextObjId()],
397 "context_obj_type" => ["text", $this->getContextObjType()],
398 "context_sub_obj_id" => ["integer", $this->getContextSubObjId()],
399 "context_sub_obj_type" => ["text", $this->getContextSubObjType()],
400 "content_type" => ["text", $this->getContentType()],
401 "creation_date" => ["timestamp", ilUtil::now()],
402 "update_date" => ["timestamp", ilUtil::now()],
403 "user_id" => ["integer", $this->getUserId()],
404 "update_user_id" => ["integer", $this->getUpdateUserId()],
405 "visibility" => ["text", $this->getVisibility()],
406 "content_long" => ["clob", $this->getContentLong()],
407 "priority" => ["integer", $this->getPriority()],
408 "content_is_lang_var" => ["integer", $this->getContentIsLangVar()],
409 "content_text_is_lang_var" => ["integer", (int) $this->getContentTextIsLangVar()],
410 "mob_id" => ["integer", $this->getMobId()],
411 "playtime" => ["text", $this->getPlaytime()]
412 ]);
413
414
415 $news_set = new ilSetting("news");
416 $max_items = $news_set->get("max_items");
417 if ($max_items <= 0) {
418 $max_items = 50;
419 }
420
421 // limit number of news
422 if ($this->getLimitation()) {
423 // Determine how many rows should be deleted
424 $query = "SELECT count(*) cnt " .
425 "FROM il_news_item " .
426 "WHERE " .
427 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
428 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
429 " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
430 " AND " . $ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true) . " ";
431
432 $set = $ilDB->query($query);
433 $rec = $ilDB->fetchAssoc($set);
434
435 // if we have more records than allowed, delete them
436 if (($rec["cnt"] > $max_items) && $this->getContextObjId() > 0) {
437 $query = "SELECT * " .
438 "FROM il_news_item " .
439 "WHERE " .
440 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
441 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
442 " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
443 " AND " . $ilDB->equals("context_sub_obj_type", $this->getContextSubObjType(), "text", true) .
444 " ORDER BY creation_date ASC";
445
446 $ilDB->setLimit($rec["cnt"] - $max_items, 0);
447 $del_set = $ilDB->query($query);
448 while ($del_item = $ilDB->fetchAssoc($del_set)) {
449 $del_news = new ilNewsItem((int) $del_item["id"]);
450 $del_news->delete();
451 }
452 }
453 }
454 }
455
462 public function update(bool $a_as_new = false): void
463 {
464 $ilDB = $this->db;
465
466 $fields = [
467 "title" => ["text", $this->getTitle()],
468 "content" => ["clob", $this->getContent()],
469 "content_html" => ["integer", (int) $this->getContentHtml()],
470 "context_obj_id" => ["integer", $this->getContextObjId()],
471 "context_obj_type" => ["text", $this->getContextObjType()],
472 "context_sub_obj_id" => ["integer", $this->getContextSubObjId()],
473 "context_sub_obj_type" => ["text", $this->getContextSubObjType()],
474 "content_type" => ["text", $this->getContentType()],
475 "user_id" => ["integer", $this->getUserId()],
476 "update_user_id" => ["integer", $this->getUpdateUserId()],
477 "visibility" => ["text", $this->getVisibility()],
478 "content_long" => ["clob", $this->getContentLong()],
479 "priority" => ["integer", $this->getPriority()],
480 "content_is_lang_var" => ["integer", $this->getContentIsLangVar()],
481 "content_text_is_lang_var" => ["integer", (int) $this->getContentTextIsLangVar()],
482 "mob_id" => ["integer", $this->getMobId()],
483 "mob_cnt_play" => ["integer", $this->getMobPlayCounter()],
484 "mob_cnt_download" => ["integer", $this->getMobDownloadCounter()],
485 "playtime" => ["text", $this->getPlaytime()]
486 ];
487
488 $now = ilUtil::now();
489 if ($a_as_new) {
490 $fields["creation_date"] = ["timestamp", $now];
491 }
492 $fields["update_date"] = ["timestamp", $now];
493
494 $ilDB->update("il_news_item", $fields, [
495 "id" => ["integer", $this->getId()]
496 ]);
497 }
498
508 public function getNewsForRefId(
509 int $a_ref_id,
510 bool $a_only_public = false,
511 bool $a_stopnesting = false,
512 $a_time_period = 0,
513 bool $a_prevent_aggregation = true,
514 bool $a_forum_group_sequences = false,
515 bool $a_no_auto_generated = false,
516 bool $a_ignore_date_filter = false,
517 ?int $a_user_id = null,
518 int $a_limit = 0,
519 array $a_excluded = []
520 ): array {
521 $obj_id = ilObject::_lookupObjId($a_ref_id);
522 $obj_type = ilObject::_lookupType($obj_id);
523
524 // get starting date
525 $starting_date = "";
526 if ($obj_type === "grp" || $obj_type === "crs") {
527 // see #31471, #30687, and ilMembershipNotification
529 $obj_id,
530 'cont_use_news',
531 '1'
532 ) || (
534 $obj_id,
535 'cont_show_news',
536 '1'
538 $obj_id,
539 'news_timeline'
540 )
541 )) {
542 return [];
543 }
544
545 $hide_news_per_date = ilBlockSetting::_lookup(
546 "news",
547 "hide_news_per_date",
548 0,
549 $obj_id
550 );
551 if ($hide_news_per_date && !$a_ignore_date_filter) {
552 $starting_date = ilBlockSetting::_lookup(
553 "news",
554 "hide_news_date",
555 0,
556 $obj_id
557 );
558 }
559 }
560
561 if ($obj_type === "cat" && !$a_stopnesting) {
562 $news = $this->getAggregatedChildNewsData(
563 $a_ref_id,
564 $a_only_public,
565 $a_time_period,
566 $a_prevent_aggregation,
567 $starting_date,
568 $a_no_auto_generated,
569 $a_excluded
570 );
571 } elseif (($obj_type === "grp" || $obj_type === "crs") &&
572 !$a_stopnesting) {
573 $news = $this->getAggregatedNewsData(
574 $a_ref_id,
575 $a_only_public,
576 $a_time_period,
577 $a_prevent_aggregation,
578 $starting_date,
579 $a_no_auto_generated,
580 $a_user_id,
581 $a_limit,
582 $a_excluded
583 );
584 } else {
585 $news_item = new ilNewsItem();
586 $news_item->setContextObjId($obj_id);
587 $news_item->setContextObjType($obj_type);
588 $news = $news_item->queryNewsForContext(
589 $a_only_public,
590 $a_time_period,
591 $starting_date,
592 $a_no_auto_generated,
593 false,
594 0,
595 $a_excluded
596 );
597 $unset = [];
598 foreach ($news as $k => $v) {
599 if (!$a_only_public || $v["visibility"] == NEWS_PUBLIC ||
600 ($v["priority"] == 0 &&
602 "news",
603 "public_notifications",
604 0,
605 $obj_id
606 ))) {
607 $news[$k]["ref_id"] = $a_ref_id;
608 } else {
609 $unset[] = $k;
610 }
611 }
612 foreach ($unset as $un) {
613 unset($news[$un]);
614 }
615 }
616
617 if (!$a_prevent_aggregation) {
618 $news = $this->aggregateForums($news);
619 } elseif ($a_forum_group_sequences) {
620 $news = $this->aggregateForums($news, true);
621 }
622
623 return $news;
624 }
625
632 protected function getAggregatedNewsData(
633 int $a_ref_id,
634 bool $a_only_public = false,
635 $a_time_period = 0,
636 bool $a_prevent_aggregation = false,
637 string $a_starting_date = "",
638 bool $a_no_auto_generated = false,
639 ?int $a_user_id = null,
640 int $a_limit = 0,
641 array $a_exclude = []
642 ): array {
643 $tree = $this->tree;
644 $ilAccess = $this->access;
645 $ilObjDataCache = $this->obj_data_cache;
646
647 // get news of parent object
648 $data = [];
649
650 // get subtree
651 $cur_node = $tree->getNodeData($a_ref_id);
652
653 // do not check for lft (materialized path)
654 if ($cur_node) {
655 $nodes = $tree->getSubTree($cur_node, true);
656 } else {
657 $nodes = [];
658 }
659
660 // preload object data cache
661 $ref_ids = [];
662 $obj_ids = [];
663 $ref_id = [];
664 foreach ($nodes as $node) {
665 $ref_ids[] = (int) $node["child"];
666 $obj_ids[] = (int) $node["obj_id"];
667 }
668
669 $ilObjDataCache->preloadReferenceCache($ref_ids);
670 if (!$a_only_public) {
672 }
673
674 // no check, for which of the objects any news are available
675 $news_obj_ids = self::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
676 //$news_obj_ids = $obj_ids;
677
678 // get news for all subtree nodes
679 $contexts = [];
680 foreach ($nodes as $node) {
681 // only go on, if news are available
682 if (!in_array($node["obj_id"], $news_obj_ids)) {
683 continue;
684 }
685
686 if (!$a_only_public) {
687 if (!$a_user_id) {
688 $acc = $ilAccess->checkAccess("read", "", (int) $node["child"]);
689 } else {
690 $acc = $ilAccess->checkAccessOfUser(
691 $a_user_id,
692 "read",
693 "",
694 (int) $node["child"]
695 );
696 }
697 if (!$acc) {
698 continue;
699 }
700 }
701
702 $ref_id[$node["obj_id"]] = $node["child"];
703 $contexts[] = [
704 "obj_id" => $node["obj_id"],
705 "obj_type" => $node["type"]
706 ];
707 }
708
709 // sort and return
710 $news = $this->queryNewsForMultipleContexts(
711 $contexts,
712 $a_only_public,
713 $a_time_period,
714 $a_starting_date,
715 $a_no_auto_generated,
716 $a_user_id,
717 $a_limit,
718 $a_exclude
719 );
720
721 $to_del = [];
722 foreach ($news as $k => $v) {
723 $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
724 }
725
726 $data = self::mergeNews($data, $news);
727 $data = ilArrayUtil::sortArray($data, "creation_date", "desc", false, true);
728
729 if (!$a_prevent_aggregation) {
730 $data = $this->aggregateFiles($data, $a_ref_id);
731 }
732
733 return $data;
734 }
735
739 protected function aggregateForums(
740 array $news,
741 bool $a_group_posting_sequence = false
742 ): array {
743 $to_del = [];
744 $forums = [];
745 $last_aggregation_forum = 0;
746
747 // aggregate
748 foreach ($news as $k => $v) {
749 if ($a_group_posting_sequence && $last_aggregation_forum > 0 &&
750 $last_aggregation_forum != $v["context_obj_id"]) {
751 $forums[$last_aggregation_forum] = null;
752 }
753
754 if ($v["context_obj_type"] === "frm") {
755 if (!isset($forums[$v["context_obj_id"]])) {
756 // $forums[forum_id] = news_id;
757 $forums[$v["context_obj_id"]] = $k;
758 $last_aggregation_forum = $v["context_obj_id"];
759 } else {
760 $to_del[] = $k;
761 }
762
763 $news[$k]["no_context_title"] = true;
764
765 // aggregate every forum into it's "k" news
766 $news[$forums[$news[$k]["context_obj_id"]]]["aggregation"][$k]
767 = $news[$k];
768 $news[$k]["agg_ref_id"]
769 = $news[$k]["ref_id"];
770 $news[$k]["content"] = "";
771 $news[$k]["content_long"] = "";
772 }
773 }
774
775 // delete double entries
776 foreach ($to_del as $k) {
777 unset($news[$k]);
778 }
779 //var_dump($news[14]["aggregation"]);
780
781 return $news;
782 }
783
787 protected function aggregateFiles(
788 array $news,
789 int $a_ref_id
790 ): array {
791 $first_file = "";
792 $to_del = [];
793 foreach ($news as $k => $v) {
794 // aggregate file related news
795 if ($v["context_obj_type"] === "file") {
796 if ($first_file === "") {
797 $first_file = $k;
798 } else {
799 $to_del[] = $k;
800 }
801 $news[$first_file]["aggregation"][$k] = $v;
802 $news[$first_file]["agg_ref_id"] = $a_ref_id;
803 $news[$first_file]["ref_id"] = $a_ref_id;
804 }
805 }
806
807 foreach ($to_del as $v) {
808 unset($news[$v]);
809 }
810
811 return $news;
812 }
813
814
820 protected function getAggregatedChildNewsData(
821 int $a_ref_id,
822 bool $a_only_public = false,
823 int $a_time_period = 0,
824 bool $a_prevent_aggregation = false,
825 string $a_starting_date = "",
826 bool $a_no_auto_generated = false,
827 array $a_excluded = []
828 ): array {
829 $tree = $this->tree;
830 $ilAccess = $this->access;
831 $ref_id = [];
832 // get news of parent object
833 $data = $this->getNewsForRefId(
834 $a_ref_id,
835 $a_only_public,
836 true,
837 $a_time_period,
838 true,
839 false,
840 $a_no_auto_generated,
841 false,
842 null,
843 0,
844 $a_excluded
845 );
846 foreach ($data as $k => $v) {
847 $data[$k]["ref_id"] = $a_ref_id;
848 }
849
850 // get childs
851 $nodes = $tree->getChilds($a_ref_id);
852
853 // no check, for which of the objects any news are available
854 $obj_ids = [];
855 foreach ($nodes as $node) {
856 $obj_ids[] = $node["obj_id"];
857 }
858 $news_obj_ids = self::filterObjIdsPerNews($obj_ids, $a_time_period, $a_starting_date);
859 //$news_obj_ids = $obj_ids;
860
861 // get news for all subtree nodes
862 $contexts = [];
863 foreach ($nodes as $node) {
864 // only go on, if news are available
865 if (!in_array($node["obj_id"], $news_obj_ids)) {
866 continue;
867 }
868
869 if (!$a_only_public && !$ilAccess->checkAccess("read", "", (int) $node["child"])) {
870 continue;
871 }
872 $ref_id[$node["obj_id"]] = $node["child"];
873 $contexts[] = [
874 "obj_id" => $node["obj_id"],
875 "obj_type" => $node["type"]
876 ];
877 }
878
879 $news = $this->queryNewsForMultipleContexts(
880 $contexts,
881 $a_only_public,
882 $a_time_period,
883 $a_starting_date,
884 $a_no_auto_generated,
885 null,
886 0,
887 $a_excluded
888 );
889 foreach ($news as $k => $v) {
890 $news[$k]["ref_id"] = $ref_id[$v["context_obj_id"]];
891 }
892 $data = self::mergeNews($data, $news);
893
894 // sort and return
895 $data = ilArrayUtil::sortArray($data, "creation_date", "desc", false, true);
896
897 if (!$a_prevent_aggregation) {
898 $data = $this->aggregateFiles($data, $a_ref_id);
899 }
900
901 return $data;
902 }
903
907 public function setContext(
908 int $a_obj_id,
909 string $a_obj_type,
910 int $a_sub_obj_id = 0,
911 string $a_sub_obj_type = ""
912 ): void {
913 $this->setContextObjId($a_obj_id);
914 $this->setContextObjType($a_obj_type);
915 $this->setContextSubObjId($a_sub_obj_id);
916 $this->setContextSubObjType($a_sub_obj_type);
917 }
918
923 protected static function handleTimePeriod($a_time_period): string
924 {
925 // time period is number of days
926 if (is_numeric($a_time_period)) {
927 if ($a_time_period > 0) {
928 return date('Y-m-d H:i:s', time() - ($a_time_period * 24 * 60 * 60));
929 }
930 }
931 // time period is datetime
932 elseif (preg_match("/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/", $a_time_period)) {
933 return $a_time_period;
934 }
935 return "";
936 }
937
944 public function queryNewsForContext(
945 bool $a_for_rss_use = false,
946 $a_time_period = 0,
947 string $a_starting_date = "",
948 bool $a_no_auto_generated = false,
949 bool $a_oldest_first = false,
950 int $a_limit = 0,
951 array $a_exclude = []
952 ): array {
953 $ilDB = $this->db;
954 $ilUser = $this->user;
955
956 $and = "";
957 if ($a_time_period > 0) {
958 $limit_ts = self::handleTimePeriod($a_time_period);
959 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
960 }
961
962 if ($a_starting_date !== "") {
963 $and .= " AND creation_date > " . $ilDB->quote($a_starting_date, "timestamp") . " ";
964 }
965
966 if ($a_no_auto_generated) {
967 $and .= " AND priority = 1 AND content_type = " . $ilDB->quote("text", "text") . " ";
968 }
969
970 // this is changed with 4.1 (news table for lm pages)
971 if ($this->getContextSubObjId() > 0) {
972 $and .= " AND context_sub_obj_id = " . $ilDB->quote($this->getContextSubObjId(), "integer") .
973 " AND context_sub_obj_type = " . $ilDB->quote($this->getContextSubObjType(), "text");
974 }
975
976 if (count($a_exclude) > 0) {
977 $and .= " AND " . $ilDB->in("id", $a_exclude, true, "integer") . " ";
978 }
979
980 $ordering = ($a_oldest_first)
981 ? " creation_date ASC, id ASC "
982 : " creation_date DESC, id DESC ";
983
984 if ($a_for_rss_use && self::getPrivateFeedId() === 0) {
985 $query = "SELECT * " .
986 "FROM il_news_item " .
987 " WHERE " .
988 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
989 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
990 $and .
991 " ORDER BY " . $ordering;
992 } elseif (self::getPrivateFeedId() > 0) {
993 $query = "SELECT il_news_item.* " .
994 ", il_news_read.user_id user_read " .
995 "FROM il_news_item LEFT JOIN il_news_read " .
996 "ON il_news_item.id = il_news_read.news_id AND " .
997 " il_news_read.user_id = " . $ilDB->quote(self::getPrivateFeedId(), "integer") .
998 " WHERE " .
999 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
1000 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
1001 $and .
1002 " ORDER BY " . $ordering;
1003 } else {
1004 $query = "SELECT il_news_item.* " .
1005 ", il_news_read.user_id as user_read " .
1006 "FROM il_news_item LEFT JOIN il_news_read " .
1007 "ON il_news_item.id = il_news_read.news_id AND " .
1008 " il_news_read.user_id = " . $ilDB->quote($ilUser->getId(), "integer") .
1009 " WHERE " .
1010 "context_obj_id = " . $ilDB->quote($this->getContextObjId(), "integer") .
1011 " AND context_obj_type = " . $ilDB->quote($this->getContextObjType(), "text") .
1012 $and .
1013 " ORDER BY " . $ordering;
1014 }
1015 //echo $query;
1016 $set = $ilDB->query($query);
1017 $result = [];
1018 while ($rec = $ilDB->fetchAssoc($set)) {
1019 if ($a_limit > 0 && count($result) >= $a_limit) {
1020 continue;
1021 }
1022 if (!$a_for_rss_use || (self::getPrivateFeedId() > 0) || ($rec["visibility"] === NEWS_PUBLIC ||
1023 ((int) $rec["priority"] === 0 &&
1025 "news",
1026 "public_notifications",
1027 0,
1028 (int) $rec["context_obj_id"]
1029 )))) {
1030 $result[$rec["id"]] = $rec;
1031 }
1032 }
1033
1034 // do we get data for rss and may the time limit by an issue?
1035 // do a second query without time limit.
1036 // this is not very performant, but I do not have a better
1037 // idea. The keep_rss_min setting is currently (Jul 2012) only set
1038 // by mediacasts
1039 if ($a_time_period && $a_for_rss_use) {
1040 $keep_rss_min = ilBlockSetting::_lookup(
1041 "news",
1042 "keep_rss_min",
1043 0,
1044 $this->getContextObjId()
1045 );
1046 if ($keep_rss_min > 0) {
1047 return $this->queryNewsForContext(
1048 true,
1049 0,
1050 $a_starting_date,
1051 $a_no_auto_generated,
1052 $a_oldest_first,
1053 (int) $keep_rss_min,
1054 $a_exclude
1055 );
1056 }
1057 }
1058
1059 return $result;
1060 }
1061
1067 public static function queryNewsByIds(array $a_news_ids): array
1068 {
1069 global $DIC;
1070 $ilDB = $DIC->database();
1071 $news = [];
1072 $set = $ilDB->query("SELECT * FROM il_news_item " .
1073 " WHERE " . $ilDB->in("id", $a_news_ids, false, "integer"));
1074 while ($rec = $ilDB->fetchAssoc($set)) {
1075 $news[$rec["id"]] = $rec;
1076 }
1077 return $news;
1078 }
1079
1086 array $objects,
1087 $a_time_period = 1
1088 ): array {
1089 $ilDB = $this->db;
1090
1091 $all = [];
1092
1093 $this->log->debug("time period: " . $a_time_period);
1094 $limit_ts = self::handleTimePeriod($a_time_period);
1095
1096 // are there any news items for relevant objects and?
1097 $query = $ilDB->query($q = "SELECT id,context_obj_id,context_obj_type" .
1098 " FROM il_news_item" .
1099 " WHERE " . $ilDB->in("context_obj_id", array_keys($objects), false, "integer") .
1100 " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp"));
1101 $this->log->debug($q);
1102 while ($rec = $ilDB->fetchAssoc($query)) {
1103 if ($objects[$rec["context_obj_id"]]["type"] == $rec["context_obj_type"]) {
1104 $all[] = (int) $rec["id"];
1105 }
1106 }
1107
1108 return $all;
1109 }
1110
1117 array $a_contexts,
1118 bool $a_for_rss_use = false,
1119 $a_time_period = 0,
1120 string $a_starting_date = "",
1121 bool $a_no_auto_generated = false,
1122 ?int $a_user_id = null,
1123 int $a_limit = 0,
1124 array $a_exclude = []
1125 ): array {
1126 $ilDB = $this->db;
1127 $ilUser = $this->user;
1128
1129 $and = "";
1130 if ($a_time_period > 0) {
1131 $limit_ts = self::handleTimePeriod($a_time_period);
1132 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
1133 }
1134
1135 if ($a_starting_date !== "") {
1136 $and .= " AND creation_date > " . $ilDB->quote($a_starting_date, "timestamp") . " ";
1137 }
1138
1139 if ($a_no_auto_generated) {
1140 $and .= " AND priority = 1 AND content_type = " . $ilDB->quote("text", "text") . " ";
1141 }
1142
1143 if ($a_limit > 0) {
1144 $ilDB->setLimit($a_limit, 0);
1145 }
1146
1147 if (is_array($a_exclude) && count($a_exclude) > 0) {
1148 $and .= " AND " . $ilDB->in("id", $a_exclude, true, "integer") . " ";
1149 }
1150
1151 $ids = [];
1152 $type = [];
1153
1154 foreach ($a_contexts as $cont) {
1155 $ids[] = $cont["obj_id"];
1156 $type[$cont["obj_id"]] = $cont["obj_type"];
1157 }
1158
1159 if ($a_for_rss_use && self::getPrivateFeedId() === 0) {
1160 $query = "SELECT * " .
1161 "FROM il_news_item " .
1162 " WHERE " .
1163 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1164 $and .
1165 " ORDER BY creation_date DESC ";
1166 } elseif (self::getPrivateFeedId() > 0) {
1167 $query = "SELECT il_news_item.* " .
1168 ", il_news_read.user_id as user_read " .
1169 "FROM il_news_item LEFT JOIN il_news_read " .
1170 "ON il_news_item.id = il_news_read.news_id AND " .
1171 " il_news_read.user_id = " . $ilDB->quote(self::getPrivateFeedId(), "integer") .
1172 " WHERE " .
1173 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1174 $and .
1175 " ORDER BY creation_date DESC ";
1176 } else {
1177 if ($a_user_id) {
1178 $user_id = $a_user_id;
1179 } else {
1180 $user_id = $ilUser->getId();
1181 }
1182 $query = "SELECT il_news_item.* " .
1183 ", il_news_read.user_id as user_read " .
1184 "FROM il_news_item LEFT JOIN il_news_read " .
1185 "ON il_news_item.id = il_news_read.news_id AND " .
1186 " il_news_read.user_id = " . $ilDB->quote($user_id, "integer") .
1187 " WHERE " .
1188 $ilDB->in("context_obj_id", $ids, false, "integer") . " " .
1189 $and .
1190 " ORDER BY creation_date DESC ";
1191 }
1192
1193 $set = $ilDB->query($query);
1194 $result = [];
1195 while ($rec = $ilDB->fetchAssoc($set)) {
1196 if ($type[$rec["context_obj_id"]] == $rec["context_obj_type"]) {
1197 if (!$a_for_rss_use || self::getPrivateFeedId() > 0 || ($rec["visibility"] === NEWS_PUBLIC ||
1198 ((int) $rec["priority"] === 0 &&
1200 "news",
1201 "public_notifications",
1202 0,
1203 (int) $rec["context_obj_id"]
1204 )))) {
1205 $result[$rec["id"]] = $rec;
1206 }
1207 }
1208 }
1209
1210 return $result;
1211 }
1212
1213
1218 public static function _setRead(
1219 int $a_user_id,
1220 int $a_news_id
1221 ): void {
1222 global $DIC;
1223
1224 $ilDB = $DIC->database();
1225 $ilAppEventHandler = $DIC["ilAppEventHandler"];
1226
1227 $ilDB->replace(
1228 "il_news_read",
1229 [
1230 "user_id" => ["integer", $a_user_id],
1231 "news_id" => ["integer", $a_news_id]
1232 ],
1233 []
1234 );
1235
1236 $ilAppEventHandler->raise(
1237 "components/ILIAS/News",
1238 "readNews",
1239 ["user_id" => $a_user_id, "news_ids" => [$a_news_id]]
1240 );
1241 }
1242
1247 public static function _setUnread(
1248 int $a_user_id,
1249 int $a_news_id
1250 ): void {
1251 global $DIC;
1252
1253 $ilDB = $DIC->database();
1254 $ilAppEventHandler = $DIC["ilAppEventHandler"];
1255
1256 $ilDB->manipulate("DELETE FROM il_news_read (user_id, news_id) VALUES (" .
1257 " WHERE user_id = " . $ilDB->quote($a_user_id, "integer") .
1258 " AND news_id = " . $ilDB->quote($a_news_id, "integer"));
1259
1260 $ilAppEventHandler->raise(
1261 "components/ILIAS/News",
1262 "unreadNews",
1263 ["user_id" => $a_user_id, "news_ids" => [$a_news_id]]
1264 );
1265 }
1266
1271 public static function mergeNews(
1272 array $n1,
1273 array $n2
1274 ): array {
1275 foreach ($n2 as $id => $news) {
1276 $n1[$id] = $news;
1277 }
1278
1279 return $n1;
1280 }
1281
1286 public static function _getDefaultVisibilityForRefId(int $a_ref_id): string
1287 {
1288 global $DIC;
1289
1290 $tree = $DIC->repositoryTree();
1291
1292 $news_set = new ilSetting("news");
1293 $default_visibility = ($news_set->get("default_visibility") != "")
1294 ? $news_set->get("default_visibility")
1295 : "users";
1296
1297 if ($tree->isInTree($a_ref_id)) {
1298 $path = $tree->getPathFull($a_ref_id);
1299
1300 foreach ($path as $key => $row) {
1301 if (!in_array($row["type"], ["root", "cat", "crs", "fold", "grp"], true)) {
1302 continue;
1303 }
1304
1305 $visibility = ilBlockSetting::_lookup(
1306 "news",
1307 "default_visibility",
1308 0,
1309 (int) $row["obj_id"]
1310 );
1311
1312 if ($visibility != "") {
1313 $default_visibility = $visibility;
1314 }
1315 }
1316 }
1317
1318 return $default_visibility;
1319 }
1320
1321
1326 public function delete(): void
1327 {
1328 $ilDB = $this->db;
1329
1330 // delete il_news_read entries
1331 $ilDB->manipulate("DELETE FROM il_news_read " .
1332 " WHERE news_id = " . $ilDB->quote($this->getId(), "integer"));
1333
1334 // delete multimedia object
1335 $mob = $this->getMobId();
1336
1337 // delete
1338 $query = "DELETE FROM il_news_item" .
1339 " WHERE id = " . $ilDB->quote($this->getId(), "integer");
1340 $ilDB->manipulate($query);
1341
1342 // delete mob after news, to have a "mob usage" of 0
1343 if ($mob > 0 && ilObject::_exists($mob)) {
1344 $mob = new ilObjMediaObject($mob);
1345 $mob->delete();
1346 }
1347 }
1348
1354 public static function getNewsOfContext(
1355 int $a_context_obj_id,
1356 string $a_context_obj_type,
1357 int $a_context_sub_obj_id = 0,
1358 string $a_context_sub_obj_type = ""
1359 ): array {
1360 global $DIC;
1361
1362 $ilDB = $DIC->database();
1363 $and = "";
1364
1365 if ($a_context_obj_id === 0 || $a_context_obj_type === "") {
1366 return [];
1367 }
1368
1369 if ($a_context_sub_obj_id > 0) {
1370 $and = " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1371 " AND context_sub_obj_type = " . $ilDB->quote($a_context_sub_obj_type, "text");
1372 }
1373
1374 // get news records
1375 $query = "SELECT id FROM il_news_item" .
1376 " WHERE context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1377 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1378 $and;
1379
1380 $news_set = $ilDB->query($query);
1381
1382 $news_arr = [];
1383 while ($news = $ilDB->fetchAssoc($news_set)) {
1384 $news_arr[] = new ilNewsItem((int) $news["id"]);
1385 }
1386 return $news_arr;
1387 }
1388
1393 public static function deleteNewsOfContext(
1394 int $a_context_obj_id,
1395 string $a_context_obj_type,
1396 int $a_context_sub_obj_id = 0,
1397 string $a_context_sub_obj_type = ""
1398 ): void {
1399 foreach (self::getNewsOfContext(
1400 $a_context_obj_id,
1401 $a_context_obj_type,
1402 $a_context_sub_obj_id,
1403 $a_context_sub_obj_type
1404 ) as $n) {
1405 $n->delete();
1406 }
1407 }
1408
1413 public static function _lookupTitle(int $a_news_id): string
1414 {
1415 global $DIC;
1416
1417 $ilDB = $DIC->database();
1418
1419 $query = "SELECT title FROM il_news_item WHERE id = " .
1420 $ilDB->quote($a_news_id, "integer");
1421 $set = $ilDB->query($query);
1422 $rec = $ilDB->fetchAssoc($set);
1423 return $rec["title"] ?? '';
1424 }
1425
1430 public static function _lookupVisibility(int $a_news_id): string
1431 {
1432 global $DIC;
1433
1434 $ilDB = $DIC->database();
1435
1436 $query = "SELECT visibility FROM il_news_item WHERE id = " .
1437 $ilDB->quote($a_news_id, "integer");
1438 $set = $ilDB->query($query);
1439 $rec = $ilDB->fetchAssoc($set);
1440
1441 return $rec["visibility"] ?? NEWS_USERS;
1442 }
1443
1448 public static function _lookupMobId(int $a_news_id): int
1449 {
1450 global $DIC;
1451
1452 $ilDB = $DIC->database();
1453
1454 $query = "SELECT mob_id FROM il_news_item WHERE id = " .
1455 $ilDB->quote($a_news_id, "integer");
1456 $set = $ilDB->query($query);
1457 $rec = $ilDB->fetchAssoc($set);
1458 return (int) ($rec["mob_id"] ?? 0);
1459 }
1460
1467 public static function filterObjIdsPerNews(
1468 array $a_obj_ids,
1469 $a_time_period = 0,
1470 string $a_starting_date = "",
1471 string $a_ending_date = '',
1472 bool $ignore_period = false
1473 ): array {
1474 global $DIC;
1475
1476 $ilDB = $DIC->database();
1477
1478 $and = "";
1479 if ($a_time_period > 0) {
1480 $limit_ts = self::handleTimePeriod($a_time_period);
1481 $and = " AND creation_date >= " . $ilDB->quote($limit_ts, "timestamp") . " ";
1482 }
1483
1484 if ($a_starting_date !== "") {
1485 $and .= " AND creation_date >= " . $ilDB->quote($a_starting_date, "timestamp");
1486 }
1487
1488 $query = "SELECT DISTINCT(context_obj_id) AS obj_id FROM il_news_item" .
1489 " WHERE " . $ilDB->in("context_obj_id", $a_obj_ids, false, "integer") . " " . $and;
1490 //" WHERE context_obj_id IN (".implode(ilUtil::quoteArray($a_obj_ids),",").")".$and;
1491
1492 $set = $ilDB->query($query);
1493 $objs = [];
1494 while ($rec = $ilDB->fetchAssoc($set)) {
1495 $objs[] = $rec["obj_id"];
1496 }
1497
1498 return $objs;
1499 }
1500
1504 public static function determineNewsTitleByNewsId(
1505 int $a_news_id,
1506 int $a_agg_ref_id = 0,
1507 array $a_aggregation = []
1508 ): string {
1509 global $DIC;
1510
1511 $ilDB = $DIC->database();
1512
1513 $query = "SELECT context_obj_type, content_is_lang_var, title FROM il_news_item WHERE id = " .
1514 $ilDB->quote($a_news_id, "integer");
1515 $set = $ilDB->query($query);
1516 $rec = $ilDB->fetchAssoc($set);
1517
1518 return self::determineNewsTitle(
1519 $rec["context_obj_type"],
1520 $rec["title"],
1521 $rec["content_is_lang_var"],
1522 $a_agg_ref_id,
1523 $a_aggregation
1524 );
1525 }
1526
1531 public static function determineNewsTitle(
1532 string $a_context_obj_type,
1533 string $a_title,
1534 bool $a_content_is_lang_var,
1535 int $a_agg_ref_id = 0,
1536 array $a_aggregation = [],
1537 ?ilLanguage $lng = null
1538 ): string {
1539 global $DIC;
1540
1541 if (is_null($lng)) {
1542 $lng = $DIC->language();
1543 }
1544 $obj_definition = $DIC["objDefinition"];
1545 $tit = "";
1546
1547 if ($a_agg_ref_id > 0) {
1548 $cnt = count($a_aggregation);
1549
1550 // forums
1551 if ($a_context_obj_type === "frm") {
1552 if ($cnt > 1) {
1553 return sprintf($lng->txt("news_x_postings"), $cnt);
1554 }
1555
1556 return $lng->txt("news_1_postings");
1557 }
1558
1559 // files
1560 $up_cnt = $cr_cnt = 0;
1561 foreach ($a_aggregation as $item) {
1562 $title = $item instanceof NewsItem ? $item->getTitle() : $item['title'];
1563 if ($title === 'file_updated') {
1564 $up_cnt++;
1565 } else {
1566 $cr_cnt++;
1567 }
1568 }
1569 $sep = "";
1570 if ($cr_cnt === 1) {
1571 $tit = $lng->txt("news_1_file_created");
1572 $sep = "<br />";
1573 } elseif ($cr_cnt > 1) {
1574 $tit = sprintf($lng->txt("news_x_files_created"), $cr_cnt);
1575 $sep = "<br />";
1576 }
1577 if ($up_cnt === 1) {
1578 $tit .= $sep . $lng->txt("news_1_file_updated");
1579 } elseif ($up_cnt > 1) {
1580 $tit .= $sep . sprintf($lng->txt("news_x_files_updated"), $up_cnt);
1581 }
1582 return $tit;
1583 }
1584
1585 if ($a_content_is_lang_var) {
1586 if ($obj_definition->isPlugin($a_context_obj_type)) {
1587 return ilObjectPlugin::lookupTxtById($a_context_obj_type, $a_title);
1588 }
1589 return $lng->txt($a_title);
1590 }
1591
1592 return $a_title;
1593 }
1594
1599 public static function determineNewsContent(
1600 string $a_context_obj_type,
1601 string $a_content,
1602 bool $a_is_lang_var,
1603 ?ilLanguage $lng = null
1604 ): string {
1605 global $DIC;
1606
1607 if (is_null($lng)) {
1608 $lng = $DIC->language();
1609 }
1610 $obj_definition = $DIC["objDefinition"];
1611
1612 if ($a_is_lang_var) {
1613 if ($obj_definition->isPlugin($a_context_obj_type)) {
1614 return ilObjectPlugin::lookupTxtById($a_context_obj_type, $a_content);
1615 }
1616 $lng->loadLanguageModule($a_context_obj_type);
1617 return $lng->txt($a_content);
1618 }
1619
1620 return $a_content;
1621 }
1622
1627 public static function getFirstNewsIdForContext(
1628 int $a_context_obj_id,
1629 string $a_context_obj_type,
1630 int $a_context_sub_obj_id = 0,
1631 string $a_context_sub_obj_type = ""
1632 ): int {
1633 global $DIC;
1634
1635 $ilDB = $DIC->database();
1636
1637 // Determine how many rows should be deleted
1638 $query = "SELECT id " .
1639 "FROM il_news_item " .
1640 "WHERE " .
1641 "context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1642 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1643 " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1644 " AND " . $ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true);
1645
1646 $set = $ilDB->query($query);
1647 $rec = $ilDB->fetchAssoc($set);
1648
1649 return (int) ($rec["id"] ?? 0);
1650 }
1651
1656 public static function getLastNewsIdForContext(
1657 int $a_context_obj_id,
1658 string $a_context_obj_type,
1659 int $a_context_sub_obj_id = 0,
1660 string $a_context_sub_obj_type = "",
1661 bool $a_only_today = false
1662 ): int {
1663 global $DIC;
1664
1665 $ilDB = $DIC->database();
1666
1667 // Determine how many rows should be deleted
1668 $query = "SELECT id, update_date " .
1669 "FROM il_news_item " .
1670 "WHERE " .
1671 "context_obj_id = " . $ilDB->quote($a_context_obj_id, "integer") .
1672 " AND context_obj_type = " . $ilDB->quote($a_context_obj_type, "text") .
1673 " AND context_sub_obj_id = " . $ilDB->quote($a_context_sub_obj_id, "integer") .
1674 " AND " . $ilDB->equals("context_sub_obj_type", $a_context_sub_obj_type, "text", true) .
1675 " ORDER BY update_date DESC";
1676
1677 $ilDB->setLimit(1, 0);
1678 $set = $ilDB->query($query);
1679 $id = 0;
1680 if ($rec = $ilDB->fetchAssoc($set)) {
1681 $id = (int) $rec["id"];
1682 if ($a_only_today) {
1683 $now = ilUtil::now();
1684 if (strpos($rec["update_date"], substr($now, 0, 10)) !== 0) {
1685 $id = 0;
1686 }
1687 }
1688 }
1689
1690 return $id;
1691 }
1692
1693
1698 public static function _lookupMediaObjectUsages(int $a_mob_id): array
1699 {
1700 global $DIC;
1701
1702 $ilDB = $DIC->database();
1703
1704 $query = "SELECT id " .
1705 "FROM il_news_item " .
1706 "WHERE " .
1707 " mob_id = " . $ilDB->quote($a_mob_id, "integer");
1708
1709 $usages = [];
1710 $set = $ilDB->query($query);
1711 while ($rec = $ilDB->fetchAssoc($set)) {
1712 $usages[$rec["id"]] = ["type" => "news", "id" => $rec["id"]];
1713 }
1714
1715 return $usages;
1716 }
1717
1722 public static function _lookupContextObjId(int $a_news_id): int
1723 {
1724 global $DIC;
1725
1726 $ilDB = $DIC->database();
1727
1728 $query = "SELECT context_obj_id " .
1729 "FROM il_news_item " .
1730 "WHERE " .
1731 " id = " . $ilDB->quote($a_news_id, "integer");
1732 $set = $ilDB->query($query);
1733 $rec = $ilDB->fetchAssoc($set);
1734
1735 return $rec["context_obj_id"];
1736 }
1737
1741 public static function _lookupDefaultPDPeriod(): int
1742 {
1743 $news_set = new ilSetting("news");
1744 $per = $news_set->get("pd_period");
1745 if ((int) $per === 0) {
1746 $per = 30;
1747 }
1748
1749 return $per;
1750 }
1751
1755 public static function _lookupUserPDPeriod(int $a_user_id): int
1756 {
1757 $news_set = new ilSetting("news");
1758 $allow_shorter_periods = $news_set->get("allow_shorter_periods");
1759 $allow_longer_periods = $news_set->get("allow_longer_periods");
1760 $default_per = self::_lookupDefaultPDPeriod();
1761
1763 "pdnews",
1764 "news_pd_period",
1765 $a_user_id,
1766 0
1767 );
1768
1769 // news period information
1770 if ($per <= 0 ||
1771 (!$allow_shorter_periods && ($per < $default_per)) ||
1772 (!$allow_longer_periods && ($per > $default_per))
1773 ) {
1774 $per = $default_per;
1775 }
1776
1777 return (int) $per;
1778 }
1779
1783 public static function _lookupRSSPeriod(): int
1784 {
1785 $news_set = new ilSetting("news");
1786 $rss_period = $news_set->get("rss_period");
1787 if ((int) $rss_period === 0) { // default to two weeks
1788 $rss_period = 14;
1789 }
1790 return $rss_period;
1791 }
1792
1796 public static function setPrivateFeedId(int $a_userId): void
1797 {
1798 self::$privFeedId = $a_userId;
1799 }
1800
1804 public static function getPrivateFeedId(): int
1805 {
1806 return self::$privFeedId;
1807 }
1808
1813 public function deliverMobFile(
1814 string $a_purpose = "Standard",
1815 bool $a_increase_download_cnt = false
1816 ): bool {
1817 $mob = $this->getMobId();
1818 $mob = new ilObjMediaObject($mob);
1819 $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
1820
1821 // check purpose
1822 if (!$mob->hasPurposeItem($a_purpose)) {
1823 return false;
1824 }
1825
1826 $m_item = $mob->getMediaItem($a_purpose);
1827 if ($m_item->getLocationType() !== "Reference") {
1828 $this->media_manager->deliverEntry($mob->getId(), "/" . $m_item->getLocation());
1829 if ($a_increase_download_cnt) {
1830 $this->increaseDownloadCounter();
1831 }
1832 $this->main_tpl->setOnScreenMessage('failure', "File not found!", true);
1833 return false;
1834 }
1835
1836 if ($a_increase_download_cnt) {
1837 $this->increaseDownloadCounter();
1838 }
1839
1840 ilUtil::redirect($m_item->getLocation());
1841 return true;
1842 }
1843
1848 public function increaseDownloadCounter(): void
1849 {
1850 $ilDB = $this->db;
1851
1852 $cnt = $this->getMobDownloadCounter();
1853 $cnt++;
1854 $this->setMobDownloadCounter($cnt);
1855 $ilDB->manipulate(
1856 "UPDATE il_news_item SET " .
1857 " mob_cnt_download = " . $ilDB->quote($cnt, "integer") .
1858 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
1859 );
1860 }
1861
1867 public function increasePlayCounter(): void
1868 {
1869 $ilDB = $this->db;
1870
1871 $cnt = $this->getMobPlayCounter();
1872 $cnt++;
1873 $this->setMobPlayCounter($cnt);
1874 $ilDB->manipulate(
1875 "UPDATE il_news_item SET " .
1876 " mob_cnt_play = " . $ilDB->quote($cnt, "integer") .
1877 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
1878 );
1879 }
1880}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
News Item DTO for transfer of news items.
Definition: NewsItem.php:29
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)
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)
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 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 _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
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