Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: bc3801cb9c
Fetching contributors…

Cannot retrieve contributors at this time

file 299 lines (254 sloc) 12.404 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file keeps track of upgrades to
* the lesson module
*
* Sometimes, changes between versions involve
* alterations to database structures and other
* major things that may break installations.
*
* The upgrade function in this file will attempt
* to perform all the necessary actions to upgrade
* your older installation to the current version.
*
* If there's something it cannot do itself, it
* will tell you what you need to do.
*
* The commands in here will all be database-neutral,
* using the methods of database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*
* @package mod
* @subpackage lesson
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 o
*/

defined('MOODLE_INTERNAL') || die();

/**
*
* @global stdClass $CFG
* @global moodle_database $DB
* @global core_renderer $OUTPUT
* @param int $oldversion
* @return bool
*/
function xmldb_lesson_upgrade($oldversion) {
    global $CFG, $DB, $OUTPUT;

    $dbman = $DB->get_manager();

//===== 1.9.0 upgrade line ======//

    if ($oldversion < 2007072201) {

        $table = new xmldb_table('lesson');
        $field = new xmldb_field('usegrademax');
        $field2 = new xmldb_field('usemaxgrade');

    /// Rename lesson->usegrademax to lesson->usemaxgrade. Some old sites can have it incorrect. MDL-13177
        if ($dbman->field_exists($table, $field) && !$dbman->field_exists($table, $field2)) {
        /// Set field specs
            $field->set_attributes(XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'ongoing');
        /// Launch rename field usegrademax to usemaxgrade
            $dbman->rename_field($table, $field, 'usemaxgrade');
        }

        upgrade_mod_savepoint(true, 2007072201, 'lesson');
    }

    if ($oldversion < 2008112601) {
        //NOTE: this is a hack, we can not call module lib.php in the middle of upgrade, the necessary db structures/data may not exist yet!
        require_once($CFG->dirroot.'/mod/lesson/lib.php');

        lesson_upgrade_grades();

        upgrade_mod_savepoint(true, 2008112601, 'lesson');
    }

    if ($oldversion < 2009111600) {
        /**
* Change the grade field within lesson_answers to an unsigned int and increment
* the length by one to ensure that no values are changed (reduced)
*/
        $table = new xmldb_table('lesson_answers');
        $field = new xmldb_field('grade');
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', false, XMLDB_NOTNULL, null, '0', 'jumpto');
        $dbman->change_field_type($table, $field);
        upgrade_mod_savepoint(true, 2009111600, 'lesson');
    }

    if ($oldversion < 2009120800) {
        /**
* Drop the lesson_default table, as of Moodle 2.0 it is no longer used
* the module now has a settings.php instead
*/
        $table = new xmldb_table('lesson_default');
        $dbman->drop_table($table);
        upgrade_mod_savepoint(true, 2009120800, 'lesson');
    }

    if ($oldversion < 2009120801) {

    /// Define field contentsformat to be added to lesson_pages
        $table = new xmldb_table('lesson_pages');
        $field = new xmldb_field('contentsformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, FORMAT_MOODLE, 'contents');

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

        // conditionally migrate to html format in contents
        if ($CFG->texteditors !== 'textarea') {
            $rs = $DB->get_recordset('lesson_pages', array('contentsformat'=>FORMAT_MOODLE), '', 'id,contents,contentsformat');
            foreach ($rs as $lp) {
                $lp->contents = text_to_html($lp->contents, false, false, true);
                $lp->contentsformat = FORMAT_HTML;
                $DB->update_record('lesson_pages', $lp);
                upgrade_set_timeout();
            }
            $rs->close();
        }
    /// lesson savepoint reached
        upgrade_mod_savepoint(true, 2009120801, 'lesson');
    }

    if ($oldversion < 2010072000) {
        // Define field answerformat to be added to lesson_answers
        $table = new xmldb_table('lesson_answers');
        $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'answer');

        // Launch add field answerformat
        $dbman->add_field($table, $field);

        // lesson savepoint reached
        upgrade_mod_savepoint(true, 2010072000, 'lesson');
    }

    if ($oldversion < 2010072001) {
        // Define field responseformat to be added to lesson_answers
        $table = new xmldb_table('lesson_answers');
        $field = new xmldb_field('responseformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'response');

        // Launch add field responseformat
        $dbman->add_field($table, $field);

        // lesson savepoint reached
        upgrade_mod_savepoint(true, 2010072001, 'lesson');
    }

    if ($oldversion < 2010072003) {
        $rs = $DB->get_recordset('lesson_answers', array());
        foreach ($rs as $answer) {
            $flags = intval($answer->flags);
            $update = false;
            if ($flags & 1) {
                $answer->answer = text_to_html($answer->answer, false, false, true);
                $answer->answerformat = FORMAT_HTML;
                $update = true;
            }
            if ($flags & 2) {
                $answer->response = text_to_html($answer->response, false, false, true);
                $answer->responseformat = FORMAT_HTML;
                $update = true;
            }
            if ($update) {
                $DB->update_record('lesson_answers', $answer);
            }
        }
        $rs->close();
        upgrade_mod_savepoint(true, 2010072003, 'lesson');
    }


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

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

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

        if ($count > 0) {
            $rs = $DB->get_recordset_sql("SELECT l.id, l.mediafile, l.course, cm.id AS cmid $sqlfrom ORDER BY l.course, l.id");

            $pbar = new progress_bar('migratelessonfiles', 500, true);
            $fs = get_file_storage();

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

                // fix images incorrectly placed in moddata subfolders,
                // this was a really bad decision done by the ppt import developer
                if (file_exists("$CFG->dataroot/$lesson->course/moddata/lesson/")) {
                    lesson_20_migrate_moddata_mixture($lesson->course, '/moddata/lesson/');
                    @rmdir("$CFG->dataroot/$lesson->course/moddata/lesson/"); // remove dir if empty
                    @rmdir("$CFG->dataroot/$lesson->course/moddata/"); // remove dir if empty
                }

                // migrate media file only if local course file selected - this is not nice at all,
                // it should better be a real block, not a lesson specific hack
                if (strpos($lesson->mediafile, '://') !== false) {
                    // some external URL

                } else if ($lesson->mediafile) {
                    $context = get_context_instance(CONTEXT_MODULE, $lesson->cmid);
                    $coursecontext = get_context_instance(CONTEXT_COURSE, $lesson->course);
                    $filepathname = clean_param('/'.$lesson->mediafile, PARAM_PATH);
                    $fullpath = "/$context->id/mod_lesson/mediafile/0$filepathname";

                    if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
                        // already converted, just update filename
                        $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
                    } else {
                        // let's copy file from current course legacy files if possible
                        $fullpath = "/$coursecontext->id/course/legacy/0$filepathname";
                        if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
                            $file_record = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'mediafile', 'itemid'=>0, 'filepath'=>$file->get_filepath(), 'filename'=>$file->get_filename(), 'sortorder'=>1);
                            $fs->create_file_from_storedfile($file_record, $file);
                            $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));

                        } else {
                            // bad luck, no such file exists
                            $DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
                        }
                    }
                }
            }
            $rs->close();
        }

        upgrade_mod_savepoint(true, 2010081200, 'lesson');
    }
    
    
    if ($oldversion < 2010121400) {
        // Fix matching question pages.
        // In Moodle 1.9 matching question pages stored the correct and incorrect
        // jumps on the third and forth answers, in Moodle 2.0 they are stored
        // in the first and second answers.
        // This upgrade block detects matching questions where this is the case
        // and fixed it by making firstjump = thirdjump && secondjump = forthjump.
        $pages = $DB->get_recordset('lesson_pages', array('qtype'=>'5'));
        foreach ($pages as $page) {
            $answers = $DB->get_records('lesson_answers', array('pageid'=>$page->id), 'id', 'id, jumpto', 0, 4);
            if (count($answers) < 4) {
                // If there are less then four answers the problem wont exist.
                // All Moodle 1.9 matching questions had a least 4 answers.
                continue;
            }
            $first = array_shift($answers);
            $second = array_shift($answers);
            $third = array_shift($answers);
            $forth = array_shift($answers);
            if ($first->jumpto !== '0' || $second->jumpto !== '0') {
                // If either are set to something other than the next page then
                // there is no problem.
                continue;
            }
            if ($third->jumpto !== '0') {
                $first->jumpto = $third->jumpto;
                $DB->update_record('lesson_answers', $first);
                $third->jumpto = '0';
                $DB->update_record('lesson_answers', $third);
            }
            if ($forth->jumpto !== '0') {
                $second->jumpto = $forth->jumpto;
                $DB->update_record('lesson_answers', $second);
                $forth->jumpto = '0';
                $DB->update_record('lesson_answers', $forth);
            }
        }
        // Close the record set
        $pages->close();
        
        upgrade_mod_savepoint(true, 2010121400, 'lesson');
    }

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

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

    return true;
}
Something went wrong with that request. Please try again.