aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/consultation/overview.php
blob: 8f80d26131d5aeb9e0b47a1dc3c6637c235e83b1 (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
<?php
require_once __DIR__ . '/consultation_controller.php';

/**
 * Overview/Student controller for the consultation app.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.3
 */
class Consultation_OverviewController extends ConsultationController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        PageLayout::setTitle(sprintf(
            '%s: %s',
            $this->getConsultationTitle(),
            $this->range->getFullName()
        ));
    }

    public function index_action($page = 0)
    {
        $this->activateNavigation('overview');
        $this->setupSidebar();

        $this->count = ConsultationBlock::countByRange($this->range);
        $this->limit = Config::get()->ENTRIES_PER_PAGE;

        if ($page >= ceil($this->count / $this->limit)) {
            $page = 0;
        }

        $this->page   = max($page, 0);
        $this->blocks = ConsultationBlock::findbyRange(
            $this->range,
            "ORDER BY start ASC LIMIT " . ($this->page * $this->limit) . ", {$this->limit}"
        );

        $action = $GLOBALS['user']->cfg->CONSULTATION_SHOW_GROUPED ? 'index' : 'ungrouped';
        $this->render_action($action);
    }

    public function booked_action($page = 0)
    {
        $this->activateNavigation('booked');

        $this->slots = ConsultationSlot::findOccupiedSlotsByUserAndRange(
            $GLOBALS['user']->id,
            $this->range
        );
    }

    public function book_action($block_id, $slot_id)
    {
        $this->slot = $this->loadSlot($block_id, $slot_id);

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            if ($this->slot->isOccupied()) {
                PageLayout::postError(_('Dieser Termin ist bereits belegt.'));
            } else {
                $booking = new ConsultationBooking();
                $booking->slot_id = $this->slot->id;
                $booking->user_id = $GLOBALS['user']->id;
                $booking->reason  = trim(Request::get('reason')) ?: null;
                $booking->store();

                PageLayout::postSuccess(_('Der Termin wurde reserviert.'));
            }

            $this->redirect("consultation/overview#block-{$block_id}");
        }
    }

    public function cancel_action($block_id, $slot_id, $from_booked = false)
    {
        $this->slot        = $this->loadSlot($block_id, $slot_id);
        $this->from_booked = $from_booked;

        if (Request::isPost()) {
            CSRFProtection::verifyUnsafeRequest();

            if (!$this->slot->isOccupied($GLOBALS['user']->id)) {
                PageLayout::postError(_('Dieser Termin ist nicht von Ihnen belegt.'));
            } else {
                $booking = $this->slot->bookings->findOneBy('user_id', $GLOBALS['user']->id);

                $booking->cancel(Request::get('reason'));

                PageLayout::postSuccess(_('Der Termin wurde abgesagt.'));
            }

            if ($from_booked) {
                $this->redirect("consultation/overview/booked#block-{$block_id}");
            } else {
                $this->redirect("consultation/overview#block-{$block_id}");
            }
        }
    }

    public function toggle_action($what, $state = null)
    {
        if ($what === 'grouped') {
            $GLOBALS['user']->cfg->store(
                'CONSULTATION_SHOW_GROUPED',
                $state === null ? !$GLOBALS['user']->cfg->CONSULTATION_SHOW_GROUPED : (bool) $state
            );
        }

        $this->redirect('consultation/overview');
    }

    private function setupSidebar()
    {
        $options = Sidebar::get()->addWidget(new OptionsWidget());
        $options->addCheckbox(
            _('Termine gruppiert anzeigen'),
            $GLOBALS['user']->cfg->CONSULTATION_SHOW_GROUPED,
            $this->toggleURL('grouped')
        );
    }
}