Newer
Older
<?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',
];

Jan-Hendrik Willms
committed
$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];

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

Jan-Hendrik Willms
committed
/**
* @return BlubberThread|null
*/
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
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')
->join('my_comments', 'blubber_comments', 'blubber_threads.thread_id = my_comments.thread_id', 'LEFT JOIN')
->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.user_id = :user_id 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.user_id = :user_id 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 = 'public' OR blubber_threads.context_type IN ('course', 'institute')) AND (my_comments.user_id = :user_id 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 = [];
foreach ($threads as $thread) {
if ($since) {
$active_time = $thread->getLatestActivity();
$since = max($since, $active_time);
}
if ($olderthan) {
$active_time = $thread->getLatestActivity();
$olderthan = min($olderthan, $active_time);
}
}
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);
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
$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 {
return (string) Course::find($this['context_id'])->name;
}
}
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)
&& count($module->getTabNavigation($this['context_id'])) > 0
&& $GLOBALS['perm']->have_studip_perm($tool->getVisibilityPermission(), $this['context_id'])
) {
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
$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']);

Jan-Hendrik Willms
committed
$template->unfollowed = !$this->isFollowedByUser();
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
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)
{

Jan-Hendrik Willms
committed
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
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'];
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
}
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') {

Jan-Hendrik Willms
committed
$query = "SELECT user_inst.user_id

Jan-Hendrik Willms
committed
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

Jan-Hendrik Willms
committed
AND user_inst.user_id != :user_id
AND blubber_threads_followstates.user_id IS NULL";
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
$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(),

Jan-Hendrik Willms
committed
$this->getLastVisit(),

Jan-Hendrik Willms
committed
'notifications' => $this->mayDisableNotifications(),
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
'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()
]);

Jan-Hendrik Willms
committed
$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)."