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

/**
 * @property BlubberThread[] $elements
 */
class BlubberThreadsWidget extends SidebarWidget
{
    protected $active_thread = null;
    protected $with_composer = false;

    /**
     * @param BlubberThread $thread
     */
    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()
            ]);

            $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();
    }
}