MDL-35339 Deprecate add_mod_to_section(), create new function course_add_cm_to_section()

Function add_mod_to_section() has very confusing arguments when object  looks like record from
table course_modules but field ->section refers to relative section number (course_sections.section).
In table course_modules the field section refers to course_sections.id.
Also add_mod_to_section() does not update table course_modules and does not call rebuild_course_cache()
which developer can forget to do afterwards.

- Added function course_add_cm_to_section()
- In the core code add_mod_to_section() is replaced with course_add_cm_to_section()
- Function add_mod_to_section() is deprecated
This commit is contained in:
Marina Glancy
2012-10-15 14:08:13 +08:00
parent 99e9f9a69d
commit 722e6ba947
9 changed files with 81 additions and 101 deletions
+2 -5
View File
@@ -556,7 +556,7 @@ class generator {
$module->name = ucfirst($moduledata->name) . ' ' . $moduledata->count++;
$module->course = $courseid;
$module->section = $i;
$module->section = 0;
$module->module = $moduledata->id;
$module->modulename = $moduledata->name;
$module->add = $moduledata->name;
@@ -564,10 +564,7 @@ class generator {
$module->coursemodule = '';
$add_instance_function = $moduledata->name . '_add_instance';
$section = get_course_section($i, $courseid);
$module->section = $section->id;
$module->coursemodule = add_course_module($module);
$module->section = $i;
if (function_exists($add_instance_function)) {
$this->verbose("Calling module function $add_instance_function");
@@ -580,7 +577,7 @@ class generator {
}
}
add_mod_to_section($module);
$module->section = course_add_cm_to_section($courseid, $module->coursemodule, $i);
$module->cmidnumber = set_coursemodule_idnumber($module->coursemodule, '');
+1 -2
View File
@@ -613,8 +613,7 @@ class dndupload_ajax_processor {
$DB->set_field('course_modules', 'instance', $instanceid, array('id' => $this->cm->id));
$sectionid = add_mod_to_section($this->cm);
$DB->set_field('course_modules', 'section', $sectionid, array('id' => $this->cm->id));
$sectionid = course_add_cm_to_section($this->course, $this->cm->id, $this->section);
set_coursemodule_visible($this->cm->id, true);
+48 -62
View File
@@ -2758,50 +2758,50 @@ function get_course_section($section, $courseid) {
}
/**
* Given a full mod object with section and course already defined, adds this module to that section.
* Adds an existing module to the section
*
* @param object $mod
* @param int $beforemod An existing ID which we will insert the new module before
* @return int The course_sections ID where the mod is inserted
* Updates both tables {course_sections} and {course_modules}
*
* @param int|stdClass $courseorid course id or course object
* @param int $modid id of the module already existing in course_modules table
* @param int $sectionnum relative number of the section (field course_sections.section)
* If section does not exist it will be created
* @param int|stdClass $beforemod id or object with field id corresponding to the module
* before which the module needs to be included. Null for inserting in the
* end of the section
* @return int The course_sections ID where the module is inserted
*/
function add_mod_to_section($mod, $beforemod=NULL) {
global $DB;
if ($section = $DB->get_record("course_sections", array("course"=>$mod->course, "section"=>$mod->section))) {
$section->sequence = trim($section->sequence);
if (empty($section->sequence)) {
$newsequence = "$mod->coursemodule";
} else if ($beforemod) {
$modarray = explode(",", $section->sequence);
if ($key = array_keys($modarray, $beforemod->id)) {
$insertarray = array($mod->id, $beforemod->id);
array_splice($modarray, $key[0], 1, $insertarray);
$newsequence = implode(",", $modarray);
} else { // Just tack it on the end anyway
$newsequence = "$section->sequence,$mod->coursemodule";
}
} else {
$newsequence = "$section->sequence,$mod->coursemodule";
}
$DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
return $section->id; // Return course_sections ID that was used.
} else { // Insert a new record
$section = new stdClass();
$section->course = $mod->course;
$section->section = $mod->section;
$section->summary = "";
$section->summaryformat = FORMAT_HTML;
$section->sequence = $mod->coursemodule;
return $DB->insert_record("course_sections", $section);
function course_add_cm_to_section($courseorid, $modid, $sectionnum, $beforemod = NULL) {
global $DB, $COURSE;
if (is_object($beforemod)) {
$beforemod = $beforemod->id;
}
if (is_object($courseorid)) {
$course = &$courseorid;
} else {
if (isset($COURSE->id) && $COURSE->id == $courseorid) {
$course = &$COURSE;
} else {
$course = $DB->get_record('course', array('id' => $courseorid), '*', MUST_EXIST);
}
}
$section = get_course_section($sectionnum, $courseid);
$modarray = explode(",", trim($section->sequence));
if (empty($modarray)) {
$newsequence = "$modid";
} else if ($beforemod && ($key = array_keys($modarray, $beforemod))) {
$insertarray = array($modid, $beforemod);
array_splice($modarray, $key[0], 1, $insertarray);
$newsequence = implode(",", $modarray);
} else {
$newsequence = "$section->sequence,$modid";
}
$DB->set_field("course_sections", "sequence", $newsequence, array("id" => $section->id));
$DB->set_field('course_modules', 'section', $section->id, array('id' => $modid));
rebuild_course_cache($course->id, true);
$course->modinfo = null;
$course->sectioncache = null;
return $section->id; // Return course_sections ID that was used.
}
function set_coursemodule_groupmode($id, $groupmode) {
@@ -2902,14 +2902,14 @@ function delete_course_module($id) {
return $DB->delete_records('course_modules', array('id'=>$cm->id));
}
function delete_mod_from_section($mod, $section) {
function delete_mod_from_section($modid, $sectionid) {
global $DB;
if ($section = $DB->get_record("course_sections", array("id"=>$section)) ) {
if ($section = $DB->get_record("course_sections", array("id"=>$sectionid)) ) {
$modarray = explode(",", $section->sequence);
if ($key = array_keys ($modarray, $mod)) {
if ($key = array_keys ($modarray, $modid)) {
array_splice($modarray, $key[0], 1);
$newsequence = implode(",", $modarray);
return $DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
@@ -3097,27 +3097,13 @@ function moveto_module($mod, $section, $beforemod=NULL) {
echo $OUTPUT->notification("Could not delete module from existing section");
}
/// Update module itself if necessary
if ($mod->section != $section->id) {
$mod->section = $section->id;
$DB->update_record("course_modules", $mod);
// if moving to a hidden section then hide module
if (!$section->visible) {
set_coursemodule_visible($mod->id, 0);
}
// if moving to a hidden section then hide module
if (!$section->visible && $mod->visible) {
set_coursemodule_visible($mod->id, 0);
}
/// Add the module into the new section
$mod->course = $section->course;
$mod->section = $section->section; // need relative reference
$mod->coursemodule = $mod->id;
if (! add_mod_to_section($mod, $beforemod)) {
return false;
}
course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
return true;
}
+1 -3
View File
@@ -474,9 +474,7 @@ if ($mform->is_cancelled()) {
// course_modules and course_sections each contain a reference
// to each other, so we have to update one of them twice.
$sectionid = add_mod_to_section($fromform);
$DB->set_field('course_modules', 'section', $sectionid, array('id'=>$fromform->coursemodule));
$sectionid = course_add_cm_to_section($course, $fromform->coursemodule, $fromform->section);
// make sure visibility is set correctly (in particular in calendar)
// note: allow them to set it even without moodle/course:activityvisibility
+20
View File
@@ -2957,3 +2957,23 @@ function get_all_sections($courseid) {
$course = $DB->get_record('course', array('id' => $courseid));
return get_fast_modinfo($course)->get_section_info_all();
}
/**
* Given a full mod object with section and course already defined, adds this module to that section.
*
* This function is deprecated, please use {@link course_add_cm_to_section()}
* Note that course_add_cm_to_section() also updates field course_modules.section and
* calls rebuild_course_cache()
*
* @deprecated since 2.4
*
* @param object $mod
* @param int $beforemod An existing ID which we will insert the new module before
* @return int The course_sections ID where the mod is inserted
*/
function add_mod_to_section($mod, $beforemod=NULL) {
debugging('Function add_mod_to_section() is deprecated, please use course_add_cm_to_section()', DEBUG_DEVELOPER);
global $DB;
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
return course_add_cm_to_section($course, $mod->coursemodule, $mod->section, $beforemod);
}
+3 -3
View File
@@ -82,12 +82,13 @@ abstract class phpunit_module_generator {
require_once("$CFG->dirroot/course/lib.php");
$modulename = $this->get_modulename();
$sectionnum = isset($options['section']) ? $options['section'] : 0;
$cm = new stdClass();
$cm->course = $courseid;
$cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
$cm->instance = 0;
$cm->section = isset($options['section']) ? $options['section'] : 0;
$cm->section = 0;
$cm->idnumber = isset($options['idnumber']) ? $options['idnumber'] : 0;
$cm->added = time();
@@ -103,9 +104,8 @@ abstract class phpunit_module_generator {
}
$cm->id = $DB->insert_record('course_modules', $cm);
$cm->coursemodule = $cm->id;
add_mod_to_section($cm);
course_add_cm_to_section($courseid, $cm->id, $sectionnum);
return $cm->id;
}
+1 -6
View File
@@ -337,12 +337,7 @@ class assign_upgrade_manager {
return false;
}
$mod = new stdClass();
$mod->course = $newcm->course;
$mod->section = $section->section;
$mod->coursemodule = $newcm->id;
$mod->id = $newcm->id;
$newcm->section = add_mod_to_section($mod, $cm);
$newcm->section = course_add_cm_to_section($newcm->course, $newcm->id, $section->section);
// make sure visibility is set correctly (in particular in calendar)
// note: allow them to set it even without moodle/course:activityvisibility
+4 -16
View File
@@ -3001,19 +3001,12 @@ function forum_get_course_forum($courseid, $type) {
$mod->module = $module->id;
$mod->instance = $forum->id;
$mod->section = 0;
if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
include_once("$CFG->dirroot/course/lib.php");
if (! $mod->coursemodule = add_course_module($mod) ) {
echo $OUTPUT->notification("Could not add a new course module to the course '" . $courseid . "'");
return false;
}
if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
echo $OUTPUT->notification("Could not add the new course module to that section");
return false;
}
$DB->set_field("course_modules", "section", $sectionid, array("id" => $mod->coursemodule));
include_once("$CFG->dirroot/course/lib.php");
rebuild_course_cache($courseid);
$sectionid = course_add_cm_to_section($courseid, $mod->coursemodule, 0);
return $DB->get_record("forum", array("id" => "$forum->id"));
}
@@ -7332,12 +7325,7 @@ function forum_convert_to_roles($forum, $forummodid, $teacherroles=array(),
if (!$cmid = add_course_module($mod)) {
print_error('cannotcreateinstanceforteacher', 'forum');
} else {
$mod->coursemodule = $cmid;
if (!$sectionid = add_mod_to_section($mod)) {
print_error('cannotaddteacherforumto', 'forum');
} else {
$DB->set_field('course_modules', 'section', $sectionid, array('id' => $cmid));
}
$sectionid = course_add_cm_to_section($forum->course, $mod->coursemodule, 0);
}
// Change the forum type to general.
+1 -4
View File
@@ -159,14 +159,11 @@ if ($xml = glossary_read_imported_file($result)) {
print_error('cannotaddcoursemodule');
}
if (! $sectionid = add_mod_to_section($mod) ) {
print_error('cannotaddcoursemoduletosection');
}
$sectionid = course_add_cm_to_section($course, $mod->coursemodule, 0);
//We get the section's visible field status
$visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
$DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
$DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
add_to_log($course->id, "course", "add mod",
"../mod/$mod->modulename/view.php?id=$mod->coursemodule",