aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/topics.php
blob: 36e25ecd851f551c8b0d25ecc2f6349a5b853ba6 (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
<?php

class Course_TopicsController extends AuthenticatedController
{
    protected $allow_nobody = true;
    protected $_autobind = true;

    public function before_filter(&$action, &$args)
    {
        PageLayout::setHelpKeyword('Basis/InVeranstaltungAblauf');

        parent::before_filter($action, $args);

        checkObject();
        checkObjectModule("schedule");

        Navigation::activateItem('/course/schedule/topics');
        PageLayout::setTitle(sprintf('%s - %s', Course::findCurrent()->getFullName(), _("Themen")));

        $course = Course::findCurrent();
        $this->forum_activated = $course->isToolActive(CoreForum::class);
        $this->documents_activated = $course->isToolActive(CoreDocuments::class);

        if ($action !== 'index' && !$GLOBALS['perm']->have_studip_perm('tutor', Context::getId())) {
            throw new AccessDeniedException();
        }

        $this->setupSidebar($action);
    }

    public function index_action()
    {
        $this->topics = CourseTopic::findBySeminar_id(Context::getId());
        $this->topic_links = $this->createLinksForTopics($this->topics);
        $this->cancelled_dates_locked = LockRules::Check(Context::getId(), 'cancelled_dates');
    }

    public function delete_action(CourseTopic $topic)
    {
        if (!Request::isPost()) {
            throw new MethodNotAllowedException();
        }

        if ($topic->seminar_id && ($topic->seminar_id !== Context::getId())) {
            throw new AccessDeniedException();
        }

        if ($topic->delete()) {
            PageLayout::postSuccess(_('Thema gelöscht.'));
        }

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

    public function edit_action(CourseTopic $topic = null)
    {
        PageLayout::setTitle($topic->isNew() ? _('Neues Thema erstellen') : sprintf(_('Bearbeiten: %s'), $topic->title));

        $this->dates = CourseDate::findBySeminar_id(Context::getId());
    }

    public function store_action(CourseTopic $topic = null)
    {
        if (!Request::isPost()) {
            throw new MethodNotAllowedException();
        }

        if ($topic->seminar_id && ($topic->seminar_id !== Context::getId())) {
            throw new AccessDeniedException();
        }

        $topic->title         = Request::i18n("title");
        $topic->description   = Request::i18n('description', null, function ($string) {
            return Studip\Markup::purifyHtml($string);

        });
        $topic->paper_related = Request::bool('paper_related', false);
        if ($topic->isNew()) {
            $topic->seminar_id = Context::getId();
        }
        $topic->store();

        //change dates for this topic
        $former_date_ids = $topic->dates->pluck('termin_id');
        $new_date_ids = array_keys(Request::getArray('date'));
        foreach (array_diff($former_date_ids, $new_date_ids) as $delete_termin_id) {
            $topic->dates->unsetByPk($delete_termin_id);
        }
        foreach (array_diff($new_date_ids, $former_date_ids) as $add_termin_id) {
            $date = CourseDate::find($add_termin_id);
            if ($date) {
                $topic->dates[] = $date;
            }
        }
        $topic->store();

        if (Request::bool('folder')) {
            $topic->connectWithDocumentFolder();
        }

        // create a connection to the module forum (can be anything)
        // will update title and description automagically
        if (Request::bool('forumthread')) {
            $topic->connectWithForumThread();
        }

        PageLayout::postSuccess(_('Thema gespeichert.'));
        $this->redirect($this->indexURL(['open' => $topic->id]));
    }

    public function swap_action(CourseTopic $a, CourseTopic $b)
    {
        if (!Request::isPost()) {
            throw new MethodNotAllowedException();
        }

        if (
            $a->seminar_id !== Context::getId()
            || $b->seminar_id !== Context::getId()
        ) {
            throw new Exception(_('Eines oder mehrere Themen gehören nicht zur ausgewählten Veranstaltung.'));
        }

        [$a->priority, $b->priority] = [$b->priority, $a->priority];

        $a->store();
        $b->store();

        $this->redirect($this->indexURL(['open' => $a->id]));
    }

    public function allow_public_action()
    {
        $config = CourseConfig::get(Context::getId());
        $config->store('COURSE_PUBLIC_TOPICS', !$config->COURSE_PUBLIC_TOPICS);
        $this->redirect("course/topics");
    }

    public function copy_action()
    {
        if (Request::submitted("copy")) {
            $prio = 1;
            foreach (Course::find(Context::getId())->topics as $topic) {
                $prio = max($prio, $topic['priority']);
            }
            foreach (Request::getArray("topic") as $topic_id => $value) {
                $topic = new CourseTopic($topic_id);
                $topic = clone $topic;
                $topic['seminar_id'] = Context::getId();
                $topic['priority'] = $prio;
                $prio++;
                $topic->setId($topic->getNewId());
                $topic->setNew(true);
                $topic->store();

                NotificationCenter::postNotification('TopicDidCopy', $topic_id, $topic->id);
            }
            PageLayout::postMessage(MessageBox::success(sprintf(_("%s Themen kopiert."), count(Request::getArray("topic")))));
            $this->redirect("course/topics");
        }
        $semester_sql = " CONCAT('(',IFNULL(GROUP_CONCAT(DISTINCT semester_data.name ORDER BY semester_data.beginn SEPARATOR '-'),'" . _('unbegrenzt') . "'),')')";
        if ($GLOBALS['perm']->have_perm("root")) {
            $this->courseSearch = new SQLSearch("
                SELECT seminare.Seminar_id, CONCAT_WS(' ', seminare.VeranstaltungsNummer, seminare.name, $semester_sql, '(', COUNT(issue_id), ')')
                FROM seminare
                LEFT JOIN semester_courses ON (seminare.Seminar_id = semester_courses.course_id)
                LEFT JOIN semester_data ON (semester_data.semester_id = semester_courses.semester_id)
                INNER JOIN themen ON themen.seminar_id = seminare.Seminar_id
                WHERE seminare.VeranstaltungsNummer LIKE :input OR seminare.name LIKE :input
                GROUP BY seminare.Seminar_id
                ORDER BY semester_data.beginn DESC, seminare.VeranstaltungsNummer ASC, seminare.name ASC
                ",
                _("Veranstaltung suchen"),
                "seminar_id"
            );
        } elseif ($GLOBALS['perm']->have_perm("admin")) {
            $this->courseSearch = new SQLSearch("
                SELECT seminare.Seminar_id, CONCAT_WS(' ', seminare.VeranstaltungsNummer, seminare.name, $semester_sql, '(', COUNT(issue_id), ')')
                FROM seminare
                    INNER JOIN seminar_inst ON (seminare.Seminar_id = seminar_inst.seminar_id)
                    INNER JOIN user_inst ON (user_inst.Institut_id = seminar_inst.institut_id)
                    LEFT JOIN semester_courses ON (seminare.Seminar_id = semester_courses.course_id)
                    LEFT JOIN semester_data ON (semester_data.semester_id = semester_courses.semester_id)
                    INNER JOIN themen ON themen.seminar_id = seminare.Seminar_id

                WHERE seminare.VeranstaltungsNummer LIKE :input OR seminare.name LIKE :input
                    AND user_inst.user_id = ".DBManager::get()->quote($GLOBALS['user']->id)."
                    AND user_inst.inst_perms = 'admin'
                GROUP BY seminare.Seminar_id
                ORDER BY semester_data.beginn DESC, seminare.VeranstaltungsNummer ASC, seminare.name ASC
                ",
                _("Veranstaltung suchen"),
                "seminar_id"
            );
        } else {
            $this->courseSearch = new SQLSearch("
                SELECT seminare.Seminar_id, CONCAT_WS(' ', seminare.VeranstaltungsNummer, seminare.name, $semester_sql, '(', COUNT(issue_id), ')')
                FROM seminare
                    INNER JOIN seminar_user ON (seminare.Seminar_id = seminar_user.Seminar_id)
                    LEFT JOIN semester_courses ON (seminare.Seminar_id = semester_courses.course_id)
                    LEFT JOIN semester_data ON (semester_data.semester_id = semester_courses.semester_id)
                    INNER JOIN themen ON themen.seminar_id = seminare.Seminar_id
                WHERE seminare.VeranstaltungsNummer LIKE :input OR seminare.name LIKE :input
                    AND seminar_user.status IN ('tutor', 'dozent')
                    AND seminar_user.user_id = ".DBManager::get()->quote($GLOBALS['user']->id)."
                GROUP BY seminare.Seminar_id
                ORDER BY semester_data.beginn DESC, seminare.VeranstaltungsNummer ASC, seminare.name ASC
                ",
                _("Veranstaltung suchen"),
                "seminar_id"
            );
        }
        PageLayout::setTitle(_("Themen aus Veranstaltung kopieren"));
    }

    public function fetch_topics_action()
    {
        $this->topics = CourseTopic::findBySeminar_id(Request::option("seminar_id"));
        $output = [
            'html' => $this->render_template_as_string("course/topics/_topiclist.php")
        ];
        $this->render_json($output);
    }

    private function setupSidebar($action)
    {
        $sidebar = Sidebar::get();

        $actions = $sidebar->addWidget(new ActionsWidget());
        if ($action === 'index') {
            $actions->addLink(
                _('Alle Themen aufklappen'),
                $this->url_for('course/topics/show'),
                Icon::create('arr_1down'),
                ['onclick' => "jQuery('table.withdetails > tbody > tr:not(.details):not(.open) > :first-child a').click(); return false;"]
            );
            $actions->addLink(
                _('Alle Themen zuklappen'),
                $this->url_for('course/topics/hide'),
                Icon::create('arr_1right'),
                ['onclick' => "jQuery('table.withdetails > tbody > tr:not(.details).open > :first-child a').click(); return false;"]
            );
        }
        if ($GLOBALS['perm']->have_studip_perm('tutor', Context::getId())) {
            $actions->addLink(
                _('Neues Thema erstellen'),
                $this->url_for('course/topics/edit'),
                Icon::create('add')
            )->asDialog();
            $actions->addLink(
                _('Themen aus Veranstaltung kopieren'),
                $this->url_for('course/topics/copy'),
                Icon::create('clipboard')
            )->asDialog();
        }

        if ($GLOBALS['perm']->have_studip_perm('tutor', Context::getId())) {
            $options = $sidebar->addWidget(new OptionsWidget());
            $options->addCheckbox(
                _('Themen öffentlich einsehbar'),
                (bool) CourseConfig::get(Context::getId())->COURSE_PUBLIC_TOPICS,
                $this->url_for('course/topics/allow_public')
            );
        }
    }

    private function createLinksForTopics(array $topics): array
    {
        $links = array_combine(
            array_column($topics, 'id'),
            array_fill(0, count($topics), ['previous' => null, 'next' => null])
        );

        $last = null;
        foreach ($topics as $topic) {
            if ($last !== null) {
                $links[$topic->id]['previous'] = $last;
            }
            $last = $topic;
        }

        $next = null;
        foreach (array_reverse($topics) as $topic) {
            if ($next !== null) {
                $links[$topic->id]['next'] = $next;
            }
            $next = $topic;
        }

        return $links;
    }
}