MDL-38314 repository: Delete repository instances on context deletion

This commit is contained in:
Frederic Massart 2013-06-06 11:50:58 +08:00
parent 4aa4d88da8
commit d802435e00
2 changed files with 30 additions and 0 deletions

View File

@ -5357,6 +5357,10 @@ abstract class context extends stdClass implements IteratorAggregate {
$fs = get_file_storage();
$fs->delete_area_files($this->_id);
// Delete all repository instances attached to this context.
require_once($CFG->dirroot . '/repository/lib.php');
repository::delete_all_for_context($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);

View File

@ -2077,6 +2077,32 @@ abstract class repository implements cacheable_object {
return true;
}
/**
* Delete all the instances associated to a context.
*
* This method is intended to be a callback when deleting
* a course or a user to delete all the instances associated
* to their context. The usual way to delete a single instance
* is to use {@link self::delete()}.
*
* @param int $contextid context ID.
* @param boolean $downloadcontents true to convert references to hard copies.
* @return void
*/
final public static function delete_all_for_context($contextid, $downloadcontents = true) {
global $DB;
$repoids = $DB->get_fieldset_select('repository_instances', 'id', 'contextid = :contextid', array('contextid' => $contextid));
if ($downloadcontents) {
foreach ($repoids as $repoid) {
$repo = repository::get_repository_by_id($repoid, $contextid);
$repo->convert_references_to_local();
}
}
cache::make('core', 'repositories')->purge();
$DB->delete_records_list('repository_instances', 'id', $repoids);
$DB->delete_records_list('repository_instance_config', 'instanceid', $repoids);
}
/**
* Hide/Show a repository
*