From 31bac9326bbf5f6733a5cd03760e225b495fc3e8 Mon Sep 17 00:00:00 2001
From: Farbod Zamani <zamani@elan-ev.de>
Date: Wed, 9 Feb 2022 10:46:34 +0000
Subject: [PATCH] Resolves "Redundant duplication of Multi-linked contents
 during CW copy"

---
 .../Courseware/BlockTypes/BlockType.php       | 24 +++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/lib/models/Courseware/BlockTypes/BlockType.php b/lib/models/Courseware/BlockTypes/BlockType.php
index 98e24aef69b..c1429157791 100755
--- a/lib/models/Courseware/BlockTypes/BlockType.php
+++ b/lib/models/Courseware/BlockTypes/BlockType.php
@@ -302,6 +302,15 @@ abstract class BlockType
      */
     protected function copyFileById(string $fileId, string $rangeId): string
     {
+        if ($this->block->container->structural_element->range_id === $rangeId) {
+            return $fileId;
+        }
+
+        static $file_map = [];
+        if (isset($file_map[$fileId])) {
+            return $file_map[$fileId];
+        }
+
         $user = \User::findCurrent();
         if ($file_ref = \FileRef::find($fileId)) {
             $copiedFile = \FileManager::copyFile(
@@ -310,7 +319,9 @@ abstract class BlockType
                 $user
             );
 
-            return isset($copiedFile->id) ? $copiedFile->id : '';
+            if (isset($copiedFile)) {
+                return $file_map[$fileId] = $copiedFile->id;
+            }
         }
 
         return '';
@@ -326,6 +337,15 @@ abstract class BlockType
      */
     protected function copyFolderById(string $folderId, string $rangeId): string
     {
+        if ($this->block->container->structural_element->range_id === $rangeId) {
+            return $folderId;
+        }
+
+        static $folder_map = [];
+        if (isset($folder_map[$folderId])) {
+            return $folder_map[$folderId];
+        }
+
         $user = \User::findCurrent();
         $destinationFolder = $this->getDestinationFolder($user, $rangeId);
         if ($sourceFolder = \Folder::find($folderId)) {
@@ -335,7 +355,7 @@ abstract class BlockType
                 $user
             );
 
-            return $copiedFolder->id;
+            return $folder_map[$folderId] = $copiedFolder->id;
         }
 
         return '';
-- 
GitLab