aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/course/files.php
blob: b0cce5e9e59d5efda5c28bf6e568965af25f1d62 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
 * files.php - controller to display files in a course
 *
 * 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       4.0
 */
class Course_FilesController extends AuthenticatedController
{
    protected $allow_nobody = true;

    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        checkObject();
        $this->studip_module = checkObjectModule('documents');
        if (!Context::isCourse()) {
            throw new CheckObjectException(_('Es wurde keine passende Veranstaltung gefunden.'));
        }
        $this->course = Context::get();
        object_set_visit_module($this->studip_module->getPluginId());
        $this->last_visitdate = object_get_visit($this->course->id, $this->studip_module->getPluginId());

        PageLayout::setHelpKeyword('Basis.Dateien');
        PageLayout::setTitle(Context::get()->getFullname() . ' - ' . _('Dateien'));

        Navigation::activateItem('/course/files');

        if (is_object($GLOBALS['user']) && $GLOBALS['user']->id !== 'nobody') {
            $constraints = FileManager::getUploadTypeConfig($this->course->id);

            PageLayout::addHeadElement('script', ['type' => 'text/javascript'], sprintf(
                'STUDIP.Files.setUploadConstraints(%s);',
                json_encode($constraints)
            ));
        }
    }

    private function buildSidebar($index = 'index')
    {
        $sidebar = Sidebar::get();

        $actions = new ActionsWidget();

        if ($this->topFolder->isEditable($GLOBALS['user']->id) && $this->topFolder->parent_id) {
            $actions->addLink(
                _("Ordner bearbeiten"),
                $this->url_for("file/edit_folder/".$this->topFolder->getId()),
                Icon::create("edit", "clickable"),
                ['data-dialog' => 1]
            );
        }

        if ($this->topFolder && $this->topFolder->isSubfolderAllowed($GLOBALS['user']->id)) {
            $actions->addLink(
                _('Neuer Ordner'),
                URLHelper::getUrl('dispatch.php/file/new_folder/' . $this->topFolder->getId()),
                Icon::create('folder-empty+add', 'clickable')
            )->asDialog();

        }
        if ($this->topFolder && $this->topFolder->isWritable($GLOBALS['user']->id)) {
            $actions->addLink(
                _('Dokument hinzufügen'),
                '#',
                Icon::create('file+add', 'clickable'),
                ['onclick' => "STUDIP.Files.openAddFilesWindow(); return false;"]
            );
        }
        if (Feedback::isActivated() && Feedback::hasCreatePerm($this->course->id)) {
            $actions->addLink(
                _('Neues Feedback-Element'),
                $this->url_for('course/feedback/create_form/' . $this->topFolder->getId() . '/Folder'),
                Icon::create('star+add')
            )->asDialog();

        }

        $actions->addLink(
            _('Bildergalerie öffnen'),
            '#g',
            Icon::create('file-pic'),
            [
                'onClick' => "STUDIP.Files.openGallery(); return false;",
                'style'   => 'display: none',
            ]
        );

        $sidebar->addWidget($actions);

        if ($this->topFolder->isWritable($GLOBALS['user']->id)) {
            $uploadArea = new SidebarWidget();
            $uploadArea->setTitle(_("Dateien hochladen"));
            $uploadArea->addElement(new WidgetElement(
                    $this->render_template_as_string('files/upload-drag-and-drop'))
            );
            $sidebar->addWidget($uploadArea);
        }



        $views = new ViewsWidget();
        $views->addLink(
            _('Ordneransicht'),
            $this->url_for('course/files/index'),
            null,
            [],
            'index'
        )->setActive(true);
        $views->addLink(
            _('Alle Dateien'),
            $this->url_for('course/files/flat'),
            null,
            [],
            'flat'
        );

        $sidebar->addWidget($views);
    }

    /**
     * Displays the files in tree view
     **/
    public function index_action($topFolderId = '')
    {
        $this->marked_element_ids = [];

        if (!$topFolderId) {
            $folder = Folder::findTopFolder($this->course->id);
        } else {
            $folder = Folder::find($topFolderId);
        }

        if (!$folder) {
            PageLayout::postError(_('Der gewählte Ordner wurde nicht gefunden.'));
            $this->relocate($this->indexURL());
            return;
        }

        $this->topFolder = $folder->getTypedFolder();

        if (!$this->topFolder->isVisible($GLOBALS['user']->id) || $this->topFolder->range_id !== $this->course->id) {
            throw new AccessDeniedException();
        }
        $this->buildSidebar('index');
        $this->render_template('files/index.php', $this->layout);
    }

    /**
     * Displays the files in flat view
     **/
    public function flat_action()
    {
        $sidebar = Sidebar::get();

        $actions = new ActionsWidget();
        $actions->addLink(
            _('Neue Dateien herunterladen'),
            $this->url_for('course/files/newest_files'),
            Icon::create('download', 'clickable'),
            ['cid' => $this->course->id]
        );
        $actions->addLink(
            _('Bildergalerie öffnen'),
            '#g',
            Icon::create('file-pic'),
            [
                'onClick' => "STUDIP.Files.openGallery(); return false;",
                'style'   => 'display: none',
            ]
        );
        if ($GLOBALS['user']->id !== 'nobody') {
            $sidebar->addWidget($actions);
        }

        $this->marked_element_ids = [];

        $folder = Folder::findTopFolder($this->course->id);
        if (!$folder) {
            throw new Exception(_('Fehler beim Laden des Hauptordners!'));
        }

        $this->topFolder = $folder->getTypedFolder();

        //find all files in all subdirectories:
        list($this->files, $this->folders) = array_values(FileManager::getFolderFilesRecursive($this->topFolder, $GLOBALS['user']->id));

        $this->range_type = 'course';
        $this->show_default_sidebar = true;
        $this->form_action = $this->url_for('file/bulk/' . $folder->getId());
        $this->enable_table_filter = true;
        $this->render_template('files/flat.php', $this->layout);
    }

    /**
     * Packs all files in this course which are considered new
     * to the current user into a ZIP file and send it to the user.
     */
    public function newest_files_action()
    {
        $user = User::findCurrent();

        //Get the course's top folder:
        $folder = Folder::findTopFolder($this->course->id);
        if (!$folder) {
            throw new Exception(_('Fehler beim Laden des Hauptordners!'));
        }

        $this->top_folder = $folder->getTypedFolder();

        //Get all files of the course:
        list($files, $folders) = array_values(
            FileManager::getFolderFilesRecursive(
                $this->top_folder,
                $user->id,
                true
            )
        );

        $last_visitdate = $this->last_visitdate;

        //Only select those files which are newer than the last visit date:
        $relevant_file_refs = [];

        foreach ($files as $file_ref) {
            if ($file_ref->chdate > $this->last_visitdate) {
                $relevant_file_refs[] = $file_ref;
            }
        }

        if (!$relevant_file_refs) {
            //There are no new files: Display an info message
            //and return to the flat file view of the course:
            PageLayout::postInfo(
                _('Es sind keine neuen Dateien in dieser Veranstaltung verfügbar!')
            );

            $this->redirect(
                'course/files/flat',
                [
                    'cid' => $this->course->id
                ]
            );
            return;
        }

        $tmp_file = tempnam($GLOBALS['TMP_PATH'], 'doc');

        //Create a ZIP archive from all relevant files the user may see:
        $archive = FileArchiveManager::createArchiveFromFileRefs(
            $relevant_file_refs,
            $user,
            $tmp_file,
            true
        );

        if ($archive) {
            //ZIP file has been created successfully.
            //Redirect to the ZIP file download URL:
            $this->redirect(
                FileManager::getDownloadURLForTemporaryFile(
                    basename($tmp_file),
                    $this->top_folder->name . '.zip'
                )
            );
        } else {
            throw new Exception('Error while creating ZIP archive!');
        }
    }
}