aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/archive.php
blob: 63a25944eb2e1624336b115f67b2943e44302009 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

/**
 * archive.php - contains Course_ArchiveController
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.5
 */

require_once('lib/archiv.inc.php'); //needed in archive_action


/**
 * Course_ArchiveController is a controller that allows users
 * which have the required permissions to archive a course.
 */
class Course_ArchiveController extends AuthenticatedController
{

    /**
     * This method checks if the current user has the required
     * permissions to archive a course.
     *
     * @param string courseId The ID of the course that is going to be archived
     * in case the user has sufficent permissions to do so.
     *
     * @return bool True, if the user has the required permissions to archive
     * a course, false otherwise.
     */
    private function userHasPermission($courseId)
    {
        //check permissions: user has to be an administrator of the course:
        $requiredPermission = 'admin';
        if (Config::get()->ALLOW_DOZENT_DELETE) {
            //members of the "dozent" role may also archive the course:
            $requiredPermission = 'dozent';
        }
        return $GLOBALS['perm']->have_studip_perm($requiredPermission, $courseId);
    }


    /**
     * A helper method that creates an HTML table out of an
     * archived course's basic data.
     *
     * The generated HTML table provides basic information about the
     * archived course. It exists for compatibility reasons with public/archiv.php
     * which creates the same output and can be used when the public/archiv.php
     * script is converted to a Trails controller.
     *
     * @param ArchivedCourse course The archived course whose HTML table shall be generated.
     *
     * @return string The HTML code for the table that displays
     */
    private function createArchivedCourseHTMLTable($course = null)
    {
        $table = '<table class="default">'
               . '<caption>' . $course->name . '</caption>'
               . '<tbody>'
               . '<tr><th>' . _('Untertitel') . ':</th><td>' . $course->untertitel . '</td></tr>'
               //. '<tr><th>' . _("Zeit") . ':</th><td>' . INSERT_ZEIT_HERE . '</td></tr>'
               . '<tr><th>' . _('Semester') . ':</th><td>' . $course->start_semester . '</td></tr>' //TODO: check if start_semester is right
               . '<tr><th>' . _('Erster Temin') . ':</th><td>' . $course->untertitel . '</td></tr>'
               //. '<tr><th>' . _("Vorbesprechung") . ':</th><td>' . INSERT_VORBESPRECHUNG_HERE . '</td></tr>'
               . '<tr><th>' . _('Ort') . ':</th><td>' . $course->ort . '</td></tr>'
               . '<tr><th>' . _('Typ der Veranstaltung') . ':</th><td>'
                    . $course->start_semester . '</td></tr>';

        $table .= '</tbody></table>';
        return $table;
    }


    /**
     * This action collects all required data about the course.
     *
     * @return null This method does not return any value.
     */
    public function confirm_action()
    {
        PageLayout::setHelpKeyword('Veranstaltungen.Löschen');

        //check the archiv_sem array and extract the relevant course IDs:
        if (Request::submitted('archiv_sem')) {
            $courseIds = Request::optionArray('archiv_sem');
        } else {
            $courseIds = [Course::findCurrent()->id];
        }

        foreach ($courseIds as $id) {
            //check if the user has the required permission
            //to archive the selected course:
            if (!$this->userHasPermission($id)) {
                //no permission: access denied!
                throw new AccessDeniedException();
            }
        }
        $this->courses = Course::findAndMapMany(function($c) {
            $result = $c->toArray(['id', 'name', 'untertitel', 'ort', 'veranstaltungsnummer']);
            $result['start_semester'] = $c->start_semester->name;
            return $result;
        }, $courseIds, "ORDER BY name");
        //TODO: enable navigation items, depending whether the user
        // is in the admin role or not.

        //check if at least one course was selected:
        if (!$this->courses) {
            //courses not found: display the "no course selected" message
            //from the view.
            return;
        }

        //activate navigation elements if they exist:
        if ($GLOBALS['perm']->have_perm('admin')) {
            if (Navigation::hasItem('/browse/my_courses/list')) {
                Navigation::activateItem('/browse/my_courses/list');
            }
        } else {
            if (Navigation::hasItem('/course/admin/main/archive')) {
                Navigation::activateItem('/course/admin/main/archive');
            }
        }

        //set the page title with the area of Stud.IP:
        PageLayout::setTitle(_('Löschen von Veranstaltungen'));

        //get the list of "dozenten" and the last activity for each course (if any course):
        $this->dozenten = [];
        $this->lastActivities = [];

        foreach ($this->courses as $course) {
            $this->dozenten[$course['id']] = SimpleCollection::createFromArray(CourseMember::findByCourseAndStatus($course['id'], 'dozent'))->toArray(['username', 'vorname', 'nachname']);
            $this->lastActivities[$course['id']] = date('d.m.Y, G:i', lastActivity($course['id']));
        }
    }


