aboutsummaryrefslogtreecommitdiff
path: root/lib/models/BlubberThread.php
blob: 4c3b5851dc4116a723e4aabab47959f315517eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
<?php
/**
 * BlubberThread
 * Model class for BlubberThreads
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Rasmus Fuhse <fuhse@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.5
 *
 * @property string $id alias column for thread_id
 * @property string $thread_id database column
 * @property string $context_type database column
 * @property string $context_id database column
 * @property string $user_id database column
 * @property int $external_contact database column
 * @property string|null $content database column
 * @property string|null $display_class database column
 * @property int $visible_in_stream database column
 * @property int $commentable database column
 * @property JSONArrayObject|null $metadata database column
 * @property int|null $chdate database column
 * @property int|null $mkdate database column
 * @property SimpleORMapCollection|BlubberComment[] $comments has_many BlubberComment
 * @property SimpleORMapCollection|BlubberMention[] $mentions has_many BlubberMention
 * @property SimpleORMapCollection|ObjectUserVisit[] $visits has_many ObjectUserVisit
 * @property User $user belongs_to User
 */

class BlubberThread extends SimpleORMap implements PrivacyObject
{
    /**
     * Configures this model.
     *
     * @param array $config Configuration array
     */
    protected static function configure($config = [])
    {
        $config['db_table'] = 'blubber_threads';

        $config['has_many']['comments'] = [
            'class_name' => BlubberComment::class,
            'on_store'   => 'store',
            'on_delete'  => 'delete',
            'order_by'   => 'ORDER BY mkdate ASC'
        ];
        $config['has_many']['mentions'] = [
            'class_name' => BlubberMention::class,
            'on_store'   => 'store',
            'on_delete'  => 'delete',
        ];
        $config['belongs_to']['user'] = [
            'class_name'        => User::class,
            'foreign_key'       => 'user_id',
            'assoc_foreign_key' => 'user_id',
        ];
        $config['has_many']['visits'] = [
            'class_name'        => ObjectUserVisit::class,
            'assoc_foreign_key' => 'object_id',
            'on_delete'         => 'delete',
        ];

        $config['serialized_fields']['metadata'] = JSONArrayObject::class;

        parent::configure($config);
    }

    /**
     * Recognizes mentions in blubber as @username or @"Firstname lastname"
     * and turns them into usual studip-links. The mentioned person is notified by
     * sending a message to him/her as a side-effect.
     * @param array $matches
     * @return string
     */
    public function mention($matches)
    {
        $username = stripslashes(mb_substr($matches[0], 1));
        if ($username[0] !== '"') {
            $user = User::findByUsername($username);
        } else {
            $name = mb_substr($username, 1, -1); // Strip quotes
            $user = User::findOneBySQL("CONCAT(Vorname, ' ', Nachname) = ?", [$name]);
        }
        if ($user
            && !$this->isNew()
            && $user->getId()
            && $user->getId() !== $GLOBALS['user']->id
        ) {
            if ($this['context_type'] === 'private') {
                $mention = new BlubberMention();
                $mention['thread_id'] = $this->getId();
                $mention['user_id'] = $user->getId();
                $mention->store();
            } elseif ($this['context_type'] === 'public') {
                PersonalNotifications::add(
                    $user->getId(),
                    $this->getURL(),
                    sprintf(_('%s hat Sie in einem Blubber erwähnt.'), get_fullname()),
                    'blubberthread_' . $this->getId(),
                    Icon::create('blubber'),
                    true
                );
            }
            $oldbase = URLHelper::setBaseURL($GLOBALS['ABSOLUTE_URI_STUDIP']);
            $url = URLHelper::getLink('dispatch.php/profile', ['username' => $user->username]);
            URLHelper::setBaseURL($oldbase);

            return '[' . $user->getFullName() . ']' . $url . ' ';
        }

        return $matches[0];
    }

    /**
     * @return BlubberThread[]
     */
    public static function findBySQL($sql, $params = [])
    {
        return parent::findAndMapBySQL(function ($thread) {
            return self::upgradeThread($thread);
        }, $sql, $params);
    }

    /**
     * @return BlubberThread|null
     */
    public static function find($id)
    {
        return self::upgradeThread(parent::find($id));
    }

    /**
     * Checks if a BlubberThread has a display_class and returns an instance of
     * display_class with the same data. Otherwise returns BlubberThread.
     * @param BlubberThread|boolean $thread : instance of BlubberThread or false
     * @return BlubberThread|boolean
     */
    public static function upgradeThread($thread)
    {
        if ($thread
            && $thread['display_class']
            && $thread['display_class'] !== 'BlubberThread'
            && is_subclass_of($thread['display_class'], 'BlubberThread')
        ) {
            $class = $thread['display_class'];
            $display_thread = $class::buildExisting($thread->toRawArray());
            return $display_thread;
        }

        return $thread;
    }

