aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/admin/courseware.php
blob: 1af8e8cdb4d78afe1a2379a48f058359b1427bba (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
<?php

use Courseware\BlockTypes\BlockType;
use Courseware\ContainerTypes\ContainerType;

/**
 * @SuppressWarnings(PHPMD.CamelCaseClassName)
 * @SuppressWarnings(PHPMD.CamelCaseMethodName)
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
class Admin_CoursewareController extends AuthenticatedController
{
    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);
        $GLOBALS['perm']->check('root');
    }

    public function index_action()
    {
        PageLayout::setTitle(_('Courseware Vorlagen'));
        Navigation::activateItem('/admin/locations/courseware_templates');
        $this->setIndexSidebar();
    }

    public function elements_action(): void
    {
        PageLayout::setTitle(_('Courseware Inhaltselemente'));
        Navigation::activateItem('/admin/locations/courseware_elements');

        $this->blockTypes = BlockType::getBlockTypes();
        usort($this->blockTypes, fn($blockTypeA, $blockTypeB) => $blockTypeA::getTitle() <=> $blockTypeB::getTitle());

        $this->containerTypes = ContainerType::getContainerTypes();

        usort(
            $this->containerTypes,
            fn($containerTypeA, $containerTypeB) => $containerTypeA::getTitle() <=> $containerTypeB::getTitle()
        );
    }

    public function activate_block_types_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $requestedBlockTypes = $this->validateBlockTypes();
        $changed = array_sum(array_map(fn($blockType) => $blockType::activate() ? 1 : 0, $requestedBlockTypes));

        PageLayout::postSuccess(
            sprintf(
                ngettext('Block-Typ erfolgreich aktiviert.', '%d Block-Typen erfolgreich aktiviert.', $changed),
                $changed
            )
        );
        $this->redirect($this->action_url('elements'));
    }

    public function deactivate_block_types_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $requestedBlockTypes = $this->validateBlockTypes();
        $changed = array_sum(array_map(fn($blockType) => $blockType::deactivate() ? 1 : 0, $requestedBlockTypes));

        PageLayout::postSuccess(
            sprintf(
                ngettext('Block-Typ erfolgreich deaktiviert.', '%d Block-Typen erfolgreich deaktiviert.', $changed),
                $changed
            )
        );
        $this->redirect($this->action_url('elements'));
    }

    private function validateBlockTypes(): iterable
    {
        $requestedBlockTypes = Request::getArray('block_types');
        $diff = array_diff($requestedBlockTypes, BlockType::getBlockTypes());
        if (count($diff)) {
            throw new Trails\Exception(400);
        }

        return $requestedBlockTypes;
    }

    public function activate_container_types_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $requestedContainerTypes = $this->validateContainerTypes();
        $changed = array_sum(
            array_map(fn($containerType) => $containerType::activate() ? 1 : 0, $requestedContainerTypes)
        );

        PageLayout::postSuccess(
            sprintf(
                ngettext('Container-Typ erfolgreich aktiviert.', '%d Container-Typen erfolgreich aktiviert.', $changed),
                $changed
            )
        );
        $this->redirect($this->action_url('elements'));
    }

    /**
     */
    public function deactivate_container_types_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $requestedContainerTypes = $this->validateContainerTypes();
        $changed = array_sum(
            array_map(fn($containerType) => $containerType::deactivate() ? 1 : 0, $requestedContainerTypes)
        );

        PageLayout::postSuccess(
            sprintf(
                ngettext(
                    'Container-Typ erfolgreich deaktiviert.',
                    '%d Container-Typen erfolgreich deaktiviert.',
                    $changed
                ),
                $changed
            )
        );
        $this->redirect($this->action_url('elements'));
    }

    private function validateContainerTypes(): iterable
    {
        $requestedContainerTypes = Request::getArray('container_types');
        $diff = array_diff($requestedContainerTypes, ContainerType::getContainerTypes());
        if (count($diff)) {
            throw new Trails\Exception(400);
        }

        return $requestedContainerTypes;
    }

    private function setIndexSidebar()
    {
        $sidebar = Sidebar::Get();

        $views = new TemplateWidget(
            _('Aktionen'),
            $this->get_template_factory()->open('admin/courseware/admin_action_widget')
        );
        $sidebar->addWidget($views)->addLayoutCSSClass('courseware-admin-action-widget');
    }
}