aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/BlockTypes/Folder.php
blob: 9f186dfd092898cc7f9b85f47d36bb8766ef8be7 (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
<?php

namespace Courseware\BlockTypes;

/**
 * This class represents the content of a Courseware folder block.
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 */
class Folder extends BlockType
{
    public static function getType(): string
    {
        return 'folder';
    }

    public static function getTitle(): string
    {
        return _('Dateiordner');
    }

    public static function getDescription(): string
    {
        return _('Stellt einen Ordner aus dem Dateibereich zur Verfügung.');
    }

    public function initialPayload(): array
    {
        return [
            'type' => '', // store folder type to detect changes
            'folder_id' => '',
            'title' => '',
        ];
    }

    /**
     * Returns the decoded payload of the block associated with this instance.
     *
     * @return mixed the decoded payload
     */
    public function getPayload()
    {
        $user = \User::findCurrent();
        $payload = $this->decodePayloadString($this->block['payload']);

        $folder = \Folder::find($payload['folder_id']);
        $payload['folder-type'] = null;
        $payload['files'] = [];

        if ($folder) {
            $typedFolder = $folder->getTypedFolder();
            $payload['folder-type'] = $typedFolder->folder_type;
            if ($typedFolder->isReadable($user->id) || $typedFolder->folder_type === 'HomeworkFolder') {   
                foreach ($typedFolder->getFiles() as $folderFile) {
                    $file['id'] = $folderFile->id;
                    $file['attributes'] = [
                        'name'          => $folderFile->name,
                        'mime-type'     => $folderFile->mime_type,
                        'filesize'      => (int) $folderFile->size,
                        'mkdate'        => date('c', $folderFile->mkdate),
                    ];
                    $file['relationships'] = [
                        'owner' => [
                            'data' => ['type' => 'users', 'id' => $folderFile->user_id],
                            'meta' => ['name' => $folderFile->getFileRef()->getAuthorName()]
                            ]
                    ];
                    $file['meta'] = [
                        'download-url'  => $folderFile->getDownloadURL(),
                    ];
        
                    if ($this->filePermission($typedFolder, $file, $user)) {
                        array_push($payload['files'], $file);
                    }
                }
            }
        }

        return $payload;
    }

    private function filePermission($typedFolder, $file, $user): bool
    {
        return $typedFolder->folder_type !== 'HomeworkFolder' || $user->id === $file['relationships']['owner']['data']['id'] || $typedFolder->isReadable($user->id);
    }


    /**
     * get all files related to this block.
     *
     * @return \FileRef[] list of file references realted to this block
     */
    public function getFiles(): array
    {
        $payload = $this->getPayload();
        if (!$payload['folder_id']) {
            return [];
        }

        return \FileRef::findByFolder_id($payload['folder_id']);
    }

    public function copyPayload(string $rangeId = ''): array
    {
        $payload = $this->getPayload();

        if ('' != $payload['folder_id']) {
            $payload['folder_id'] = $this->copyFolderById($payload['folder_id'], $rangeId);
        }

        return $payload;
    }

    public static function getJsonSchema(): string
    {
        $schemaFile = __DIR__.'/Folder.json';
        return file_get_contents($schemaFile);
    }

    public static function getCategories(): array
    {
        return ['files'];
    }

    public static function getContentTypes(): array
    {
        return ['files'];
    }

    public static function getFileTypes(): array
    {
        return ['all'];
    }

    public static function getTags(): array
    {
        return [
            _('Hausaufgaben'), _('Upload'), _('Download'), _('Datei'), _('hochladen'),
            _('herunterladen'), _('einstellen'), 'pdf', 'ppt', 'doc', 'xls',
            'odt', 'ods', 'odp', 'PowerPoint', 'Word', 'Excel', 'LibreOffice',
            'OpenOffice', _('Ordner'), _('Dokument'), _('Link'), _('verlinken'), _('Skript'),
            _('Material'), _('Zusatz'), 'zip', _('Mappe'), _('Sammlung'), _('Sicherung'),
            _('Literatur'), _('Zusatzmaterial'), _('Semesterapparat'), _('Infomaterial')
        ];
    }
}