From 9e316669dc9b7e8a13391f353d839e66513e80a5 Mon Sep 17 00:00:00 2001
From: Ron Lucke <lucke@elan-ev.de>
Date: Wed, 1 Jun 2022 10:44:35 +0000
Subject: [PATCH] fix #1112

Closes #1112

Merge request studip/studip!670
---
 .../Routes/Courseware/TaskGroupsCreate.php    |  4 +-
 lib/models/Courseware/StructuralElement.php   | 37 +++++++++++++------
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/lib/classes/JsonApi/Routes/Courseware/TaskGroupsCreate.php b/lib/classes/JsonApi/Routes/Courseware/TaskGroupsCreate.php
index 9cf6069daf4..28c4e9ce65c 100644
--- a/lib/classes/JsonApi/Routes/Courseware/TaskGroupsCreate.php
+++ b/lib/classes/JsonApi/Routes/Courseware/TaskGroupsCreate.php
@@ -188,9 +188,9 @@ class TaskGroupsCreate extends JsonApiController
             ]);
 
             // copy task template
-            $taskElement = $taskTemplate->copy($lecturer, $target);
+            $purpose = 'task';
+            $taskElement = $taskTemplate->copy($lecturer, $target, $purpose);
             $taskElement->title = $title;
-            $taskElement->purpose = 'task';
             $taskElement->store();
 
             //update task with element id
diff --git a/lib/models/Courseware/StructuralElement.php b/lib/models/Courseware/StructuralElement.php
index 69d9d22da62..ac6116f205e 100755
--- a/lib/models/Courseware/StructuralElement.php
+++ b/lib/models/Courseware/StructuralElement.php
@@ -309,16 +309,19 @@ class StructuralElement extends \SimpleORMap
                 }
 
                 if ($this->isTask()) {
-                    // TODO: Was tun wir, wenn dieses Strukturelement purpose=task aber keinen Task hat?
-                    if (!$this->task) {
-                        return false;
+                    $task = $this->task;
+                    if (!$task) {
+                        $task = $this->findParentTask();
+                        if (!$task) {
+                            return false;
+                        }
                     }
 
-                    if ($this->task->isSubmitted() && $this->hasEditingPermission($user)) {
+                    if ($task->isSubmitted() && $this->hasEditingPermission($user)) {
                         return true;
                     }
 
-                    return $this->task->userIsASolver($user);
+                    return $task->userIsASolver($user);
                 }
 
                 if ($this->canEdit($user)) {
@@ -660,7 +663,7 @@ SQL;
      *
      * @return StructuralElement the copy of this instance
      */
-    public function copy(User $user, StructuralElement $parent): StructuralElement
+    public function copy(User $user, StructuralElement $parent, string $purpose = ''): StructuralElement
     {
         $file_ref_id = self::copyImage($user, $parent);
 
@@ -672,7 +675,7 @@ SQL;
             'editor_id' => $user->id,
             'edit_blocker_id' => null,
             'title' => $this->title,
-            'purpose' => $this->purpose,
+            'purpose' => empty($purpose) ? $this->purpose : $purpose,
             'position' => $parent->countChildren(),
             'payload' => $this->payload,
             'image_id' => $file_ref_id,
@@ -682,12 +685,12 @@ SQL;
 
         self::copyContainers($user, $element);
 
-        self::copyChildren($user, $element);
+        self::copyChildren($user, $element, $purpose);
 
         return $element;
     }
 
-    private function copyImage(User $user, StructuralElement $parent) : ?String
+    private function copyImage(User $user, StructuralElement $parent) : ?string
     {
         $file_ref_id = null;
 
@@ -764,12 +767,12 @@ SQL;
         }
     }
 
-    private function copyChildren(User $user, StructuralElement $newElement): void
+    private function copyChildren(User $user, StructuralElement $newElement, string $purpose = ''): void
     {
         $children = self::findBySQL('parent_id = ?', [$this->id]);
 
         foreach ($children as $child) {
-            $child->copy($user, $newElement);
+            $child->copy($user, $newElement, $purpose);
         }
     }
 
@@ -837,4 +840,16 @@ SQL;
 
         return $html;
     }
+
+    private function findParentTask()
+    {
+        if ($this->isRootNode()) {
+            return null;
+        }
+        if ($this->task) {
+            return $this->task;
+        }
+
+        return $this->parent->findParentTask();
+    }
 }
-- 
GitLab