    /**
     * @param string $limit      optional; limits the number of results
     * @param string $since      optional; selects threads after this date (exclusive)
     * @param string $olderthan  optional; selects threads before this date (exclusive)
     * @param string $user_id    optional; use this ID instead of $GLOBALS['user']->id
     * @param string $search     optional; filters the threads by a search string
     *
     * @return array  an array of the user's global BlubberThreads
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public static function findMyGlobalThreads($limit = 51, $since = null, $olderthan = null, string $user_id = null, $search = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;

        $condition = "LEFT JOIN blubber_comments
                        ON blubber_comments.thread_id = blubber_threads.thread_id
                      WHERE (blubber_threads.content IS NULL OR blubber_threads.content = '')
                        AND blubber_comments.comment_id IS NULL
                        AND (display_class IS NULL OR display_class = 'BlubberThread')
                        AND UNIX_TIMESTAMP() - blubber_threads.mkdate > 60 * 60";
        self::deleteBySQL($condition);



        $query = SQLQuery::table('blubber_threads');
        $query->join('my_comments', 'blubber_comments', 'blubber_threads.thread_id = my_comments.thread_id AND my_comments.user_id = :user_id', 'LEFT JOIN');
        $query->join('blubber_mentions', 'blubber_mentions', 'blubber_mentions.thread_id = blubber_threads.thread_id', 'LEFT JOIN');

        if (!$GLOBALS['perm']->have_perm('admin', $user_id)) {
            //user, autor, tutor, dozent
            $query->where('mycourses', implode(' OR ', [
                "(blubber_threads.context_type = 'public' AND (my_comments.comment_id IS NOT NULL OR blubber_threads.user_id = :user_id OR blubber_threads.thread_id = 'global'))",
                "(blubber_threads.context_type = 'course' AND blubber_threads.context_id IN (:seminar_ids))",
                "(blubber_threads.context_type = 'institute' AND blubber_threads.context_id IN (:institut_ids))",
                "(blubber_threads.context_type = 'private' AND blubber_mentions.user_id = :user_id AND blubber_mentions.external_contact = 0)",
            ]), [
                'seminar_ids'  => self::getMyBlubberCourses($user_id),
                'institut_ids' => self::getMyBlubberInstitutes($user_id),
            ]);
        } elseif (!$GLOBALS['perm']->have_perm('root', $user_id)) {
            //admin
            $query->where('mycourses', implode(' OR ', [
                "(blubber_threads.context_type = 'public' AND (my_comments.comment_id IS NOT NULL OR blubber_threads.user_id = :user_id OR blubber_threads.thread_id = 'global'))",
                "(blubber_threads.context_type = 'institute' AND blubber_threads.context_id IN (:institut_ids))",
                "(blubber_threads.context_type = 'private' AND blubber_mentions.user_id = :user_id AND blubber_mentions.external_contact = 0)",
            ]), ['institut_ids' => self::getMyBlubberInstitutes($user_id)]);
        } else {
            //root
            $query->where(implode(' OR ', [
                "(blubber_threads.context_type IN ('public', 'course', 'institute') AND (my_comments.comment_id IS NOT NULL OR blubber_threads.user_id = :user_id OR blubber_threads.thread_id = 'global'))",
                "(blubber_threads.context_type = 'private' AND blubber_mentions.user_id = :user_id AND blubber_mentions.external_contact = '0')",
            ]));
        }
        $query->where("blubber_threads.visible_in_stream = 1");
        $query->parameter('user_id', $user_id);
        $query->groupBy('blubber_threads.thread_id');

        $thread_ids = $query->fetchAll("thread_id");

        $threads = [];

        do {
            list($newthreads, $filtered, $new_since, $new_olderthan) = self::getOrderedThreads(
                $thread_ids,
                $limit - count($threads),
                $since,
                $olderthan,
                $user_id,
                $search
            );

            if ($since) {
                $since = max($since, $new_since);
            }
            if ($olderthan) {
                $olderthan = min($olderthan, $new_olderthan);
            } else {
                $olderthan = $new_olderthan;
            }
            $threads = array_merge($threads, $newthreads);
        } while ($filtered && $limit);

        return $threads;
    }

    /**
     * This method is used to get the ordered (upgraded) threads. Because a thread is also able to
     * manage its own visibility and not only pure SQL, we need to execute
     * @param $thread_ids
     * @param string $limit      optional; limits the number of results
     * @param string $since      optional; selects threads after this date (exclusive)
     * @param string $olderthan  optional; selects threads before this date (exclusive)
     * @param string $user_id    optional; use this ID instead of $GLOBALS['user']->id
     * @param string $search     optional; filters the threads by a search string
     * @return array
     */
    protected static function getOrderedThreads($thread_ids, $limit = 51, $since = null, $olderthan = null, string $user_id = null, $search = null)
    {
        $query = SQLQuery::table('blubber_threads')->join(
            'blubber_comments',
            'blubber_comments', 'blubber_threads.thread_id = blubber_comments.thread_id',
            'LEFT JOIN'
        );

        $query->where(
            "filter_thread_ids",
            "blubber_threads.thread_id IN (:thread_ids)",
            ['thread_ids' => $thread_ids]
        );
        if ($search !== null) {
            $query->where(
                "search",
                "(blubber_threads.content LIKE :search OR blubber_comments.content LIKE :search)",
                ['search' => '%' . $search . '%']
            );
        }
        if ($since !== null) {
            $query->where(
                'since',
                '(blubber_comments.mkdate > :since OR blubber_threads.mkdate > :since)',
                compact('since')
            );
        }
        $query->groupBy('blubber_threads.thread_id');
        if ($olderthan !== null) {
            $query->having(
                'olderthan',
                "IFNULL(MAX(blubber_comments.mkdate), blubber_threads.mkdate) < :olderthan",
                ['olderthan' => $olderthan]
            );
        }
        $query->orderBy("MAX(blubber_comments.mkdate) DESC, blubber_threads.mkdate DESC");
        $query->limit($limit);

        $threads = $query->fetchAll(static::class);

        $upgraded_threads = array_map(function ($thread) {
            return self::upgradeThread($thread);
        }, $threads);

        $since = 0;
        $olderthan = time();
        foreach ($upgraded_threads as $thread) {
            $active_time = $thread->getLatestActivity(true);
            $since = max($since, $active_time);
            $olderthan = min($olderthan, $active_time);
        }

        $old_count = count($upgraded_threads);

        $upgraded_threads = array_filter($upgraded_threads, function ($thread) use ($user_id) {
            return $thread->isVisibleInStream() && $thread->isReadable($user_id);
        });

        return [$upgraded_threads, $old_count !== count($upgraded_threads), $since, $olderthan];
    }

