aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Tree/DetailsOfTreeNodeCourse.php
blob: b3a31efbc5f4add99d10ad21d341d12a8655152a (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
<?php

namespace JsonApi\Routes\Tree;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\NonJsonApiController;

class DetailsOfTreeNodeCourse extends NonJsonApiController
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameters)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        $course = \Course::find($args['id']);
        if (!$course) {
            throw new RecordNotFoundException();
        }

        // Get course dates in textual form
        $dates = $course->getAllDatesInSemester();

        $data = [
            'semester' => $course->semester_text,
            'lecturers' => [],
            'admissionstate' => null,
            'dates' => $dates->toHtml(false, true)
        ];

        // Get lecturers
        $lecturers = \SimpleCollection::createFromArray(
            \CourseMember::findByCourseAndStatus($args['id'], 'dozent')
        )->orderBy('position, nachname, vorname');
        foreach ($lecturers as $l) {
            $data['lecturers'][] = [
                'id' => $l->user_id,
                'username' => $l->username,
                'name' => $l->getUserFullname()
            ];
        }

        // Get admission state indicator if necessary
        if (\Config::get()->COURSE_SEARCH_SHOW_ADMISSION_STATE) {
            switch (\GlobalSearchCourses::getStatusCourseAdmission($course->id, $course->admission_prelim)) {
                case 1:
                    $data['admissionstate'] = [
                        'icon' => 'decline-circle',
                        'role' => \Icon::ROLE_STATUS_YELLOW,
                        'info' => _('Eingeschränkter Zugang')
                    ];
                    break;
                case 2:
                    $data['admissionstate'] = [
                        'icon' => 'decline-circle',
                        'role' => \Icon::ROLE_STATUS_RED,
                        'info' => _('Kein Zugang')
                    ];
                    break;
                default:
                    $data['admissionstate'] = [
                        'icon' => 'check-circle',
                        'role' => \Icon::ROLE_STATUS_GREEN,
                        'info' => _('Uneingeschränkter Zugang')
                    ];
            }

        }

        $response->getBody()->write(json_encode($data));

        return $response->withHeader('Content-type', 'application/json');
    }
}