Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/data/db/upgrade.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file keeps track of upgrades to
00004 // the data 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_data_upgrade($oldversion) {
00024     global $CFG, $DB, $OUTPUT;
00025 
00026     $dbman = $DB->get_manager();
00027 
00028 //===== 1.9.0 upgrade line ======//
00029 
00030     if ($oldversion < 2007101512) {
00032 
00033         $table = new xmldb_table('data');
00034         $field = new xmldb_field('asearchtemplate', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'jstemplate');
00035 
00036         if (!$dbman->field_exists($table, $field)) {
00037             $dbman->add_field($table, $field);
00038         }
00039         upgrade_mod_savepoint(true, 2007101512, 'data');
00040     }
00041 
00042     if ($oldversion < 2007101513) {
00043         // Upgrade all the data->notification currently being
00044         // NULL to 0
00045         $sql = "UPDATE {data} SET notification=0 WHERE notification IS NULL";
00046         $DB->execute($sql);
00047 
00048         $table = new xmldb_table('data');
00049         $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'editany');
00050         // First step, Set NOT NULL
00051         $dbman->change_field_notnull($table, $field);
00052         // Second step, Set default to 0
00053         $dbman->change_field_default($table, $field);
00054         upgrade_mod_savepoint(true, 2007101513, 'data');
00055     }
00056 
00057     if ($oldversion < 2008081400) {
00058         $pattern = '/\#\#delete\#\#(\s+)\#\#approve\#\#/';
00059         $replacement = '##delete##$1##approve##$1##export##';
00060         $rs = $DB->get_recordset('data');
00061         foreach ($rs as $data) {
00062             $data->listtemplate = preg_replace($pattern, $replacement, $data->listtemplate);
00063             $data->singletemplate = preg_replace($pattern, $replacement, $data->singletemplate);
00064             $DB->update_record('data', $data);
00065         }
00066         $rs->close();
00067 
00068         upgrade_mod_savepoint(true, 2008081400, 'data');
00069     }
00070 
00071     if ($oldversion < 2008091400) {
00072 
00076 
00077         $fs = get_file_storage();
00078 
00079         $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
00080 
00081         $sqlfrom = "FROM {data_content} c
00082                     JOIN {data_fields} f     ON f.id = c.fieldid
00083                     JOIN {data_records} r    ON r.id = c.recordid
00084                     JOIN {data} d            ON d.id = r.dataid
00085                     JOIN {modules} m         ON m.name = 'data'
00086                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = d.id)
00087                    WHERE ".$DB->sql_compare_text('c.content', 2)." <> '$empty' AND c.content IS NOT NULL
00088                          AND (f.type = 'file' OR f.type = 'picture')";
00089 
00090         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
00091 
00092         $rs = $DB->get_recordset_sql("SELECT c.id, f.type, r.dataid, c.recordid, f.id AS fieldid, r.userid, c.content, c.content1, d.course, r.userid, cm.id AS cmid $sqlfrom ORDER BY d.course, d.id");
00093 
00094         if ($rs->valid()) {
00095             $pbar = new progress_bar('migratedatafiles', 500, true);
00096 
00097             $i = 0;
00098             foreach ($rs as $content) {
00099                 $i++;
00100                 upgrade_set_timeout(60); // set up timeout, may also abort execution
00101                 $pbar->update($i, $count, "Migrating data entries - $i/$count.");
00102 
00103                 $filepath = "$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/$content->content";
00104                 $context = get_context_instance(CONTEXT_MODULE, $content->cmid);
00105 
00106                 if (!file_exists($filepath)) {
00107                     continue;
00108                 }
00109 
00110                 $filearea = 'content';
00111                 $oldfilename = $content->content;
00112                 $filename    = clean_param($oldfilename, PARAM_FILE);
00113                 if ($filename === '') {
00114                     continue;
00115                 }
00116                 if (!$fs->file_exists($context->id, 'mod_data', $filearea, $content->id, '/', $filename)) {
00117                     $file_record = array('contextid'=>$context->id, 'component'=>'mod_data', 'filearea'=>$filearea, 'itemid'=>$content->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$content->userid);
00118                     if ($fs->create_file_from_pathname($file_record, $filepath)) {
00119                         unlink($filepath);
00120                         if ($oldfilename !== $filename) {
00121                             // update filename if needed
00122                             $DB->set_field('data_content', 'content', $filename, array('id'=>$content->id));
00123                         }
00124                         if ($content->type == 'picture') {
00125                             // migrate thumb
00126                             $filepath = "$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/thumb/$content->content";
00127                             if (file_exists($filepath)) {
00128                                 if (!$fs->file_exists($context->id, 'mod_data', $filearea, $content->id, '/', 'thumb_'.$filename)) {
00129                                     $file_record['filename'] = 'thumb_'.$file_record['filename'];
00130                                     $fs->create_file_from_pathname($file_record, $filepath);
00131                                 }
00132                                 unlink($filepath);
00133                             }
00134                         }
00135                     }
00136                 }
00137 
00138                 // remove dirs if empty
00139                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid/thumb");
00140                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid/$content->recordid");
00141                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid/$content->fieldid");
00142                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data/$content->dataid");
00143                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata/data");
00144                 @rmdir("$CFG->dataroot/$content->course/$CFG->moddata");
00145             }
00146         }
00147         $rs->close();
00148 
00149         upgrade_mod_savepoint(true, 2008091400, 'data');
00150     }
00151 
00152     if ($oldversion < 2008112700) {
00153         if (!get_config('data', 'requiredentriesfixflag')) {
00154             $databases = $DB->get_records_sql("SELECT d.*, c.fullname
00155                                                  FROM {data} d, {course} c
00156                                                 WHERE d.course = c.id
00157                                                       AND (d.requiredentries > 0 OR d.requiredentriestoview > 0)
00158                                              ORDER BY c.fullname, d.name");
00159             if (!empty($databases)) {
00160                 $a = new stdClass();
00161                 $a->text = '';
00162                 foreach($databases as $database) {
00163                     $a->text .= $database->fullname." - " .$database->name. " (course id: ".$database->course." - database id: ".$database->id.")<br/>";
00164                 }
00165                 //TODO: MDL-17427 send this info to "upgrade log" which will be implemented in 2.0
00166                 echo $OUTPUT->notification(get_string('requiredentrieschanged', 'admin', $a));
00167             }
00168         }
00169         unset_config('requiredentriesfixflag', 'data'); // remove old flag
00170         upgrade_mod_savepoint(true, 2008112700, 'data');
00171     }
00172 
00173     if ($oldversion < 2009042000) {
00174 
00176         $table = new xmldb_table('data');
00177         $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
00178 
00180         if (!$dbman->field_exists($table, $field)) {
00181             $dbman->add_field($table, $field);
00182         }
00183 
00184         // conditionally migrate to html format in intro
00185         if ($CFG->texteditors !== 'textarea') {
00186             $rs = $DB->get_recordset('data', array('introformat'=>FORMAT_MOODLE), '', 'id,intro,introformat');
00187             foreach ($rs as $d) {
00188                 $d->intro       = text_to_html($d->intro, false, false, true);
00189                 $d->introformat = FORMAT_HTML;
00190                 $DB->update_record('data', $d);
00191                 upgrade_set_timeout();
00192             }
00193             $rs->close();
00194         }
00195 
00197         upgrade_mod_savepoint(true, 2009042000, 'data');
00198     }
00199 
00200     if ($oldversion < 2009111701) {
00201         upgrade_set_timeout(60*20);
00202 
00204         $table = new xmldb_table('data_comments');
00205 
00207         if ($dbman->table_exists($table)) {
00208             $sql = "SELECT d.id AS dataid,
00209                            d.course AS courseid,
00210                            c.userid,
00211                            r.id AS itemid,
00212                            c.id AS commentid,
00213                            c.content AS commentcontent,
00214                            c.format AS format,
00215                            c.created AS timecreated
00216                       FROM {data_comments} c, {data_records} r, {data} d
00217                      WHERE c.recordid=r.id AND r.dataid=d.id
00218                   ORDER BY dataid, courseid";
00220             $lastdataid = null;
00221             $lastcourseid = null;
00222             $modcontext = null;
00223             $rs = $DB->get_recordset_sql($sql);
00224             foreach($rs as $res) {
00225                 if ($res->dataid != $lastdataid || $res->courseid != $lastcourseid) {
00226                     $cm = get_coursemodule_from_instance('data', $res->dataid, $res->courseid);
00227                     if ($cm) {
00228                         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00229                     }
00230                     $lastdataid = $res->dataid;
00231                     $lastcourseid = $res->courseid;
00232                 }
00233                 $cmt = new stdClass();
00234                 $cmt->contextid   = $modcontext->id;
00235                 $cmt->commentarea = 'database_entry';
00236                 $cmt->itemid      = $res->itemid;
00237                 $cmt->content     = $res->commentcontent;
00238                 $cmt->format      = $res->format;
00239                 $cmt->userid      = $res->userid;
00240                 $cmt->timecreated = $res->timecreated;
00241                 // comments class will throw an exception if error occurs
00242                 $cmt_id = $DB->insert_record('comments', $cmt);
00243                 if (!empty($cmt_id)) {
00244                     $DB->delete_records('data_comments', array('id'=>$res->commentid));
00245                 }
00246             }
00247             $rs->close();
00248             // the default exception handler will stop the script if error occurs before
00249             $dbman->drop_table($table);
00250         }
00251 
00253         upgrade_mod_savepoint(true, 2009111701, 'data');
00254     }
00255 
00256     if ($oldversion < 2010031602) {
00257         //add assesstimestart and assesstimefinish columns to data
00258         $table = new xmldb_table('data');
00259 
00260         $field = new xmldb_field('assesstimestart');
00261         if (!$dbman->field_exists($table, $field)) {
00262             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'assessed');
00263             $dbman->add_field($table, $field);
00264         }
00265 
00266         $field = new xmldb_field('assesstimefinish');
00267         if (!$dbman->field_exists($table, $field)) {
00268             $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'assesstimestart');
00269             $dbman->add_field($table, $field);
00270         }
00271 
00272         upgrade_mod_savepoint(true, 2010031602, 'data');
00273     }
00274 
00275     if ($oldversion < 2010042800) {
00276         //migrate data ratings to the central rating table
00277         $table = new xmldb_table('data_ratings');
00278         if ($dbman->table_exists($table)) {
00279             //data ratings didnt store time created and modified so Im using the times from the record the rating was attached to
00280             $sql = "INSERT INTO {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified)
00281 
00282                     SELECT cxt.id, d.scale, r.recordid AS itemid, r.rating, r.userid, re.timecreated AS timecreated, re.timemodified AS timemodified
00283                       FROM {data_ratings} r
00284                       JOIN {data_records} re ON r.recordid=re.id
00285                       JOIN {data} d ON d.id=re.dataid
00286                       JOIN {course_modules} cm ON cm.instance=d.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             $params['modname'] = 'data';
00291             $params['contextlevel'] = CONTEXT_MODULE;
00292 
00293             $DB->execute($sql, $params);
00294 
00295             //now drop data_ratings
00296             $dbman->drop_table($table);
00297         }
00298 
00299         upgrade_mod_savepoint(true, 2010042800, 'data');
00300     }
00301 
00302     //rerun the upgrade see MDL-24470
00303     if ($oldversion < 2010100101) {
00304         // Upgrade all the data->notification currently being
00305         // NULL to 0
00306         $sql = "UPDATE {data} SET notification=0 WHERE notification IS NULL";
00307         $DB->execute($sql);
00308 
00309         $table = new xmldb_table('data');
00310         $field = new xmldb_field('notification', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'editany');
00311         // First step, Set NOT NULL
00312         $dbman->change_field_notnull($table, $field);
00313         // Second step, Set default to 0
00314         $dbman->change_field_default($table, $field);
00315         upgrade_mod_savepoint(true, 2010100101, 'data');
00316     }
00317 
00318     if ($oldversion < 2011052300) {
00319         // rating.component and rating.ratingarea have now been added as mandatory fields.
00320         // Presently you can only rate data entries so component = 'mod_data' and ratingarea = 'entry'
00321         // for all ratings with a data context.
00322         // We want to update all ratings that belong to a data context and don't already have a
00323         // component set.
00324         // This could take a while reset upgrade timeout to 5 min
00325         upgrade_set_timeout(60 * 20);
00326         $sql = "UPDATE {rating}
00327                 SET component = 'mod_data', ratingarea = 'entry'
00328                 WHERE contextid IN (
00329                     SELECT ctx.id
00330                       FROM {context} ctx
00331                       JOIN {course_modules} cm ON cm.id = ctx.instanceid
00332                       JOIN {modules} m ON m.id = cm.module
00333                      WHERE ctx.contextlevel = 70 AND
00334                            m.name = 'data'
00335                 ) AND component = 'unknown'";
00336         $DB->execute($sql);
00337 
00338         upgrade_mod_savepoint(true, 2011052300, 'data');
00339     }
00340 
00341     // Moodle v2.1.0 release upgrade line
00342     // Put any upgrade step following this
00343 
00344     // Moodle v2.2.0 release upgrade line
00345     // Put any upgrade step following this
00346 
00347     return true;
00348 }
00349 
00350 
 All Data Structures Namespaces Files Functions Variables Enumerations