function upgrade_blocks_plugins($continueto)
{
    global $CFG, $db;
    $blocktitles = array();
    $invalidblocks = array();
    $validblocks = array();
    $notices = array();
    //Count the number of blocks in db
    $blockcount = count_records('block');
    //If there isn't records. This is the first install, so I remember it
    if ($blockcount == 0) {
        $first_install = true;
    } else {
        $first_install = false;
    }
    $site = get_site();
    if (!($blocks = get_list_of_plugins('blocks', 'db'))) {
        error('No blocks installed!');
    }
    include_once $CFG->dirroot . '/blocks/moodleblock.class.php';
    if (!class_exists('block_base')) {
        error('Class block_base is not defined or file not found for /blocks/moodleblock.class.php');
    }
    foreach ($blocks as $blockname) {
        if ($blockname == 'NEWBLOCK') {
            // Someone has unzipped the template, ignore it
            continue;
        }
        if (!block_is_compatible($blockname)) {
            // This is an old-style block
            //$notices[] = 'Block '. $blockname .' is not compatible with the current version of Mooodle and needs to be updated by a programmer.';
            $invalidblocks[] = $blockname;
            continue;
        }
        $fullblock = $CFG->dirroot . '/blocks/' . $blockname;
        if (is_readable($fullblock . '/block_' . $blockname . '.php')) {
            include_once $fullblock . '/block_' . $blockname . '.php';
        } else {
            $notices[] = 'Block ' . $blockname . ': ' . $fullblock . '/block_' . $blockname . '.php was not readable';
            continue;
        }
        $oldupgrade = false;
        $newupgrade = false;
        if (@is_dir($fullblock . '/db/')) {
            if (@is_readable($fullblock . '/db/' . $CFG->dbtype . '.php')) {
                include_once $fullblock . '/db/' . $CFG->dbtype . '.php';
                // defines old upgrading function
                $oldupgrade = true;
            }
            if (@is_readable($fullblock . '/db/upgrade.php')) {
                include_once $fullblock . '/db/upgrade.php';
                // defines new upgrading function
                $newupgrade = true;
            }
        }
        $classname = 'block_' . $blockname;
        if (!class_exists($classname)) {
            $notices[] = 'Block ' . $blockname . ': ' . $classname . ' not implemented';
            continue;
        }
        // Here is the place to see if the block implements a constructor (old style),
        // an init() function (new style) or nothing at all (error time).
        $constructor = get_class_constructor($classname);
        if (empty($constructor)) {
            // No constructor
            $notices[] = 'Block ' . $blockname . ': class does not have a constructor';
            $invalidblocks[] = $blockname;
            continue;
        }
        $block = new stdClass();
        // This may be used to update the db below
        $blockobj = new $classname();
        // This is what we 'll be testing
        // Inherits from block_base?
        if (!is_subclass_of($blockobj, 'block_base')) {
            $notices[] = 'Block ' . $blockname . ': class does not inherit from block_base';
            continue;
        }
        // OK, it's as we all hoped. For further tests, the object will do them itself.
        if (!$blockobj->_self_test()) {
            $notices[] = 'Block ' . $blockname . ': self test failed';
            continue;
        }
        $block->version = $blockobj->get_version();
        if (!isset($block->version)) {
            $notices[] = 'Block ' . $blockname . ': has no version support. It must be updated by a programmer.';
            continue;
        }
        $block->name = $blockname;
        // The name MUST match the directory
        $blocktitle = $blockobj->get_title();
        if ($currblock = get_record('block', 'name', $block->name)) {
            if ($currblock->version == $block->version) {
                // do nothing
            } else {
                if ($currblock->version < $block->version) {
                    if (empty($updated_blocks)) {
                        $strblocksetup = get_string('blocksetup');
                        print_header($strblocksetup, $strblocksetup, build_navigation(array(array('name' => $strblocksetup, 'link' => null, 'type' => 'misc'))), '', upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
                    }
                    $updated_blocks = true;
                    upgrade_log_start();
                    print_heading('New version of ' . $blocktitle . ' (' . $block->name . ') exists');
                    @set_time_limit(0);
                    // To allow slow databases to complete the long SQL
                    /// Run de old and new upgrade functions for the module
                    $oldupgrade_function = $block->name . '_upgrade';
                    $newupgrade_function = 'xmldb_block_' . $block->name . '_upgrade';
                    /// First, the old function if exists
                    $oldupgrade_status = true;
                    if ($oldupgrade && function_exists($oldupgrade_function)) {
                        $db->debug = true;
                        $oldupgrade_status = $oldupgrade_function($currblock->version, $block);
                    } else {
                        if ($oldupgrade) {
                            notify('Upgrade function ' . $oldupgrade_function . ' was not available in ' . $fullblock . '/db/' . $CFG->dbtype . '.php');
                        }
                    }
                    /// Then, the new function if exists and the old one was ok
                    $newupgrade_status = true;
                    if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
                        $db->debug = true;
                        $newupgrade_status = $newupgrade_function($currblock->version, $block);
                    } else {
                        if ($newupgrade) {
                            notify('Upgrade function ' . $newupgrade_function . ' was not available in ' . $fullblock . '/db/upgrade.php');
                        }
                    }
                    $db->debug = false;
                    /// Now analyze upgrade results
                    if ($oldupgrade_status && $newupgrade_status) {
                        // No upgrading failed
                        // Set the block cron on upgrade
                        $block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
                        // OK so far, now update the block record
                        $block->id = $currblock->id;
                        if (!update_record('block', $block)) {
                            error('Could not update block ' . $block->name . ' record in block table!');
                        }
                        $component = 'block/' . $block->name;
                        if (!update_capabilities($component)) {
                            error('Could not update ' . $block->name . ' capabilities!');
                        }
                        events_update_definition($component);
                        notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
                    } else {
                        notify('Upgrading block ' . $block->name . ' from ' . $currblock->version . ' to ' . $block->version . ' FAILED!');
                    }
                    echo '<hr />';
                } else {
                    upgrade_log_start();
                    error('Version mismatch: block ' . $block->name . ' can\'t downgrade ' . $currblock->version . ' -> ' . $block->version . '!');
                }
            }
        } else {
            // block not installed yet, so install it
            // If it allows multiples, start with it enabled
            if ($blockobj->instance_allow_multiple()) {
                $block->multiple = 1;
            }
            // Set the block cron on install
            $block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
            // [pj] Normally this would be inline in the if, but we need to
            //      check for NULL (necessary for 4.0.5 <= PHP < 4.2.0)
            $conflictblock = array_search($blocktitle, $blocktitles);
            if ($conflictblock !== false && $conflictblock !== NULL) {
                // Duplicate block titles are not allowed, they confuse people
                // AND PHP's associative arrays ;)
                error('<strong>Naming conflict</strong>: block <strong>' . $block->name . '</strong> has the same title with an existing block, <strong>' . $conflictblock . '</strong>!');
            }
            if (empty($updated_blocks)) {
                $strblocksetup = get_string('blocksetup');
                print_header($strblocksetup, $strblocksetup, build_navigation(array(array('name' => $strblocksetup, 'link' => null, 'type' => 'misc'))), '', upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
            }
            $updated_blocks = true;
            upgrade_log_start();
            print_heading($block->name);
            $db->debug = true;
            @set_time_limit(0);
            // To allow slow databases to complete the long SQL
            /// Both old .sql files and new install.xml are supported
            /// but we priorize install.xml (XMLDB) if present
            $status = false;
            if (file_exists($fullblock . '/db/install.xml')) {
                $status = install_from_xmldb_file($fullblock . '/db/install.xml');
                //New method
            } else {
                if (file_exists($fullblock . '/db/' . $CFG->dbtype . '.sql')) {
                    $status = modify_database($fullblock . '/db/' . $CFG->dbtype . '.sql');
                    //Old method
                } else {
                    $status = true;
                }
            }
            $db->debug = false;
            if ($status) {
                if ($block->id = insert_record('block', $block)) {
                    $blockobj->after_install();
                    $component = 'block/' . $block->name;
                    if (!update_capabilities($component)) {
                        notify('Could not set up ' . $block->name . ' capabilities!');
                    }
                    events_update_definition($component);
                    notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
                    echo '<hr />';
                } else {
                    error($block->name . ' block could not be added to the block list!');
                }
            } else {
                error('Block ' . $block->name . ' tables could NOT be set up successfully!');
            }
        }
        $blocktitles[$block->name] = $blocktitle;
    }
    if (!empty($notices)) {
        upgrade_log_start();
        foreach ($notices as $notice) {
            notify($notice);
        }
    }
    // Finally, if we are in the first_install of BLOCKS (this means that we are
    // upgrading from Moodle < 1.3), put blocks in all existing courses.
    if ($first_install) {
        upgrade_log_start();
        //Iterate over each course
        if ($courses = get_records('course')) {
            foreach ($courses as $course) {
                $page = page_create_object(PAGE_COURSE_VIEW, $course->id);
                blocks_repopulate_page($page);
            }
        }
    }
    if (!empty($CFG->siteblocksadded)) {
        /// This is a once-off hack to make a proper upgrade
        upgrade_log_start();
        $page = page_create_object(PAGE_COURSE_VIEW, SITEID);
        blocks_repopulate_page($page);
        delete_records('config', 'name', 'siteblocksadded');
    }
    upgrade_log_finish();
    if (!empty($updated_blocks)) {
        print_continue($continueto);
        print_footer('none');
        die;
    }
}
function blocks_upgrade($oldversion = 0)
{
    global $CFG;
    $result = true;
    if ($oldversion < 2004041000 and $result) {
        $result = execute_sql("\n                                CREATE TABLE {$CFG->prefix}blocks \n                                (\n                                  id SERIAL8 PRIMARY KEY,\n                                  name varchar(40) NOT NULL default '',\n                                  version INT8 NOT NULL default '0',\n                                  cron INT8  NOT NULL default '0',\n                                  lastcron INT8  NOT NULL default '0',\n                                  visible int NOT NULL default '1'\n                                )\n        ");
    }
    if ($oldversion < 2004101900 && $result) {
        $result = execute_sql("CREATE TABLE {$CFG->prefix}block (\n                        id SERIAL8 PRIMARY KEY,\n                        name varchar(40) NOT NULL default '',\n                        version INT8 NOT NULL default '0',\n                        cron INT8 NOT NULL default '0',\n                        lastcron INT8 NOT NULL default '0',\n                        visible int NOT NULL default '1',\n                        multiple int NOT NULL default '0'\n                     ) \n                     ");
        if (!$result) {
            return false;
        }
        $records = get_records('blocks');
        if (!empty($records)) {
            foreach ($records as $block) {
                $block->multiple = 0;
                insert_record('block', $block, false);
            }
            execute_sql("SELECT setval('{$CFG->prefix}block_id_seq', (SELECT MAX(id) FROM {$CFG->prefix}block), true)");
        }
        execute_sql("DROP TABLE {$CFG->prefix}blocks");
        $result = execute_sql("CREATE TABLE {$CFG->prefix}block_instance (\n                        id SERIAL8 PRIMARY KEY,\n                        blockid INT8 not null default '0',\n                        pageid INT8 not null default '0',\n                        pagetype varchar(12) not null default '',\n                        position char not null default 'l' check (position in ('l', 'r')) ,\n                        weight int not null default '0',\n                        visible int not null default '0',\n                        configdata text not null default ''\n                    )");
        if (!$result) {
            return false;
        }
        $records = get_records('course', '', '', '', 'id, shortname, blockinfo');
        if (!empty($records)) {
            foreach ($records as $thiscourse) {
                // The @ suppresses a notice emitted if there is no : in the string
                @(list($left, $right) = split(':', $thiscourse->blockinfo));
                if (!empty($left)) {
                    $arr = explode(',', $left);
                    foreach ($arr as $weight => $blk) {
                        $instance = new stdClass();
                        $instance->blockid = abs($blk);
                        $instance->pageid = $thiscourse->id;
                        $instance->pagetype = PAGE_COURSE_VIEW;
                        $instance->position = BLOCK_POS_LEFT;
                        $instance->weight = $weight;
                        $instance->visible = $blk > 0 ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
                if (!empty($right)) {
                    $arr = explode(',', $right);
                    foreach ($arr as $weight => $blk) {
                        $instance = new stdClass();
                        $instance->blockid = abs($blk);
                        $instance->pageid = $thiscourse->id;
                        $instance->pagetype = PAGE_COURSE_VIEW;
                        $instance->position = BLOCK_POS_RIGHT;
                        $instance->weight = $weight;
                        $instance->visible = $blk > 0 ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
            }
        }
        execute_sql("ALTER TABLE {$CFG->prefix}course DROP COLUMN blockinfo");
    }
    if ($oldversion < 2004112900 && $result) {
        $result = $result && table_column('block_instance', 'pagetype', 'pagetype', 'varchar', '20', '');
        $result = $result && table_column('block_instance', 'position', 'position', 'varchar', '10', '');
    }
    if ($oldversion < 2005043000 && $result) {
        $records = get_records('block');
        if (!empty($records)) {
            foreach ($records as $block) {
                if (!block_is_compatible($block->name)) {
                    $block->visible = 0;
                    update_record('block', $block);
                    notify('The ' . $block->name . ' block has been disabled because it is not compatible with Moodle 1.5 and needs to be updated by a programmer.');
                }
            }
        }
    }
    if ($oldversion < 2005022401 && $result) {
        // Mass cleanup of bad upgrade scripts
        execute_sql("CREATE INDEX {$CFG->prefix}block_instance_pageid_idx ON {$CFG->prefix}block_instance (pageid)", false);
        // this one should be quiet...
        modify_database('', 'ALTER TABLE prefix_block_instance ALTER pagetype SET DEFAULT \'\'');
        modify_database('', 'ALTER TABLE prefix_block_instance ALTER position SET DEFAULT \'\'');
        modify_database('', 'ALTER TABLE prefix_block_instance ALTER pagetype SET NOT NULL');
        modify_database('', 'ALTER TABLE prefix_block_instance ALTER position SET NOT NULL');
    }
    if ($oldversion < 2005081600) {
        modify_database('', "CREATE TABLE prefix_block_pinned ( \n            id SERIAL8 PRIMARY KEY,\n            blockid INT8 NOT NULL default 0,\n            pagetype varchar(20) NOT NULL default '',\n            position varchar(10) NOT NULL default '',\n            weight INT NOT NULL default 0,\n            visible INT NOT NULL default 0,\n            configdata text NOT NULL default 0\n          );");
    }
    if ($oldversion < 2005090200) {
        execute_sql("CREATE INDEX {$CFG->prefix}block_instance_pagetype_idx ON {$CFG->prefix}block_instance (pagetype);", false);
        // do it silently, in case it's already there from 1.5
        modify_database('', 'CREATE INDEX prefix_block_pinned_pagetype_idx ON prefix_block_pinned (pagetype);');
    }
    //////  DO NOT ADD NEW THINGS HERE!!  USE upgrade.php and the lib/ddllib.php functions.
    //Finally, return result
    return $result;
}
Beispiel #3
0
function blocks_upgrade($oldversion = 0)
{
    global $CFG;
    $result = true;
    if ($oldversion < 2004041000 && $result) {
        $result = execute_sql("CREATE TABLE `{$CFG->prefix}blocks` (\n                        `id` int(10) unsigned NOT NULL auto_increment,\n                        `name` varchar(40) NOT NULL default '',\n                        `version` int(10) NOT NULL default '0',\n                        `cron` int(10) unsigned NOT NULL default '0',\n                        `lastcron` int(10) unsigned NOT NULL default '0',\n                        `visible` tinyint(1) NOT NULL default '1',\n                        PRIMARY KEY (`id`)\n                     ) \n                     COMMENT = 'To register and update all the available blocks'");
    }
    if ($oldversion < 2004101900 && $result) {
        $result = execute_sql("CREATE TABLE `{$CFG->prefix}block` (\n                        `id` int(10) unsigned NOT NULL auto_increment,\n                        `name` varchar(40) NOT NULL default '',\n                        `version` int(10) NOT NULL default '0',\n                        `cron` int(10) unsigned NOT NULL default '0',\n                        `lastcron` int(10) unsigned NOT NULL default '0',\n                        `visible` tinyint(1) NOT NULL default '1',\n                        `multiple` tinyint(1) NOT NULL default '0',\n                        PRIMARY KEY (`id`)\n                     ) \n                     COMMENT = 'To register and update all the available blocks'");
        if (!$result) {
            return false;
        }
        $records = get_records('blocks');
        if (!empty($records)) {
            foreach ($records as $block) {
                $block->multiple = 0;
                insert_record('block', $block, false);
            }
        }
        execute_sql("DROP TABLE `{$CFG->prefix}blocks`");
        $result = execute_sql("CREATE TABLE `{$CFG->prefix}block_instance` (\n                        `id` int(10) not null auto_increment,\n                        `blockid` int(10) not null default '0',\n                        `pageid` int(10) not null default '0',\n                        `pagetype` varchar(12) not null default '',\n                        `position` enum('l', 'r') not null default 'l',\n                        `weight` tinyint(3) not null default '0',\n                        `visible` tinyint(1) not null default '0',\n                        `configdata` text not null default '',\n                        \n                        PRIMARY KEY(`id`),\n                        INDEX pageid(`pageid`)\n                    )");
        if (!$result) {
            return false;
        }
        $records = get_records('course', '', '', '', 'id, shortname, blockinfo');
        if (!empty($records)) {
            foreach ($records as $thiscourse) {
                // The @ suppresses a notice emitted if there is no : in the string
                @(list($left, $right) = split(':', $thiscourse->blockinfo));
                if (!empty($left)) {
                    $arr = explode(',', $left);
                    foreach ($arr as $weight => $blk) {
                        $instance = new stdClass();
                        $instance->blockid = abs($blk);
                        $instance->pageid = $thiscourse->id;
                        $instance->pagetype = PAGE_COURSE_VIEW;
                        $instance->position = BLOCK_POS_LEFT;
                        $instance->weight = $weight;
                        $instance->visible = $blk > 0 ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
                if (!empty($right)) {
                    $arr = explode(',', $right);
                    foreach ($arr as $weight => $blk) {
                        $instance = new stdClass();
                        $instance->blockid = abs($blk);
                        $instance->pageid = $thiscourse->id;
                        $instance->pagetype = PAGE_COURSE_VIEW;
                        $instance->position = BLOCK_POS_RIGHT;
                        $instance->weight = $weight;
                        $instance->visible = $blk > 0 ? 1 : 0;
                        $instance->configdata = '';
                        insert_record('block_instance', $instance, false);
                    }
                }
            }
        }
        execute_sql("ALTER TABLE `{$CFG->prefix}course` DROP COLUMN blockinfo");
    }
    if ($oldversion < 2004112900 && $result) {
        $result = $result && table_column('block_instance', 'pagetype', 'pagetype', 'varchar', '20', '');
        $result = $result && table_column('block_instance', 'position', 'position', 'varchar', '10', '');
    }
    if ($oldversion < 2004112900 && $result) {
        execute_sql('UPDATE ' . $CFG->prefix . 'block_instance SET pagetype = \'' . PAGE_COURSE_VIEW . '\' WHERE pagetype = \'\'');
    }
    if ($oldversion < 2005043000 && $result) {
        $records = get_records('block');
        if (!empty($records)) {
            foreach ($records as $block) {
                if (!block_is_compatible($block->name)) {
                    $block->visible = 0;
                    update_record('block', $block);
                    notify('The ' . $block->name . ' block has been disabled because it is not compatible with Moodle 1.5 and needs to be updated by a programmer.');
                }
            }
        }
    }
    if ($oldversion < 2005081600) {
        $result = $result && modify_database('', "CREATE TABLE `prefix_block_pinned` (\n           `id` int(10) not null auto_increment,\n           `blockid` int(10) not null default '0',\n           `pagetype` varchar(20) not null default '',\n           `position` varchar(10) not null default '',\n           `weight` tinyint(3) not null default '0',\n           `visible` tinyint(1) not null default '0',\n           `configdata` text not null default '',\n           PRIMARY KEY(`id`)\n          ) TYPE=MyISAM;");
    }
    if ($oldversion < 2005090200) {
        execute_sql("ALTER TABLE {$CFG->prefix}block_instance ADD INDEX pagetype (pagetype);", false);
        // do it silently, in case it's already there from 1.5
        modify_database('', 'ALTER TABLE prefix_block_pinned ADD INDEX pagetype (pagetype);');
    }
    //////  DO NOT ADD NEW THINGS HERE!!  USE upgrade.php and the lib/ddllib.php functions.
    //Finally, return result
    return $result;
}
        $a->block = $strblockname;
        $a->directory = $CFG->dirroot . '/blocks/' . $block->name;
        notice(get_string('blockdeletefiles', '', $a), 'blocks.php');
    }
}
admin_externalpage_print_header();
print_heading($strmanageblocks);
/// Main display starts here
/// Get and sort the existing blocks
if (false === ($blocks = get_records('block'))) {
    error('No blocks found!');
    // Should never happen
}
$incompatible = array();
foreach ($blocks as $block) {
    if (!block_is_compatible($block->name)) {
        notify('Block ' . $block->name . ' is not compatible with the current version of Moodle and needs to be updated by a programmer.');
        $incompatible[] = $block;
        continue;
    }
    if (($blockobject = block_instance($block->name)) === false) {
        // Failed to load
        continue;
    }
    $blockbyname[$blockobject->get_title()] = $block->id;
    $blockobjects[$block->id] = $blockobject;
}
if (empty($blockbyname)) {
    error('One or more blocks are registered in the database, but they all failed to load!');
}
ksort($blockbyname);