Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/glossary/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file keeps track of upgrades to
00004 // the glossary module
00005 //
00006 // Sometimes, changes between versions involve
00007 // alterations to database structures and other
00008 // major things that may break installations.
00009 //
00010 // The upgrade function in this file will attempt
00011 // to perform all the necessary actions to upgrade
00012 // your older installation to the current version.
00013 //
00014 // If there's something it cannot do itself, it
00015 // will tell you what you need to do.
00016 //
00017 // The commands in here will all be database-neutral,
00018 // using the methods of database_manager class
00019 //
00020 // Please do not forget to use upgrade_set_timeout()
00021 // before any action that may take longer time to finish.
00022 
00023 function xmldb_glossary_upgrade($oldversion) {
00024     global $CFG, $DB, $OUTPUT;
00025 
00026     $dbman = $DB->get_manager();
00027 
00028 //===== 1.9.0 upgrade line ======//
00029 
00030     if ($oldversion < 2008081900) {
00031 
00035 
00036         $fs = get_file_storage();
00037 
00038         $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
00039 
00040         $sqlfrom = "FROM {glossary_entries} ge
00041                     JOIN {glossary} g ON g.id = ge.glossaryid
00042                     JOIN {modules} m ON m.name = 'glossary'
00043                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = g.id)
00044                    WHERE ge.attachment <> '$empty' AND ge.attachment <> '1'";
00045 
00046         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
00047 
00048         $rs = $DB->get_recordset_sql("SELECT ge.id, ge.userid, ge.attachment, ge.glossaryid, ge.sourceglossaryid, g.course, cm.id AS cmid $sqlfrom ORDER BY g.course, g.id");
00049         if ($rs->valid()) {
00050 
00051             $pbar = new progress_bar('migrateglossaryfiles', 500, true);
00052 
00053             $i = 0;
00054             foreach ($rs as $entry) {
00055                 $i++;
00056                 upgrade_set_timeout(60); // set up timeout, may also abort execution
00057                 $pbar->update($i, $count, "Migrating glossary entries - $i/$count.");
00058 
00059                 $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id/$entry->attachment";
00060                 if ($entry->sourceglossaryid and !is_readable($filepath)) {
00061                     //eh - try the second possible location
00062                     $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->sourceglossaryid/$entry->id/$entry->attachment";
00063 
00064                 }
00065                 if (!is_readable($filepath)) {
00066                     //file missing??
00067                     echo $OUTPUT->notification("File not readable, skipping: $filepath");
00068                     $entry->attachment = '';
00069                     $DB->update_record('glossary_entries', $entry);
00070                     continue;
00071                 }
00072                 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
00073 
00074                 $filearea = 'attachment';
00075                 $filename = clean_param($entry->attachment, PARAM_FILE);
00076                 if ($filename === '') {
00077                     echo $OUTPUT->notification("Unsupported entry filename, skipping: ".$filepath);
00078                     $entry->attachment = '';
00079                     $DB->update_record('glossary_entries', $entry);
00080                     continue;
00081                 }
00082                 if (!$fs->file_exists($context->id, 'mod_glossary', $filearea, $entry->id, '/', $filename)) {
00083                     $file_record = array('contextid'=>$context->id, 'component'=>'mod_glossary', 'filearea'=>$filearea, 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
00084                     if ($fs->create_file_from_pathname($file_record, $filepath)) {
00085                         $entry->attachment = '1';
00086                         $DB->update_record('glossary_entries', $entry);
00087                         unlink($filepath);
00088                     }
00089                 }
00090 
00091                 // remove dirs if empty
00092                 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid/$entry->id");
00093                 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary/$entry->glossaryid");
00094                 @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/glossary");
00095             }
00096         }
00097         $rs->close();
00098 
00099         upgrade_mod_savepoint(true, 2008081900, 'glossary');
00100     }
00101 
00102     if ($oldversion < 2009042000) {
00103 
00105         $table = new xmldb_table('glossary_entries');
00106         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definition');
00107 
00109         $dbman->rename_field($table, $field, 'definitionformat');
00110 
00112         upgrade_mod_savepoint(true, 2009042000, 'glossary');
00113     }
00114 
00115     if ($oldversion < 2009042001) {
00116 
00118         $table = new xmldb_table('glossary_entries');
00119         $field = new xmldb_field('definitiontrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'definitionformat');
00120 
00122         $dbman->add_field($table, $field);
00123 
00125         upgrade_mod_savepoint(true, 2009042001, 'glossary');
00126     }
00127 
00128     if ($oldversion < 2009042002) {
00129 
00131         $table = new xmldb_table('glossary_comments');
00132         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycomment');
00133 
00135         $dbman->rename_field($table, $field, 'entrycommentformat');
00136 
00138         upgrade_mod_savepoint(true, 2009042002, 'glossary');
00139     }
00140 
00141     if ($oldversion < 2009042003) {
00142 
00144         $table = new xmldb_table('glossary_comments');
00145         $field = new xmldb_field('entrycommenttrust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'entrycommentformat');
00146 
00148         if (!$dbman->field_exists($table, $field)) {
00149             $dbman->add_field($table, $field);
00150         }
00151 
00153         upgrade_mod_savepoint(true, 2009042003, 'glossary');
00154     }
00155 
00156     if ($oldversion < 2009042004) {
00157         $trustmark = '#####TRUSTTEXT#####';
00158         $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_entries} WHERE definition LIKE ?", array($trustmark.'%'));
00159         foreach ($rs as $entry) {
00160             if (strpos($entry->definition, $trustmark) !== 0) {
00161                 // probably lowercase in some DBs
00162                 continue;
00163             }
00164             $entry->definition      = str_replace($trustmark, '', $entry->definition);
00165             $entry->definitiontrust = 1;
00166             $DB->update_record('glossary_entries', $entry);
00167         }
00168         $rs->close();
00169 
00171         upgrade_mod_savepoint(true, 2009042004, 'glossary');
00172     }
00173 
00174     if ($oldversion < 2009042005) {
00175         $trustmark = '#####TRUSTTEXT#####';
00176         $rs = $DB->get_recordset_sql("SELECT * FROM {glossary_comments} WHERE entrycomment LIKE ?", array($trustmark.'%'));
00177         foreach ($rs as $comment) {
00178             if (strpos($comment->entrycomment, $trustmark) !== 0) {
00179                 // probably lowercase in some DBs
00180                 continue;
00181             }
00182             $comment->entrycomment      = str_replace($trustmark, '', $comment->entrycomment);
00183             $comment->entrycommenttrust = 1;
00184             $DB->update_record('glossary_comments', $comment);
00185         }
00186         $rs->close();
00187 
00189         upgrade_mod_savepoint(true, 2009042005, 'glossary');
00190     }
00191 
00192     if ($oldversion < 2009042006) {
00193 
00195         $table = new xmldb_table('glossary');
00196         $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
00197 
00199         if (!$dbman->field_exists($table, $field)) {
00200             $dbman->add_field($table, $field);
00201         }
00202 
00203         // conditionally migrate to html format in intro
00204         if ($CFG->texteditors !== 'textarea') {
00205             $rs = $DB->get_recordset('glossary', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
00206             foreach ($rs as $g) {
00207                 $g->intro       = text_to_html($g->intro, false, false, true);
00208                 $g->introformat = FORMAT_HTML;
00209                 $DB->update_record('glossary', $g);
00210                 upgrade_set_timeout();
00211             }
00212             $rs->close();
00213         }
00214 
00216         upgrade_mod_savepoint(true, 2009042006, 'glossary');
00217     }
00218     if ($oldversion < 2009110800) {
00219         require_once($CFG->dirroot . '/comment/lib.php');
00220         upgrade_set_timeout(60*20);
00221 
00223         $table = new xmldb_table('glossary_comments');
00224 
00226         if ($dbman->table_exists($table)) {
00227             $sql = "SELECT e.glossaryid AS glossaryid,
00228                            g.course AS courseid,
00229                            c.userid,
00230                            e.id AS itemid,
00231                            c.id AS old_id,
00232                            c.entrycomment AS commentcontent,
00233                            c.entrycommentformat AS format,
00234                            c.entrycommenttrust AS trust,
00235                            c.timemodified AS timemodified
00236                       FROM {glossary_comments} c, {glossary_entries} e, {glossary} g
00237                      WHERE c.entryid=e.id AND e.glossaryid=g.id
00238                   ORDER BY glossaryid, courseid";
00239             $lastglossaryid = null;
00240             $lastcourseid   = null;
00241             $modcontext     = null;
00242 
00244             $rs = $DB->get_recordset_sql($sql);
00245             foreach($rs as $res) {
00246                 if ($res->glossaryid != $lastglossaryid || $res->courseid != $lastcourseid) {
00247                     $cm = get_coursemodule_from_instance('glossary', $res->glossaryid, $res->courseid);
00248                     if ($cm) {
00249                         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00250                     }
00251                     $lastglossaryid = $res->glossaryid;
00252                     $lastcourseid   = $res->courseid;
00253                 }
00254                 $cmt = new stdClass();
00255                 $cmt->contextid     = $modcontext->id;
00256                 $cmt->commentarea   = 'glossary_entry';
00257                 $cmt->itemid        = $res->itemid;
00258                 $cmt->content       = $res->commentcontent;
00259                 $cmt->format        = $res->format;
00260                 $cmt->userid        = $res->userid;
00261                 $cmt->timecreated    = $res->timemodified;
00262                 $cmt_id = $DB->insert_record('comments', $cmt);
00263                 if (!empty($cmt_id)) {
00264                     $DB->delete_records('glossary_comments', array('id'=>$res->old_id));
00265                 }
00266             }
00267             $rs->close();
00268             $dbman->drop_table($table);
00269         }
00270 
00272         upgrade_mod_savepoint(true, 2009110800, 'glossary');
00273     }
00274 
00275     if ($oldversion < 2010042800) {
00276         //migrate glossary_ratings to the central rating table
00277         $table = new xmldb_table('glossary_ratings');
00278         if ($dbman->table_exists($table)) {
00279             //glossary ratings only have a single time column so use it for both time created and modified
00280             $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
00281 
00282                     SELECT cxt.id, g.scale, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified
00283                       FROM {glossary_ratings} r
00284                       JOIN {glossary_entries} ge ON ge.id=r.entryid
00285                       JOIN {glossary} g ON g.id=ge.glossaryid
00286                       JOIN {course_modules} cm ON cm.instance=g.id
00287                       JOIN {context} cxt ON cxt.instanceid=cm.id
00288                       JOIN {modules} m ON m.id=cm.module
00289                      WHERE m.name = :modname AND cxt.contextlevel = :contextlevel";
00290 
00291             $params['modname'] = 'glossary';
00292             $params['contextlevel'] = CONTEXT_MODULE;
00293 
00294             $DB->execute($sql, $params);
00295 
00296             //now drop glossary_ratings
00297             $dbman->drop_table($table);
00298         }
00299 
00300         upgrade_mod_savepoint(true, 2010042800, 'glossary');
00301     }
00302 
00303     if ($oldversion < 2010111500) {
00304         // Delete orphaned glossary_entries not belonging to any glossary (MDL-25227)
00305         $sql = "DELETE FROM {glossary_entries}
00306                 WHERE NOT EXISTS (
00307                     SELECT 'x' FROM {glossary} g
00308                     WHERE g.id = glossaryid)";
00309         $DB->execute($sql);
00310 
00311         upgrade_mod_savepoint(true, 2010111500, 'glossary');
00312     }
00313 
00314     if ($oldversion < 2010111501) {
00315 
00316         // Define field completionentries to be added to glossary
00317         $table = new xmldb_table('glossary');
00318         $field = new xmldb_field('completionentries', XMLDB_TYPE_INTEGER, '9', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'timemodified');
00319 
00320         // Conditionally launch add field completionentries
00321         if (!$dbman->field_exists($table, $field)) {
00322             $dbman->add_field($table, $field);
00323         }
00324 
00325         // glossary savepoint reached
00326         upgrade_mod_savepoint(true, 2010111501, 'glossary');
00327     }
00328 
00329     if ($oldversion < 2011052300) {
00330         // rating.component and rating.ratingarea have now been added as mandatory fields.
00331         // Presently you can only rate data entries so component = 'mod_glossary' and ratingarea = 'entry'
00332         // for all ratings with a glossary context.
00333         // We want to update all ratings that belong to a glossary context and don't already have a
00334         // component set.
00335         // This could take a while reset upgrade timeout to 5 min
00336         upgrade_set_timeout(60 * 20);
00337         $sql = "UPDATE {rating}
00338                 SET component = 'mod_glossary', ratingarea = 'entry'
00339                 WHERE contextid IN (
00340                     SELECT ctx.id
00341                       FROM {context} ctx
00342                       JOIN {course_modules} cm ON cm.id = ctx.instanceid
00343                       JOIN {modules} m ON m.id = cm.module
00344                      WHERE ctx.contextlevel = 70 AND
00345                            m.name = 'glossary'
00346                 ) AND component = 'unknown'";
00347         $DB->execute($sql);
00348 
00349         upgrade_mod_savepoint(true, 2011052300, 'glossary');
00350     }
00351 
00352     // Moodle v2.1.0 release upgrade line
00353     // Put any upgrade step following this
00354 
00355     // Moodle v2.2.0 release upgrade line
00356     // Put any upgrade step following this
00357 
00358     return true;
00359 }
00360 
00361 
 All Data Structures Namespaces Files Functions Variables Enumerations