MDL-35339 deprecate get_course_section() replace with course_create_sections_if_missing()

By passing course object instead of course id we ensure proper cache reset"
This commit is contained in:
Marina Glancy 2012-10-08 10:36:01 +08:00
parent 9796014660
commit 4ede27b253
9 changed files with 76 additions and 45 deletions

View file

@ -61,8 +61,9 @@ class block_site_main_menu extends block_list {
/// slow & hacky editing mode
$ismoving = ismoving($course->id);
$section = get_course_section(0, $course->id);
course_create_sections_if_missing($course, 0);
$modinfo = get_fast_modinfo($course);
$section = $modinfo->get_section_info(0);
$groupbuttons = $course->groupmode;
$groupbuttonslink = (!$course->groupmodeforce);

View file

@ -45,12 +45,7 @@ if (($marker >=0) && has_capability('moodle/course:setcurrentsection', $context)
}
// make sure all sections are created
$modinfo = get_fast_modinfo($course);
for ($sectionnum = 0; $sectionnum <= $course->numsections; $sectionnum++) {
if (!$modinfo->get_section_info($sectionnum)) {
get_course_section($sectionnum, $course->id);
}
}
course_create_sections_if_missing($course, range(0, $course->numsections));
$renderer = $PAGE->get_renderer('format_topics');

View file

@ -38,12 +38,7 @@ if ($week = optional_param('week', 0, PARAM_INT)) {
// End backwards-compatible aliasing..
// make sure all sections are created
$modinfo = get_fast_modinfo($course);
for ($sectionnum = 0; $sectionnum <= $course->numsections; $sectionnum++) {
if (!$modinfo->get_section_info($sectionnum)) {
get_course_section($sectionnum, $course->id);
}
}
course_create_sections_if_missing($course, range(0, $course->numsections));
$renderer = $PAGE->get_renderer('format_weeks');

View file

@ -2691,27 +2691,37 @@ function add_course_module($mod) {
}
/**
* Returns course section - creates new if does not exist yet
* Creates missing course section(s) and rebuilds course cache
*
* @param int $section relative section number (field course_sections.section)
* @param int $courseid
* @return stdClass record from table {course_sections}
* @param stdClass $course course object
* @param int|array $sections list of relative section numbers to create
* @return bool if there were any sections created
*/
function get_course_section($section, $courseid) {
function course_create_sections_if_missing(&$course, $sections) {
global $DB;
if ($cw = $DB->get_record("course_sections", array("section"=>$section, "course"=>$courseid))) {
return $cw;
if (!is_array($sections)) {
$sections = array($sections);
}
$cw = new stdClass();
$cw->course = $courseid;
$cw->section = $section;
$cw->summary = "";
$cw->summaryformat = FORMAT_HTML;
$cw->sequence = "";
$id = $DB->insert_record("course_sections", $cw);
rebuild_course_cache($courseid, true);
return $DB->get_record("course_sections", array("id"=>$id));
$existing = array_keys(get_fast_modinfo($course)->get_section_info_all());
$coursechanged = false;
foreach ($sections as $sectionnum) {
if (!in_array($sectionnum, $existing)) {
$cw = new stdClass();
$cw->course = $course->id;
$cw->section = $sectionnum;
$cw->summary = '';
$cw->summaryformat = FORMAT_HTML;
$cw->sequence = '';
$id = $DB->insert_record("course_sections", $cw);
$coursechanged = true;
}
}
if ($coursechanged) {
rebuild_course_cache($course->id, true);
$course->modinfo = null;
$course->sectioncache = null;
}
return $coursechanged;
}
/**
@ -2742,7 +2752,8 @@ function course_add_cm_to_section($courseorid, $modid, $sectionnum, $beforemod =
$course = $DB->get_record('course', array('id' => $courseorid), '*', MUST_EXIST);
}
}
$section = get_course_section($sectionnum, $courseid);
course_create_sections_if_missing($course, $sectionnum);
$section = get_fast_modinfo($course)->get_section_info($sectionnum);
$modarray = explode(",", trim($section->sequence));
if (empty($modarray)) {
$newsequence = "$modid";
@ -3745,11 +3756,8 @@ function create_course($data, $editoroptions = NULL) {
// Setup the blocks
blocks_add_default_course_blocks($course);
$section = new stdClass();
$section->course = $course->id; // Create a default section.
$section->section = 0;
$section->summaryformat = FORMAT_HTML;
$DB->insert_record('course_sections', $section);
// Create a default section.
course_create_sections_if_missing($course, 0);
fix_course_sortorder();

View file

@ -59,7 +59,8 @@ if (!empty($add)) {
$context = context_course::instance($course->id);
require_capability('moodle/course:manageactivities', $context);
$cw = get_course_section($section, $course->id);
course_create_sections_if_missing($course, $section);
$cw = get_fast_modinfo($course)->get_section_info($section);
if (!course_allowed_module($course, $module->name)) {
print_error('moduledisable');

View file

@ -243,7 +243,7 @@
echo html_writer::start_tag('div', array('class'=>'course-content'));
// make sure that section 0 exists (this function will create one if it is missing)
$section0 = get_course_section(0, $course->id);
course_create_sections_if_missing($course, 0);
// get information about course modules and existing module types
// format.php in course formats may rely on presence of these variables

View file

@ -434,11 +434,8 @@ function process_group_tag($tagcontents) {
$course = $DB->get_record('course', array('id' => $courseid));
blocks_add_default_course_blocks($course);
$section = new stdClass();
$section->course = $course->id; // Create a default section.
$section->section = 0;
$section->summaryformat = FORMAT_HTML;
$section->id = $DB->insert_record("course_sections", $section);
// Create default 0-section
course_create_sections_if_missing($course, 0);
add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$course->fullname (ID $course->id)");

View file

@ -109,8 +109,8 @@
} else {
if ($editing) {
// make sure section with number 1 exists, this function will create section
get_course_section(1, $SITE->id);
// make sure section with number 1 exists
course_create_sections_if_missing($SITE, 1);
// re-request modinfo in case section was created
$modinfo = get_fast_modinfo($SITE);
}

View file

@ -3013,3 +3013,37 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname
$mods = $modinfo->get_cms();
$modnamesused = $modinfo->get_used_module_names();
}
/**
* Returns course section - creates new if does not exist yet
*
* This function is deprecated. To create a course section call:
* course_create_sections_if_missing($course, $sections);
* to get the section call:
* get_fast_modinfo($course)->get_section_info($sectionnum);
*
* @see course_create_sections_if_missing()
* @see get_fast_modinfo()
* @deprecated since 2.4
*
* @param int $section relative section number (field course_sections.section)
* @param int $courseid
* @return stdClass record from table {course_sections}
*/
function get_course_section($section, $courseid) {
global $DB;
debugging('Function get_course_section() is deprecated. Please use course_create_sections_if_missing() and get_fast_modinfo() instead.', DEBUG_DEVELOPER);
if ($cw = $DB->get_record("course_sections", array("section"=>$section, "course"=>$courseid))) {
return $cw;
}
$cw = new stdClass();
$cw->course = $courseid;
$cw->section = $section;
$cw->summary = "";
$cw->summaryformat = FORMAT_HTML;
$cw->sequence = "";
$id = $DB->insert_record("course_sections", $cw);
rebuild_course_cache($courseid, true);
return $DB->get_record("course_sections", array("id"=>$id));
}