    /**
     * @param string $institut_id  the ID of an institute
     * @param string $only_in_stream  optional; filter threads by `visible_in_stream`
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     */
    public static function findByInstitut($institut_id, $only_in_stream = false, string $user_id = null)
    {
        return self::findByContext($institut_id, $only_in_stream, 'institute', $user_id);
    }

    /**
     * @param string $seminar_id  the ID of a course
     * @param string $only_in_stream  optional; filter threads by `visible_in_stream`
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     */
    public static function findBySeminar($seminar_id, $only_in_stream = false, string $user_id = null)
    {
        return self::findByContext($seminar_id, $only_in_stream, 'course', $user_id);
    }

    /**
     * @param string $seminar_id  the ID of a course
     * @param string $only_in_stream  optional; filter threads by `visible_in_stream`
     * @param string $context_type  optional; filter threads by `context_type`
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     */
    public static function findByContext($context_id, $only_in_stream = false, $context_type = 'course', string $user_id = null)
    {
        if (!BlubberThread::findOneBySQL("context_type = :type AND context_id = :context_id AND visible_in_stream = '1' AND content IS NULL AND display_class IS NULL", ['context_id' => $context_id, 'type' => $context_type])) {
            //create the default-thread for this context
            $coursethread = new BlubberThread();
            $coursethread['user_id'] = $user_id ?? $GLOBALS['user']->id;
            $coursethread['external_contact'] = 0;
            $coursethread['context_type'] = $context_type;
            $coursethread['context_id'] = $context_id;
            $coursethread['visible_in_stream'] = 1;
            $coursethread['commentable'] = 1;
            $coursethread->store();
        }
        $query = SQLQuery::table('blubber_threads')
            ->join('blubber_comments', 'blubber_comments', 'blubber_threads.thread_id = blubber_comments.thread_id', 'LEFT JOIN');
        if ($only_in_stream) {
            $query->where("blubber_threads.visible_in_stream = 1");
        }
        $query->where("context", "blubber_threads.context_type = :context_type AND blubber_threads.context_id = :context_id", [
            'context_id' => $context_id,
            'context_type' => $context_type
        ]);
        $query->groupBy('blubber_threads.thread_id');
        $query->orderBy("IFNULL(MAX(blubber_comments.mkdate), blubber_threads.mkdate) DESC");

        $threads = $query->fetchAll(static::class);

        $threads = array_map(function ($thread) {
            return self::upgradeThread($thread);
        }, $threads);
        $threads = array_filter($threads, function ($t) use ($user_id){
            return $t->isVisibleInStream() && $t->isReadable($user_id);
        });
        return $threads;
    }

