MDL-29108 Remove all associated grading areas when the context is being deleted
authorDavid Mudrak <david@moodle.com>
Sun, 13 Nov 2011 12:50:00 +0000 (13:50 +0100)
committerDavid Mudrak <david@moodle.com>
Sun, 13 Nov 2011 12:50:00 +0000 (13:50 +0100)
grade/grading/lib.php
lib/accesslib.php

index 09ce100..a2c2d26 100644 (file)
@@ -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
      *
index 4479379..d96d135 100644 (file)
@@ -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));