Example #1
0
}
if (!empty($unprotect) && confirm_sesskey()) {
    if (!($block = $DB->get_record('block', array('id' => $unprotect)))) {
        print_error('blockdoesnotexist', 'error');
    }
    if (in_array($block->name, $undeletableblocktypes)) {
        $undeletableblocktypes = array_diff($undeletableblocktypes, array($block->name));
        set_config('undeletableblocktypes', implode(',', $undeletableblocktypes));
    }
    admin_get_root(true, false);
    // settings not required - only pages
}
if (!empty($delete) && confirm_sesskey()) {
    echo $OUTPUT->header();
    echo $OUTPUT->heading($strmanageblocks);
    if (!($block = blocks_get_record($delete))) {
        print_error('blockdoesnotexist', 'error');
    }
    if (get_string_manager()->string_exists('pluginname', "block_{$block->name}")) {
        $strblockname = get_string('pluginname', "block_{$block->name}");
    } else {
        $strblockname = $block->name;
    }
    if (!$confirm) {
        echo $OUTPUT->confirm(get_string('blockdeleteconfirm', '', $strblockname), 'blocks.php?delete=' . $block->id . '&confirm=1', 'blocks.php');
        echo $OUTPUT->footer();
        exit;
    } else {
        uninstall_plugin('block', $block->name);
        $a = new stdClass();
        $a->block = $strblockname;
function blocks_repopulate_page($page)
{
    global $CFG;
    $allblocks = blocks_get_record();
    if (empty($allblocks)) {
        error('Could not retrieve blocks from the database');
    }
    // Assemble the information to correlate block names to ids
    $idforname = array();
    foreach ($allblocks as $block) {
        $idforname[$block->name] = $block->id;
    }
    /// If the site override has been defined, it is the only valid one.
    if (!empty($CFG->defaultblocks_override)) {
        $blocknames = $CFG->defaultblocks_override;
    } else {
        $blocknames = $page->blocks_get_default();
    }
    $positions = $page->blocks_get_positions();
    $posblocks = explode(':', $blocknames);
    // Now one array holds the names of the positions, and the other one holds the blocks
    // that are going to go in each position. Luckily for us, both arrays are numerically
    // indexed and the indexes match, so we can work straight away... but CAREFULLY!
    // Ready to start creating block instances, but first drop any existing ones
    blocks_delete_all_on_page($page->get_type(), $page->get_id());
    // Here we slyly count $posblocks and NOT $positions. This can actually make a difference
    // if the textual representation has undefined slots in the end. So we only work with as many
    // positions were retrieved, not with all the page says it has available.
    $numpositions = count($posblocks);
    for ($i = 0; $i < $numpositions; ++$i) {
        $position = $positions[$i];
        $blocknames = explode(',', $posblocks[$i]);
        $weight = 0;
        foreach ($blocknames as $blockname) {
            $newinstance = new stdClass();
            $newinstance->blockid = $idforname[$blockname];
            $newinstance->pageid = $page->get_id();
            $newinstance->pagetype = $page->get_type();
            $newinstance->position = $position;
            $newinstance->weight = $weight;
            $newinstance->visible = 1;
            $newinstance->configdata = '';
            if (!empty($newinstance->blockid)) {
                // Only add block if it was recognized
                insert_record('block_instance', $newinstance);
                ++$weight;
            }
        }
    }
    return true;
}
Example #3
0
 /**
  * The list of block types that may be added to this page.
  *
  * @return array block name => record from block table.
  */
 public function get_addable_blocks()
 {
     $this->check_is_loaded();
     if (!is_null($this->addableblocks)) {
         return $this->addableblocks;
     }
     // Lazy load.
     $this->addableblocks = array();
     $allblocks = blocks_get_record();
     if (empty($allblocks)) {
         return $this->addableblocks;
     }
     $unaddableblocks = self::get_undeletable_block_types();
     $pageformat = $this->page->pagetype;
     foreach ($allblocks as $block) {
         if (!($bi = block_instance($block->name))) {
             continue;
         }
         if ($block->visible && !in_array($block->name, $unaddableblocks) && ($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) && blocks_name_allowed_in_format($block->name, $pageformat) && $bi->user_can_addto($this->page)) {
             $this->addableblocks[$block->name] = $block;
         }
     }
     return $this->addableblocks;
 }
Example #4
0
/**
 * Clear a course out completely, deleting all content
 * but don't delete the course itself
 *
 * @uses $CFG
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true)
{
    global $CFG;
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->libdir . '/gradelib.php';
    $result = true;
    if (!($course = get_record('course', 'id', $courseid))) {
        error('Course ID was incorrect (can\'t find it)');
    }
    $strdeleted = get_string('deleted');
    /// Clean up course formats (iterate through all formats in the even the course format was ever changed)
    $formats = get_list_of_plugins('course/format');
    foreach ($formats as $format) {
        $formatdelete = $format . '_course_format_delete_course';
        $formatlib = "{$CFG->dirroot}/course/format/{$format}/lib.php";
        if (file_exists($formatlib)) {
            include_once $formatlib;
            if (function_exists($formatdelete)) {
                if ($showfeedback) {
                    notify($strdeleted . ' ' . $format);
                }
                $formatdelete($course->id);
            }
        }
    }
    /// Delete every instance of every module
    if ($allmods = get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = get_records($modname, 'course', $course->id)) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                /// Delete activity context questions and question categories
                                question_delete_activity($cm, $showfeedback);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                notify('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')');
                                $result = false;
                            }
                            if ($cm) {
                                // delete cm and its context in correct order
                                delete_records('course_modules', 'id', $cm->id);
                                delete_context(CONTEXT_MODULE, $cm->id);
                            }
                        }
                    }
                } else {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, $showfeedback);
                }
            }
            if ($showfeedback) {
                notify($strdeleted . ' ' . $count . ' x ' . $modname);
            }
        }
    } else {
        error('No modules are installed!');
    }
    /// Give local code a chance to delete its references to this course.
    require_once $CFG->libdir . '/locallib.php';
    notify_local_delete_course($courseid, $showfeedback);
    /// Delete course blocks
    if ($blocks = get_records_sql("SELECT *\n                                   FROM {$CFG->prefix}block_instance\n                                   WHERE pagetype = '" . PAGE_COURSE_VIEW . "'\n                                   AND pageid = {$course->id}")) {
        if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' block_instance');
            }
            require_once $CFG->libdir . '/blocklib.php';
            foreach ($blocks as $block) {
                /// Delete any associated contexts for this block
                delete_context(CONTEXT_BLOCK, $block->id);
                // fix for MDL-7164
                // Get the block object and call instance_delete()
                if (!($record = blocks_get_record($block->blockid))) {
                    $result = false;
                    continue;
                }
                if (!($obj = block_instance($record->name, $block))) {
                    $result = false;
                    continue;
                }
                // Return value ignored, in core mods this does not do anything, but just in case
                // third party blocks might have stuff to clean up
                // we execute this anyway
                $obj->instance_delete();
            }
        } else {
            $result = false;
        }
    }
    /// Delete any groups, removing members and grouping/course links first.
    require_once $CFG->dirroot . '/group/lib.php';
    groups_delete_groupings($courseid, $showfeedback);
    groups_delete_groups($courseid, $showfeedback);
    /// Delete all related records in other tables that may have a courseid
    /// This array stores the tables that need to be cleared, as
    /// table_name => column_name that contains the course id.
    $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'backup_courses' => 'courseid', 'user_lastaccess' => 'courseid', 'backup_log' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        if (delete_records($table, $col, $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' ' . $table);
            }
        } else {
            $result = false;
        }
    }
    /// Clean up metacourse stuff
    if ($course->metacourse) {
        delete_records("course_meta", "parent_course", $course->id);
        sync_metacourse($course->id);
        // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
        if ($showfeedback) {
            notify("{$strdeleted} course_meta");
        }
    } else {
        if ($parents = get_records("course_meta", "child_course", $course->id)) {
            foreach ($parents as $parent) {
                remove_from_metacourse($parent->parent_course, $parent->child_course);
                // this will do the unenrolments as well.
            }
            if ($showfeedback) {
                notify("{$strdeleted} course_meta");
            }
        }
    }
    /// Delete questions and question categories
    question_delete_course($course, $showfeedback);
    /// Remove all data from gradebook
    $context = get_context_instance(CONTEXT_COURSE, $courseid);
    remove_course_grades($courseid, $showfeedback);
    remove_grade_letters($context, $showfeedback);
    return $result;
}
function backup_course_blocks($bf, $preferences)
{
    global $CFG;
    $status = true;
    // Read all of the block table
    $blocks = blocks_get_record();
    $pages = array();
    $pages[] = page_create_object(PAGE_COURSE_VIEW, $preferences->backup_course);
    if (!empty($CFG->showblocksonmodpages)) {
        // get course structure
        $course = get_record('course', 'id', $preferences->backup_course);
        $modinfo =& get_fast_modinfo($course);
        foreach ($preferences->mods as $module) {
            if (!$module->backup) {
                continue;
            }
            if (empty($modinfo->instances[$module->name])) {
                continue;
            }
            $pagetypes = page_import_types('mod/' . $module->name . '/');
            if (empty($pagetypes)) {
                continue;
            }
            foreach ($pagetypes as $pagetype) {
                foreach ($modinfo->instances[$module->name] as $cm) {
                    if (!empty($module->instances[$cm->instance]->backup)) {
                        $pages[] = page_create_object($pagetype, $cm->instance);
                    }
                }
            }
        }
    }
    //Blocks open tag
    fwrite($bf, start_tag('BLOCKS', 2, true));
    foreach ($pages as $page) {
        if ($instances = blocks_get_by_page($page)) {
            //Iterate over every block
            foreach ($instances as $position) {
                foreach ($position as $instance) {
                    //If we somehow have a block with an invalid id, skip it
                    if (empty($blocks[$instance->blockid]->name)) {
                        continue;
                    }
                    $blockname = $blocks[$instance->blockid]->name;
                    if (!($blockobj = block_instance($blockname, $instance))) {
                        // Invalid block
                        continue;
                    }
                    // encode absolute links in block config
                    $instance->configdata = $blockobj->get_backup_encoded_config();
                    //Begin Block
                    fwrite($bf, start_tag('BLOCK', 3, true));
                    fwrite($bf, full_tag('ID', 4, false, $instance->id));
                    fwrite($bf, full_tag('NAME', 4, false, $blockname));
                    fwrite($bf, full_tag('PAGEID', 4, false, $instance->pageid));
                    fwrite($bf, full_tag('PAGETYPE', 4, false, $instance->pagetype));
                    fwrite($bf, full_tag('POSITION', 4, false, $instance->position));
                    fwrite($bf, full_tag('WEIGHT', 4, false, $instance->weight));
                    fwrite($bf, full_tag('VISIBLE', 4, false, $instance->visible));
                    fwrite($bf, full_tag('CONFIGDATA', 4, false, $instance->configdata));
                    // Write instance data if needed
                    if ($blockobj->backuprestore_instancedata_used()) {
                        fwrite($bf, start_tag('INSTANCEDATA', 4, true));
                        $status = $blockobj->instance_backup($bf, $preferences);
                        fwrite($bf, end_tag('INSTANCEDATA', 4, true));
                    }
                    $context = get_context_instance(CONTEXT_BLOCK, $instance->id);
                    write_role_overrides_xml($bf, $context, 4);
                    /// write role_assign code here
                    write_role_assignments_xml($bf, $preferences, $context, 4);
                    //End Block
                    fwrite($bf, end_tag('BLOCK', 3, true));
                }
            }
        }
    }
    //Blocks close tag
    $status = fwrite($bf, end_tag('BLOCKS', 2, true));
    return $status;
}
 /**
  * The list of block types that may be added to this page.
  *
  * @return array block name => record from block table.
  */
 public function get_addable_blocks()
 {
     $this->check_is_loaded();
     if (!is_null($this->addableblocks)) {
         return $this->addableblocks;
     }
     // Lazy load.
     $this->addableblocks = array();
     $allblocks = blocks_get_record();
     if (empty($allblocks)) {
         return $this->addableblocks;
     }
     $pageformat = $this->page->pagetype;
     foreach ($allblocks as $block) {
         if ($block->visible && (block_method_result($block->name, 'instance_allow_multiple') || !$this->is_block_present($block->name)) && blocks_name_allowed_in_format($block->name, $pageformat) && block_method_result($block->name, 'user_can_addto', $this->page)) {
             $this->addableblocks[$block->name] = $block;
         }
     }
     return $this->addableblocks;
 }
Example #7
0
function array_for_blocks($blocks)
{
    if (empty($blocks)) {
        return array();
    }
    $return_array = array();
    $block_types = blocks_get_record();
    foreach ($blocks as $block) {
        // Only use roles if they're enabled.
        $can_see_hidden_blocks = FALSE;
        if ($CFG->rolesactive) {
            $context = get_context_instance(CONTEXT_COURSE, $course->id);
            $can_see_hidden_blocks = (has_capability('moodle/site:manageblocks', $context) or $block->visible);
        } else {
            $can_see_hidden_blocks = isteacher($course->id, $USER->id, true);
        }
        $show_block = ($block->visible or $can_see_hidden_blocks);
        if ($show_block) {
            $this_block_type = $block_types[intval($block->blockid)];
            if ($this_block_type->name == "calendar_upcoming") {
                $block_array = array();
                $block_array["name"] = $this_block_type->name;
                $return_array[] = $block_array;
            } else {
                if ($this_block_type->name == "calendar_month") {
                    $block_array = array();
                    $block_array["name"] = $this_block_type->name;
                    $return_array[] = $block_array;
                }
            }
        }
    }
    return $return_array;
}
Example #8
0
/**
 * This function removes blocks/modules from a page in the case of
 * blocks it also removes the entry from block_instance 
 *
 * @param object $pageitem a fully populated page_item object
 * @uses $CFG
 * @uses $COURSE
 */
function page_block_delete($pageitem)
{
    global $CFG, $COURSE;
    require_once $CFG->libdir . '/blocklib.php';
    // we leave module cleanup to the manage modules tab... blocks need some help though.
    if (!empty($pageitem->blockinstance)) {
        if ($blockinstance = get_record('block_instance', 'id', $pageitem->blockinstance)) {
            // see if this is the last reference to the blockinstance
            $count = count_records('format_page_items', 'blockinstance', $pageitem->blockinstance);
            if ($count == 1) {
                if ($block = blocks_get_record($blockinstance->blockid)) {
                    if ($block->name != 'course_menu') {
                        // At this point, the format has done all of its own checking,
                        // hand it off to block API
                        blocks_delete_instance($blockinstance);
                    }
                }
            }
        }
    }
    delete_records('format_page_items', 'id', $pageitem->id);
    execute_sql("UPDATE {$CFG->prefix}format_page_items\n                    SET sortorder = sortorder - 1\n                  WHERE pageid = {$pageitem->pageid}\n                    AND position = '{$pageitem->position}'\n                    AND sortorder > {$pageitem->sortorder}", false);
}
Example #9
0
 //var_dump($context);
 //$block_instances = $DB->get_records('block_instances', array('pagetypepattern' => 'site-index'));
 //$block_instances = $DB->get_records('block_instances', array('pagetypepattern' => 'site-index'));
 /*
 $json_output["site_blocks"] = array_for_blocks($blocks);
 foreach ($blocks as $block) {
 	if($block->blockname == "site_main_menu") {
 		$json_output["main_menu_visible"] = $block;
 		break;
 	}
 }
 */
 //var_dump($DB->get_recordset_sql("SELECT id, blockname FROM $DB->prefix_block_instances WHERE pagetypepattern = 'site-index'"));
 //}
 //else {
 $block_types = blocks_get_record();
 $blocks = get_records_select('block_instance', "pageid = '1' AND pagetype = 'course-view'");
 $pinned_blocks = get_records_select('block_pinned', "pagetype = 'course-view'");
 foreach ($pinned_blocks as $pinned_block) {
     $blocks[] = $pinned_block;
 }
 $json_output["site_blocks"] = array_for_blocks($blocks);
 $site_menu_id = NULL;
 foreach ($block_types as $block_type) {
     if ($block_type->name == "site_main_menu") {
         $site_menu_id = $block_type->id;
     }
 }
 $main_menu_block = get_records_select('block_instance', "pageid = '1' AND pagetype = 'course-view' AND blockid = '" . $site_menu_id . "'", 'position, weight');
 $main_menu_block = $main_menu_block[1];
 if ($main_menu_block->visible) {
Example #10
0
function backup_course_blocks($bf, $preferences)
{
    global $CFG;
    $status = true;
    // Read all of the block table
    $blocks = blocks_get_record();
    $pages = array();
    $pages[] = page_create_object(PAGE_COURSE_VIEW, $preferences->backup_course);
    // Let's see if we have to backup blocks from modules
    $modulerecords = get_records_sql('SELECT name, id FROM ' . $CFG->prefix . 'modules');
    foreach ($preferences->mods as $module) {
        if (!$module->backup) {
            continue;
        }
        $cmods = get_records_select('course_modules', 'course = ' . $preferences->backup_course . ' AND module = ' . $modulerecords[$module->name]->id);
        if (empty($cmods)) {
            continue;
        }
        $pagetypes = page_import_types('mod/' . $module->name . '/');
        if (empty($pagetypes)) {
            continue;
        }
        foreach ($pagetypes as $pagetype) {
            foreach ($cmods as $cmod) {
                $pages[] = page_create_object($pagetype, $cmod->instance);
            }
        }
    }
    //Blocks open tag
    fwrite($bf, start_tag('BLOCKS', 2, true));
    while ($page = array_pop($pages)) {
        if ($instances = blocks_get_by_page($page)) {
            //Iterate over every block
            foreach ($instances as $position) {
                foreach ($position as $instance) {
                    //If we somehow have a block with an invalid id, skip it
                    if (empty($blocks[$instance->blockid]->name)) {
                        continue;
                    }
                    //Begin Block
                    fwrite($bf, start_tag('BLOCK', 3, true));
                    fwrite($bf, full_tag('ID', 4, false, $instance->id));
                    fwrite($bf, full_tag('NAME', 4, false, $blocks[$instance->blockid]->name));
                    fwrite($bf, full_tag('PAGEID', 4, false, $instance->pageid));
                    fwrite($bf, full_tag('PAGETYPE', 4, false, $instance->pagetype));
                    fwrite($bf, full_tag('POSITION', 4, false, $instance->position));
                    fwrite($bf, full_tag('WEIGHT', 4, false, $instance->weight));
                    fwrite($bf, full_tag('VISIBLE', 4, false, $instance->visible));
                    fwrite($bf, full_tag('CONFIGDATA', 4, false, $instance->configdata));
                    $context = get_context_instance(CONTEXT_BLOCK, $instance->id);
                    write_role_overrides_xml($bf, $context, 4);
                    /// write role_assign code here
                    write_role_assignments_xml($bf, $preferences, $context, 4);
                    //End Block
                    fwrite($bf, end_tag('BLOCK', 3, true));
                }
            }
        }
    }
    //Blocks close tag
    $status = fwrite($bf, end_tag('BLOCKS', 2, true));
    return $status;
}
Example #11
0
<?php

// $Id: block.php,v 1.18.4.1 2007/12/19 17:38:40 skodak Exp $
// block.php - allows admin to edit all local configuration variables for a block
require_once '../config.php';
require_once $CFG->libdir . '/adminlib.php';
require_once $CFG->libdir . '/blocklib.php';
$blockid = required_param('block', PARAM_INT);
if (!($blockrecord = blocks_get_record($blockid))) {
    error('This block does not exist');
}
admin_externalpage_setup('blocksetting' . $blockrecord->name);
$block = block_instance($blockrecord->name);
if ($block === false) {
    error('Problem in instantiating block object');
}
// Define the data we're going to silently include in the instance config form here,
// so we can strip them from the submitted data BEFORE handling it.
$hiddendata = array('block' => $blockid, 'sesskey' => $USER->sesskey);
/// If data submitted, then process and store.
if ($config = data_submitted()) {
    if (!confirm_sesskey()) {
        error(get_string('confirmsesskeybad', 'error'));
    }
    if (!$block->has_config()) {
        error('This block does not support global configuration');
    }
    $remove = array_keys($hiddendata);
    foreach ($remove as $item) {
        unset($config->{$item});
    }
Example #12
0
<?php

// $Id: block.php,v 1.16.6.1 2007/04/20 07:49:39 nicolasconnault Exp $
// block.php - allows admin to edit all local configuration variables for a block
require_once '../config.php';
require_once $CFG->libdir . '/adminlib.php';
$adminroot = admin_get_root();
admin_externalpage_setup('manageblocks', $adminroot);
require_once $CFG->libdir . '/blocklib.php';
$blockid = required_param('block', PARAM_INT);
if (($blockrecord = blocks_get_record($blockid)) === false) {
    error('This block does not exist');
}
$block = block_instance($blockrecord->name);
if ($block === false) {
    error('Problem in instantiating block object');
}
// Define the data we're going to silently include in the instance config form here,
// so we can strip them from the submitted data BEFORE handling it.
$hiddendata = array('block' => $blockid, 'sesskey' => $USER->sesskey);
/// If data submitted, then process and store.
if ($config = data_submitted()) {
    if (!confirm_sesskey()) {
        error(get_string('confirmsesskeybad', 'error'));
    }
    if (!$block->has_config()) {
        error('This block does not support global configuration');
    }
    $remove = array_keys($hiddendata);
    foreach ($remove as $item) {
        unset($config->{$item});
Example #13
0
function restore_decode_content_links($restore)
{
    global $CFG;
    $status = true;
    if (!defined('RESTORE_SILENTLY')) {
        echo "<ul>";
    }
    // Restore links in modules.
    foreach ($restore->mods as $name => $info) {
        //If the module is being restored
        if (isset($info->restore) && $info->restore == 1) {
            //Check if the xxxx_decode_content_links_caller exists
            include_once "{$CFG->dirroot}/mod/{$name}/restorelib.php";
            $function_name = $name . "_decode_content_links_caller";
            if (function_exists($function_name)) {
                if (!defined('RESTORE_SILENTLY')) {
                    echo "<li>" . get_string("from") . " " . get_string("modulenameplural", $name);
                }
                $status = $function_name($restore);
                if (!defined('RESTORE_SILENTLY')) {
                    echo '</li>';
                }
            }
        }
    }
    // Process all html text also in blocks too
    if (!defined('RESTORE_SILENTLY')) {
        echo '<li>' . get_string('from') . ' ' . get_string('blocks');
    }
    if (!empty($restore->blockinstanceids)) {
        $blocks = blocks_get_record();
        $instances = get_records_list('block_instance', 'id', implode(',', $restore->blockinstanceids), '', 'id,blockid,configdata');
        foreach ($instances as $instance) {
            if (!isset($blocks[$instance->blockid]->blockobject)) {
                $blocks[$instance->blockid]->blockobject = block_instance($blocks[$instance->blockid]->name);
            }
            $config = unserialize(base64_decode($instance->configdata));
            if ($blocks[$instance->blockid]->blockobject->restore_decode_absolute_links_in_config($config)) {
                $instance->configdata = base64_encode(serialize($config));
                $status = $status && update_record('block_instance', $instance);
            }
        }
    }
    if (!defined('RESTORE_SILENTLY')) {
        echo '</li>';
    }
    // Restore links in questions.
    require_once "{$CFG->dirroot}/question/restorelib.php";
    if (!defined('RESTORE_SILENTLY')) {
        echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
    }
    $status = question_decode_content_links_caller($restore);
    if (!defined('RESTORE_SILENTLY')) {
        echo '</li>';
    }
    if (!defined('RESTORE_SILENTLY')) {
        echo "</ul>";
    }
    return $status;
}
Example #14
0
/**
 * Clear a course out completely, deleting all content
 * but don't delete the course itself
 *
 * @uses $CFG
 * @param int $courseid The id of the course that is being deleted
 * @param bool $showfeedback Whether to display notifications of each action the function performs.
 * @return bool true if all the removals succeeded. false if there were any failures. If this
 *             method returns false, some of the removals will probably have succeeded, and others
 *             failed, but you have no way of knowing which.
 */
function remove_course_contents($courseid, $showfeedback = true)
{
    global $CFG;
    $result = true;
    if (!($course = get_record('course', 'id', $courseid))) {
        error('Course ID was incorrect (can\'t find it)');
    }
    $strdeleted = get_string('deleted');
    /// First delete every instance of every module
    if ($allmods = get_records('modules')) {
        foreach ($allmods as $mod) {
            $modname = $mod->name;
            $modfile = $CFG->dirroot . '/mod/' . $modname . '/lib.php';
            $moddelete = $modname . '_delete_instance';
            // Delete everything connected to an instance
            $moddeletecourse = $modname . '_delete_course';
            // Delete other stray stuff (uncommon)
            $count = 0;
            if (file_exists($modfile)) {
                include_once $modfile;
                if (function_exists($moddelete)) {
                    if ($instances = get_records($modname, 'course', $course->id)) {
                        foreach ($instances as $instance) {
                            if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
                                delete_context(CONTEXT_MODULE, $cm->id);
                            }
                            if ($moddelete($instance->id)) {
                                $count++;
                            } else {
                                notify('Could not delete ' . $modname . ' instance ' . $instance->id . ' (' . format_string($instance->name) . ')');
                                $result = false;
                            }
                        }
                    }
                } else {
                    notify('Function ' . $moddelete . '() doesn\'t exist!');
                    $result = false;
                }
                if (function_exists($moddeletecourse)) {
                    $moddeletecourse($course, $showfeedback);
                }
            }
            if ($showfeedback) {
                notify($strdeleted . ' ' . $count . ' x ' . $modname);
            }
        }
    } else {
        error('No modules are installed!');
    }
    /// Give local code a chance to delete its references to this course.
    require_once 'locallib.php';
    notify_local_delete_course($courseid, $showfeedback);
    /// Delete course blocks
    if ($blocks = get_records_sql("SELECT *\n                                   FROM {$CFG->prefix}block_instance\n                                   WHERE pagetype = '" . PAGE_COURSE_VIEW . "'\n                                   AND pageid = {$course->id}")) {
        if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' block_instance');
            }
            require_once $CFG->libdir . '/blocklib.php';
            foreach ($blocks as $block) {
                /// Delete any associated contexts for this block
                // Block instances are rarely created. Since the block instance is gone from the above delete
                // statement, calling delete_context() will generate a warning as get_context_instance could
                // no longer create the context as the block is already gone.
                if (record_exists('context', 'contextlevel', CONTEXT_BLOCK, 'instanceid', $block->id)) {
                    delete_context(CONTEXT_BLOCK, $block->id);
                }
                // fix for MDL-7164
                // Get the block object and call instance_delete()
                if (!($record = blocks_get_record($block->blockid))) {
                    $result = false;
                    continue;
                }
                if (!($obj = block_instance($record->name, $block))) {
                    $result = false;
                    continue;
                }
                // Return value ignored, in core mods this does not do anything, but just in case
                // third party blocks might have stuff to clean up
                // we execute this anyway
                $obj->instance_delete();
            }
        } else {
            $result = false;
        }
    }
    /// Delete any groups, removing members and grouping/course links first.
    //TODO: If groups or groupings are to be shared between courses, think again!
    if ($groupids = groups_get_groups($course->id)) {
        foreach ($groupids as $groupid) {
            if (groups_remove_all_members($groupid)) {
                if ($showfeedback) {
                    notify($strdeleted . ' groups_members');
                }
            } else {
                $result = false;
            }
            /// Delete any associated context for this group ??
            delete_context(CONTEXT_GROUP, $groupid);
            if (groups_delete_group($groupid)) {
                if ($showfeedback) {
                    notify($strdeleted . ' groups');
                }
            } else {
                $result = false;
            }
        }
    }
    /// Delete any groupings.
    $result = groups_delete_all_groupings($course->id);
    if ($result && $showfeedback) {
        notify($strdeleted . ' groupings');
    }
    /// Delete all related records in other tables that may have a courseid
    /// This array stores the tables that need to be cleared, as
    /// table_name => column_name that contains the course id.
    $tablestoclear = array('event' => 'courseid', 'log' => 'course', 'course_sections' => 'course', 'course_modules' => 'course', 'grade_category' => 'courseid', 'grade_exceptions' => 'courseid', 'grade_item' => 'courseid', 'grade_letter' => 'courseid', 'grade_preferences' => 'courseid', 'backup_courses' => 'courseid', 'backup_log' => 'courseid');
    foreach ($tablestoclear as $table => $col) {
        if (delete_records($table, $col, $course->id)) {
            if ($showfeedback) {
                notify($strdeleted . ' ' . $table);
            }
        } else {
            $result = false;
        }
    }
    /// Clean up metacourse stuff
    if ($course->metacourse) {
        delete_records("course_meta", "parent_course", $course->id);
        sync_metacourse($course->id);
        // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
        if ($showfeedback) {
            notify("{$strdeleted} course_meta");
        }
    } else {
        if ($parents = get_records("course_meta", "child_course", $course->id)) {
            foreach ($parents as $parent) {
                remove_from_metacourse($parent->parent_course, $parent->child_course);
                // this will do the unenrolments as well.
            }
            if ($showfeedback) {
                notify("{$strdeleted} course_meta");
            }
        }
    }
    /// Delete questions and question categories
    include_once $CFG->libdir . '/questionlib.php';
    question_delete_course($course, $showfeedback);
    /// Delete all roles and overiddes in the course context (but keep the course context)
    if ($courseid != SITEID) {
        delete_context(CONTEXT_COURSE, $course->id);
    }
    // fix for MDL-9016
    // clear the cache because the course context is deleted, and
    // we don't want to write assignment, overrides and context_rel table
    // with this old context id!
    get_context_instance('clearcache');
    return $result;
}