$cached = $context;
return $cached;
}
+
/**
- * Remove a context record and any dependent entries
+ * Remove a context record and any dependent entries,
+ * removes context from static context cache too
* @param $level
* @param $instanceid
*
* @return bool properly deleted
*/
function delete_context($contextlevel, $instanceid) {
- if ($context = get_context_instance($contextlevel, $instanceid)) {
- mark_context_dirty($context->path);
- return delete_records('context', 'id', $context->id) &&
- delete_records('role_assignments', 'contextid', $context->id) &&
- delete_records('role_capabilities', 'contextid', $context->id);
+ global $context_cache, $context_cache_id;
+
+ // do not use get_context_instance(), because the related object might not exist,
+ // or the context does not exist yet and it would be created now
+ if ($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instanceid)) {
+ $result = delete_records('role_assignments', 'contextid', $context->id) &&
+ delete_records('role_capabilities', 'contextid', $context->id) &&
+ delete_records('context', 'id', $context->id);
+
+ // do not mark dirty contexts if parents unknown
+ if (!is_null($context->path) and $context->depth > 0) {
+ mark_context_dirty($context->path);
+ }
+
+ // purge static context cache if entry present
+ unset($context_cache[$contextlevel][$instanceid]);
+ unset($context_cache_id[$context->id]);
+
+ return $result;
+ } else {
+
+ return true;
}
- return true;
}
/**
return $grade_item;
}
+/**
+ * Remove grade letters for given context
+ * @param object $context
+ */
+function remove_grade_letters($context, $showfeedback) {
+ $strdeleted = get_string('deleted');
+
+ delete_records('grade_letters', 'contextid', $context->id);
+ if ($showfeedback) {
+ notify($strdeleted.' - '.get_string('letters', 'grades'));
+ }
+}
/**
* Remove all grade related course data - history is kept
* @param int $courseid
if ($showfeedback) {
notify($strdeleted.' - '.get_string('scales'));
}
+
+ delete_records('grade_settings', 'courseid', $courseid);
+ if ($showfeedback) {
+ notify($strdeleted.' - '.get_string('settings', 'grades'));
+ }
}
/**
require_once($CFG->libdir.'/gradelib.php');
$result = true;
+ // frontpage course can not be deleted!!
+ if ($courseid == SITEID) {
+ return false;
+ }
+
+ $context = get_context_instance(CONTEXT_COURSE, $courseid);
+
if (!remove_course_contents($courseid, $showfeedback)) {
if ($showfeedback) {
notify("An error occurred while deleting some of the course contents.");
}
remove_course_grades($courseid, $showfeedback);
+ remove_grade_letters($context, $showfeedback);
if (!delete_records("course", "id", $courseid)) {
if ($showfeedback) {
$result = false;
}
- if (!delete_records('context', 'contextlevel', CONTEXT_COURSE, 'instanceid', $courseid)) {
+/// Delete all roles and overiddes in the course context
+ if (!delete_context(CONTEXT_COURSE, $courseid)) {
if ($showfeedback) {
- notify("An error occurred while deleting the main context record.");
+ notify("An error occurred while deleting the main course context.");
}
$result = false;
}
if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
/// Delete activity context questions and question categories
question_delete_activity($cm, $showfeedback);
- delete_context(CONTEXT_MODULE, $cm->id);
}
if ($moddelete($instance->id)) {
$count++;
notify('Could not delete '. $modname .' instance '. $instance->id .' ('. format_string($instance->name) .')');
$result = false;
}
+ if ($cm) {
+ // delete cm and its context in correct order
+ delete_records('course_modules', 'id', $cm->id);
+ delete_context(CONTEXT_MODULE, $cm->id);
+ }
}
}
} else {
require_once($CFG->libdir.'/blocklib.php');
foreach ($blocks as $block) { /// Delete any associated contexts for this block
- // Block instances are rarely created. Since the block instance is gone from the above delete
- // statement, calling delete_context() will generate a warning as get_context_instance could
- // no longer create the context as the block is already gone.
- if (record_exists('context', 'contextlevel', CONTEXT_BLOCK, 'instanceid', $block->id)) {
- delete_context(CONTEXT_BLOCK, $block->id);
- }
+ delete_context(CONTEXT_BLOCK, $block->id);
// fix for MDL-7164
// Get the block object and call instance_delete()
// third party blocks might have stuff to clean up
// we execute this anyway
$obj->instance_delete();
+
}
} else {
$result = false;
'course_sections' => 'course', // Delete any course stuff
'course_modules' => 'course',
'backup_courses' => 'courseid', // Delete scheduled backup stuff
+ 'user_lastaccess' => 'courseid',
'backup_log' => 'courseid'
);
foreach ($tablestoclear as $table => $col) {
/// Delete questions and question categories
question_delete_course($course, $showfeedback);
-/// Delete all roles and overiddes in the course context (but keep the course context)
- if ($courseid != SITEID) {
- delete_context(CONTEXT_COURSE, $course->id);
- }
-
return $result;
}