    /**
     * This action does the actual archiving of a course.
     *
     * @return null This method does not return any value.
     */
    public function archive_action()
    {
        //now pick the courses IDs:
        $courseIds = Request::optionArray('courseIds');

        //check if the user has the required permission
        //to archive all selected courses:

        $this->deletedCourses = [];
        foreach ($courseIds as $courseId) {
            if (!$this->userHasPermission($courseId)) {
                //no permission for one of the selected courses: access denied!
                throw new AccessDeniedException();
            }


            $course = Course::find($courseId);
            if ($course) {
                $seminar = new Seminar($course);
                $coursename = $course->getFullname();
                if ($seminar->delete()) {
                    $this->deletedCourses[] = $courseId;
                    PageLayout::postSuccess(sprintf(
                        _('Die Veranstaltung %s wurde erfolgreich gelöscht.'),
                        htmlReady($coursename)
                    ));
                }
            } else {
                throw new Exception(_('Veranstaltung nicht in Datenbank gefunden!'));
            }
        }

        if (!empty($this->deletedCourses)) {
            if ($GLOBALS['perm']->have_perm('admin')){
                $this->redirect('admin/courses/index');
            } else {
                $this->redirect(URLHelper::getURL('dispatch.php/my_courses', [], true));
            }
        }

        /*
        // enable the following code when archive.inc.php is replaced

        //get all courses:
        $courses = Course::findMany($courseIds);

        //now create ArchivedCourse objects out of the Course objects:

        foreach ($courses as $course) {
            in_archiv($course->id);
        }
            $archivedCourse = new ArchivedCourse();
            $archivedCourse->id = $course->id;
            $archivedCourse->name = $course->name;
            $archivedCourse->untertitel = $course->untertitel;
            $archivedCourse->beschreibung = $course->beschreibung;
            $archivedCourse->start_time = $course->start_time;
            $archivedCourse->semester = $course->end_semester; //TODO: maybe start_semester is better
            $archivedCourse->heimat_inst_id = $course->home_institut->id;
            $archivedCourse->institute = $course->institutes;

            //get "dozenten":
            $archivedCourse->dozenten = $course->members->filter(
                                function ($member) {
                                    return $member['status'] === "dozent";
                                }
                            );

            $archivedCourse->fakultaet = $course->home_institut->faculty;


            //dump is an HTML table with the seminar data
            $archivedCourse->dump = $this->createArchivedCourseHTMLTable($course);

            //TODO:
            //$archivedCourse->archiv_file_id =
            //$archivedCourse->archiv_protected_file_id =
            $archivedCourse->mkdate = time();
            //$archivedCourse->forumdump =
            //$archivedCourse->wikidump =
            $archivedCourse->studienbereiche = $course->study_areas;
            $archivedCourse->veranstaltungsnummer = $course->veranstaltungsnummer;
            $archivedCourse->members = $course->members;
            $archivedCourse->home_institut = $course->home_institut;
        }
        */
    }
}