diff --git a/admin/tool/uploadcourse/classes/course.php b/admin/tool/uploadcourse/classes/course.php index 7656ae8b593..c0f7b539b2f 100644 --- a/admin/tool/uploadcourse/classes/course.php +++ b/admin/tool/uploadcourse/classes/course.php @@ -703,10 +703,18 @@ class tool_uploadcourse_course { $coursedata += $courseformat->validate_course_format_options($this->rawdata); } - // Special case, 'numsections' is not a course format option any more but still should apply from defaults. + // Special case, 'numsections' is not a course format option any more but still should apply from the template course, + // if any, and otherwise from defaults. if (!$exists || !array_key_exists('numsections', $coursedata)) { if (isset($this->rawdata['numsections']) && is_numeric($this->rawdata['numsections'])) { $coursedata['numsections'] = (int)$this->rawdata['numsections']; + } else if (isset($this->options['templatecourse'])) { + $numsections = tool_uploadcourse_helper::get_coursesection_count($this->options['templatecourse']); + if ($numsections != 0) { + $coursedata['numsections'] = $numsections; + } else { + $coursedata['numsections'] = get_config('moodlecourse', 'numsections'); + } } else { $coursedata['numsections'] = get_config('moodlecourse', 'numsections'); } diff --git a/admin/tool/uploadcourse/classes/helper.php b/admin/tool/uploadcourse/classes/helper.php index 2e514a70f21..2325c9c02ae 100644 --- a/admin/tool/uploadcourse/classes/helper.php +++ b/admin/tool/uploadcourse/classes/helper.php @@ -286,6 +286,25 @@ class tool_uploadcourse_helper { return $roles; } + /** + * Helper to detect how many sections a course with a given shortname has. + * + * @param string $shortname shortname of a course to count sections from. + * @return integer count of sections. + */ + public static function get_coursesection_count($shortname) { + global $DB; + if (!empty($shortname) || is_numeric($shortname)) { + // Creating restore from an existing course. + $course = $DB->get_record('course', array('shortname' => $shortname)); + } + if (!empty($course)) { + $courseformat = course_get_format($course); + return $courseformat->get_last_section_number(); + } + return 0; + } + /** * Get the role renaming data from the passed data. *