本文整理汇总了PHP中question_delete_activity函数的典型用法代码示例。如果您正苦于以下问题:PHP question_delete_activity函数的具体用法?PHP question_delete_activity怎么用?PHP question_delete_activity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了question_delete_activity函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: remove_course_contents
/**
* Clear a course out completely, deleting all content but don't delete the course itself.
*
* This function does not verify any permissions.
*
* Please note this function also deletes all user enrolments,
* enrolment instances and role assignments by default.
*
* $options:
* - 'keep_roles_and_enrolments' - false by default
* - 'keep_groups_and_groupings' - false by default
*
* @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.
* @param array $options extra options
* @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, array $options = null)
{
global $CFG, $DB, $OUTPUT;
require_once $CFG->libdir . '/badgeslib.php';
require_once $CFG->libdir . '/completionlib.php';
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/group/lib.php';
require_once $CFG->dirroot . '/comment/lib.php';
require_once $CFG->dirroot . '/rating/lib.php';
require_once $CFG->dirroot . '/notes/lib.php';
// Handle course badges.
badges_handle_course_deletion($courseid);
// NOTE: these concatenated strings are suboptimal, but it is just extra info...
$strdeleted = get_string('deleted') . ' - ';
// Some crazy wishlist of stuff we should skip during purging of course content.
$options = (array) $options;
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($courseid);
$fs = get_file_storage();
// Delete course completion information, this has to be done before grades and enrols.
$cc = new completion_info($course);
$cc->clear_criteria();
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . get_string('completion', 'completion'), 'notifysuccess');
}
// Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference
// the course modules that own the grades.
remove_course_grades($courseid, $showfeedback);
remove_grade_letters($coursecontext, $showfeedback);
// Delete course blocks in any all child contexts,
// they may depend on modules so delete them first.
$childcontexts = $coursecontext->get_child_contexts();
// Returns all subcontexts since 2.2.
foreach ($childcontexts as $childcontext) {
blocks_delete_all_for_context($childcontext->id);
}
unset($childcontexts);
blocks_delete_all_for_context($coursecontext->id);
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . get_string('type_block_plural', 'plugin'), 'notifysuccess');
}
// Get the list of all modules that are properly installed.
$allmodules = $DB->get_records_menu('modules', array(), '', 'name, id');
// Delete every instance of every module,
// this has to be done before deleting of course level stuff.
$locations = core_component::get_plugin_list('mod');
foreach ($locations as $modname => $moddir) {
if ($modname === 'NEWMODULE') {
continue;
}
if (array_key_exists($modname, $allmodules)) {
$sql = "SELECT cm.*, m.id AS modinstance, m.name, '{$modname}' AS modname\n FROM {" . $modname . "} m\n LEFT JOIN {course_modules} cm ON cm.instance = m.id AND cm.module = :moduleid\n WHERE m.course = :courseid";
$instances = $DB->get_records_sql($sql, array('courseid' => $course->id, 'modulename' => $modname, 'moduleid' => $allmodules[$modname]));
include_once "{$moddir}/lib.php";
// Shows php warning only if plugin defective.
$moddelete = $modname . '_delete_instance';
// Delete everything connected to an instance.
$moddeletecourse = $modname . '_delete_course';
// Delete other stray stuff (uncommon).
if ($instances) {
foreach ($instances as $cm) {
if ($cm->id) {
// Delete activity context questions and question categories.
question_delete_activity($cm, $showfeedback);
// Notify the competency subsystem.
\core_competency\api::hook_course_module_deleted($cm);
}
if (function_exists($moddelete)) {
// This purges all module data in related tables, extra user prefs, settings, etc.
$moddelete($cm->modinstance);
} else {
// NOTE: we should not allow installation of modules with missing delete support!
debugging("Defective module '{$modname}' detected when deleting course contents: missing function {$moddelete}()!");
$DB->delete_records($modname, array('id' => $cm->modinstance));
}
if ($cm->id) {
// Delete cm and its context - orphaned contexts are purged in cron in case of any race condition.
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
$DB->delete_records('course_modules', array('id' => $cm->id));
//.........这里部分代码省略.........
示例2: test_question_delete_activity
/**
* This function tests the question_delete_activity function.
*
* @param bool $feedback Whether to return feedback
* @dataProvider provider_feedback
*/
public function test_question_delete_activity($feedback)
{
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
// Test that the feedback works.
if ($feedback) {
$this->expectOutputRegex('|' . get_string('unusedcategorydeleted', 'question') . '|');
}
question_delete_activity($cm, $feedback);
// Verify category deleted.
$criteria = array('id' => $qcat->id);
$this->assertEquals(0, $DB->count_records('question_categories', $criteria));
// Verify questions deleted or moved.
$criteria = array('category' => $qcat->id);
$this->assertEquals(0, $DB->count_records('question', $criteria));
}
示例3: remove_course_contents
/**
* 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;
//.........这里部分代码省略.........
示例4: course_delete_module
/**
* This function will handle the whole deletion process of a module. This includes calling
* the modules delete_instance function, deleting files, events, grades, conditional data,
* the data in the course_module and course_sections table and adding a module deletion
* event to the DB.
*
* @param int $cmid the course module id
* @since Moodle 2.5
*/
function course_delete_module($cmid)
{
global $CFG, $DB;
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->dirroot . '/blog/lib.php';
require_once $CFG->dirroot . '/calendar/lib.php';
// Get the course module.
if (!($cm = $DB->get_record('course_modules', array('id' => $cmid)))) {
return true;
}
// Get the module context.
$modcontext = context_module::instance($cm->id);
// Get the course module name.
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
// Get the file location of the delete_instance function for this module.
$modlib = "{$CFG->dirroot}/mod/{$modulename}/lib.php";
// Include the file required to call the delete_instance function for this module.
if (file_exists($modlib)) {
require_once $modlib;
} else {
throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null, "Cannot delete this module as the file mod/{$modulename}/lib.php is missing.");
}
$deleteinstancefunction = $modulename . '_delete_instance';
// Ensure the delete_instance function exists for this module.
if (!function_exists($deleteinstancefunction)) {
throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null, "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/{$modulename}/lib.php.");
}
// Delete activity context questions and question categories.
question_delete_activity($cm);
// Call the delete_instance function, if it returns false throw an exception.
if (!$deleteinstancefunction($cm->instance)) {
throw new moodle_exception('cannotdeletemoduleinstance', '', '', null, "Cannot delete the module {$modulename} (instance).");
}
// Remove all module files in case modules forget to do that.
$fs = get_file_storage();
$fs->delete_area_files($modcontext->id);
// Delete events from calendar.
if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
foreach ($events as $event) {
$calendarevent = calendar_event::load($event->id);
$calendarevent->delete();
}
}
// Delete grade items, outcome items and grades attached to modules.
if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
foreach ($grade_items as $grade_item) {
$grade_item->delete('moddelete');
}
}
// Delete completion and availability data; it is better to do this even if the
// features are not turned on, in case they were turned on previously (these will be
// very quick on an empty table).
$DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
// Delete all tag instances associated with the instance of this module.
core_tag_tag::delete_instances('mod_' . $modulename, null, $modcontext->id);
core_tag_tag::remove_all_item_tags('core', 'course_modules', $cm->id);
// Delete the context.
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
// Delete the module from the course_modules table.
$DB->delete_records('course_modules', array('id' => $cm->id));
// Delete module from that section.
if (!delete_mod_from_section($cm->id, $cm->section)) {
throw new moodle_exception('cannotdeletemodulefromsection', '', '', null, "Cannot delete the module {$modulename} (instance) from section.");
}
// Trigger event for course module delete action.
$event = \core\event\course_module_deleted::create(array('courseid' => $cm->course, 'context' => $modcontext, 'objectid' => $cm->id, 'other' => array('modulename' => $modulename, 'instanceid' => $cm->instance)));
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
rebuild_course_cache($cm->course, true);
}
示例5: remove_course_contents
/**
* Clear a course out completely, deleting all content
* but don't delete the course itself.
* This function does not verify any permissions.
*
* Please note this function also deletes all user enrolments,
* enrolment instances and role assignments.
*
* @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, $OUTPUT;
require_once $CFG->libdir . '/completionlib.php';
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/group/lib.php';
require_once $CFG->dirroot . '/tag/coursetagslib.php';
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST);
$strdeleted = get_string('deleted');
// Delete course completion information,
// this has to be done before grades and enrols
$cc = new completion_info($course);
$cc->clear_criteria();
// remove roles and enrolments
role_unassign_all(array('contextid' => $context->id), true);
enrol_course_delete($course);
// Clean up course formats (iterate through all formats in the even the course format was ever changed)
$formats = get_plugin_list('format');
foreach ($formats as $format => $formatdir) {
$formatdelete = 'format_' . $format . '_delete_course';
$formatlib = "{$formatdir}/lib.php";
if (file_exists($formatlib)) {
include_once $formatlib;
if (function_exists($formatdelete)) {
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . ' ' . $format);
}
$formatdelete($course->id);
}
}
}
// Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference
// the course modules that own the grades.
remove_course_grades($courseid, $showfeedback);
remove_grade_letters($context, $showfeedback);
// Remove all data from availability and completion tables that is associated
// with course-modules belonging to this course. Note this is done even if the
// features are not enabled now, in case they were enabled previously
$DB->delete_records_select('course_modules_completion', 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid));
$DB->delete_records_select('course_modules_availability', 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid));
// Delete course blocks - they may depend on modules so delete them first
blocks_delete_all_for_context($context->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 {
echo $OUTPUT->notification('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')');
}
if ($cm) {
// delete cm and its context in correct order
delete_context(CONTEXT_MODULE, $cm->id);
// some callbacks may try to fetch context, better delete first
$DB->delete_records('course_modules', array('id' => $cm->id));
}
}
}
} else {
//note: we should probably delete these anyway
echo $OUTPUT->notification('Function ' . $moddelete . '() doesn\'t exist!');
}
if (function_exists($moddeletecourse)) {
$moddeletecourse($course, $showfeedback);
}
}
if ($showfeedback) {
//.........这里部分代码省略.........
示例6: remove_course_contents
/**
* Clear a course out completely, deleting all content
* but don't delete the course itself.
* This function does not verify any permissions.
*
* Please note this function also deletes all user enrolments,
* enrolment instances and role assignments by default.
*
* $options:
* - 'keep_roles_and_enrolments' - false by default
* - 'keep_groups_and_groupings' - false by default
*
* @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.
* @param array $options extra options
* @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, array $options = null)
{
global $CFG, $DB, $OUTPUT;
require_once $CFG->libdir . '/completionlib.php';
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/group/lib.php';
require_once $CFG->dirroot . '/tag/coursetagslib.php';
require_once $CFG->dirroot . '/comment/lib.php';
require_once $CFG->dirroot . '/rating/lib.php';
// NOTE: these concatenated strings are suboptimal, but it is just extra info...
$strdeleted = get_string('deleted') . ' - ';
// Some crazy wishlist of stuff we should skip during purging of course content
$options = (array) $options;
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($courseid);
$fs = get_file_storage();
// Delete course completion information, this has to be done before grades and enrols
$cc = new completion_info($course);
$cc->clear_criteria();
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . get_string('completion', 'completion'), 'notifysuccess');
}
// Remove all data from gradebook - this needs to be done before course modules
// because while deleting this information, the system may need to reference
// the course modules that own the grades.
remove_course_grades($courseid, $showfeedback);
remove_grade_letters($coursecontext, $showfeedback);
// Delete course blocks in any all child contexts,
// they may depend on modules so delete them first
$childcontexts = $coursecontext->get_child_contexts();
// returns all subcontexts since 2.2
foreach ($childcontexts as $childcontext) {
blocks_delete_all_for_context($childcontext->id);
}
unset($childcontexts);
blocks_delete_all_for_context($coursecontext->id);
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . get_string('type_block_plural', 'plugin'), 'notifysuccess');
}
// Delete every instance of every module,
// this has to be done before deleting of course level stuff
$locations = get_plugin_list('mod');
foreach ($locations as $modname => $moddir) {
if ($modname === 'NEWMODULE') {
continue;
}
if ($module = $DB->get_record('modules', array('name' => $modname))) {
include_once "{$moddir}/lib.php";
// Shows php warning only if plugin defective
$moddelete = $modname . '_delete_instance';
// Delete everything connected to an instance
$moddeletecourse = $modname . '_delete_course';
// Delete other stray stuff (uncommon)
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 (function_exists($moddelete)) {
// This purges all module data in related tables, extra user prefs, settings, etc.
$moddelete($instance->id);
} else {
// NOTE: we should not allow installation of modules with missing delete support!
debugging("Defective module '{$modname}' detected when deleting course contents: missing function {$moddelete}()!");
$DB->delete_records($modname, array('id' => $instance->id));
}
if ($cm) {
// Delete cm and its context - orphaned contexts are purged in cron in case of any race condition
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
$DB->delete_records('course_modules', array('id' => $cm->id));
}
}
}
if (function_exists($moddeletecourse)) {
// Execute ptional course cleanup callback
$moddeletecourse($course, $showfeedback);
}
if ($instances and $showfeedback) {
echo $OUTPUT->notification($strdeleted . get_string('pluginname', $modname), 'notifysuccess');
//.........这里部分代码省略.........
示例7: remove_course_contents
/**
* Clear a course out completely, deleting all content
* but don't delete the course itself
*
* @global object
* @global object
* @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, $OUTPUT;
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_plugin_list('format');
foreach ($formats as $format => $formatdir) {
$formatdelete = $format . '_course_format_delete_course';
$formatlib = "{$formatdir}/lib.php";
if (file_exists($formatlib)) {
include_once $formatlib;
if (function_exists($formatdelete)) {
if ($showfeedback) {
echo $OUTPUT->notification($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 {
echo $OUTPUT->notification('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 {
echo $OUTPUT->notification('Function ' . $moddelete . '() doesn\'t exist!');
$result = false;
}
if (function_exists($moddeletecourse)) {
$moddeletecourse($course, $showfeedback);
}
}
if ($showfeedback) {
echo $OUTPUT->notification($strdeleted . ' ' . $count . ' x ' . $modname);
}
}
} else {
print_error('nomodules', 'debug');
}
/// 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) {
echo $OUTPUT->notification($strdeleted . ' ' . $table);
}
//.........这里部分代码省略.........
示例8: course_delete_module
/**
* This function will handle the whole deletion process of a module. This includes calling
* the modules delete_instance function, deleting files, events, grades, conditional data,
* the data in the course_module and course_sections table and adding a module deletion
* event to the DB.
*
* @param int $cmid the course module id
* @param bool $async whether or not to try to delete the module using an adhoc task. Async also depends on a plugin hook.
* @throws moodle_exception
* @since Moodle 2.5
*/
function course_delete_module($cmid, $async = false)
{
// Check the 'course_module_background_deletion_recommended' hook first.
// Only use asynchronous deletion if at least one plugin returns true and if async deletion has been requested.
// Both are checked because plugins should not be allowed to dictate the deletion behaviour, only support/decline it.
// It's up to plugins to handle things like whether or not they are enabled.
if ($async && ($pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended'))) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
if ($pluginfunction()) {
return course_module_flag_for_async_deletion($cmid);
}
}
}
}
global $CFG, $DB;
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->libdir . '/questionlib.php';
require_once $CFG->dirroot . '/blog/lib.php';
require_once $CFG->dirroot . '/calendar/lib.php';
// Get the course module.
if (!($cm = $DB->get_record('course_modules', array('id' => $cmid)))) {
return true;
}
// Get the module context.
$modcontext = context_module::instance($cm->id);
// Get the course module name.
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
// Get the file location of the delete_instance function for this module.
$modlib = "{$CFG->dirroot}/mod/{$modulename}/lib.php";
// Include the file required to call the delete_instance function for this module.
if (file_exists($modlib)) {
require_once $modlib;
} else {
throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null, "Cannot delete this module as the file mod/{$modulename}/lib.php is missing.");
}
$deleteinstancefunction = $modulename . '_delete_instance';
// Ensure the delete_instance function exists for this module.
if (!function_exists($deleteinstancefunction)) {
throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null, "Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/{$modulename}/lib.php.");
}
// Allow plugins to use this course module before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_course_module_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($cm);
}
}
}
// Delete activity context questions and question categories.
question_delete_activity($cm);
// Call the delete_instance function, if it returns false throw an exception.
if (!$deleteinstancefunction($cm->instance)) {
throw new moodle_exception('cannotdeletemoduleinstance', '', '', null, "Cannot delete the module {$modulename} (instance).");
}
// Remove all module files in case modules forget to do that.
$fs = get_file_storage();
$fs->delete_area_files($modcontext->id);
// Delete events from calendar.
if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
foreach ($events as $event) {
$calendarevent = calendar_event::load($event->id);
$calendarevent->delete();
}
}
// Delete grade items, outcome items and grades attached to modules.
if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename, 'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
foreach ($grade_items as $grade_item) {
$grade_item->delete('moddelete');
}
}
// Delete completion and availability data; it is better to do this even if the
// features are not turned on, in case they were turned on previously (these will be
// very quick on an empty table).
$DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'course' => $cm->course, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
// Delete all tag instances associated with the instance of this module.
core_tag_tag::delete_instances('mod_' . $modulename, null, $modcontext->id);
core_tag_tag::remove_all_item_tags('core', 'course_modules', $cm->id);
// Notify the competency subsystem.
\core_competency\api::hook_course_module_deleted($cm);
// Delete the context.
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
// Delete the module from the course_modules table.
$DB->delete_records('course_modules', array('id' => $cm->id));
// Delete module from that section.
if (!delete_mod_from_section($cm->id, $cm->section)) {
throw new moodle_exception('cannotdeletemodulefromsection', '', '', null, "Cannot delete the module {$modulename} (instance) from section.");
}
//.........这里部分代码省略.........
示例9: empty_course_contents
/**
* @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;
}