aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/Filesystem/PublicFolder.php
blob: 10af85cc7d647cbcddae34d9d1167606767fc91a (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
<?php

namespace Courseware\Filesystem;

use ArrayAccess;
use Courseware\Instance;
use FileType;
use Flexi\Template;
use Folder;
use FolderType;
use Icon;
use MessageBox;
use Request;
use SimpleORMap;
use StandardFolder;

class PublicFolder extends StandardFolder
{
    public static function findOrCreateTopFolder(Instance $instance): PublicFolder
    {
        if (!($folder = self::findTopFolder($instance))) {
            $folder = self::createTopFolder($instance);
        }

        return $folder;
    }

    public static function findTopFolder(Instance $instance): ?PublicFolder
    {
        if ($folder = Folder::findOneByrange_id($instance->getRoot()->id)) {
            return new PublicFolder($folder);
        }

        return null;
    }

    public static function createTopFolder(Instance $instance): PublicFolder
    {
        return new PublicFolder(Folder::createTopFolder($instance->getRoot()->id, 'courseware', PublicFolder::class));
    }

    protected $folder;

    /**
     * {@inheritdoc}
     */
    public function __construct($folder = null)
    {
        $this->folder = $folder instanceof Folder ? $folder : Folder::build($folder);
        $this->folder['folder_type'] = get_class($this);
    }

    /**
     * {@inheritdoc}
     */
    public function __get($attribute)
    {
        return $this->folder[$attribute];
    }

    /**
     * {@inheritdoc}
     */
    public static function getTypeName(): string
    {
        return _('Ein Ordner für öffentlich zugängliche Dateien einer Courseware');
    }

    /**
     * {@inheritdoc}
     */
    public static function availableInRange(SimpleORMap|string $range_id_or_object, string $user_id): bool
    {
        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function getIcon(string $role = Icon::DEFAULT_ROLE): Icon
    {
        return Icon::create('folder-public-full', $role);
    }

    /**
     * {@inheritdoc}
     */
    public function getId(): ?string
    {
        return $this->folder->id;
    }

    /**
     * {@inheritdoc}
     */
    public function isVisible(string $user_id): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isReadable(string $user_id): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isWritable(string $user_id): bool
    {
        // TODO
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isEditable(string $user_id): bool
    {
        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function isSubfolderAllowed(string $user_id): bool
    {
        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function getDescriptionTemplate(): Template|string|null
    {
        return '';
    }

    /**
     * {@inheritdoc}
     */
    public function getSubfolders(): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getParent(): ?FolderType
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function setDataFromEditTemplate(array|ArrayAccess $folderdata): FolderType|MessageBox
    {
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function deleteFile(string $file_ref_id): bool|array
    {
        $fileRefs = $this->folder->file_refs;

        if (is_array($fileRefs)) {
            foreach ($fileRefs as $fileRef) {
                if ($fileRef->id === $file_ref_id) {
                    return $fileRef->delete();
                }
            }
        }

        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function store(): bool
    {
        return $this->folder->store();
    }

    /**
     * {@inheritdoc}
     */
    public function createSubfolder(FolderType $foldertype): ?FolderType
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function deleteSubfolder(string $subfolder_id): bool
    {
        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function delete(): bool
    {
        return $this->folder->delete();
    }

    /**
     * {@inheritdoc}
     */
    public function isFileDownloadable(string $file_ref_id, string $user_id): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isFileEditable(string $file_ref_id, string $user_id): bool
    {
        return false;
    }

    /**
     * {@inheritdoc}
     */
    public function isFileWritable(string $file_ref_id, string $user_id): bool
    {
        return false;
    }
}