本文整理汇总了PHP中delete_context函数的典型用法代码示例。如果您正苦于以下问题:PHP delete_context函数的具体用法?PHP delete_context怎么用?PHP delete_context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了delete_context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: delete_course
/**
* Delete a course, including all related data from the database,
* and any associated files.
*
* @global object
* @global object
* @param mixed $courseorid The id of the course or course object to delete.
* @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 delete_course($courseorid, $showfeedback = true)
{
global $DB;
if (is_object($courseorid)) {
$courseid = $courseorid->id;
$course = $courseorid;
} else {
$courseid = $courseorid;
if (!($course = $DB->get_record('course', array('id' => $courseid)))) {
return false;
}
}
$context = get_context_instance(CONTEXT_COURSE, $courseid);
// frontpage course can not be deleted!!
if ($courseid == SITEID) {
return false;
}
// make the course completely empty
remove_course_contents($courseid, $showfeedback);
// delete the course and related context instance
delete_context(CONTEXT_COURSE, $courseid);
$DB->delete_records("course", array("id" => $courseid));
//trigger events
$course->context = $context;
// you can not fetch context in the event because it was already deleted
events_trigger('course_deleted', $course);
return true;
}
示例2: 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();
}
delete_context(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));
}
}
示例3: process_category_group_node
private function process_category_group_node($groupnode, $xpath)
{
global $DB, $CFG;
$group = new stdClass();
if ($groupnode->getAttribute("recstatus") != 3) {
$groupname = $xpath->evaluate("description/short", $groupnode)->item(0);
if ($groupname) {
$group->name = $groupname->nodeValue;
}
$groupdescription = $xpath->evaluate("sourcedid/id", $groupnode)->item(0);
if ($groupdescription) {
$group->description = htmlspecialchars_decode($groupdescription->nodeValue);
}
$parentgroup = $xpath->evaluate("relationship/sourcedid/id", $groupnode)->item(0);
if ($parentgroup) {
$parentid = $DB->get_field_select('course_categories', 'id', 'description =\'
' . htmlspecialchars_decode($parentgroup->nodeValue) . '\'');
if ($parentid) {
$group->parent = $parentid;
}
} else {
$group->parent = 0;
}
$id = $DB->get_record_select('course_categories', 'description=\'' . $group->description . '\'');
if (!$id) {
$group->id = $DB->insert_record('course_categories', $group);
$classname = context_helper::get_class_for_level(CONTEXT_COURSECAT);
$group->context = $classname::instance($group->id, IGNORE_MISSING);
mark_context_dirty($group->context->path);
$DB->update_record('course_categories', $group);
fix_course_sortorder();
}
} else {
$groupname = $xpath->evaluate("description/short", $groupnode)->item(0);
if ($groupname) {
$group->name = $groupname->nodeValue;
}
$groupdescription = $xpath->evaluate("sourcedid/id", $groupnode)->item(0);
if ($groupdescription) {
$group->description = htmlspecialchars_decode($groupdescription->nodeValue);
}
$parentgroup = $xpath->evaluate("relationship/sourcedid/id", $groupnode)->item(0);
if ($parentgroup) {
$parentid = $DB->get_field_select('course_categories', 'id', 'description =\'
' . htmlspecialchars_decode($parentgroup->nodeValue) . '\'');
if ($parentid) {
$group->parent = $parentid;
}
} else {
$group->parent = 0;
}
$id = $DB->get_record_select('course_categories', 'description=\'' . $group->description . '\'');
if ($id) {
if ($children = $DB->get_records('course_categories', array('parent' => $id->id), 'sortorder ASC')) {
echo 'has cats!';
} else {
if ($courses = $DB->get_records('course', array('category' => $id->id), 'sortorder ASC')) {
echo 'has courses!';
} else {
$DB->delete_records('course_categories', array('id' => $id->id));
delete_context(CONTEXT_COURSECAT, $id->id);
}
}
}
}
}
示例4: test_everything_in_accesslib
//.........这里部分代码省略.........
$context = context::instance_by_id($contextid);
$this->assertSame(get_context_instance_by_id($contextid), $context);
$this->assertSame(get_context_instance($record->contextlevel, $record->instanceid), $context);
$this->assertSame(get_parent_contexts($context), $context->get_parent_context_ids());
if ($context->id == SYSCONTEXTID) {
$this->assertSame(get_parent_contextid($context), false);
} else {
$this->assertSame(get_parent_contextid($context), $context->get_parent_context()->id);
}
}
$CFG->debug = 0;
$children = get_child_contexts($systemcontext);
$CFG->debug = DEBUG_DEVELOPER;
$this->assertEquals(count($children), $DB->count_records('context')-1);
unset($children);
$DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK));
create_contexts();
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK)));
$DB->set_field('context', 'depth', 0, array('contextlevel'=>CONTEXT_BLOCK));
build_context_path();
$this->assertFalse($DB->record_exists('context', array('depth'=>0)));
$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));
cleanup_contexts();
$count = 1; //system
$count += $DB->count_records('user', array('deleted'=>0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEquals($DB->count_records('context'), $count);
context_helper::reset_caches();
preload_course_contexts($SITE->id);
$this->assertEquals(context_inspection::test_context_cache_size(), 1);
context_helper::reset_caches();
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$sql = "SELECT c.id $select FROM {course_categories} c $join";
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
context_instance_preload($record);
$record = (array)$record;
$this->assertEquals(1, count($record)); // only id left
}
$this->assertEquals(count($records), context_inspection::test_context_cache_size());
accesslib_clear_all_caches(true);
$DB->delete_records('cache_flags', array());
mark_context_dirty($systemcontext->path);
$dirty = get_cache_flags('accesslib/dirtycontexts', time()-2);
$this->assertTrue(isset($dirty[$systemcontext->path]));
accesslib_clear_all_caches(false);
$DB->delete_records('cache_flags', array());
$course = $DB->get_record('course', array('id'=>$testcourses[2]));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$oldpath = $context->path;
$miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
$categorycontext = context_coursecat::instance($miscid);
$course->category = $miscid;
$DB->update_record('course', $course);
context_moved($context, $categorycontext);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$this->assertEquals($context->get_parent_context(), $categorycontext);
$this->assertTrue($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2])));
delete_context(CONTEXT_COURSE, $testcourses[2]);
$this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$testcourses[2])));
$name = get_contextlevel_name(CONTEXT_COURSE);
$this->assertFalse(empty($name));
$context = get_context_instance(CONTEXT_COURSE, $testcourses[2]);
$name = print_context_name($context);
$this->assertFalse(empty($name));
$url = get_context_url($coursecontext);
$this->assertFalse($url instanceof modole_url);
$page = $DB->get_record('page', array('id'=>$testpages[7]));
$context = get_context_instance(CONTEXT_MODULE, $page->id);
$coursecontext = get_course_context($context);
$this->assertEquals($coursecontext->contextlevel, CONTEXT_COURSE);
$this->assertEquals(get_courseid_from_context($context), $page->course);
$caps = fetch_context_capabilities($systemcontext);
$this->assertTrue(is_array($caps));
unset($caps);
}
示例5: link_to_gdoc
//.........这里部分代码省略.........
$fromform->groupmode = 0;
// do not set groupmode
}
if (!course_allowed_module($course, $fromform->modulename)) {
print_error('moduledisable', '', '', $fromform->modulename);
}
// first add course_module record because we need the context
$newcm = new stdClass();
$newcm->course = $course->id;
$newcm->module = $fromform->module;
$newcm->instance = 0;
// not known yet, will be updated later (this is similar to restore code)
$newcm->visible = $fromform->visible;
$newcm->groupmode = $fromform->groupmode;
$newcm->groupingid = $fromform->groupingid;
$newcm->groupmembersonly = $fromform->groupmembersonly;
$completion = new completion_info($course);
if ($completion->is_enabled()) {
$newcm->completion = $fromform->completion;
$newcm->completiongradeitemnumber = $fromform->completiongradeitemnumber;
$newcm->completionview = $fromform->completionview;
$newcm->completionexpected = $fromform->completionexpected;
}
if (!empty($CFG->enableavailability)) {
$newcm->availablefrom = $fromform->availablefrom;
$newcm->availableuntil = $fromform->availableuntil;
$newcm->showavailability = $fromform->showavailability;
}
if (isset($fromform->showdescription)) {
$newcm->showdescription = $fromform->showdescription;
} else {
$newcm->showdescription = 0;
}
if (!($fromform->coursemodule = add_course_module($newcm))) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
$draftid_editor = file_get_submitted_draft_itemid('introeditor');
file_prepare_draft_area($draftid_editor, null, null, null, null);
$fromform->introeditor = array('text' => '', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
// TODO: add better default
}
if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
$introeditor = $fromform->introeditor;
unset($fromform->introeditor);
$fromform->intro = $introeditor['text'];
$fromform->introformat = $introeditor['format'];
}
$addinstancefunction = $fromform->modulename . "_add_instance";
$updateinstancefunction = $fromform->modulename . "_update_instance";
$returnfromfunc = $addinstancefunction($fromform, $mform);
// $returnfromfunc = url_add_instance($fromform, $mform);
if (!$returnfromfunc or !is_number($returnfromfunc)) {
// undo everything we can
$modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
delete_context(CONTEXT_MODULE, $fromform->coursemodule);
$DB->delete_records('course_modules', array('id' => $fromform->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $cw->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $fromform->modulename);
}
}
$fromform->instance = $returnfromfunc;
$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $fromform->coursemodule));
// update embedded links and save files
$modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
if (!empty($introeditor)) {
$fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
$DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance));
}
// course_modules and course_sections each contain a reference
// to each other, so we have to update one of them twice.
$sectionid = add_mod_to_section($fromform);
$DB->set_field('course_modules', 'section', $sectionid, array('id' => $fromform->coursemodule));
// make sure visibility is set correctly (in particular in calendar)
set_coursemodule_visible($fromform->coursemodule, $fromform->visible);
if (isset($fromform->cmidnumber)) {
//label
// set cm idnumber
set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber);
}
// Set up conditions
if ($CFG->enableavailability) {
condition_info::update_cm_from_form((object) array('id' => $fromform->coursemodule), $fromform, false);
}
$eventname = 'mod_created';
add_to_log($course->id, "course", "add mod", "../mod/{$fromform->modulename}/view.php?id={$fromform->coursemodule}", "{$fromform->modulename} {$fromform->instance}");
add_to_log($course->id, $fromform->modulename, "add", "view.php?id={$fromform->coursemodule}", "{$fromform->instance}", $fromform->coursemodule);
// Trigger mod_created/mod_updated event with information about this module.
$eventdata = new stdClass();
$eventdata->modulename = $fromform->modulename;
$eventdata->name = $fromform->name;
$eventdata->cmid = $fromform->coursemodule;
$eventdata->courseid = $course->id;
$eventdata->userid = $USER->id;
events_trigger($eventname, $eventdata);
rebuild_course_cache($course->id);
return 1;
}
示例6: delete_course
/**
* Delete a course, including all related data from the database,
* and any associated files.
*
* @global object
* @global object
* @param mixed $courseorid The id of the course or course object to delete.
* @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 delete_course($courseorid, $showfeedback = true)
{
global $DB;
if (is_object($courseorid)) {
$courseid = $courseorid->id;
$course = $courseorid;
} else {
$courseid = $courseorid;
if (!($course = $DB->get_record('course', array('id' => $courseid)))) {
return false;
}
}
$context = get_context_instance(CONTEXT_COURSE, $courseid);
// frontpage course can not be deleted!!
if ($courseid == SITEID) {
return false;
}
// make the course completely empty
remove_course_contents($courseid, $showfeedback);
// delete the course and related context instance
delete_context(CONTEXT_COURSE, $courseid);
// We will update the course's timemodified, as it will be passed to the course_deleted event,
// which should know about this updated property, as this event is meant to pass the full course record
$course->timemodified = time();
$DB->delete_records("course", array("id" => $courseid));
//trigger events
$course->context = $context;
// you can not fetch context in the event because it was already deleted
events_trigger('course_deleted', $course);
return true;
}
示例7: 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;
//.........这里部分代码省略.........
示例8: 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);
}
//.........这里部分代码省略.........
示例9: local_ltiprovider_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 local_ltiprovider_add_moduleinfo($moduleinfo, $course, $mform = null)
{
global $DB, $CFG;
$moduleinfo->course = $course->id;
$moduleinfo = local_ltiprovider_set_moduleinfo_defaults($moduleinfo);
if (!empty($course->groupmodeforce) or !isset($moduleinfo->groupmode)) {
$moduleinfo->groupmode = 0;
// Do not set groupmode.
}
if (!course_allowed_module($course, $moduleinfo->modulename)) {
print_error('moduledisable', '', '', $moduleinfo->modulename);
}
// 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;
$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)) {
$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);
delete_context(CONTEXT_MODULE, $moduleinfo->coursemodule);
$DB->delete_records('course_modules', array('id' => $moduleinfo->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $cw->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $cw->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);
// Make sure visibility is set correctly (in particular in calendar).
// Note: allow them to set it even without moodle/course:activityvisibility.
set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
if (isset($moduleinfo->cmidnumber)) {
// Label.
// Set cm idnumber - uniqueness is already verified by form validation.
set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
}
// Set up conditions.
if ($CFG->enableavailability) {
condition_info::update_cm_from_form((object) array('id' => $moduleinfo->coursemodule), $moduleinfo, false);
}
$eventname = 'mod_created';
add_to_log($course->id, "course", "add mod", "../mod/{$moduleinfo->modulename}/view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->modulename} {$moduleinfo->instance}");
add_to_log($course->id, $moduleinfo->modulename, "add", "view.php?id={$moduleinfo->coursemodule}", "{$moduleinfo->instance}", $moduleinfo->coursemodule);
//.........这里部分代码省略.........
示例10: test_everything_in_accesslib
//.........这里部分代码省略.........
$prevsize = context_inspection::test_context_cache_size();
for ($i = 0; $i < 100; $i++) {
context_user::instance($testusers[$i]);
$this->assertEqual(context_inspection::test_context_cache_size(), $prevsize);
}
context_user::instance($testusers[102]);
$this->assertEqual(context_inspection::test_context_cache_size(), $prevsize + 1);
unset($testusers);
// =================================================================
// ======= basic test of legacy functions ==========================
// =================================================================
// note: watch out, the fake site might be pretty borked already
$this->assertIdentical(get_system_context(), context_system::instance());
foreach ($DB->get_records('context') as $contextid => $record) {
$context = context::instance_by_id($contextid);
$this->assertIdentical(get_context_instance_by_id($contextid), $context);
$this->assertIdentical(get_context_instance($record->contextlevel, $record->instanceid), $context);
$this->assertIdentical(get_parent_contexts($context), $context->get_parent_context_ids());
if ($context->id == SYSCONTEXTID) {
$this->assertIdentical(get_parent_contextid($context), false);
} else {
$this->assertIdentical(get_parent_contextid($context), $context->get_parent_context()->id);
}
}
$children = get_child_contexts($systemcontext);
$this->assertEqual(count($children), $DB->count_records('context') - 1);
unset($children);
$DB->delete_records('context', array('contextlevel' => CONTEXT_BLOCK));
create_contexts();
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK)));
$DB->set_field('context', 'depth', 0, array('contextlevel' => CONTEXT_BLOCK));
build_context_path();
$this->assertFalse($DB->record_exists('context', array('depth' => 0)));
$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));
cleanup_contexts();
$count = 1;
//system
$count += $DB->count_records('user', array('deleted' => 0));
$count += $DB->count_records('course_categories');
$count += $DB->count_records('course');
$count += $DB->count_records('course_modules');
$count += $DB->count_records('block_instances');
$this->assertEqual($DB->count_records('context'), $count);
context_helper::reset_caches();
preload_course_contexts($SITE->id);
$this->assertEqual(context_inspection::test_context_cache_size(), 1);
context_helper::reset_caches();
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$sql = "SELECT c.id {$select} FROM {course_categories} c {$join}";
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
context_instance_preload($record);
$record = (array) $record;
$this->assertEqual(1, count($record));
// only id left
}
$this->assertEqual(count($records), context_inspection::test_context_cache_size());
accesslib_clear_all_caches(true);
$DB->delete_records('cache_flags', array());
mark_context_dirty($systemcontext->path);
$dirty = get_cache_flags('accesslib/dirtycontexts', time() - 2);
$this->assertTrue(isset($dirty[$systemcontext->path]));
accesslib_clear_all_caches(false);
$DB->delete_records('cache_flags', array());
$course = $DB->get_record('course', array('id' => $testcourses[2]));
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$oldpath = $context->path;
$miscid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
$categorycontext = context_coursecat::instance($miscid);
$course->category = $miscid;
$DB->update_record('course', $course);
context_moved($context, $categorycontext);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
$this->assertIdentical($context->get_parent_context(), $categorycontext);
$this->assertTrue($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2])));
delete_context(CONTEXT_COURSE, $testcourses[2]);
$this->assertFalse($DB->record_exists('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $testcourses[2])));
$name = get_contextlevel_name(CONTEXT_COURSE);
$this->assertFalse(empty($name));
$context = get_context_instance(CONTEXT_COURSE, $testcourses[2]);
$name = print_context_name($context);
$this->assertFalse(empty($name));
$url = get_context_url($coursecontext);
$this->assertFalse($url instanceof modole_url);
$page = $DB->get_record('page', array('id' => $testpages[7]));
$context = get_context_instance(CONTEXT_MODULE, $page->id);
$coursecontext = get_course_context($context);
$this->assertEqual($coursecontext->contextlevel, CONTEXT_COURSE);
$this->assertEqual(get_courseid_from_context($context), $page->course);
$caps = fetch_context_capabilities($systemcontext);
$this->assertTrue(is_array($caps));
unset($caps);
}
示例11: delete
public function delete()
{
global $CFG;
$result = false;
$muser = cm_get_moodleuserid($this->id);
if (empty($muser) || !is_primary_admin($muser)) {
$level = context_level_base::get_custom_context_level('user', 'block_curr_admin');
$result = attendance::delete_for_user($this->id);
$result = $result && curriculumstudent::delete_for_user($this->id);
$result = $result && instructor::delete_for_user($this->id);
$result = $result && student::delete_for_user($this->id);
$result = $result && student_grade::delete_for_user($this->id);
$result = $result && usertrack::delete_for_user($this->id);
$result = $result && usercluster::delete_for_user($this->id);
$result = $result && clusterassignment::delete_for_user($this->id);
$result = $result && waitlist::delete_for_user($this->id);
$result = $result && delete_context($level, $this->id);
// Delete Moodle user.
if ($muser = get_record('user', 'idnumber', $this->idnumber, 'mnethostid', $CFG->mnet_localhost_id, 'deleted', 0)) {
$result = $result && delete_user($muser);
}
$result = $result && parent::delete();
}
return $result;
}
示例12: delete
/**
* Perform the necessary actions required to "delete" a cluster from the system.
*
* @uses CURMAN
* @uses CFG
* @param none
* @return bool True on success, False otherwise.
*/
function delete($deletesubs = 0)
{
global $CURMAN, $CFG;
require_once CURMAN_DIRLOCATION . '/cluster/profile/lib.php';
$result = true;
$delete_ids = array();
$promote_ids = array();
$cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
if ($deletesubs > 0) {
/// Figure out all the sub-cluster ids and whether to delete or promote them
$LIKE = $CURMAN->db->sql_compare();
$cluster_context_instance = get_context_instance($cluster_context_level, $this->id);
$instance_id = $cluster_context_instance->id;
$instance_path = $cluster_context_instance->path;
$sql = "SELECT instanceid FROM {$CFG->prefix}context\n WHERE path {$LIKE} '{$instance_path}/%' ORDER BY instanceid DESC";
$clusters = get_records_sql($sql);
foreach ($clusters as $cluster) {
if ($deletesubs == 1) {
// This sub-cluster will be deleted
$delete_ids[] = $cluster->instanceid;
} else {
// This sub-cluster will be promoted
$promote_ids[] = $cluster->instanceid;
}
}
}
$delete_ids[] = $this->id;
// The specified cluster always gets deleted
foreach ($delete_ids as $delete_id) {
// Cascade to regular datarecords
$result = $result && clustercurriculum::delete_for_cluster($delete_id);
// in clustercurriculum
$result = $result && clustertrack::delete_for_cluster($delete_id);
// in clustercurriculum
$result = $result && clusterassignment::delete_for_cluster($delete_id);
$result = $result && usercluster::delete_for_cluster($delete_id);
$result = $result && delete_context($cluster_context_level, $delete_id);
// Cascade to all plugins
$plugins = $this->get_plugins();
foreach ($plugins as $plugin) {
require_once CURMAN_DIRLOCATION . '/cluster/' . $plugin . '/lib.php';
$result = $result && call_user_func('cluster_' . $plugin . '_delete_for_cluster', $delete_id);
}
$result = $result && datarecord::data_delete_record($delete_id);
// this record
}
if (count($promote_ids) > 0) {
foreach ($promote_ids as $promote_id) {
$cluster_data = get_record(CLSTTABLE, 'id', $promote_id);
$lower_depth = $cluster_data->depth - 1;
$select = "id='{$cluster_data->parent}'";
$parent_cnt = $CURMAN->db->count_records_select(CLSTTABLE, $select);
$newclusterdata = new stdClass();
$newclusterdata->id = $promote_id;
if ($parent_cnt < 1) {
/// Parent not found so this cluster will be top-level
$newclusterdata->parent = 0;
$newclusterdata->depth = 1;
} else {
/// A child cluster found so lets lower the depth
$newclusterdata->depth = $lower_depth;
}
$result = update_record(CLSTTABLE, $newclusterdata);
$cluster_context_level = context_level_base::get_custom_context_level('cluster', 'block_curr_admin');
$sql = "UPDATE {$CFG->prefix}context\n SET depth=0, path=NULL\n WHERE contextlevel='{$cluster_context_level}' AND instanceid='{$promote_id}'";
$feedback = "";
execute_sql($sql, $feedback);
}
build_context_path();
// Re-build the context table for all sub-clusters
}
return $result;
}
示例13: print_error
$newcm->showavailability = $fromform->showavailability;
}
if (!($fromform->coursemodule = add_course_module($newcm))) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) {
$introeditor = $fromform->introeditor;
unset($fromform->introeditor);
$fromform->intro = $introeditor['text'];
$fromform->introformat = $introeditor['format'];
}
$returnfromfunc = $addinstancefunction($fromform, $mform);
if (!$returnfromfunc or !is_number($returnfromfunc)) {
// undo everything we can
$modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
delete_context(CONTEXT_MODULE, $fromform->coursemodule);
$DB->delete_records('course_modules', array('id' => $fromform->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', "view.php?id={$course->id}#section-{$cw->section}");
} else {
print_error('cannotaddnewmodule', '', "view.php?id={$course->id}#section-{$cw->section}", $fromform->modulename);
}
}
$fromform->instance = $returnfromfunc;
$DB->set_field('course_modules', 'instance', $returnfromfunc, array('id' => $fromform->coursemodule));
// update embedded links and save files
$modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
if (!empty($introeditor)) {
$fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_' . $fromform->modulename, 'intro', 0, array('subdirs' => true), $introeditor['text']);
$DB->set_field($fromform->modulename, 'intro', $fromform->intro, array('id' => $fromform->instance));
}
示例14: delete
function delete()
{
$status = true;
if (!empty($this->id)) {
instructor::delete_for_class($this->id);
student::delete_for_class($this->id);
trackassignmentclass::delete_for_class($this->id);
classmoodlecourse::delete_for_class($this->id);
student_grade::delete_for_class($this->id);
attendance::delete_for_class($this->id);
taginstance::delete_for_class($this->id);
waitlist::delete_for_class($this->id);
classmoodlecourse::delete_for_class($this->id);
$level = context_level_base::get_custom_context_level('class', 'block_curr_admin');
$result = delete_context($level, $this->id);
$status = $this->data_delete_record();
}
return $status;
}
示例15: 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, $USER;
require_once $CFG->libdir . '/gradelib.php';
require_once $CFG->dirroot . '/blog/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) {
delete_event($event->id);
}
}
// 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.
delete_context(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 a mod_deleted event with information about this module.
$eventdata = new stdClass();
$eventdata->modulename = $modulename;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
events_trigger('mod_deleted', $eventdata);
add_to_log($cm->course, 'course', "delete mod", "view.php?id={$cm->course}", "{$modulename} {$cm->instance}", $cm->id);
rebuild_course_cache($cm->course, true);
}