aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/tree.php
blob: 9bde648f4fc48dd52583593a219dc66a6c34b932 (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
<?php

class TreeController extends AuthenticatedController
{
    public function export_csv_action()
    {
        if (!Request::isPost()) {
            throw new MethodNotAllowedException();
        }

        $ids = explode(',', Request::get('courses', ''));
        $courses = Course::findMany($ids);

        $captions = [
            _('Veranstaltungsnummer'),
            _('Name'),
            _('Semester'),
            _('Zeiten'),
            _('Lehrende'),
            _('Bereich')
        ];

        $data = [];
        foreach ($courses as $course) {
            $lecturers = SimpleCollection::createFromArray(
                CourseMember::findByCourseAndStatus($course->id, 'dozent')
            )->orderBy(
                'position, nachname, vorname'
            )->map(
                function($member) { return $member->getUserFullname(); }
            );

            $studyAreaPaths = [];
            foreach ($course->study_areas as $area) {
                $studyAreaPaths[] = $area->getPath(' > ');
            }

            $data[] = [
                $course->veranstaltungsnummer,
                $course->getFullName('type-number-name'),
                $course->getTextualSemester(),
                implode("\n", $course->getAllDatesInSemester()->toStringArray()),
                implode(', ', $lecturers),
                implode("\n", $studyAreaPaths)
            ];
        }

        $tmpname = md5(uniqid('ErgebnisVeranstaltungssuche'));
        if (array_to_csv($data, $GLOBALS['TMP_PATH'] . '/' . $tmpname, $captions)) {
            $this->render_text(FileManager::getDownloadURLForTemporaryFile(
                $tmpname,
                'veranstaltungssuche.csv'
            ));
        } else {
            $this->set_status(400, 'The csv could not be created.');
        }
    }
}