From 9e32368b2993b92400a1b17a58ca232144524b2a Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sun, 13 Nov 2011 13:50:00 +0100 Subject: [PATCH] MDL-29108 Remove all associated grading areas when the context is being deleted --- grade/grading/lib.php | 24 ++++++++++++++++++++++++ lib/accesslib.php | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/grade/grading/lib.php b/grade/grading/lib.php index 09ce1005dc7..a2c2d2610fd 100644 --- a/grade/grading/lib.php +++ b/grade/grading/lib.php @@ -564,6 +564,30 @@ class grading_manager { return $DB->insert_record('grading_areas', $area); } + /** + * Removes all data associated with the given context + * + * This is called by {@link context::delete_content()} + * + * @param int $contextid context id + */ + public static function delete_all_for_context($contextid) { + global $DB; + + $areaids = $DB->get_fieldset_select('grading_areas', 'id', 'contextid = ?', array($contextid)); + $methods = array_keys(self::available_methods(false)); + + foreach($areaids as $areaid) { + $manager = get_grading_manager($areaid); + foreach ($methods as $method) { + $controller = $manager->get_controller($method); + $controller->delete_definition(); + } + } + + $DB->delete_records_list('grading_areas', 'id', $areaids); + } + /** * Helper method to tokenize the given string * diff --git a/lib/accesslib.php b/lib/accesslib.php index 44793792c72..d96d135d420 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -4919,6 +4919,10 @@ abstract class context extends stdClass { $fs = get_file_storage(); $fs->delete_area_files($this->_id); + // delete all advanced grading data attached to this context + require_once($CFG->dirroot.'/grade/grading/lib.php'); + grading_manager::delete_all_for_context($this->_id); + // now delete stuff from role related tables, role_unassign_all // and unenrol should be called earlier to do proper cleanup $DB->delete_records('role_assignments', array('contextid'=>$this->_id)); -- 2.43.0