Merge branch 'wip-MDL-57769-master' of https://github.com/marinaglancy/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2017-04-05 01:31:08 +02:00
commit aa2b5b214d
43 changed files with 1365 additions and 388 deletions

View file

@ -832,6 +832,52 @@ function add_course_module($mod) {
return $cmid;
}
/**
* Creates a course section and adds it to the specified position
*
* @param int|stdClass $courseorid course id or course object
* @param int $position position to add to, 0 means to the end. If position is greater than
* number of existing secitons, the section is added to the end. This will become sectionnum of the
* new section. All existing sections at this or bigger position will be shifted down.
* @param bool $skipcheck the check has already been made and we know that the section with this position does not exist
* @return stdClass created section object
*/
function course_create_section($courseorid, $position = 0, $skipcheck = false) {
global $DB;
$courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
// Find the last sectionnum among existing sections.
if ($skipcheck) {
$lastsection = $position - 1;
} else {
$lastsection = (int)$DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$courseid]);
}
// First add section to the end.
$cw = new stdClass();
$cw->course = $courseid;
$cw->section = $lastsection + 1;
$cw->summary = '';
$cw->summaryformat = FORMAT_HTML;
$cw->sequence = '';
$cw->name = null;
$cw->visible = 1;
$cw->availability = null;
$cw->id = $DB->insert_record("course_sections", $cw);
// Now move it to the specified position.
if ($position > 0 && $position <= $lastsection) {
$course = is_object($courseorid) ? $courseorid : get_course($courseorid);
move_section_to($course, $cw->section, $position, true);
$cw->section = $position;
}
core\event\course_section_created::create_from_section($cw)->trigger();
rebuild_course_cache($courseid, true);
return $cw;
}
/**
* Creates missing course section(s) and rebuilds course cache
*
@ -840,31 +886,17 @@ function add_course_module($mod) {
* @return bool if there were any sections created
*/
function course_create_sections_if_missing($courseorid, $sections) {
global $DB;
if (!is_array($sections)) {
$sections = array($sections);
}
$existing = array_keys(get_fast_modinfo($courseorid)->get_section_info_all());
if (is_object($courseorid)) {
$courseorid = $courseorid->id;
}
$coursechanged = false;
foreach ($sections as $sectionnum) {
if (!in_array($sectionnum, $existing)) {
$cw = new stdClass();
$cw->course = $courseorid;
$cw->section = $sectionnum;
$cw->summary = '';
$cw->summaryformat = FORMAT_HTML;
$cw->sequence = '';
$id = $DB->insert_record("course_sections", $cw);
$coursechanged = true;
if ($newsections = array_diff($sections, $existing)) {
foreach ($newsections as $sectionnum) {
course_create_section($courseorid, $sectionnum, true);
}
return true;
}
if ($coursechanged) {
rebuild_course_cache($courseorid, true);
}
return $coursechanged;
return false;
}
/**
@ -2398,8 +2430,14 @@ function create_course($data, $editoroptions = NULL) {
// Setup the blocks
blocks_add_default_course_blocks($course);
// Create a default section.
course_create_sections_if_missing($course, 0);
// Create default section and initial sections if specified (unless they've already been created earlier).
// We do not want to call course_create_sections_if_missing() because to avoid creating course cache.
$numsections = isset($data->numsections) ? $data->numsections : 0;
$existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$newcourseid]);
$newsections = array_diff(range(0, $numsections), $existingsections);
foreach ($newsections as $sectionnum) {
course_create_section($newcourseid, $sectionnum, true);
}
// Save any custom role names.
save_local_role_names($course->id, (array)$data);