    /**
     * Export available blubber threads of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $sorm = self::findBySQL("user_id = ? AND external_contact = '0'", [$storage->user_id]);
        if ($sorm) {
            $field_data = [];
            foreach ($sorm as $row) {
                $field_data[] = $row->toRawArray();
            }
            if ($field_data) {
                $storage->addTabularData(_('Blubber-Threads'), 'blubberthreads', $field_data);
            }
        }
    }

    public function getName()
    {
        if ($this['context_type'] === 'public') {
            return sprintf(_('Blubber von %s'), $this->user ? $this->user->getFullName() : _('unbekannt'));
        }

        if ($this['context_type'] === 'private') {
            $query = "SELECT IFNULL(external_users.name, CONCAT(auth_user_md5.Vorname, ' ', auth_user_md5.Nachname)) AS name
                      FROM blubber_mentions
                      LEFT JOIN auth_user_md5
                        ON blubber_mentions.user_id = auth_user_md5.user_id
                           AND blubber_mentions.external_contact = 0
                      LEFT JOIN external_users
                        ON external_users.external_contact_id = blubber_mentions.user_id
                           AND blubber_mentions.external_contact = 1
                      WHERE blubber_mentions.thread_id = :thread_id
                        AND blubber_mentions.user_id != :me
                      ORDER BY name";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                'thread_id' => $this->getId(),
                'me'        => $GLOBALS['user']->id,
            ]);
            $names = $statement->fetchFirst();
            $names = array_map(function ($name) {
                return $name ?? _('unbekannt');
            }, $names);

            $names[] = _('ich');
            $names = implode(', ', $names);
            return mb_substr($names, 0, 60);
        }

        if ($this['context_type'] === 'course') {
            if ($this['content']) {
                return mb_substr((string) Course::find($this['context_id'])->name . ': ' . $this['content'], 0, 50) . ' ...';
            } else {
                if ($course = Course::find($this['context_id'])) {
                    return (string) $course->name;
                } else {
                    return _('unbekannt');
                }
            }
        }

        if ($this['context_type'] === 'institute') {
            if ($this['content']) {
                return mb_substr((string) Institute::find($this['context_id'])->name . ': ' . $this['content'], 0, 50) . ' ...';
            } else {
                return (string) Institute::find($this['context_id'])->name;
            }
        }

        return _('Ein mysteröser Blubber');
    }

    public function getContentTemplate()
    {
        $template = $GLOBALS['template_factory']->open('blubber/thread_content');
        $template->thread = $this;
        return $template;
    }

    /**
     * Returns a template (or null) to display this in the context container
     */
    public function getContextTemplate()
    {
        if ($this['context_type'] === 'course') {
            $course = Course::find($this['context_id']);
            $icons = [];
            $schedule_active = false;
            foreach ($course->tools as $tool) {
                if ($module = $tool->getStudipModule()) {
                    $last_visit = object_get_visit($this['context_id'], $module->getPluginId());
                    $nav = $module->getIconNavigation($this['context_id'], $last_visit, $GLOBALS['user']->id);
                    if (
                        isset($nav)
                        && $nav->isVisible(true)
                        && $module->getTabNavigation($this['context_id'])
                        && $GLOBALS['perm']->have_studip_perm($tool->getVisibilityPermission(), $this['context_id'])
                    ) {
                        $icons[] = $nav;
                    }
                    if ($module instanceof CoreSchedule) {
                        $schedule_active = true;
                    }
                }
            }

            $nextdate = false;
            if ($schedule_active) {
                $nextdate = CourseDate::findOneBySQL("range_id = ? AND `date` >= UNIX_TIMESTAMP() ORDER BY `date` ASC", [$this['context_id']]);
            }

            $teachers       = CourseMember::findBySQL("Seminar_id = ? AND status = 'dozent' ORDER BY position ASC", [$this['context_id']]);
            $tutors         = CourseMember::findBySQL("Seminar_id = ? AND status = 'tutor' ORDER BY position ASC", [$this['context_id']]);
            $students_count = CourseMember::countBySQL("Seminar_id = ? AND status IN ('autor', 'user') ORDER BY position ASC", [$this['context_id']]);

            $template = $GLOBALS['template_factory']->open('blubber/course_context');
            $template->thread         = $this;
            $template->course         = $course;
            $template->icons          = $icons;
            $template->nextdate       = $nextdate;
            $template->teachers       = $teachers;
            $template->tutors         = $tutors;
            $template->students_count = $students_count;
            $template->hashtags       = $this->getHashtags();
            $template->unfollowed     = !$this->isFollowedByUser();
            return $template;
        }

        if ($this['context_type'] === 'private') {
            $query = "SELECT *
                      FROM blubber_mentions
                      LEFT JOIN auth_user_md5
                        ON blubber_mentions.user_id = auth_user_md5.user_id
                           AND blubber_mentions.external_contact = 0
                      LEFT JOIN external_users
                        ON blubber_mentions.user_id = external_users.external_contact_id
                           AND blubber_mentions.external_contact = 1
                      WHERE thread_id = ?
                      ORDER BY IFNULL(external_users.name, CONCAT(auth_user_md5.Vorname, ' ', auth_user_md5.Nachname))";
            $mentions = DBManager::get()->prepare($query);
            $mentions->execute([$this->getId()]);

            $template = $GLOBALS['template_factory']->open('blubber/private_context');
            $template->thread = $this;
            $template->mentions = $mentions->fetchAll(PDO::FETCH_ASSOC);
            return $template;
        }

        if ($this['context_type'] === 'public') {
            $template = $GLOBALS['template_factory']->open('blubber/public_context');
            $template->thread = $this;
            return $template;
        }

        if ($this['context_type'] === 'institute') {
            $template = $GLOBALS['template_factory']->open('blubber/institute_context');
            $template->thread = $this;
            $template->institute = Institute::find($this['context_id']);
            $template->unfollowed = !$this->isFollowedByUser();
            return $template;
        }
    }

