Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00045 defined('MOODLE_INTERNAL') || die();
00046 
00055 function xmldb_lesson_upgrade($oldversion) {
00056     global $CFG, $DB, $OUTPUT;
00057 
00058     $dbman = $DB->get_manager();
00059 
00060 //===== 1.9.0 upgrade line ======//
00061 
00062     if ($oldversion < 2007072201) {
00063 
00064         $table = new xmldb_table('lesson');
00065         $field = new xmldb_field('usegrademax');
00066         $field2 = new xmldb_field('usemaxgrade');
00067 
00069         if ($dbman->field_exists($table, $field) && !$dbman->field_exists($table, $field2)) {
00071             $field->set_attributes(XMLDB_TYPE_INTEGER, '3', null, XMLDB_NOTNULL, null, '0', 'ongoing');
00073             $dbman->rename_field($table, $field, 'usemaxgrade');
00074         }
00075 
00076         upgrade_mod_savepoint(true, 2007072201, 'lesson');
00077     }
00078 
00079     if ($oldversion < 2008112601) {
00080         //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!
00081         require_once($CFG->dirroot.'/mod/lesson/lib.php');
00082 
00083         lesson_upgrade_grades();
00084 
00085         upgrade_mod_savepoint(true, 2008112601, 'lesson');
00086     }
00087 
00088     if ($oldversion < 2009111600) {
00093         $table = new xmldb_table('lesson_answers');
00094         $field = new xmldb_field('grade');
00095         $field->set_attributes(XMLDB_TYPE_INTEGER, '4', false, XMLDB_NOTNULL, null, '0', 'jumpto');
00096         $dbman->change_field_type($table, $field);
00097         upgrade_mod_savepoint(true, 2009111600, 'lesson');
00098     }
00099 
00100     if ($oldversion < 2009120800) {
00105         $table = new xmldb_table('lesson_default');
00106         $dbman->drop_table($table);
00107         upgrade_mod_savepoint(true, 2009120800, 'lesson');
00108     }
00109 
00110     if ($oldversion < 2009120801) {
00111 
00113         $table = new xmldb_table('lesson_pages');
00114         $field = new xmldb_field('contentsformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, FORMAT_MOODLE, 'contents');
00115 
00117         if (!$dbman->field_exists($table, $field)) {
00118             $dbman->add_field($table, $field);
00119         }
00120 
00121         // conditionally migrate to html format in contents
00122         if ($CFG->texteditors !== 'textarea') {
00123             $rs = $DB->get_recordset('lesson_pages', array('contentsformat'=>FORMAT_MOODLE), '', 'id,contents,contentsformat');
00124             foreach ($rs as $lp) {
00125                 $lp->contents       = text_to_html($lp->contents, false, false, true);
00126                 $lp->contentsformat = FORMAT_HTML;
00127                 $DB->update_record('lesson_pages', $lp);
00128                 upgrade_set_timeout();
00129             }
00130             $rs->close();
00131         }
00133         upgrade_mod_savepoint(true, 2009120801, 'lesson');
00134     }
00135 
00136     if ($oldversion < 2010072000) {
00137         // Define field answerformat to be added to lesson_answers
00138         $table = new xmldb_table('lesson_answers');
00139         $field = new xmldb_field('answerformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'answer');
00140 
00141         // Launch add field answerformat
00142         $dbman->add_field($table, $field);
00143 
00144         // lesson savepoint reached
00145         upgrade_mod_savepoint(true, 2010072000, 'lesson');
00146     }
00147 
00148     if ($oldversion < 2010072001) {
00149         // Define field responseformat to be added to lesson_answers
00150         $table = new xmldb_table('lesson_answers');
00151         $field = new xmldb_field('responseformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'response');
00152 
00153         // Launch add field responseformat
00154         $dbman->add_field($table, $field);
00155 
00156         // lesson savepoint reached
00157         upgrade_mod_savepoint(true, 2010072001, 'lesson');
00158     }
00159 
00160     if ($oldversion < 2010072003) {
00161         $rs = $DB->get_recordset('lesson_answers', array());
00162         foreach ($rs as $answer) {
00163             $flags = intval($answer->flags);
00164             $update = false;
00165             if ($flags & 1) {
00166                 $answer->answer       = text_to_html($answer->answer, false, false, true);
00167                 $answer->answerformat = FORMAT_HTML;
00168                 $update = true;
00169             }
00170             if ($flags & 2) {
00171                 $answer->response       = text_to_html($answer->response, false, false, true);
00172                 $answer->responseformat = FORMAT_HTML;
00173                 $update = true;
00174             }
00175             if ($update) {
00176                 $DB->update_record('lesson_answers', $answer);
00177             }
00178         }
00179         $rs->close();
00180         upgrade_mod_savepoint(true, 2010072003, 'lesson');
00181     }
00182 
00183 
00184     if ($oldversion < 2010081200) {
00185         require_once("$CFG->dirroot/mod/lesson/db/upgradelib.php");
00186 
00187         $sqlfrom = "FROM {lesson} l
00188                     JOIN {modules} m ON m.name = 'lesson'
00189                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = l.id)";
00190 
00191         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
00192 
00193         if ($count > 0) {
00194             $rs = $DB->get_recordset_sql("SELECT l.id, l.mediafile, l.course, cm.id AS cmid $sqlfrom ORDER BY l.course, l.id");
00195 
00196             $pbar = new progress_bar('migratelessonfiles', 500, true);
00197             $fs = get_file_storage();
00198 
00199             $i = 0;
00200             foreach ($rs as $lesson) {
00201                 $i++;
00202                 upgrade_set_timeout(120); // set up timeout, may also abort execution
00203                 $pbar->update($i, $count, "Migrating lesson files - $i/$count.");
00204 
00205                 // fix images incorrectly placed in moddata subfolders,
00206                 // this was a really bad decision done by the ppt import developer
00207                 if (file_exists("$CFG->dataroot/$lesson->course/moddata/lesson/")) {
00208                     lesson_20_migrate_moddata_mixture($lesson->course, '/moddata/lesson/');
00209                     @rmdir("$CFG->dataroot/$lesson->course/moddata/lesson/"); // remove dir if empty
00210                     @rmdir("$CFG->dataroot/$lesson->course/moddata/"); // remove dir if empty
00211                 }
00212 
00213                 // migrate media file only if local course file selected - this is not nice at all,
00214                 // it should better be a real block, not a lesson specific hack
00215                 if (strpos($lesson->mediafile, '://') !== false) {
00216                     // some external URL
00217 
00218                 } else if ($lesson->mediafile) {
00219                     $context = get_context_instance(CONTEXT_MODULE, $lesson->cmid);
00220                     $coursecontext = get_context_instance(CONTEXT_COURSE, $lesson->course);
00221                     $filepathname = clean_param('/'.$lesson->mediafile, PARAM_PATH);
00222                     $fullpath = "/$context->id/mod_lesson/mediafile/0$filepathname";
00223 
00224                     if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
00225                         // already converted, just update filename
00226                         $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
00227                     } else {
00228                         // let's copy file from current course legacy files if possible
00229                         $fullpath = "/$coursecontext->id/course/legacy/0$filepathname";
00230                         if ($file = $fs->get_file_by_hash(sha1($fullpath)) and !$file->is_directory()) {
00231                             $file_record = array('contextid'=>$context->id, 'component'=>'mod_lesson', 'filearea'=>'mediafile', 'itemid'=>0, 'filepath'=>$file->get_filepath(), 'filename'=>$file->get_filename(), 'sortorder'=>1);
00232                             $fs->create_file_from_storedfile($file_record, $file);
00233                             $DB->set_field('lesson', 'mediafile', $filepathname, array('id'=>$lesson->id));
00234 
00235                         } else {
00236                             // bad luck, no such file exists
00237                             $DB->set_field('lesson', 'mediafile', '', array('id'=>$lesson->id));
00238                         }
00239                     }
00240                 }
00241             }
00242             $rs->close();
00243         }
00244 
00245         upgrade_mod_savepoint(true, 2010081200, 'lesson');
00246     }
00247     
00248     
00249     if ($oldversion < 2010121400) {
00250         // Fix matching question pages.
00251         // In Moodle 1.9 matching question pages stored the correct and incorrect
00252         // jumps on the third and forth answers, in Moodle 2.0 they are stored
00253         // in the first and second answers.
00254         // This upgrade block detects matching questions where this is the case
00255         // and fixed it by making firstjump = thirdjump && secondjump = forthjump.
00256         $pages = $DB->get_recordset('lesson_pages', array('qtype'=>'5'));
00257         foreach ($pages as $page) {
00258             $answers = $DB->get_records('lesson_answers', array('pageid'=>$page->id), 'id', 'id, jumpto', 0, 4);
00259             if (count($answers) < 4) {
00260                 // If there are less then four answers the problem wont exist.
00261                 // All Moodle 1.9 matching questions had a least 4 answers.
00262                 continue;
00263             }
00264             $first  = array_shift($answers);
00265             $second = array_shift($answers);
00266             $third  = array_shift($answers);
00267             $forth  = array_shift($answers);
00268             if ($first->jumpto !== '0' || $second->jumpto !== '0') {
00269                 // If either are set to something other than the next page then
00270                 // there is no problem.
00271                 continue;
00272             }
00273             if ($third->jumpto !== '0') {
00274                 $first->jumpto = $third->jumpto;
00275                 $DB->update_record('lesson_answers', $first);
00276                 $third->jumpto = '0';
00277                 $DB->update_record('lesson_answers', $third);
00278             }
00279             if ($forth->jumpto !== '0') {
00280                 $second->jumpto = $forth->jumpto;
00281                 $DB->update_record('lesson_answers', $second);
00282                 $forth->jumpto = '0';
00283                 $DB->update_record('lesson_answers', $forth);
00284             }
00285         }
00286         // Close the record set
00287         $pages->close();
00288         
00289         upgrade_mod_savepoint(true, 2010121400, 'lesson');
00290     }
00291 
00292     // Moodle v2.1.0 release upgrade line
00293     // Put any upgrade step following this
00294 
00295     // Moodle v2.2.0 release upgrade line
00296     // Put any upgrade step following this
00297 
00298     return true;
00299 }
00300 
00301 
 All Data Structures Namespaces Files Functions Variables Enumerations