Further fixes for MDL-6601 ... that should be complete now.

This commit is contained in:
moodler 2006-09-23 06:10:48 +00:00
parent ea9fda2f88
commit 9991d157ea
2 changed files with 58 additions and 39 deletions

View file

@ -1087,8 +1087,7 @@ function islegacy($capabilityname) {
**********************************/ **********************************/
/** /**
* This should be called prolly everytime a user, group, module, course, * Create a new context record for use by all roles-related stuff
* coursecat or site is set up maybe?
* @param $level * @param $level
* @param $instanceid * @param $instanceid
*/ */
@ -1101,6 +1100,20 @@ function create_context($contextlevel, $instanceid) {
} }
} }
/**
* Create a new context record for use by all roles-related stuff
* @param $level
* @param $instanceid
*/
function delete_context($contextlevel, $instanceid) {
if ($context = get_context_instance($contextlevel, $instanceid)) {
return delete_records('context', 'id', $context->id) &&
delete_records('role_assignments', 'contextid', $context->id) &&
delete_records('role_role_capabilities', 'contextid', $context->id);
}
return true;
}
/** /**
* Get the context instance as an object. This function will create the * Get the context instance as an object. This function will create the
@ -2752,4 +2765,4 @@ function get_users_from_role_on_context($role, $context) {
AND roleid = $role->id"); AND roleid = $role->id");
} }
?> ?>

View file

@ -2597,7 +2597,7 @@ function delete_course($courseid, $showfeedback = true) {
$result = false; $result = false;
} }
if (!delete_records('context', 'contextlevel', CONTEXT_COURSE, 'instance', $courseid)) { if (!delete_records('context', 'contextlevel', CONTEXT_COURSE, 'instanceid', $courseid)) {
if ($showfeedback) { if ($showfeedback) {
notify("An error occurred while deleting the main context record."); notify("An error occurred while deleting the main context record.");
} }
@ -2637,7 +2637,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
$strdeleted = get_string('deleted'); $strdeleted = get_string('deleted');
// First delete every instance of every module /// First delete every instance of every module
if ($allmods = get_records('modules') ) { if ($allmods = get_records('modules') ) {
foreach ($allmods as $mod) { foreach ($allmods as $mod) {
@ -2652,7 +2652,7 @@ function remove_course_contents($courseid, $showfeedback=true) {
if ($instances = get_records($modname, 'course', $course->id)) { if ($instances = get_records($modname, 'course', $course->id)) {
foreach ($instances as $instance) { foreach ($instances as $instance) {
if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) { if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
delete_records('context', 'contextlevel', CONTEXT_MODULE, 'instance', $cm->id); delete_context(CONTEXT_MODULE, $cm->id);
} }
if ($moddelete($instance->id)) { if ($moddelete($instance->id)) {
$count++; $count++;
@ -2680,27 +2680,50 @@ function remove_course_contents($courseid, $showfeedback=true) {
error('No modules are installed!'); error('No modules are installed!');
} }
// Give local code a chance to delete its references to this course. /// Give local code a chance to delete its references to this course.
require_once('locallib.php'); require_once('locallib.php');
notify_local_delete_course($courseid, $showfeedback); notify_local_delete_course($courseid, $showfeedback);
// Delete course blocks /// Delete course blocks
if ($blocks = get_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) { if ($blocks = get_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
foreach ($blocks as $block) {
delete_records('context', 'contextlevel', CONTEXT_BLOCK, 'instance', $block->id);
}
if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) { if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
if ($showfeedback) { if ($showfeedback) {
notify($strdeleted .' block_instance'); notify($strdeleted .' block_instance');
} }
foreach ($blocks as $block) { /// Delete any associated contexts for this block
delete_context(CONTEXT_BLOCK, $block->id);
}
} else { } else {
$result = false; $result = false;
} }
} }
// Delete Other stuff. /// Delete any groups
// This array stores the tables that need to be cleared, as if ($groups = get_records('groups', 'courseid', $course->id)) {
// table_name => column_name that contains the course id. if (delete_records('groups', 'courseid', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' groups');
}
foreach ($groups as $group) {
if (delete_records('groups_members', 'groupid', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups_members');
}
} else {
$result = false;
}
/// Delete any associated context for this group
delete_context(CONTEXT_GROUP, $group->id);
}
} else {
$result = false;
}
}
/// Delete all related records in other tables that may have a courseid
/// This array stores the tables that need to be cleared, as
/// table_name => column_name that contains the course id.
$tablestoclear = array( $tablestoclear = array(
'event' => 'courseid', // Delete events 'event' => 'courseid', // Delete events
'log' => 'course', // Delete logs 'log' => 'course', // Delete logs
@ -2722,25 +2745,8 @@ function remove_course_contents($courseid, $showfeedback=true) {
} }
} }
// Delete any groups
if ($groups = get_records('groups', 'courseid', $course->id)) { /// Clean up metacourse stuff
foreach ($groups as $group) {
if (delete_records('groups_members', 'groupid', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups_members');
}
} else {
$result = false;
}
if (delete_records('groups', 'id', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups');
}
} else {
$result = false;
}
}
}
if ($course->metacourse) { if ($course->metacourse) {
delete_records("course_meta","parent_course",$course->id); delete_records("course_meta","parent_course",$course->id);
@ -2759,17 +2765,17 @@ function remove_course_contents($courseid, $showfeedback=true) {
} }
} }
// Delete questions and question categories /// Delete questions and question categories
include_once($CFG->libdir.'/questionlib.php'); include_once($CFG->libdir.'/questionlib.php');
question_delete_course($course, $showfeedback); question_delete_course($course, $showfeedback);
// deletes all role assignments, and local override, these have no courseid in table and needs separate process /// Delete all roles and overiddes in the course context (but keep the course context)
$context = get_context_instance(CONTEXT_COURSE, $course->id); if ($context = get_context_instance(CONTEXT_COURSE, $course->id)) {
delete_records('role_assignments', 'contextid', $context->id); delete_records('role_assignments', 'contextid', $context->id);
delete_records('role_role_capabilities', 'contextid', $context->id); delete_records('role_capabilities', 'contextid', $context->id);
}
return $result; return $result;
} }