    /**
     * Lets a user follow a thread
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     */
    public function addFollowingByUser($user_id = null)
    {
        $query = "DELETE FROM `blubber_threads_followstates`
                  WHERE `thread_id` = :thread_id
                    AND `user_id` = :user_id";
        DBManager::get()->execute($query, [
            ':thread_id' => $this->id,
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);
    }

    /**
     * Lets a user unfollow a thread
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     */
    public function removeFollowingByUser($user_id = null)
    {
        $query = "REPLACE INTO `blubber_threads_followstates`
                  VALUES (:thread_id, :user_id, 'unfollowed', UNIX_TIMESTAMP())";
        DBManager::get()->execute($query, [
            ':thread_id' => $this->id,
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);
    }

    /**
     * Returns whether a user follows a thread.
     *
     * @param string|null $user_id Id of the user (optional, defaults to current user
     * @return bool
     */
    public function isFollowedByUser($user_id = null)
    {
        $query = "SELECT 1
                  FROM `blubber_threads_followstates`
                  WHERE `thread_id` = :thread_id
                    AND `user_id` = :user_id
                    AND `state` = 'unfollowed'";
        $unfollowed = (bool) DBManager::get()->fetchColumn($query, [
            ':thread_id' => $this->id,
            ':user_id'   => $user_id ?? $GLOBALS['user']->id,
        ]);

        return !$unfollowed;
    }

    public function getOpenGraphURLs()
    {
        return OpenGraph::extract($this['content']);
    }

    public function getLatestActivity(bool $include_mkdate = false)
    {
        $newest_comment = BlubberComment::findOneBySQL("thread_id = ? ORDER BY mkdate DESC", [$this->getId()]);
        if ($newest_comment) {
            return $newest_comment->mkdate;
        }
        return $include_mkdate ? $this->mkdate : null;
    }

    public function getURL()
    {
        if (($this['context_type'] === "course") || ($this['context_type'] === "institute")) {
            return URLHelper::getURL('dispatch.php/course/messenger/course/' . $this->getId(), ['cid' => $this['context_id']]);
        }
        return URLHelper::getURL('dispatch.php/blubber/index/' . $this->getId());
    }

    /**
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function getLastVisit(string $user_id = null)
    {
        return object_get_visit(
            $this->id,
            $this->getBlubberPluginId(),
            '',
            '',
            $user_id ?? User::findCurrent()->id
        );
    }

    /**
     * Sets the last visit timestamp for this thread
     *
     * @param string|null $user_id
     */
    public function setLastVisit(string $user_id = null): void
    {
        object_set_visit(
            $this->id,
            $this->getBlubberPluginId(),
            $user_id ?? User::findCurrent()->id
        );
    }

    /**
     * Returns the id of the blubber plugin.
     *
     * @return int Id of the plugin
     */
    protected function getBlubberPluginId(): int
    {
        $plugin_info = PluginManager::getInstance()->getPluginInfo(Blubber::class);
        return (int) $plugin_info['id'];

    }

    public function notifyUsersForNewComment($comment)
    {
        $data = $this->getNotificationUsersQueryAndParameters();

        if ($data === false) {
            return;
        }

        $query = "SELECT user_id, `preferred_language` AS language
                  FROM `user_info`
                  WHERE `user_id` IN (
                      {$data['query']}
                  )";

        $statement = DBManager::get()->prepare($query);
        foreach ($data['parameters'] as $key => $value) {
            $statement->bindValue($key, $value);
        }
        $statement->execute();
        $statement->setFetchMode(PDO::FETCH_ASSOC);

        $notifications = [];
        foreach ($statement as $row) {
            $user_id  = $row['user_id'];
            $language = $row['language'] ?? Config::get()->DEFAULT_LANGUAGE;

            if (!isset($notifications[$language])) {
                setTempLanguage(false, $language);

                $notifications[$language] = PersonalNotifications::create([
                    'url'     => $this->getURL(),
                    'text'    => sprintf(_('%s hat eine Nachricht geschrieben.'), get_fullname()),
                    'avatar'  => Icon::create('blubber')->asImagePath(),
                    'dialog'  => true,
                    'html_id' => "blubberthread_{$this->id}",
                ]);

                restoreLanguage();
            }

            $notifications[$language]->link($user_id);
        }
    }

    /**
     * Returns an array that includes the query and parameters to retrieve the
     * user ids of all users that should be notified by a new post in this
     * thread.
     *
     * The array needs to have the following structure:
     *
     * [
     *     'query' => ...,
     *     'parameters' => ...
     * ]
     *
     * @return array|false
     */
    protected function getNotificationUsersQueryAndParameters()
    {
        // Default set of parameters
        $parameters = [
            ':thread_id' => $this->id,
            ':user_id'   =>  $GLOBALS['user']->id,
        ];

        // Public context: Notify all users that participated
        if ($this->context_type === 'public') {
            $query = "SELECT DISTINCT `user_id`
                      FROM `blubber_comments`
                      WHERE `thread_id` = :thread_id
                          AND `external_contact` = 0
                          AND `user_id` != :user_id";

            if (!$this->external_contact && $this->user_id !== $GLOBALS['user']->id) {
                $query .= " UNION SELECT '{$this->user_id}' AS `user_id`";
            }

            return compact('query', 'parameters');
        }

        // Private context: Notify all mentioned users
        if ($this->context_type === 'private') {
            $query = "SELECT user_id
                      FROM blubber_mentions
                      WHERE thread_id = :thread_id
                        AND external_contact = 0
                        AND user_id != :user_id";

            return compact('query', 'parameters');
        }

        // Course context: Notify all members of the course except the ones that
        // turned the notifications off
        if ($this->context_type === 'course') {
            $query = "SELECT seminar_user.user_id
                      FROM seminar_user
                      LEFT JOIN blubber_threads_followstates ON (
                          seminar_user.user_id = blubber_threads_followstates.user_id
                          AND blubber_threads_followstates.thread_id = :thread_id
                          AND blubber_threads_followstates.state = 'unfollowed'
                      )
                      WHERE seminar_user.Seminar_id = :context_id
                          AND seminar_user.user_id != :user_id
                          AND blubber_threads_followstates.user_id IS NULL";

            $parameters[':context_id'] = $this->context_id;

            return compact('query', 'parameters');
        }

        // Institute context: Notify all members of the institute
        if ($this->context_type === 'institute') {
            $query = "SELECT user_inst.user_id
                      FROM user_inst
                      LEFT JOIN blubber_threads_followstates ON (
                          user_inst.user_id = blubber_threads_followstates.user_id
                          AND blubber_threads_followstates.thread_id = :thread_id
                          AND blubber_threads_followstates.state = 'unfollowed'
                      )
                      WHERE Institut_id = :context_id
                          AND user_inst.user_id != :user_id
                          AND blubber_threads_followstates.user_id IS NULL";

            $parameters[':context_id'] = $this->context_id;

            return compact('query', 'parameters');
        }

        return false;
    }

    public function isVisibleInStream()
    {
        return $this['visible_in_stream'];
    }

    /**
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function isWritable(string $user_id = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;
        if ($this['context_type'] === 'course' || $this['context_type'] === 'institute') {
            return $GLOBALS['perm']->have_studip_perm('tutor', $this['context_id'], $user_id);
        } else {
            return $GLOBALS['perm']->have_perm('root', $user_id) || $this['user_id'] === $user_id;
        }
    }

    /**
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function isReadable(string $user_id = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;
        if ($this['context_type'] === 'public') {
            return true;
        }

        if ($this['context_type'] === 'private') {
            $query = "SELECT 1
                      FROM blubber_mentions
                      WHERE thread_id = :thread_id
                        AND user_id = :me
                        AND external_contact = 0";
            return (bool) DBManager::get()->fetchColumn($query, [
                'me'        => $user_id,
                'thread_id' => $this->getId()
            ]);
        }

        if (in_array($this['context_type'], ['course', 'institute'])) {
            return $GLOBALS['perm']->have_studip_perm('user', $this['context_id'], $user_id);
        }

        return false;
    }

    /**
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     */
    public function isCommentable(string $user_id = null)
    {
        return $this->isReadable($user_id) && $this['commentable'];
    }

    public function getAvatar()
    {
        if ($this['context_type'] === 'course') {
            return CourseAvatar::getAvatar($this['context_id'])->getURL(Avatar::MEDIUM);
        }

        if ($this['context_type'] === 'institute') {
            return InstituteAvatar::getAvatar($this['context_id'])->getURL(Avatar::MEDIUM);
        }

        if ($this['context_type'] === 'private') {
            $query = "SELECT user_id, external_contact
                      FROM blubber_mentions
                      WHERE thread_id = ?";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([$this->getId()]);
            $mentions = $statement->fetchAll(PDO::FETCH_ASSOC);

            if (count($mentions) === 1) {
                return Avatar::getAvatar($mentions[0]['user_id'])->getURL(Avatar::MEDIUM);
            }

            if (count($mentions) === 2 && $mentions[0]['user_id'] === $GLOBALS['user']->id && !$mentions[0]['external_contact']) {
                return Avatar::getAvatar($mentions[1]['user_id'])->getURL(Avatar::MEDIUM);
            }

            if (count($mentions) === 2 && $mentions[1]['user_id'] === $GLOBALS['user']->id && !$mentions[1]['external_contact']) {
                return Avatar::getAvatar($mentions[0]['user_id'])->getURL(Avatar::MEDIUM);
            }

            return Icon::create('group3')->asImagePath();
        }

        if ($this['context_type'] === 'public') {
            return Icon::create('globe')->asImagePath();
        }

        return CourseAvatar::getNobody()->getURL(Avatar::MEDIUM);
    }

    public function getJSONData($limit_comments = 50, $user_id = null, $search = null)
    {
        $user_id || $user_id = $GLOBALS['user']->id;
        $output = [
            'thread_posting'  => $this->toRawArray(),
            'context_info'    => '',
            'comments'        => [],
            'more_up'         => 0,
            'more_down'       => 0,
            'unseen_comments' => BlubberComment::countBySQL("thread_id = ? AND mkdate >= ? AND user_id != ?", [
                $this->getId(),
                $this->getLastVisit(),
                $user_id
            ]),
            'notifications' => $this->mayDisableNotifications(),
            'followed' => $this->isFollowedByUser(),
        ];
        $context_info = $this->getContextTemplate();
        if ($context_info) {
            $output['context_info'] = $context_info->render();
        }
        $output['thread_posting']['name'] = $this->getName();
        $output['thread_posting']['user_name'] = $this->user ? $this->user->getFullName() : _("unbekannt");
        $output['thread_posting']['user_username'] = $this->user ? $this->user['username'] : "";
        $output['thread_posting']['avatar'] = Avatar::getAvatar($this['user_id'])->getURL(Avatar::MEDIUM);
        $output['thread_posting']['html'] = $this->getContentTemplate()->render();
        $output['thread_posting']['writable'] = $this->isWritable() ? 1 : 0;
        $output['thread_posting']['chdate'] = (int) $output['thread_posting']['chdate'];
        $output['thread_posting']['mkdate'] = (int) $output['thread_posting']['mkdate'];

        if ($search) {
            $query = "SELECT blubber_comments.*
                      FROM blubber_comments
                      WHERE blubber_comments.thread_id = :thread_id
                          AND content LIKE :search
                      ORDER BY mkdate DESC";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                'thread_id' => $this->getId(),
                'search'    => '%' . $search . '%'
            ]);
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        } else {
            $query = "SELECT blubber_comments.*
                      FROM blubber_comments
                      WHERE blubber_comments.thread_id = :thread_id
                      ORDER BY mkdate DESC
                      LIMIT :limit";
            $statement = DBManager::get()->prepare($query);
            $statement->execute([
                'thread_id' => $this->getId(),
                'limit' => $limit_comments + 1,
            ]);
            $result = $statement->fetchAll(PDO::FETCH_ASSOC);
            if (count($result) > $limit_comments) {
                $output['more_up'] = 1;
            }
        }

        foreach ($result as $data) {
            $comment = BlubberComment::buildExisting($data);
            $output['comments'][] = $comment->getJSONData();
        }

        return $output;
    }

