diff --git a/lib/models/Courseware/StructuralElement.php b/lib/models/Courseware/StructuralElement.php index 9aa4f90e6259233a08b3327e783e618a1b7154d2..dbe2e62581db92a620499d5f6dce8a8d49a4089c 100644 --- a/lib/models/Courseware/StructuralElement.php +++ b/lib/models/Courseware/StructuralElement.php @@ -817,6 +817,12 @@ SQL; */ public function copy(User $user, StructuralElement $parent, string $purpose = ''): StructuralElement { + $ancestorIds = array_column($parent->findAncestors(), 'id'); + $ancestorIds[] = $parent->id; + if (in_array($this->id, $ancestorIds)) { + throw new \InvalidArgumentException('Cannot copy into descendants.'); + } + $file_ref_id = $this->copyImage($user, $parent); $element = self::build([