diff --git a/lib/models/Courseware/BlockTypes/Gallery.php b/lib/models/Courseware/BlockTypes/Gallery.php index 849e791376108b19ec707d3c6a29212a8b727c0a..9ebc9df064a9578fc7ad89eb38923bc3a1fb3cdf 100644 --- a/lib/models/Courseware/BlockTypes/Gallery.php +++ b/lib/models/Courseware/BlockTypes/Gallery.php @@ -29,6 +29,62 @@ class Gallery extends BlockType return _('Bilder aus einem Ordner im Dateibereich zeigen.'); } + /** + * 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(); + if ($typedFolder->folder_type === 'HiddenFolder' && !$typedFolder->download_allowed) { + return $payload; + } + + $payload['folder-type'] = $typedFolder->folder_type; + + foreach ($typedFolder->getFiles() as $folderFile) { + $fileRef = $folderFile->getFileRef(); + $file = []; + $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' => $fileRef->getAuthorName()] + ] + ]; + $file['meta'] = [ + 'download-url' => $folderFile->getDownloadURL(), + ]; + + if ($this->filePermission($typedFolder, $file, $user) && $fileRef->isImage()) { + 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); + } + public function initialPayload(): array { return [ diff --git a/resources/vue/components/courseware/CoursewareGalleryBlock.vue b/resources/vue/components/courseware/CoursewareGalleryBlock.vue index 9e4fced7a0f6061b1c7c9056016fb8738dbc06ee..8b331473674cac39e532f6fa692fdc63cf4fe9b3 100644 --- a/resources/vue/components/courseware/CoursewareGalleryBlock.vue +++ b/resources/vue/components/courseware/CoursewareGalleryBlock.vue @@ -5,9 +5,9 @@ :canEdit="canEdit" :isTeacher="isTeacher" :preview="true" - @showEdit="initCurrentData" + @showEdit="showEdit" @storeEdit="storeBlock" - @closeEdit="initCurrentData" + @closeEdit="closeEdit" > <template #content> <div v-if="files.length !== 0" class="cw-block-gallery-content" :style="{ 'max-height': currentHeight + 'px' }"> @@ -19,7 +19,7 @@ > <div class="cw-block-gallery-number-text">{{ index + 1 }} / {{ files.length }}</div> <img - :src="image.download_url" + :src="image.meta['download-url']" :style="{ 'max-height': currentHeight + 'px' }" @load=" if (files.length - 1 === index) { @@ -28,7 +28,7 @@ " /> <div v-if="currentShowFileNames === 'true'" class="cw-block-gallery-file-name"> - <span>{{ image.name }}</span> + <span>{{ image.attributes.name }}</span> </div> </div> <div v-if="currentNav === 'true'"> @@ -107,8 +107,9 @@ export default { currentHeight: '', currentShowFileNames: '', currentAutoplayTimer: '', - files: [], + editModeFiles: [], slideIndex: 0, + editMode: false, }; }, computed: { @@ -135,6 +136,12 @@ export default { showFileNames() { return this.block?.attributes?.payload?.show_filenames; }, + files() { + if (!this.editMode) { + return this.block?.attributes?.payload?.files; + } + return this.editModeFiles; + } }, mounted() { this.initCurrentData(); @@ -169,7 +176,7 @@ export default { this.processFiles(files); }, processFiles(files) { - this.files = files + this.editModeFiles = files .filter((file) => { if (this.relatedTermOfUse({parent: file, relationship: 'terms-of-use'}).attributes['download-condition'] !== 0) { return false; @@ -182,14 +189,26 @@ export default { }) .map((file) => ({ id: file.id, - name: file.attributes.name, - download_url: this.urlHelper.getURL( - 'sendfile.php', - { type: 0, file_id: file.id, file_name: file.attributes.name }, - true - ), + attributes: { + name: file.attributes.name + }, + meta: { + 'download-url': this.urlHelper.getURL( + 'sendfile.php', + { type: 0, file_id: file.id, file_name: file.attributes.name }, + true + ), + }, })); }, + showEdit() { + this.editMode = true; + this.initCurrentData(); + }, + closeEdit() { + this.editMode = false; + this.initCurrentData(); + }, storeBlock() { let attributes = {}; attributes.payload = {}; @@ -206,10 +225,10 @@ export default { containerId: this.block.relationships.container.data.id, }); }, - plusSlides: function (n) { + plusSlides(n) { this.showSlides((this.slideIndex += n)); }, - showSlides: function (n) { + showSlides(n) { let slides = this.$refs.images; if (slides === undefined) { return false; @@ -225,7 +244,7 @@ export default { }); slides[this.slideIndex].style.display = 'block'; }, - playSlides: function () { + playSlides() { let slides = this.$refs.images; slides.forEach((slide) => { slide.style.display = 'none';