    /**
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function markAsRead(string $user_id = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;

        $statement = DBManager::get()->prepare("
            UPDATE personal_notifications_user
                INNER JOIN personal_notifications USING (personal_notification_id)
            SET personal_notifications_user.seen = '1'
            WHERE personal_notifications_user.user_id = :user_id
                AND personal_notifications.html_id = :html_id
        ");
        $statement->execute([
            'user_id' => $user_id,
            'html_id' => "blubberthread_".$this->getId()
        ]);

        $this->setLastVisit($user_id);
    }

    public function getHashtags($since = null)
    {
        $query = "
            SELECT *
            FROM blubber_comments
            WHERE thread_id = ".DBManager::get()->quote($this->getId())."
                AND content REGEXP '(^|[[:blank:]]|[[:cntrl:]])#[[:graph:]]' > 0
        ";
        if ($since) {
            $get_hashtags = DBManager::get()->query($query ."
                    AND mkdate > ".DBManager::get()->quote($since)."
            ");
        } else {
            $get_hashtags = DBManager::get()->query($query);
        }
        $hashtags = [];
        foreach ($get_hashtags->fetchAll(PDO::FETCH_ASSOC) as $comment_data) {
            $matched = preg_match_all(
                '/'. BlubberFormat::REGEXP_HASHTAG . '/uS',
                $comment_data['content'],
                $matches
            );

            if ($matched === 0) {
                continue;
            }

            foreach ($matches[1] as $tag) {
                if (!isset($hashtags[mb_strtolower($tag)])) {
                    $hashtags[mb_strtolower($tag)] = 0;
                }
                $hashtags[mb_strtolower($tag)] += 1;
            }
        }
        asort($hashtags);
        return array_reverse($hashtags);
    }

    /**
     * Returns all Seminar_ids to courses I am member of and in which blubber
     * is an active plugin.
     *
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @return array of string : array of Seminar_ids
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    protected static function getMyBlubberCourses(string $user_id = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;
        if ($GLOBALS['perm']->have_perm('admin', $user_id)) {
            return [];
        }

        $is_deputy = Config::get()->DEPUTIES_ENABLE && Deputy::countByUser_id($user_id) > 0;
        $blubber_plugin_info = PluginManager::getInstance()->getPluginInfo('Blubber');

        $parameters = [
            'me'                => $user_id,
            'blubber_plugin_id' => $blubber_plugin_info['id'],
        ];

        $query = "SELECT seminar_user.Seminar_id
                  FROM seminar_user
                  INNER JOIN tools_activated
                    ON plugin_id = :blubber_plugin_id
                       AND tools_activated.range_id = seminar_user.Seminar_id
                  WHERE seminar_user.user_id = :me";

        $my_courses = DBManager::get()->fetchFirst($query, $parameters);

        if ($is_deputy) {
            $query = "SELECT deputies.range_id
                      FROM deputies
                      INNER JOIN tools_activated
                    ON plugin_id = :blubber_plugin_id
                       AND tools_activated.range_id = deputies.range_id
                  WHERE deputies.user_id = :me";
            $my_courses = array_merge(
                $my_courses,
                DBManager::get()->fetchFirst($query, $parameters)
            );
        }
        return $my_courses;
    }

    /**
     * @param ?string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @return array
     *
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    protected static function getMyBlubberInstitutes(string $user_id = null)
    {
        $user_id = $user_id ?? $GLOBALS['user']->id;
        if ($GLOBALS['perm']->have_perm('root', $user_id)) {
            return [];
        }

        $query = "SELECT Institut_id
                  FROM user_inst
                  WHERE user_id = ?";
        $institut_ids = DBManager::get()->fetchFirst($query, [$user_id]);
        $blubberplugin = PluginManager::getInstance()->getPlugin(Blubber::class);
        if (!$blubberplugin) {
            return [];
        }

        foreach ($institut_ids as $index => $institut_id) {
            if (!PluginManager::getInstance()->isPluginActivated($blubberplugin->getPluginId(), $institut_id)) {
                unset($institut_ids[$index]);
            }
        }
        return $institut_ids;
    }

    /**
     * Returns whether the notifications for this thread may be disabled.
     *
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     * @return bool
     */
    public function mayDisableNotifications(string $user_id = null): bool
    {
        // Notifications may always be disabled for global blubber stream
        if ($this->id === 'global') {
            return true;
        }

        // Notifications may not be disabled outside of course and institute
        // streams
        if (!in_array($this->context_type, ['course', 'institute'])) {
            return false;
        }

        // Only users with permission below admin may disable the notifications.
        $user_id = $user_id ?? $GLOBALS['user']->id;

        return !$GLOBALS['perm']->have_perm('admin', $user_id);
    }

    /**
     * Count all unseen comments of this thread.
     *
     * @param string $user_id  optional; use this ID instead of $GLOBALS['user']->id
     *
     */
    public function countUnseenComments(string $user_id = null): int
    {
        return \BlubberComment::countBySQL(
            'thread_id = ? AND mkdate >= ?',
            [
                $this->getId(),
                $this->getLastVisit($user_id ?? $GLOBALS['user']->id) ?: object_get_visit_threshold(),
            ]
        );
    }
}