MDL-21781 move cohorts away when deleting course category

This commit is contained in:
Petr Skoda 2010-04-23 09:14:36 +00:00
parent 9e1065a812
commit e60ff53619
2 changed files with 34 additions and 0 deletions

View File

@ -65,6 +65,32 @@ function cohort_delete_cohort($cohort) {
$DB->delete_records('cohort', array('id'=>$cohort->id));
}
/**
* Somehow deal with cohorts when deleting course category,
* we can not just delete them because they might be used in enrol
* plugins or referenced in external systems.
* @param object $category
* @return void
*/
function cohort_delete_category($category) {
global $DB;
// TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
$oldcontext = get_context_instance(CONTEXT_COURSECAT, $category->id, MUST_EXIST);
if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
$parentcontext = get_context_instance(CONTEXT_COURSECAT, $parent->id, MUST_EXIST);
$sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
$params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
} else {
$syscontext = get_context_instance(CONTEXT_SYSTEM);
$sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
$params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
}
$DB->execute($sql, $params);
}
/**
* Remove cohort member
* @param int $cohortid

View File

@ -3025,6 +3025,7 @@ function category_delete_full($category, $showfeedback=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
if ($children = $DB->get_records('course_categories', array('parent'=>$category->id), 'sortorder ASC')) {
foreach ($children as $childcat) {
@ -3042,6 +3043,9 @@ function category_delete_full($category, $showfeedback=true) {
}
}
// move or delete cohorts in this context
cohort_delete_category($category);
// now delete anything that may depend on course category context
grade_course_category_delete($category->id, 0, $showfeedback);
if (!question_delete_course_category($category, 0, $showfeedback)) {
@ -3067,6 +3071,7 @@ function category_delete_move($category, $newparentid, $showfeedback=true) {
global $CFG, $DB, $OUTPUT;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/questionlib.php');
require_once($CFG->dirroot.'/cohort/lib.php');
if (!$newparentcat = $DB->get_record('course_categories', array('id'=>$newparentid))) {
return false;
@ -3086,6 +3091,9 @@ function category_delete_move($category, $newparentid, $showfeedback=true) {
echo $OUTPUT->notification(get_string('coursesmovedout', '', format_string($category->name)), 'notifysuccess');
}
// move or delete cohorts in this context
cohort_delete_category($category);
// now delete anything that may depend on course category context
grade_course_category_delete($category->id, $newparentid, $showfeedback);
if (!question_delete_course_category($category, $newparentcat, $showfeedback)) {