Example #1
0
/**
 * Clear a course out completely, deleting all content
 * but don't delete the course itself
 *
 * @uses $CFG
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true)
{
    global $CFG;
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->libdir . '/gradelib.php';
    $result = true;
    if (!($course = get_record('course', 'id', $courseid))) {
        error('Course ID was incorrect (can\'t find it)');
    }
    $strdeleted = get_string('deleted');
    /// Clean up course formats (iterate through all formats in the even the course format was ever changed)
    $formats = get_list_of_plugins('course/format');
    foreach ($formats as $format) {
        $formatdelete = $format . '_course_format_delete_course';
        $formatlib = "{$CFG->dirroot}/course/format/{$format}/lib.php";
        if (file_exists($formatlib)) {
            include_once $formatlib;
            if (function_exists($formatdelete)) {
                if ($showfeedback) {
                    notify($strdeleted . ' ' . $format);
                }
                $formatdelete($course->id);
            }
        }
    }
    /// Delete every instance of every module
    if ($allmods = get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = get_records($modname, 'course', $course->id)) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                /// Delete activity context questions and question categories
                                question_delete_activity($cm, $showfeedback);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                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 {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, $showfeedback);
                }
            }
            if ($showfeedback) {
                notify($strdeleted . ' ' . $count . ' x ' . $modname);
            }
        }
    } else {
        error('No modules are installed!');
    }
    /// Give local code a chance to delete its references to this course.
    require_once $CFG->libdir . '/locallib.php';
    notify_local_delete_course($courseid, $showfeedback);
    /// Delete course blocks
    if ($blocks = get_records_sql("SELECT *\n                                   FROM {$CFG->prefix}block_instance\n                                   WHERE pagetype = '" . PAGE_COURSE_VIEW . "'\n                                   AND pageid = {$course->id}")) {
        if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' block_instance');
            }
            require_once $CFG->libdir . '/blocklib.php';
            foreach ($blocks as $block) {
                /// Delete any associated contexts for this block
                delete_context(CONTEXT_BLOCK, $block->id);
                // fix for MDL-7164
                // Get the block object and call instance_delete()
                if (!($record = blocks_get_record($block->blockid))) {
                    $result = false;
                    continue;
                }
                if (!($obj = block_instance($record->name, $block))) {
                    $result = false;
                    continue;
                }
                // Return value ignored, in core mods this does not do anything, but just in case
                // third party blocks might have stuff to clean up
                // we execute this anyway
                $obj->instance_delete();
            }
        } else {
            $result = false;
        }
    }
    /// Delete any groups, removing members and grouping/course links first.
    require_once $CFG->dirroot . '/group/lib.php';
    groups_delete_groupings($courseid, $showfeedback);
    groups_delete_groups($courseid, $showfeedback);
    /// Delete all related records in other tables that may have a courseid
    /// This array stores the tables that need to be cleared, as
    /// table_name => column_name that contains the course id.
    $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        if (delete_records($table, $col, $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' ' . $table);
            }
        } else {
            $result = false;
        }
    }
    /// Clean up metacourse stuff
    if ($course->metacourse) {
        delete_records("course_meta", "parent_course", $course->id);
        sync_metacourse($course->id);
        // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
        if ($showfeedback) {
            notify("{$strdeleted} course_meta");
        }
    } else {
        if ($parents = get_records("course_meta", "child_course", $course->id)) {
            foreach ($parents as $parent) {
                remove_from_metacourse($parent->parent_course, $parent->child_course);
                // this will do the unenrolments as well.
            }
            if ($showfeedback) {
                notify("{$strdeleted} course_meta");
            }
        }
    }
    /// Delete questions and question categories
    question_delete_course($course, $showfeedback);
    /// Remove all data from gradebook
    $context = get_context_instance(CONTEXT_COURSE, $courseid);
    remove_course_grades($courseid, $showfeedback);
    remove_grade_letters($context, $showfeedback);
    return $result;
}
Example #2
0
/**
 * Clear a course out completely, deleting all content
 * but don't delete the course itself
 *
 * @uses $CFG
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true)
{
    global $CFG;
    $result = true;
    if (!($course = get_record('course', 'id', $courseid))) {
        error('Course ID was incorrect (can\'t find it)');
    }
    $strdeleted = get_string('deleted');
    /// First delete every instance of every module
    if ($allmods = get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = get_records($modname, 'course', $course->id)) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                delete_context(CONTEXT_MODULE, $cm->id);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                notify('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')');
                                $result = false;
                            }
                        }
                    }
                } else {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, $showfeedback);
                }
            }
            if ($showfeedback) {
                notify($strdeleted . ' ' . $count . ' x ' . $modname);
            }
        }
    } else {
        error('No modules are installed!');
    }
    /// Give local code a chance to delete its references to this course.
    require_once 'locallib.php';
    notify_local_delete_course($courseid, $showfeedback);
    /// Delete course blocks
    if ($blocks = get_records_sql("SELECT *\n                                   FROM {$CFG->prefix}block_instance\n                                   WHERE pagetype = '" . PAGE_COURSE_VIEW . "'\n                                   AND pageid = {$course->id}")) {
        if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' block_instance');
            }
            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);
                }
                // fix for MDL-7164
                // Get the block object and call instance_delete()
                if (!($record = blocks_get_record($block->blockid))) {
                    $result = false;
                    continue;
                }
                if (!($obj = block_instance($record->name, $block))) {
                    $result = false;
                    continue;
                }
                // Return value ignored, in core mods this does not do anything, but just in case
                // third party blocks might have stuff to clean up
                // we execute this anyway
                $obj->instance_delete();
            }
        } else {
            $result = false;
        }
    }
    /// Delete any groups, removing members and grouping/course links first.
    //TODO: If groups or groupings are to be shared between courses, think again!
    if ($groupids = groups_get_groups($course->id)) {
        foreach ($groupids as $groupid) {
            if (groups_remove_all_members($groupid)) {
                if ($showfeedback) {
                    notify($strdeleted . ' groups_members');
                }
            } else {
                $result = false;
            }
            /// Delete any associated context for this group ??
            delete_context(CONTEXT_GROUP, $groupid);
            if (groups_delete_group($groupid)) {
                if ($showfeedback) {
                    notify($strdeleted . ' groups');
                }
            } else {
                $result = false;
            }
        }
    }
    /// Delete any groupings.
    $result = groups_delete_all_groupings($course->id);
    if ($result && $showfeedback) {
        notify($strdeleted . ' groupings');
    }
    /// Delete all related records in other tables that may have a courseid
    /// This array stores the tables that need to be cleared, as
    /// table_name => column_name that contains the course id.
    $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'grade_category' => 'courseid', 'grade_exceptions' => 'courseid', 'grade_item' => 'courseid', 'grade_letter' => 'courseid', 'grade_preferences' => 'courseid', 'backup_courses' => 'courseid', 'backup_log' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        if (delete_records($table, $col, $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' ' . $table);
            }
        } else {
            $result = false;
        }
    }
    /// Clean up metacourse stuff
    if ($course->metacourse) {
        delete_records("course_meta", "parent_course", $course->id);
        sync_metacourse($course->id);
        // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
        if ($showfeedback) {
            notify("{$strdeleted} course_meta");
        }
    } else {
        if ($parents = get_records("course_meta", "child_course", $course->id)) {
            foreach ($parents as $parent) {
                remove_from_metacourse($parent->parent_course, $parent->child_course);
                // this will do the unenrolments as well.
            }
            if ($showfeedback) {
                notify("{$strdeleted} course_meta");
            }
        }
    }
    /// Delete questions and question categories
    include_once $CFG->libdir . '/questionlib.php';
    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);
    }
    // fix for MDL-9016
    // clear the cache because the course context is deleted, and
    // we don't want to write assignment, overrides and context_rel table
    // with this old context id!
    get_context_instance('clearcache');
    return $result;
}
Example #3
0
/**
 * Clear a course out completely, deleting all content
 * but don't delete the course itself
 *
 * @uses $CFG
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->libdir . '/gradelib.php';
    $result = true;
    if (!($course = $DB->get_record('course', array('id' => $courseid)))) {
        print_error('invalidcourseid');
    }
    $context = get_context_instance(CONTEXT_COURSE, $courseid);
    $strdeleted = get_string('deleted');
    /// Clean up course formats (iterate through all formats in the even the course format was ever changed)
    $formats = get_list_of_plugins('course/format');
    foreach ($formats as $format) {
        $formatdelete = $format . '_course_format_delete_course';
        $formatlib = "{$CFG->dirroot}/course/format/{$format}/lib.php";
        if (file_exists($formatlib)) {
            include_once $formatlib;
            if (function_exists($formatdelete)) {
                if ($showfeedback) {
                    notify($strdeleted . ' ' . $format);
                }
                $formatdelete($course->id);
            }
        }
    }
    /// Delete every instance of every module
    if ($allmods = $DB->get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = $DB->get_records($modname, array('course' => $course->id))) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                /// Delete activity context questions and question categories
                                question_delete_activity($cm, $showfeedback);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                notify('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')');
                                $result = false;
                            }
                            if ($cm) {
                                // delete cm and its context in correct order
                                $DB->delete_records('course_modules', array('id' => $cm->id));
                                delete_context(CONTEXT_MODULE, $cm->id);
                            }
                        }
                    }
                } else {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, $showfeedback);
                }
            }
            if ($showfeedback) {
                notify($strdeleted . ' ' . $count . ' x ' . $modname);
            }
        }
    } else {
        print_error('nomodules', 'debug');
    }
    /// Give local code a chance to delete its references to this course.
    require_once $CFG->libdir . '/locallib.php';
    notify_local_delete_course($courseid, $showfeedback);
    /// Delete course blocks
    blocks_delete_all_for_context($context->id);
    /// Delete any groups, removing members and grouping/course links first.
    require_once $CFG->dirroot . '/group/lib.php';
    groups_delete_groupings($courseid, $showfeedback);
    groups_delete_groups($courseid, $showfeedback);
    /// Delete all related records in other tables that may have a courseid
    /// This array stores the tables that need to be cleared, as
    /// table_name => column_name that contains the course id.
    $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        if ($DB->delete_records($table, array($col => $course->id))) {
            if ($showfeedback) {
                notify($strdeleted . ' ' . $table);
            }
        } else {
            $result = false;
        }
    }
    /// Clean up metacourse stuff
    if ($course->metacourse) {
        $DB->delete_records("course_meta", array("parent_course" => $course->id));
        sync_metacourse($course->id);
        // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
        if ($showfeedback) {
            notify("{$strdeleted} course_meta");
        }
    } else {
        if ($parents = $DB->get_records("course_meta", array("child_course" => $course->id))) {
            foreach ($parents as $parent) {
                remove_from_metacourse($parent->parent_course, $parent->child_course);
                // this will do the unenrolments as well.
            }
            if ($showfeedback) {
                notify("{$strdeleted} course_meta");
            }
        }
    }
    /// Delete questions and question categories
    question_delete_course($course, $showfeedback);
    /// Remove all data from gradebook
    remove_course_grades($courseid, $showfeedback);
    remove_grade_letters($context, $showfeedback);
    /// Delete course tags
    require_once $CFG->dirroot . '/tag/coursetagslib.php';
    coursetag_delete_course_tags($course->id, $showfeedback);
    return $result;
}
Example #4
0
/**
 * @see /lib/moodlelib.php - remove_course_contents()
 * - $showfeedback is always false
 * - don't let local code delete its reference to the course
 * - don't delete course blocks
 * - don't delete groups, members, etc
 * - don't delete related records from: event, log, course_sections,
 *   course_modules, backup_courses, user_lastaccess, backup_log
 * - don't clean up metacourse stuff
 * - don't delete questions and question categories
 * - don't remove data from gradebook
 */
function empty_course_contents($courseid)
{
    global $CFG;
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->libdir . '/gradelib.php';
    if (!($course = get_record('course', 'id', $courseid))) {
        error('Course ID was incorrect (can\'t find it)');
    }
    $result = true;
    /// Delete every instance of every module
    if ($allmods = get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = get_records($modname, 'course', $course->id)) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                /// Delete activity context questions and question categories
                                question_delete_activity($cm, false);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                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 {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, false);
                }
            }
        }
    } else {
        error('No modules are installed!');
    }
    /// Give local code a chance to delete its references to this course.
    require_once $CFG->libdir . '/locallib.php';
    notify_local_delete_course($courseid, false);
    return $result;
}