aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/BlubberThreadsWidget.php
blob: b02e027e2d14d14017e71d9ebe5ecdf837634198 (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
<?php

class BlubberThreadsWidget extends SidebarWidget
{
    protected $active_thread = null;
    protected $with_composer = false;

    public function addThread($thread)
    {
        $this->elements[] = $thread;
    }

    public function setActive($thread_id)
    {
        $this->active_thread = $thread_id;
    }

    public function withComposer($with = true)
    {
        $this->with_composer = $with;
    }

    public function render($variables = [])
    {
        $template = $GLOBALS['template_factory']->open('blubber/threads-overview.php');
        if (count($this->elements) > 30) {
            array_pop($this->elements);
            $template->more_down = true;
        }

        $json = [];
        foreach ($this->elements as $thread) {
            $unseen_comments = BlubberComment::countBySQL("thread_id = ? AND mkdate >= ?", [
                $thread->getId(),
                $thread->getLastVisit() ?: object_get_visit_threshold()
            ]);

            $json[] = [
                'thread_id' => $thread->getId(),
                'avatar' => $thread->getAvatar(),
                'name' => $thread->getName(),
                'timestamp' => (int) $thread->getLatestActivity(),
                'mkdate' => (int) $thread->mkdate,
                'unseen_comments' => $unseen_comments,
                'notifications' => $thread->id === 'global' || ($thread->context_type === 'course' && !$GLOBALS['perm']->have_perm('admin')),
                'followed' => $thread->isFollowedByUser(),
            ];
        }

        $template->threads = $this->elements;
        $template->with_composer = $this->with_composer;
        $template->json = $json;
        return $template->render();
    }
}