本文整理汇总了PHP中context_helper::delete_instance方法的典型用法代码示例。如果您正苦于以下问题:PHP context_helper::delete_instance方法的具体用法?PHP context_helper::delete_instance怎么用?PHP context_helper::delete_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类context_helper
的用法示例。
在下文中一共展示了context_helper::delete_instance方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: delete_context
/**
* Remove a context record and any dependent entries,
* removes context from static context cache too
*
* @deprecated since 2.2, use $context->delete_content() instead
* @param int $contextlevel
* @param int $instanceid
* @param bool $deleterecord false means keep record for now
* @return bool returns true or throws an exception
*/
function delete_context($contextlevel, $instanceid, $deleterecord = true)
{
if ($deleterecord) {
context_helper::delete_instance($contextlevel, $instanceid);
} else {
$classname = context_helper::get_class_for_level($contextlevel);
if ($context = $classname::instance($instanceid, IGNORE_MISSING)) {
$context->delete_content();
}
}
return true;
}
示例2: course_delete_module
/**
* This function will handles 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 2.5
*/
function course_delete_module($cmid)
{
global $CFG, $DB;
require_once $CFG->libdir . '/gradelib.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.");
}
// 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_modules_availability', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_modules_avail_fields', array('coursemoduleid' => $cm->id));
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id, 'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
// 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);
}
示例3: 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));
//.........这里部分代码省略.........
示例4: test_everything_in_accesslib
//.........这里部分代码省略.........
$this->assertTrue($DB->record_exists('context', array('id'=>$context->id)));
$this->assertEquals(0, $DB->count_records('block_instances', array('parentcontextid'=>$context->id)));
// ====== $context->delete() =============================
context_helper::reset_caches();
$context = context_module::instance($testpages[4]);
$this->assertTrue($DB->record_exists('context', array('id'=>$context->id)));
$this->assertEquals(1, $DB->count_records('block_instances', array('parentcontextid'=>$context->id)));
$bi = $DB->get_record('block_instances', array('parentcontextid'=>$context->id));
$bicontext = context_block::instance($bi->id);
$DB->delete_records('cache_flags', array());
$context->delete(); // should delete also linked blocks
$dirty = get_cache_flags('accesslib/dirtycontexts', time()-2);
$this->assertTrue(isset($dirty[$context->path]));
$this->assertFalse($DB->record_exists('context', array('id'=>$context->id)));
$this->assertFalse($DB->record_exists('context', array('id'=>$bicontext->id)));
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_MODULE, 'instanceid'=>$testpages[4])));
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK, 'instanceid'=>$bi->id)));
$this->assertEquals(0, $DB->count_records('block_instances', array('parentcontextid'=>$context->id)));
context_module::instance($testpages[4]);
// ====== context_helper::delete_instance() =============================
context_helper::reset_caches();
$lastcourse = array_pop($testcourses);
$this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse)));
$coursecontext = context_course::instance($lastcourse);
$this->assertEquals(context_inspection::test_context_cache_size(), 1);
$this->assertFalse($coursecontext->instanceid == CONTEXT_COURSE);
$DB->delete_records('cache_flags', array());
context_helper::delete_instance(CONTEXT_COURSE, $lastcourse);
$dirty = get_cache_flags('accesslib/dirtycontexts', time()-2);
$this->assertTrue(isset($dirty[$coursecontext->path]));
$this->assertEquals(context_inspection::test_context_cache_size(), 0);
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$lastcourse)));
context_course::instance($lastcourse);
// ======= context_helper::create_instances() ==========================
$prevcount = $DB->count_records('context');
$DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK));
context_helper::create_instances(null, true);
$this->assertSame($DB->count_records('context'), $prevcount);
$this->assertEquals($DB->count_records('context', array('depth'=>0)), 0);
$this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0);
$DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK));
$DB->delete_records('block_instances', array());
$prevcount = $DB->count_records('context');
$DB->delete_records_select('context', 'contextlevel <> '.CONTEXT_SYSTEM);
context_helper::create_instances(null, true);
$this->assertSame($DB->count_records('context'), $prevcount);
$this->assertEquals($DB->count_records('context', array('depth'=>0)), 0);
$this->assertEquals($DB->count_records('context', array('path'=>NULL)), 0);
// ======= context_helper::cleanup_instances() ==========================
$lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}");
$DB->delete_records('course', array('id'=>$lastcourse));
$lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}");
$DB->delete_records('course_categories', array('id'=>$lastcategory));
示例5: delete_context
/**
* Remove a context record and any dependent entries,
* removes context from static context cache too
*
* @deprecated since Moodle 2.2
* @see context_helper::delete_instance() or context::delete_content()
* @param int $contextlevel
* @param int $instanceid
* @param bool $deleterecord false means keep record for now
* @return bool returns true or throws an exception
*/
function delete_context($contextlevel, $instanceid, $deleterecord = true)
{
if ($deleterecord) {
debugging('delete_context() is deprecated, please use context_helper::delete_instance() instead.', DEBUG_DEVELOPER);
context_helper::delete_instance($contextlevel, $instanceid);
} else {
debugging('delete_context() is deprecated, please use $context->delete_content() instead.', DEBUG_DEVELOPER);
$classname = context_helper::get_class_for_level($contextlevel);
if ($context = $classname::instance($instanceid, IGNORE_MISSING)) {
$context->delete_content();
}
}
return true;
}
示例6: blocks_delete_instance
/**
* Delete a block, and associated data.
*
* @param object $instance a row from the block_instances table
* @param bool $nolongerused legacy parameter. Not used, but kept for backwards compatibility.
* @param bool $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
*/
function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false)
{
global $DB;
// Allow plugins to use this block before we completely delete it.
if ($pluginsfunction = get_plugins_with_function('pre_block_delete')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$pluginfunction($instance);
}
}
}
if ($block = block_instance($instance->blockname, $instance)) {
$block->instance_delete();
}
context_helper::delete_instance(CONTEXT_BLOCK, $instance->id);
if (!$skipblockstables) {
$DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
$DB->delete_records('block_instances', array('id' => $instance->id));
$DB->delete_records_list('user_preferences', 'name', array('block' . $instance->id . 'hidden', 'docked_block_instance_' . $instance->id));
}
}
示例7: blocks_delete_instance
/**
* Delete a block, and associated data.
*
* @param object $instance a row from the block_instances table
* @param bool $nolongerused legacy parameter. Not used, but kept for backwards compatibility.
* @param bool $skipblockstables for internal use only. Makes @see blocks_delete_all_for_context() more efficient.
*/
function blocks_delete_instance($instance, $nolongerused = false, $skipblockstables = false)
{
global $DB;
if ($block = block_instance($instance->blockname, $instance)) {
$block->instance_delete();
}
context_helper::delete_instance(CONTEXT_BLOCK, $instance->id);
if (!$skipblockstables) {
$DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
$DB->delete_records('block_instances', array('id' => $instance->id));
$DB->delete_records_list('user_preferences', 'name', array('block' . $instance->id . 'hidden', 'docked_block_instance_' . $instance->id));
}
}
示例8: add_moduleinfo
/**
* Add course module.
*
* The function does not check user capabilities.
* The function creates course module, module instance, add the module to the correct section.
* It also trigger common action that need to be done after adding/updating a module.
*
* @param object $moduleinfo the moudle data
* @param object $course the course of the module
* @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
* @return object the updated module info
*/
function add_moduleinfo($moduleinfo, $course, $mform = null)
{
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = 0;
// Do not set groupmode.
}
// First add course_module record because we need the context.
$newcm = new stdClass();
$newcm->course = $course->id;
$newcm->module = $moduleinfo->module;
$newcm->instance = 0;
// Not known yet, will be updated later (this is similar to restore code).
$newcm->visible = $moduleinfo->visible;
$newcm->visibleold = $moduleinfo->visible;
if (isset($moduleinfo->cmidnumber)) {
$newcm->idnumber = $moduleinfo->cmidnumber;
}
$newcm->groupmode = $moduleinfo->groupmode;
$newcm->groupingid = $moduleinfo->groupingid;
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$newcm->completion = $moduleinfo->completion;
$newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
$newcm->completionview = $moduleinfo->completionview;
$newcm->completionexpected = $moduleinfo->completionexpected;
}
if (!empty($CFG->enableavailability)) {
// This code is used both when submitting the form, which uses a long
// name to avoid clashes, and by unit test code which uses the real
// name in the table.
$newcm->availability = null;
if (property_exists($moduleinfo, 'availabilityconditionsjson')) {
if ($moduleinfo->availabilityconditionsjson !== '') {
$newcm->availability = $moduleinfo->availabilityconditionsjson;
}
} else {
if (property_exists($moduleinfo, 'availability')) {
$newcm->availability = $moduleinfo->availability;
}
}
}
if (isset($moduleinfo->showdescription)) {
$newcm->showdescription = $moduleinfo->showdescription;
} else {
$newcm->showdescription = 0;
}
// From this point we make database changes, so start transaction.
$transaction = $DB->start_delegated_transaction();
if (!($moduleinfo->coursemodule = add_course_module($newcm))) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) && isset($moduleinfo->introeditor)) {
$introeditor = $moduleinfo->introeditor;
unset($moduleinfo->introeditor);
$moduleinfo->intro = $introeditor['text'];
$moduleinfo->introformat = $introeditor['format'];
}
$addinstancefunction = $moduleinfo->modulename . "_add_instance";
try {
$returnfromfunc = $addinstancefunction($moduleinfo, $mform);
} catch (moodle_exception $e) {
$returnfromfunc = $e;
}
if (!$returnfromfunc or !is_number($returnfromfunc)) {
// Undo everything we can. This is not necessary for databases which
// support transactions, but improves consistency for other databases.
$modcontext = context_module::instance($moduleinfo->coursemodule);
context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
$DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
if ($e instanceof moodle_exception) {
throw $e;
} else {
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
}
}
}
$moduleinfo->instance = $returnfromfunc;
$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $moduleinfo->coursemodule));
// Update embedded links and save files.
$modcontext = context_module::instance($moduleinfo->coursemodule);
//.........这里部分代码省略.........
示例9: 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');
//.........这里部分代码省略.........
示例10: add_moduleinfo
/**
* Add course module.
*
* The function does not check user capabilities.
* The function creates course module, module instance, add the module to the correct section.
* It also trigger common action that need to be done after adding/updating a module.
*
* @param object $moduleinfo the moudle data
* @param object $course the course of the module
* @param object $mform this is required by an existing hack to deal with files during MODULENAME_add_instance()
* @return object the updated module info
*/
function add_moduleinfo($moduleinfo, $course, $mform = null)
{
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = 0;
// Do not set groupmode.
}
// First add course_module record because we need the context.
$newcm = new stdClass();
$newcm->course = $course->id;
$newcm->module = $moduleinfo->module;
$newcm->instance = 0;
// Not known yet, will be updated later (this is similar to restore code).
$newcm->visible = $moduleinfo->visible;
$newcm->visibleold = $moduleinfo->visible;
if (isset($moduleinfo->cmidnumber)) {
$newcm->idnumber = $moduleinfo->cmidnumber;
}
$newcm->groupmode = $moduleinfo->groupmode;
$newcm->groupingid = $moduleinfo->groupingid;
$newcm->groupmembersonly = $moduleinfo->groupmembersonly;
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$newcm->completion = $moduleinfo->completion;
$newcm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber;
$newcm->completionview = $moduleinfo->completionview;
$newcm->completionexpected = $moduleinfo->completionexpected;
}
if (!empty($CFG->enableavailability)) {
$newcm->availablefrom = $moduleinfo->availablefrom;
$newcm->availableuntil = $moduleinfo->availableuntil;
$newcm->showavailability = $moduleinfo->showavailability;
}
if (isset($moduleinfo->showdescription)) {
$newcm->showdescription = $moduleinfo->showdescription;
} else {
$newcm->showdescription = 0;
}
if (!($moduleinfo->coursemodule = add_course_module($newcm))) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) && isset($moduleinfo->introeditor)) {
$introeditor = $moduleinfo->introeditor;
unset($moduleinfo->introeditor);
$moduleinfo->intro = $introeditor['text'];
$moduleinfo->introformat = $introeditor['format'];
}
$addinstancefunction = $moduleinfo->modulename . "_add_instance";
$returnfromfunc = $addinstancefunction($moduleinfo, $mform);
if (!$returnfromfunc or !is_number($returnfromfunc)) {
// Undo everything we can.
$modcontext = context_module::instance($moduleinfo->coursemodule);
context_helper::delete_instance(CONTEXT_MODULE, $moduleinfo->coursemodule);
$DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
}
}
$moduleinfo->instance = $returnfromfunc;
$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $moduleinfo->coursemodule));
// Update embedded links and save files.
$modcontext = context_module::instance($moduleinfo->coursemodule);
if (!empty($introeditor)) {
$moduleinfo->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $moduleinfo->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
$DB->set_field($moduleinfo->modulename, 'intro', $moduleinfo->intro, array('id' => $moduleinfo->instance));
}
// Course_modules and course_sections each contain a reference to each other.
// So we have to update one of them twice.
$sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
// Set up conditions.
if ($CFG->enableavailability) {
condition_info::update_cm_from_form((object) array('id' => $moduleinfo->coursemodule), $moduleinfo, false);
}
// Trigger event based on the action we did.
$event = \core\event\course_module_created::create(array('courseid' => $course->id, 'context' => $modcontext, 'objectid' => $moduleinfo->coursemodule, 'other' => array('modulename' => $moduleinfo->modulename, 'name' => $moduleinfo->name, 'instanceid' => $moduleinfo->instance)));
$event->trigger();
add_to_log($course->id, $moduleinfo->modulename, "add", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
$moduleinfo = edit_module_post_actions($moduleinfo, $course);
return $moduleinfo;
}
示例11: test_everything_in_accesslib
//.........这里部分代码省略.........
context_helper::reset_caches();
$context = context_module::instance($testpages[3]);
$this->assertTrue($DB->record_exists('context', array('id' => $context->id)));
$this->assertEqual(1, $DB->count_records('block_instances', array('parentcontextid' => $context->id)));
$context->delete_content();
$this->assertTrue($DB->record_exists('context', array('id' => $context->id)));
$this->assertEqual(0, $DB->count_records('block_instances', array('parentcontextid' => $context->id)));
// ====== $context->delete() =============================
context_helper::reset_caches();
$context = context_module::instance($testpages[4]);
$this->assertTrue($DB->record_exists('context', array('id' => $context->id)));
$this->assertEqual(1, $DB->count_records('block_instances', array('parentcontextid' => $context->id)));
$bi = $DB->get_record('block_instances', array('parentcontextid' => $context->id));
$bicontext = context_block::instance($bi->id);
$DB->delete_records('cache_flags', array());
$context->delete();
// should delete also linked blocks
$dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2);
$this->assertTrue(isset($dirty[$context->path]));
$this->assertFalse($DB->record_exists('context', array('id' => $context->id)));
$this->assertFalse($DB->record_exists('context', array('id' => $bicontext->id)));
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_MODULE, 'instanceid' => $testpages[4])));
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $bi->id)));
$this->assertEqual(0, $DB->count_records('block_instances', array('parentcontextid' => $context->id)));
context_module::instance($testpages[4]);
// ====== context_helper::delete_instance() =============================
context_helper::reset_caches();
$lastcourse = array_pop($testcourses);
$this->assertTrue($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $lastcourse)));
$coursecontext = context_course::instance($lastcourse);
$this->assertEqual(context_inspection::test_context_cache_size(), 1);
$this->assertFalse($coursecontext->instanceid == CONTEXT_COURSE);
$DB->delete_records('cache_flags', array());
context_helper::delete_instance(CONTEXT_COURSE, $lastcourse);
$dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2);
$this->assertTrue(isset($dirty[$coursecontext->path]));
$this->assertEqual(context_inspection::test_context_cache_size(), 0);
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $lastcourse)));
context_course::instance($lastcourse);
// ======= context_helper::create_instances() ==========================
$prevcount = $DB->count_records('context');
$DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK));
context_helper::create_instances(null, true);
$this->assertIdentical($DB->count_records('context'), $prevcount);
$this->assertEqual($DB->count_records('context', array('depth' => 0)), 0);
$this->assertEqual($DB->count_records('context', array('path' => NULL)), 0);
$DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK));
$DB->delete_records('block_instances', array());
$prevcount = $DB->count_records('context');
$DB->delete_records_select('context', 'contextlevel <> ' . CONTEXT_SYSTEM);
context_helper::create_instances(null, true);
$this->assertIdentical($DB->count_records('context'), $prevcount);
$this->assertEqual($DB->count_records('context', array('depth' => 0)), 0);
$this->assertEqual($DB->count_records('context', array('path' => NULL)), 0);
// ======= context_helper::cleanup_instances() ==========================
$lastcourse = $DB->get_field_sql("SELECT MAX(id) FROM {course}");
$DB->delete_records('course', array('id' => $lastcourse));
$lastcategory = $DB->get_field_sql("SELECT MAX(id) FROM {course_categories}");
$DB->delete_records('course_categories', array('id' => $lastcategory));
$lastuser = $DB->get_field_sql("SELECT MAX(id) FROM {user} WHERE deleted=0");
$DB->delete_records('user', array('id' => $lastuser));
$DB->delete_records('block_instances', array('parentcontextid' => $frontpagepagecontext->id));
$DB->delete_records('course_modules', array('id' => $frontpagepagecontext->instanceid));
context_helper::cleanup_instances();
$count = 1;
//system
示例12: xmldb_main_upgrade
//.........这里部分代码省略.........
// Main savepoint reached
upgrade_main_savepoint(true, 2011091600.01);
}
if ($oldversion < 2011092800.01) {
// Check for potential missing columns in the grade_items_history
$table = new xmldb_table('grade_items_history');
$field = new xmldb_field('display', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'sortorder');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('decimals', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, null, 'display');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
} else {
//check that the grade_items_history.decimals allows nulls
//Somehow some Moodle databases have this column marked as "not null"
$columns = $DB->get_columns('grade_items_history');
if (array_key_exists('display', $columns) && !empty($columns['display']->not_null)) {
$dbman->change_field_notnull($table, $field);
}
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011092800.01);
}
if ($oldversion < 2011092800.02) {
// Check for potential missing columns in the grade_categories_history
$table = new xmldb_table('grade_categories_history');
$field = new xmldb_field('hidden', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'timemodified');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011092800.02);
}
if ($oldversion < 2011092800.03) {
// Check for potential missing columns in the grade_outcomes_history
$table = new xmldb_table('grade_outcomes_history');
$field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'description');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011092800.03);
}
if ($oldversion < 2011100700.02) {
// Define field idnumber to be added to course_categories
$table = new xmldb_table('course_categories');
$field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'name');
// Conditionally launch add field idnumber
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011100700.02);
}
if ($oldversion < 2011101200.01) {
// The conditional availability date system used to rely on dates being
// set to 23:59:59 for the end date, but now that exact times are
// supported, it uses midnight on the following day.
// The query is restricted on 'time mod 10 = 9' in order that
// it is safe to run this upgrade twice if something goes wrong.
$DB->execute('UPDATE {course_modules} SET availableuntil = availableuntil + 1 ' . 'WHERE availableuntil > 0 AND ' . $DB->sql_modulo('availableuntil', 10) . ' = 9');
// Because availableuntil is stored in modinfo, we need to clear modinfo
// for all courses.
rebuild_course_cache(0, true);
// Main savepoint reached
upgrade_main_savepoint(true, 2011101200.01);
}
if ($oldversion < 2011101900.02) {
// remove unused setting
unset_config('enablehtmlpurifier');
upgrade_main_savepoint(true, 2011101900.02);
}
if ($oldversion < 2011102700.01) {
// purge everything related to abandoned experimental global search
// unset setting - this disables it in case user does not delete the dirs
unset_config('enableglobalsearch');
// Delete block, instances and db table
$table = new xmldb_table('block_search_documents');
if ($dbman->table_exists($table)) {
$instances = $DB->get_records('block_instances', array('blockname' => 'search'));
foreach ($instances as $instance) {
context_helper::delete_instance(CONTEXT_BLOCK, $instance->id);
$DB->delete_records('block_positions', array('blockinstanceid' => $instance->id));
$DB->delete_records('block_instances', array('id' => $instance->id));
}
$DB->delete_records('block', array('name' => 'search'));
$dbman->drop_table($table);
}
// purge all settings used by the search block
$like = $DB->sql_like('name', '?', true, true, false, '|');
$params = array($DB->sql_like_escape('block_search_', '|') . '%', $DB->sql_like_escape('search_in_', '|') . '%');
$settings = $DB->get_records_select('config', "{$like} OR {$like}", $params);
foreach ($settings as $setting) {
unset_config($setting->name);
}
upgrade_main_savepoint(true, 2011102700.01);
}
return true;
}
示例13: 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.");
}
//.........这里部分代码省略.........
示例14: uninstall_cleanup
/**
* Pre-uninstall hook.
*
* This is intended for disabling of plugin, some DB table purging, etc.
*
* NOTE: to be called from uninstall_plugin() only.
* @private
*/
public function uninstall_cleanup()
{
global $DB, $CFG;
if (!($module = $DB->get_record('modules', array('name' => $this->name)))) {
parent::uninstall_cleanup();
return;
}
// Delete all the relevant instances from all course sections.
if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) {
foreach ($coursemods as $coursemod) {
// Do not verify results, there is not much we can do anyway.
delete_mod_from_section($coursemod->id, $coursemod->section);
}
}
// Increment course.cacherev for courses that used this module.
// This will force cache rebuilding on the next request.
increment_revision_number('course', 'cacherev', "id IN (SELECT DISTINCT course\n FROM {course_modules}\n WHERE module=?)", array($module->id));
// Delete all the course module records.
$DB->delete_records('course_modules', array('module' => $module->id));
// Delete module contexts.
if ($coursemods) {
foreach ($coursemods as $coursemod) {
\context_helper::delete_instance(CONTEXT_MODULE, $coursemod->id);
}
}
// Delete the module entry itself.
$DB->delete_records('modules', array('name' => $module->name));
// Cleanup the gradebook.
require_once $CFG->libdir . '/gradelib.php';
grade_uninstalled_module($module->name);
// Do not look for legacy $module->name . '_uninstall any more,
// they should have migrated to db/uninstall.php by now.
parent::uninstall_cleanup();
}