diff --git a/course/format/classes/base.php b/course/format/classes/base.php index 86f4b5a2e27..b0f8bc35ce9 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -2051,19 +2051,42 @@ abstract class base { } $course = $this->get_course(); - $oldsectioninfo = get_fast_modinfo($course)->get_section_info($originalsection->section); - $newsection = course_create_section($course, $oldsectioninfo->section + 1); // Place new section after existing one. + $context = context_course::instance($course->id); + $newsection = course_create_section($course, $originalsection->section + 1); // Place new section after existing one. + $newsectiondata = new stdClass(); if (!empty($originalsection->name)) { - $newsection->name = get_string('duplicatedsection', 'moodle', $originalsection->name); + $newsectiondata->name = get_string('duplicatedsection', 'moodle', $originalsection->name); } else { - $newsection->name = $originalsection->name; + $newsectiondata->name = $originalsection->name; + } + $newsectiondata->summary = $originalsection->summary; + $newsectiondata->summaryformat = $originalsection->summaryformat; + $newsectiondata->visible = $originalsection->visible; + $newsectiondata->availability = $originalsection->availability; + foreach ($this->section_format_options() as $key => $value) { + $newsectiondata->$key = $originalsection->$key; + } + course_update_section($course, $newsection, $newsectiondata); + + try { + $fs = get_file_storage(); + $files = $fs->get_area_files($context->id, 'course', 'section', $originalsection->id); + + foreach ($files as $f) { + + $fileinfo = [ + 'contextid' => $context->id, + 'component' => 'course', + 'filearea' => 'section', + 'itemid' => $newsection->id, + ]; + + $fs->create_file_from_storedfile($fileinfo, $f); + } + } catch (\Exception $e) { + debugging('Error copying section files.' . $e->getMessage(), DEBUG_DEVELOPER); } - $newsection->summary = $originalsection->summary; - $newsection->summaryformat = $originalsection->summaryformat; - $newsection->visible = $originalsection->visible; - $newsection->availability = $originalsection->availability; - course_update_section($course, $newsection, $newsection); $modinfo = $this->get_modinfo(); @@ -2071,7 +2094,9 @@ abstract class base { if (array_key_exists($originalsection->section, $modinfo->sections)) { foreach ($modinfo->sections[$originalsection->section] as $modnumber) { $originalcm = $modinfo->cms[$modnumber]; - duplicate_module($course, $originalcm, $newsection->id, false); + if (!$originalcm->deletioninprogress) { + duplicate_module($course, $originalcm, $newsection->id, false); + } } } diff --git a/course/format/classes/output/local/content/section/controlmenu.php b/course/format/classes/output/local/content/section/controlmenu.php index 8b0d98fc711..e7fdb208e38 100644 --- a/course/format/classes/output/local/content/section/controlmenu.php +++ b/course/format/classes/output/local/content/section/controlmenu.php @@ -193,19 +193,21 @@ class controlmenu implements named_templatable, renderable { 'attr' => ['class' => 'icon edit'], ]; - $duplicatesectionurl = clone($baseurl); - $duplicatesectionurl->param('section', $section->section); - $duplicatesectionurl->param('duplicatesection', $section->section); - if (!is_null($sectionreturn)) { - $duplicatesectionurl->param('sr', $sectionreturn); + if ($section->section) { + $duplicatesectionurl = clone($baseurl); + $duplicatesectionurl->param('sectionid', $section->id); + $duplicatesectionurl->param('duplicatesection', 1); + if (!is_null($sectionreturn)) { + $duplicatesectionurl->param('sr', $sectionreturn); + } + $controls['duplicate'] = [ + 'url' => $duplicatesectionurl, + 'icon' => 't/copy', + 'name' => get_string('duplicate'), + 'pixattr' => ['class' => ''], + 'attr' => ['class' => 'icon duplicate'], + ]; } - $controls['duplicate'] = [ - 'url' => $duplicatesectionurl, - 'icon' => 't/copy', - 'name' => get_string('duplicate'), - 'pixattr' => ['class' => ''], - 'attr' => ['class' => 'icon duplicate'], - ]; } if ($section->section) {