aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/forum/topics.php
blob: b6a1307974ed6cc418b97cffa46e1038af45403c (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
<?php
require_once 'BaseController.php';

use Forum\Category;
use Forum\Subscription;
use Forum\Topic;

class Course_Forum_TopicsController extends Forum\BaseController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        unset($_SESSION['forum'][$this->range_id]['search_filter']);

        Navigation::activateItem('course/forum/topics');
    }

    public function index_action()
    {
        $this->render_vue_app(
            Studip\VueApp::create('forum/topics/Index')
        );
    }

    public function show_action($topic_id)
    {
        $topic = Topic::find($topic_id);

        if (!$topic) {
            throw new AccessDeniedException();
        }

        PageLayout::setTitle($topic->name);

        $user_subscription = Subscription::findOneBySQL(
            "subject = :subject AND subject_id = :subject_id AND user_id = :user_id",
            [
                'subject' => 'topic',
                'subject_id' => $topic->getId(),
                'user_id' => User::findCurrent()->user_id
            ]
        );

        $this->render_vue_app(
            Studip\VueApp::create('forum/topics/Show')
                ->withProps([
                    'topic' => $topic->transformData(),
                    'category' => $topic->category ? $topic->category->transformData() : [],
                    'user_subscription' => $user_subscription ? $user_subscription->toRawArray() : [],
                    'metadata' => [
                        'postings_count' => (int) $topic->metadata['postings_count'],
                        'users_count' => (int) $topic->metadata['users_count'],
                        'recent_activity' => date('c', $topic->metadata['recent_activity'])
                    ]
                ])
        );
    }

    public function edit_action($topic_id = null)
    {
        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        if ($topic_id) {
            PageLayout::setTitle(_('Thema bearbeiten'));
            $topic = Topic::getCourseTopic($this->range_id, $topic_id);

            if (!$topic) {
                throw new AccessDeniedException();
            }
        } else {
            PageLayout::setTitle(_('Neues Thema anlegen'));
            $topic = new Topic();
            $topic['category_id'] = Request::get('category_id');
        }

        $categories = DBManager::get()->fetchAll(
            "SELECT * FROM `forum_categories` WHERE `range_id` = ? ORDER BY `position` ASC, `mkdate` DESC",
            [$this->range_id]
        );

        $this->render_vue_app(
            Studip\VueApp::create('forum/topics/Edit')
                ->withProps([
                    'topic' => $topic->transformData(),
                    'categories' => $categories
                ])
        );
    }

    public function save_action($topic_id = null)
    {
        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        CSRFProtection::verifyUnsafeRequest();

        if ($topic_id) {
            $topic = Topic::getCourseTopic($this->range_id, $topic_id);
            if (!$topic) {
                throw new AccessDeniedException();
            }
        } else {
            $topic = new Topic();
            $topic->range_id = $this->range_id;
        }

        $category = json_decode(Request::get('category'), true);

        if (empty($category['category_id']) && !empty($category['name'])) {
            $newCategory = Category::create([
                'range_id' => $this->range_id,
                'color' => '#28497C',
                'name' => $category['name']
            ]);

            $category['category_id'] = $newCategory->category_id;
        } else {
            $topic->category_id = null;
        }

        if (!empty($category['category_id'])) {
            $topic->category_id = $category['category_id'];
        }

        $topic->name = Request::get('name');
        $topic->description = Request::get('description');

        $topic->store();

        PageLayout::postSuccess(_('Das Thema wurde gespeichert.'));

        $this->relocate('course/forum/topics/show/' . $topic->topic_id);
    }

    public function delete_action($topic_id)
    {
        if (!$this->is_moderator) {
            throw new AccessDeniedException();
        }

        $topic = Topic::getCourseTopic($this->range_id, $topic_id);

        if (!$topic) {
            throw new AccessDeniedException();
        }

        $topic->delete();

        PageLayout::postSuccess(_('Das Thema wurde gelöscht.'));

        $this->relocate('course/forum/topics');
    }
}