From efda629364fe8963ffd4488a97e3144449d1310a Mon Sep 17 00:00:00 2001 From: Ron Lucke <lucke@elan-ev.de> Date: Thu, 14 Oct 2021 12:48:54 +0000 Subject: [PATCH] fixes #306 --- .../Courseware/BlockTypes/BlockType.php | 3 +- .../Courseware/BlockTypes/DialogCards.php | 22 +++++++++ .../Courseware/BlockTypes/Download.json | 2 +- lib/models/Courseware/BlockTypes/Download.php | 2 +- lib/models/Courseware/BlockTypes/Folder.json | 2 +- lib/models/Courseware/BlockTypes/Headline.php | 18 ++++--- .../courseware/CoursewareHeadlineBlock.vue | 47 +++++++++++++++---- resources/vue/mixins/courseware/export.js | 37 +++++++++++---- 8 files changed, 103 insertions(+), 30 deletions(-) diff --git a/lib/models/Courseware/BlockTypes/BlockType.php b/lib/models/Courseware/BlockTypes/BlockType.php index a25672dd315..aa2c478815c 100755 --- a/lib/models/Courseware/BlockTypes/BlockType.php +++ b/lib/models/Courseware/BlockTypes/BlockType.php @@ -283,10 +283,9 @@ abstract class BlockType protected function getFileById(string $fileId) { $file_ref = \FileRef::find($fileId); - $file = $file_ref->getFileType(); $user = \User::findCurrent(); - if ($file_ref && $file->isDownloadable($user->id)) { + if ($file_ref && $file_ref->getFileType()->isDownloadable($user->id)) { return $file_ref->toArray(); } else { return []; diff --git a/lib/models/Courseware/BlockTypes/DialogCards.php b/lib/models/Courseware/BlockTypes/DialogCards.php index 023a41693c1..7d3c8ba0735 100755 --- a/lib/models/Courseware/BlockTypes/DialogCards.php +++ b/lib/models/Courseware/BlockTypes/DialogCards.php @@ -56,6 +56,28 @@ class DialogCards extends BlockType return $payload; } + /** + * get all files related to this block. + * + * @return \FileRef[] list of file references related to this block + */ + public function getFiles(): array + { + $payload = $this->getPayload(); + $files = []; + + foreach ($payload['cards'] as &$card) { + if ($card['front_file_id'] && $fileRef = \FileRef::find($card['front_file_id'])) { + $files[] = $fileRef; + } + if ($card['back_file_id'] && $fileRef = \FileRef::find($card['back_file_id'])) { + $files[] = $fileRef; + } + } + + return $files; + } + public function copyPayload(string $rangeId = ''): array { $payload = $this->getPayload(); diff --git a/lib/models/Courseware/BlockTypes/Download.json b/lib/models/Courseware/BlockTypes/Download.json index b919e1393ad..bcc5f1dd67d 100755 --- a/lib/models/Courseware/BlockTypes/Download.json +++ b/lib/models/Courseware/BlockTypes/Download.json @@ -12,7 +12,7 @@ "type": "string" }, "grade": { - "type": "string" + "type": ["string", "boolean"] }, "file_id": { "type": "string" diff --git a/lib/models/Courseware/BlockTypes/Download.php b/lib/models/Courseware/BlockTypes/Download.php index 2e60bf4c1f4..6656d9f0bc8 100755 --- a/lib/models/Courseware/BlockTypes/Download.php +++ b/lib/models/Courseware/BlockTypes/Download.php @@ -35,7 +35,7 @@ class Download extends BlockType 'title' => '', 'info' => '', 'success' => '', - 'grade' => 'false', + 'grade' => false, 'file_id' => '', ]; } diff --git a/lib/models/Courseware/BlockTypes/Folder.json b/lib/models/Courseware/BlockTypes/Folder.json index a3320493f31..24d57920a42 100755 --- a/lib/models/Courseware/BlockTypes/Folder.json +++ b/lib/models/Courseware/BlockTypes/Folder.json @@ -13,7 +13,7 @@ } }, "required": [ - "type", "folder_id" + "folder_id" ], "additionalProperties": false } diff --git a/lib/models/Courseware/BlockTypes/Headline.php b/lib/models/Courseware/BlockTypes/Headline.php index addde05a11f..855e2a94a54 100755 --- a/lib/models/Courseware/BlockTypes/Headline.php +++ b/lib/models/Courseware/BlockTypes/Headline.php @@ -53,17 +53,21 @@ class Headline extends BlockType return Schema::fromJsonString(file_get_contents($schemaFile)); } - public function getPayload() + /** + * get all files related to this block. + * + * @return \FileRef[] list of file references related to this block + */ + public function getFiles(): array { - $payload = $this->decodePayloadString($this->block['payload']); + $payload = $this->getPayload(); + $files = []; - if ('' != $payload['background_image_id']) { - $payload['background_image'] = $this->getFileById($payload['background_image_id']); - } else { - $payload['background_image'] = []; + if ($payload['background_image_id'] && $fileRef = \FileRef::find($payload['background_image_id'])) { + $files[] = $fileRef; } - return $payload; + return $files; } public function copyPayload(string $rangeId = ''): array diff --git a/resources/vue/components/courseware/CoursewareHeadlineBlock.vue b/resources/vue/components/courseware/CoursewareHeadlineBlock.vue index 8f148350270..349ac9545c2 100755 --- a/resources/vue/components/courseware/CoursewareHeadlineBlock.vue +++ b/resources/vue/components/courseware/CoursewareHeadlineBlock.vue @@ -169,7 +169,7 @@ <script> import CoursewareDefaultBlock from './CoursewareDefaultBlock.vue'; import CoursewareFileChooser from './CoursewareFileChooser.vue'; -import { mapActions } from 'vuex'; +import { mapGetters, mapActions } from 'vuex'; import contentIcons from './content-icons.js'; export default { @@ -196,9 +196,15 @@ export default { currentBackgroundType: '', currentBackgroundImageId: '', currentBackgroundImage: {}, + currentBackgroundURL: '', }; }, computed: { + ...mapGetters({ + fileRefById: 'file-refs/byId', + urlHelper: 'urlHelper', + relatedTermOfUse: 'terms-of-use/related', + }), title() { return this.block?.attributes?.payload?.title; }, @@ -292,16 +298,14 @@ export default { } return style; - }, - currentBackgroundURL() { - return this.currentBackgroundImage.download_url; - }, + } }, mounted() { this.initCurrentData(); }, methods: { ...mapActions({ + loadFileRef: 'file-refs/loadById', updateBlock: 'updateBlockInContainer', }), initCurrentData() { @@ -315,13 +319,31 @@ export default { this.currentIconColor = this.iconColor; this.currentBackgroundType = this.backgroundType; this.currentBackgroundImageId = this.backgroundImageId; - if (typeof this.backgroundImage === 'object' && !Array.isArray(this.backgroundImage)) { - this.currentBackgroundImage = this.backgroundImage; + if (this.currentBackgroundImageId !== '') { + this.loadFile(); + } + }, + async loadFile() { + const id = this.currentBackgroundImageId; + const options = { include: 'terms-of-use' }; + await this.loadFileRef({ id: id, options }); + const fileRef = this.fileRefById({ id: id }); + if (fileRef && this.relatedTermOfUse({parent: fileRef, relationship: 'terms-of-use'}).attributes['download-condition'] === 0) { + this.updateCurrentBackgroundImage({ + id: fileRef.id, + name: fileRef.attributes.name, + download_url: this.urlHelper.getURL( + 'sendfile.php', + { type: 0, file_id: fileRef.id, file_name: fileRef.attributes.name }, + true + ), + }); } }, updateCurrentBackgroundImage(file) { this.currentBackgroundImage = file; this.currentBackgroundImageId = file.id; + this.currentBackgroundURL = file.download_url; }, closeEdit() { this.initCurrentData(); @@ -333,12 +355,19 @@ export default { attributes.payload.subtitle = this.currentSubtitle; attributes.payload.style = this.currentStyle; attributes.payload.height = this.currentHeight; - attributes.payload.background_color = this.currentBackgroundColor; attributes.payload.text_color = this.currentTextColor; attributes.payload.icon = this.currentIcon; attributes.payload.icon_color = this.currentIconColor; - attributes.payload.background_image_id = this.currentBackgroundImageId; attributes.payload.background_type = this.currentBackgroundType; + attributes.payload.background_color = ''; + attributes.payload.background_image_id = ''; + + if (this.currentBackgroundType === 'color') { + attributes.payload.background_color = this.currentBackgroundColor; + } + if (this.currentBackgroundType === 'image') { + attributes.payload.background_image_id = this.currentBackgroundImageId; + } this.updateBlock({ attributes: attributes, diff --git a/resources/vue/mixins/courseware/export.js b/resources/vue/mixins/courseware/export.js index 65e8ece5b3d..410a366c00d 100755 --- a/resources/vue/mixins/courseware/export.js +++ b/resources/vue/mixins/courseware/export.js @@ -266,22 +266,41 @@ export default { async exportFileRefs(block_id) { // load file-ref data - let refs = await this.loadFileRefs(block_id); + let refs = [] + try { + refs = await this.loadFileRefs(block_id); + } catch(e) { + //TODO: Companion explains error + } // add infos to exportFiles JSON for (let ref_id in refs) { - let fileref = {}; + let fileref = {}; let folderId = refs[ref_id].relationships.parent.data.id; - await this.loadFolder(folderId); - let folder = this.folderById({id: folderId}); - + let folder = null; fileref.attributes = refs[ref_id].attributes; fileref.related_block_id = block_id; fileref.id = refs[ref_id].id; - fileref.folder = { - id: folder.id, - name: folder.attributes.name, - type: folder.attributes['folder-type'] + + try { + await this.loadFolder(folderId); + folder = this.folderById({id: folderId}); + } catch(e) { + //TODO: Companion explains error + } + + if (folder) { + fileref.folder = { + id: folder.id, + name: folder.attributes.name, + type: folder.attributes['folder-type'] + } + } else { + fileref.folder = { + id: folderId, + name: 'Unknown', + type: 'StandardFolder' + } } this.exportFiles.json.push(fileref); -- GitLab