Exemple #1
0
/**
 * Book module upgrade task
 *
 * @param int $oldversion the version we are upgrading from
 * @return bool always true
 */
function xmldb_book_upgrade($oldversion) {
    global $CFG, $DB;

    $dbman = $DB->get_manager();

    // Moodle v2.2.0 release upgrade line
    // Put any upgrade step following this

    // Moodle v2.3.0 release upgrade line
    // Put any upgrade step following this

    // Note: The next steps (up to 2012090408 included, are a "replay" of old upgrade steps,
    // because some sites updated to Moodle 2.3 didn't have the latest contrib mod_book
    // installed, so some required changes were missing.
    //
    // All the steps are run conditionally so sites upgraded from latest contrib mod_book or
    // new (2.3 and upwards) sites won't get affected.
    //
    // Warn: It will be safe to delete these steps once Moodle 2.5 (not 2.4!) is declared as minimum
    // requirement (environment.xml) in some future Moodle 2.x version. Never, never, before!
    //
    // See MDL-35297 and commit msg for more information.

    if ($oldversion < 2012090401) {
        // Rename field summary on table book to intro
        $table = new xmldb_table('book');
        $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');

        // Launch rename field summary
        if ($dbman->field_exists($table, $field)) {
            $dbman->rename_field($table, $field, 'intro');
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090401, 'book');
    }

    if ($oldversion < 2012090402) {
        // Define field introformat to be added to book
        $table = new xmldb_table('book');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');

        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
            // Conditionally migrate to html format in intro
            // Si está activo el htmleditor!!!!!
            if ($CFG->texteditors !== 'textarea') {
                $rs = $DB->get_recordset('book', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
                foreach ($rs as $b) {
                    $b->intro       = text_to_html($b->intro, false, false, true);
                    $b->introformat = FORMAT_HTML;
                    $DB->update_record('book', $b);
                    upgrade_set_timeout();
                }
                unset($b);
                $rs->close();
            }
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090402, 'book');
    }

    if ($oldversion < 2012090403) {
        // Define field introformat to be added to book
        $table = new xmldb_table('book_chapters');
        $field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'content');

        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);

            $DB->set_field('book_chapters', 'contentformat', FORMAT_HTML, array());
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090403, 'book');
    }

    if ($oldversion < 2012090404) {
        require_once("$CFG->dirroot/mod/book/db/upgradelib.php");

        $sqlfrom = "FROM {book} b
                    JOIN {modules} m ON m.name = 'book'
                    JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = b.id)";

        $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");

        if ($rs = $DB->get_recordset_sql("SELECT b.id, b.course, cm.id AS cmid $sqlfrom ORDER BY b.course, b.id")) {

            $pbar = new progress_bar('migratebookfiles', 500, true);

            $i = 0;
            foreach ($rs as $book) {
                $i++;
                upgrade_set_timeout(360); // set up timeout, may also abort execution
                $pbar->update($i, $count, "Migrating book files - $i/$count.");

                $context = context_module::instance($book->cmid);

                mod_book_migrate_moddata_dir_to_legacy($book, $context, '/');

                // remove dirs if empty
                @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/$book->id/");
                @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/book/");
                @rmdir("$CFG->dataroot/$book->course/$CFG->moddata/");
                @rmdir("$CFG->dataroot/$book->course/");
            }
            $rs->close();
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090404, 'book');
    }

    if ($oldversion < 2012090405) {
        // Define field disableprinting to be dropped from book
        $table = new xmldb_table('book');
        $field = new xmldb_field('disableprinting');

        // Conditionally launch drop field disableprinting
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090405, 'book');
    }

    if ($oldversion < 2012090406) {
        unset_config('book_tocwidth');

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090406, 'book');
    }

    if ($oldversion < 2012090407) {
        require_once("$CFG->dirroot/mod/book/db/upgradelib.php");

        mod_book_migrate_all_areas();

        upgrade_mod_savepoint(true, 2012090407, 'book');
    }

    if ($oldversion < 2012090408) {

        // Define field revision to be added to book
        $table = new xmldb_table('book');
        $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'customtitles');

        // Conditionally launch add field revision
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // book savepoint reached
        upgrade_mod_savepoint(true, 2012090408, 'book');
    }
    // End of MDL-35297 "replayed" steps.

    // Moodle v2.4.0 release upgrade line
    // Put any upgrade step following this


    return true;
}
function xmldb_book_upgrade($oldversion)
{
    global $CFG, $DB;
    $dbman = $DB->get_manager();
    if ($oldversion < 2004081100) {
        throw new upgrade_exception('mod_book', $oldversion, 'Can not upgrade such an old book module, sorry, you should have upgraded it long time ago in 1.9 already.');
    }
    if ($oldversion < 2007052001) {
        /// Changing type of field importsrc on table book_chapters to char
        $table = new xmldb_table('book_chapters');
        $field = new xmldb_field('importsrc', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'timemodified');
        /// Launch change of type for field importsrc
        $dbman->change_field_type($table, $field);
        upgrade_mod_savepoint(true, 2007052001, 'book');
    }
    //===== 1.9.0 upgrade line ======//
    if ($oldversion < 2010120801) {
        // Rename field summary on table book to intro
        $table = new xmldb_table('book');
        $field = new xmldb_field('summary', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'name');
        // Launch rename field summary
        if ($dbman->field_exists($table, $field)) {
            $dbman->rename_field($table, $field, 'intro');
        }
        // book savepoint reached
        upgrade_mod_savepoint(true, 2010120801, 'book');
    }
    if ($oldversion < 2010120802) {
        // Rename field summary on table book to intro
        $table = new xmldb_table('book');
        $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'big', null, null, null, null, 'name');
        // Launch rename field summary
        $dbman->change_field_precision($table, $field);
        // book savepoint reached
        upgrade_mod_savepoint(true, 2010120802, 'book');
    }
    if ($oldversion < 2010120803) {
        // Define field introformat to be added to book
        $table = new xmldb_table('book');
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // conditionally migrate to html format in intro
        if ($CFG->texteditors !== 'textarea') {
            $rs = $DB->get_recordset('book', array('introformat' => FORMAT_MOODLE), '', 'id,intro,introformat');
            foreach ($rs as $b) {
                $b->intro = text_to_html($b->intro, false, false, true);
                $b->introformat = FORMAT_HTML;
                $DB->update_record('book', $b);
                upgrade_set_timeout();
            }
            unset($b);
            $rs->close();
        }
        // book savepoint reached
        upgrade_mod_savepoint(true, 2010120803, 'book');
    }
    if ($oldversion < 2010120804) {
        // Define field introformat to be added to book
        $table = new xmldb_table('book_chapters');
        $field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'content');
        // Launch add field introformat
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $DB->set_field('book_chapters', 'contentformat', FORMAT_HTML, array());
        // book savepoint reached
        upgrade_mod_savepoint(true, 2010120804, 'book');
    }
    if ($oldversion < 2010120805) {
        require_once "{$CFG->dirroot}/mod/book/db/upgradelib.php";
        $sqlfrom = "FROM {book} b\n                    JOIN {modules} m ON m.name = 'book'\n                    JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = b.id)";
        $count = $DB->count_records_sql("SELECT COUNT('x') {$sqlfrom}");
        if ($rs = $DB->get_recordset_sql("SELECT b.id, b.course, cm.id AS cmid {$sqlfrom} ORDER BY b.course, b.id")) {
            $pbar = new progress_bar('migratebookfiles', 500, true);
            $i = 0;
            foreach ($rs as $book) {
                $i++;
                upgrade_set_timeout(360);
                // set up timeout, may also abort execution
                $pbar->update($i, $count, "Migrating book files - {$i}/{$count}.");
                $context = get_context_instance(CONTEXT_MODULE, $book->cmid);
                mod_book_migrate_moddata_dir_to_legacy($book, $context, '/');
                // remove dirs if empty
                @rmdir("{$CFG->dataroot}/{$book->course}/{$CFG->moddata}/book/{$book->id}/");
                @rmdir("{$CFG->dataroot}/{$book->course}/{$CFG->moddata}/book/");
                @rmdir("{$CFG->dataroot}/{$book->course}/{$CFG->moddata}/");
                @rmdir("{$CFG->dataroot}/{$book->course}/");
            }
            $rs->close();
        }
        // book savepoint reached
        upgrade_mod_savepoint(true, 2010120805, 'book');
    }
    if ($oldversion < 2011011600) {
        // Define field disableprinting to be dropped from book
        $table = new xmldb_table('book');
        $field = new xmldb_field('disableprinting');
        // Conditionally launch drop field disableprinting
        if ($dbman->field_exists($table, $field)) {
            $dbman->drop_field($table, $field);
        }
        // book savepoint reached
        upgrade_mod_savepoint(true, 2011011600, 'book');
    }
    if ($oldversion < 2011011601) {
        unset_config('book_tocwidth');
        // book savepoint reached
        upgrade_mod_savepoint(true, 2011011601, 'book');
    }
    if ($oldversion < 2011090800) {
        require_once "{$CFG->dirroot}/mod/book/db/upgradelib.php";
        mod_book_migrate_all_areas();
        upgrade_mod_savepoint(true, 2011090800, 'book');
    }
    if ($oldversion < 2011100900) {
        // Define field revision to be added to book
        $table = new xmldb_table('book');
        $field = new xmldb_field('revision', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'customtitles');
        // Conditionally launch add field revision
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // book savepoint reached
        upgrade_mod_savepoint(true, 2011100900, 'book');
    }
    return true;
}