aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/consultation/consultation_controller.php
blob: eba81ff4967ce57ce8f97dd9857e95c1d77e1ab1 (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
<?php
/**
 * Abstract controller for the consultation app.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since   Stud.IP 4.3
 */
abstract class ConsultationController extends AuthenticatedController
{
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (Request::submitted('username')) {
            $this->range = User::findByUsername(Request::username('username'));
        } elseif (Request::submitted('cid')) {
            $this->range = Context::get();
        } else {
            $this->range = $GLOBALS['user']->getAuthenticatedUser();
        }

        if ($this->range instanceof User) {
            URLHelper::addLinkParam('username', $this->range->username);
        } elseif ($this->range instanceof Course || $this->range instanceof Institute) {
            URLHelper::addLinkParam('cid', $this->range->id);
        }

        // Restore request if present
        if (isset($this->flash['request'])) {
            foreach ($this->flash['request'] as $key => $value) {
                Request::set($key, $value);
            }
        }

        // This defines the function to display a note. Not really a partial,
        // not a controller method. This has no real place...
        $this->displayNote = function ($what, $length = 40, $position = 'above') {
            $what = trim($what);
            if (!$what) {
                return '';
            }

            if (mb_strlen($what)  < $length) {
                return '<div class="consultation-note consultation-note-' . $position . '">' . formatLinks($what) . '</div>';
            }

            return sprintf(
                '<div class="consultation-note consultation-note-%s shortened" data-tooltip=\'%s\'>%s&hellip;</div>',
                $position,
                json_encode(['html' => formatLinks($what)]),
                htmlReady(substr($what, 0, $length))
            );
        };
    }

    protected function activateNavigation($path)
    {
        $path = ltrim($path, '/');

        if ($this->range instanceof User) {
            Navigation::activateItem("/profile/consultation/{$path}");
        } elseif ($this->range instanceof Course || $this->range instanceof Institute) {
            Navigation::activateItem("/course/consultation/{$path}");
        } else {
            throw new Exception('Not implemented yet');
        }
    }

    protected function getConsultationTitle()
    {
        return $this->range->getConfiguration()->CONSULTATION_TAB_TITLE;
    }

    protected function keepRequest()
    {
        $this->flash['request'] = Request::getInstance()->getIterator()->getArrayCopy();
    }

    /**
     * @param $block_id
     *
     * @return ConsultationBlock|ConsultationBlock[]
     * @throws AccessDeniedException
     */
    protected function loadBlock($block_id)
    {
        if (is_array($block_id)) {
            return array_map([$this, 'loadBlock'], $block_id);
        }

        $block = ConsultationBlock::find($block_id);

        if (!$block->range->isAccessibleToUser()) {
            throw new AccessDeniedException();
        }

        return $block;
    }

    protected function loadSlot($block_id, $slot_id)
    {
        $block = $this->loadBlock($block_id);
        $slot  = $block->slots->find($slot_id);

        if (!$slot) {
            throw new Exception(_('Dieser Termin existiert nicht'));
        }

        return $slot;
    }

    protected function loadBooking($block_id, $slot_id, $booking_id)
    {
        $slot    = $this->loadSlot($block_id, $slot_id);
        $booking = $slot->bookings->find($booking_id);

        if (!$booking) {
            throw new Exception(_('Diese Buchung existiert nicht'));
        }

        return $booking;
    }
}