Skip to content
Snippets Groups Projects
courseware.php 6.69 KiB
Newer Older
Ron Lucke's avatar
Ron Lucke committed
<?php

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

/**
 * @SuppressWarnings(PHPMD.CamelCaseClassName)
 * @SuppressWarnings(PHPMD.CamelCaseMethodName)
 * @SuppressWarnings(PHPMD.StaticAccess)
 */
Ron Lucke's avatar
Ron Lucke committed
class Admin_CoursewareController extends AuthenticatedController
{
    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
Ron Lucke's avatar
Ron Lucke committed
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);
        $GLOBALS['perm']->check('root');
        PageLayout::setTitle(_('Coursewareverwaltung'));
        Navigation::activateItem('/admin/locations/courseware');
    }

    public function index_action()
    {
        $this->setSidebar();
    }

    public function block_types_action()
    {
        PageLayout::addStyleSheet('courseware.css');

        $views = Sidebar::get()->addWidget(new ViewsWidget());
        $views->addLink(_('Templates'), $this->indexURL());
        $views->addLink(_('Block-Typen'), $this->block_typesURL())->setActive(true);
        $views->addLink(_('Container-Typen'), $this->container_typesURL());

        $this->blockTypes = BlockType::getBlockTypes();

        usort($this->blockTypes, function ($blockTypeA, $blockTypeB) {
            return $blockTypeA::getTitle() <=> $blockTypeB::getTitle();
        });
    }

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

        switch (Request::quoted('bulk_action')) {
            case 'activate':
                return $this->activate_block_types_action();
            case 'deactivate':
                return $this->deactivate_block_types_action();
        }

        PageLayout::postInfo(_("Keine Aktion ausgewählt."));
        $this->redirect($this->action_url('block_types'));
    }

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

        $requestedBlockTypes = $this->validateBlockTypes();
        $changed = array_sum(
            array_map(function ($blockType) {
                return $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('block_types'));
    }

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

        $requestedBlockTypes = $this->validateBlockTypes();
        $changed = array_sum(
            array_map(function ($blockType) {
                return $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('block_types'));
    }

    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 container_types_action()
    {
        PageLayout::addStyleSheet('courseware.css');

        $views = Sidebar::get()->addWidget(new ViewsWidget());
        $views->addLink(_('Templates'), $this->indexURL());
        $views->addLink(_('Block-Typen'), $this->block_typesURL());
        $views->addLink(_('Container-Typen'), $this->container_typesURL())->setActive(true);

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

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

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

        switch (Request::quoted('bulk_action')) {
            case 'activate':
                return $this->activate_container_types_action();
            case 'deactivate':
                return $this->deactivate_container_types_action();
        }

        PageLayout::postInfo(_("Keine Aktion ausgewählt."));
        $this->redirect($this->action_url('container_types'));
    }

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

        $requestedContainerTypes = $this->validateContainerTypes();
        $changed = array_sum(
            array_map(function ($containerType) {
                return $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('container_types'));
    }

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

        $requestedContainerTypes = $this->validateContainerTypes();
        $changed = array_sum(
            array_map(function ($containerType) {
                return $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('container_types'));
    }

    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;
    }

Ron Lucke's avatar
Ron Lucke committed
    private function setSidebar()
    {
        $sidebar = Sidebar::Get();
        $views = new TemplateWidget(
            _('Ansichten'),
            $this->get_template_factory()->open('admin/courseware/admin_view_widget')
        );
        $sidebar->addWidget($views)->addLayoutCSSClass('courseware-admin-view-widget');

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