Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/workshop/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 
00036 function xmldb_workshop_upgrade($oldversion) {
00037     global $CFG, $DB, $OUTPUT;
00038 
00039     $dbman = $DB->get_manager();
00040 
00041     //===== 1.9.0 upgrade line ======//
00042 
00048     if ($oldversion < 2007101510) {
00049         $orphans = $DB->get_records_sql('SELECT wa.id
00050                                            FROM {workshop_assessments} wa
00051                                       LEFT JOIN {workshop_submissions} ws ON wa.submissionid = ws.id
00052                                           WHERE ws.id IS NULL');
00053         if (!empty($orphans)) {
00054             echo $OUTPUT->notification('Orphaned assessment records found - cleaning...');
00055             $DB->delete_records_list('workshop_assessments', 'id', array_keys($orphans));
00056         }
00057         upgrade_mod_savepoint(true, 2007101510, 'workshop');
00058     }
00059 
00060     //===== end of 1.9.0 upgrade line ======//
00061 
00072     if ($oldversion < 2009102901) {
00073         echo $OUTPUT->notification('Renaming old workshop module tables', 'notifysuccess');
00074         foreach (array('workshop', 'workshop_elements', 'workshop_rubrics', 'workshop_submissions', 'workshop_assessments',
00075                 'workshop_grades', 'workshop_comments', 'workshop_stockcomments') as $tableorig) {
00076             $tablearchive = $tableorig . '_old';
00077             if ($dbman->table_exists($tableorig)) {
00078                 $dbman->rename_table(new xmldb_table($tableorig), $tablearchive);
00079             }
00080             // append a new field 'newplugin' into every archived table. In this field, the name of the subplugin
00081             // who adopted the record during the migration is stored. null value means the record is not migrated yet
00082             $table = new xmldb_table($tablearchive);
00083             $field = new xmldb_field('newplugin', XMLDB_TYPE_CHAR, '28', null, null, null, null);
00084             if (!$dbman->field_exists($table, $field)) {
00085                 $dbman->add_field($table, $field);
00086             }
00087             // append a new field 'newid' in every archived table. null value means the record was not migrated yet.
00088             // the field will hold the new id of the migrated record
00089             $table = new xmldb_table($tablearchive);
00090             $field = new xmldb_field('newid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null);
00091             if (!$dbman->field_exists($table, $field)) {
00092                 $dbman->add_field($table, $field);
00093             }
00094         }
00095         upgrade_mod_savepoint(true, 2009102901, 'workshop');
00096     }
00097 
00101     if ($oldversion < 2009102902) {
00102         require_once(dirname(__FILE__) . '/upgradelib.php');
00103         echo $OUTPUT->notification('Preparing new workshop module tables', 'notifysuccess');
00104         workshop_upgrade_prepare_20_tables();
00105         upgrade_mod_savepoint(true, 2009102902, 'workshop');
00106     }
00107 
00111     if ($oldversion < 2009102903) {
00112         require_once(dirname(__FILE__) . '/upgradelib.php');
00113         echo $OUTPUT->notification('Copying workshop core data', 'notifysuccess');
00114         workshop_upgrade_module_instances();
00115         upgrade_mod_savepoint(true, 2009102903, 'workshop');
00116     }
00117 
00121     if ($oldversion < 2009102904) {
00122         require_once(dirname(__FILE__) . '/upgradelib.php');
00123         echo $OUTPUT->notification('Copying submissions', 'notifysuccess');
00124         workshop_upgrade_submissions();
00125         upgrade_mod_savepoint(true, 2009102904, 'workshop');
00126     }
00127 
00131     if ($oldversion < 2009102905) {
00132         // $filearea = "$workshop->course/$CFG->moddata/workshop/$submission->id";
00133         $fs     = get_file_storage();
00134         $from   = 'FROM {workshop_submissions} s
00135                    JOIN {workshop} w ON (w.id = s.workshopid)
00136                    JOIN {modules} m ON (m.name = :modulename)
00137                    JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = w.id)
00138                   WHERE s.attachment <> 1';
00139         $params = array('modulename' => 'workshop');
00140         $count  = $DB->count_records_sql('SELECT COUNT(s.id) ' . $from, $params);
00141         $rs     = $DB->get_recordset_sql('SELECT s.id, s.authorid, s.workshopid, cm.course, cm.id AS cmid ' .
00142                                             $from . ' ORDER BY cm.course, w.id', $params);
00143         $pbar   = new progress_bar('migrateworkshopsubmissions', 500, true);
00144         $i      = 0;
00145         foreach ($rs as $submission) {
00146             $i++;
00147             upgrade_set_timeout(60); // set up timeout, may also abort execution
00148             $pbar->update($i, $count, "Migrating workshop submissions - $i/$count");
00149 
00150             $filedir = "$CFG->dataroot/$submission->course/$CFG->moddata/workshop/$submission->id";
00151             if ($files = get_directory_list($filedir, '', false)) {
00152                 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
00153                 foreach ($files as $filename) {
00154                     $filepath = $filedir . '/' . $filename;
00155                     if (!is_readable($filepath)) {
00156                         echo $OUTPUT->notification('File not readable: ' . $filepath);
00157                         continue;
00158                     }
00159                     $filename = clean_param($filename, PARAM_FILE);
00160                     if ($filename === '') {
00161                         echo $OUTPUT->notification('Unsupported submission filename: ' . $filepath);
00162                         continue;
00163                     }
00164                     if (! $fs->file_exists($context->id, 'mod_workshop', 'submission_attachment', $submission->id, '/', $filename)) {
00165                         $filerecord = array('contextid' => $context->id,
00166                                             'component' => 'mod_workshop',
00167                                             'filearea'  => 'submission_attachment',
00168                                             'itemid'    => $submission->id,
00169                                             'filepath'  => '/',
00170                                             'filename'  => $filename,
00171                                             'userid'    => $submission->authorid);
00172                         if ($fs->create_file_from_pathname($filerecord, $filepath)) {
00173                             $submission->attachment = 1;
00174                             $DB->update_record('workshop_submissions', $submission);
00175                             unlink($filepath);
00176                         }
00177                     }
00178                 }
00179             }
00180             // remove dirs if empty
00181             @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/workshop/$submission->id");
00182             @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/workshop");
00183             @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata");
00184             @rmdir("$CFG->dataroot/$submission->course");
00185         }
00186         $rs->close();
00187         upgrade_mod_savepoint(true, 2009102905, 'workshop');
00188     }
00189 
00193     if ($oldversion < 2009102906) {
00194         require_once(dirname(__FILE__) . '/upgradelib.php');
00195         echo $OUTPUT->notification('Copying assessments', 'notifysuccess');
00196         workshop_upgrade_assessments();
00197         upgrade_mod_savepoint(true, 2009102906, 'workshop');
00198     }
00199 
00207     if ($oldversion < 2009121800) {
00208         $table = new xmldb_table('workshop_submissions');
00209         $field = new xmldb_field('published', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0', 'timegraded');
00210         if (!$dbman->field_exists($table, $field)) {
00211             $dbman->add_field($table, $field);
00212         }
00213         upgrade_mod_savepoint(true, 2009121800, 'workshop');
00214     }
00215 
00219     if ($oldversion < 2010070700) {
00220         $table = new xmldb_table('workshop');
00221         $field = new xmldb_field('evaluation', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'strategy');
00222         if (!$dbman->field_exists($table, $field)) {
00223             $dbman->add_field($table, $field);
00224         }
00225         upgrade_mod_savepoint(true, 2010070700, 'workshop');
00226     }
00227 
00231     if ($oldversion < 2010070701) {
00232         $DB->set_field('workshop', 'evaluation', 'best');
00233         upgrade_mod_savepoint(true, 2010070701, 'workshop');
00234     }
00235 
00239     if ($oldversion < 2010072300) {
00240         $table = new xmldb_table('workshop_submissions');
00241         $field = new xmldb_field('late', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'published');
00242         if (!$dbman->field_exists($table, $field)) {
00243             $dbman->add_field($table, $field);
00244         }
00245         upgrade_mod_savepoint(true, 2010072300, 'workshop');
00246     }
00247 
00256     if ($oldversion < 2010111200) {
00257         foreach (array('workshop', 'workshop_elements', 'workshop_rubrics', 'workshop_submissions', 'workshop_assessments',
00258                 'workshop_grades', 'workshop_comments', 'workshop_stockcomments') as $tableorig) {
00259             $tablearchive = $tableorig . '_old';
00260             if (!$dbman->table_exists($tablearchive)) {
00261                 $dbman->install_one_table_from_xmldb_file($CFG->dirroot.'/mod/workshop/db/install.xml', $tablearchive);
00262             }
00263         }
00264         upgrade_mod_savepoint(true, 2010111200, 'workshop');
00265     }
00266 
00275     if ($oldversion < 2011021100) {
00276         $sql = "SELECT cm.id, cm.course, cm.instance
00277                   FROM {course_modules} cm
00278                  WHERE cm.module IN (SELECT id
00279                                        FROM {modules}
00280                                       WHERE name = ?)";
00281         $rs = $DB->get_recordset_sql($sql, array('workshop'));
00282         $map = array(); // returned stdClasses by instance id
00283         foreach ($rs as $cm) {
00284             $map[$cm->instance][$cm->id] = $cm;
00285         }
00286         $rs->close();
00287 
00288         $problems = array();
00289         foreach ($map as $instanceid => $cms) {
00290             if (count($cms) > 1) {
00291                 $problems[] = 'workshop instance ' . $instanceid . ' referenced by course_modules ' . implode(', ', array_keys($cms));
00292             }
00293         }
00294         if ($problems) {
00295             echo $OUTPUT->notification('¡Ay, caramba! Data integrity corruption has been detected in your workshop ' . PHP_EOL .
00296                 'module database tables. This might be caused by a bug in workshop upgrade code. ' . PHP_EOL .
00297                 'Please report this issue immediately in workshop module support forum at ' . PHP_EOL .
00298                 'http://moodle.org so that we can help to fix this problem. Please copy and keep ' . PHP_EOL .
00299                 'following information for future reference:');
00300             foreach ($problems as $problem) {
00301                 echo $OUTPUT->notification($problem);
00302                 upgrade_log(UPGRADE_LOG_NOTICE, 'mod_workshop', 'course_modules integrity problem', $problem);
00303             }
00304         }
00305 
00306         unset($problems);
00307         unset($map);
00308         upgrade_mod_savepoint(true, 2011021100, 'workshop');
00309     }
00310 
00311     // Moodle v2.1.0 release upgrade line
00312     // Put any upgrade step following this
00313 
00317     if ($oldversion < 2011110400) {
00318         $dbman->reset_sequence('workshop');
00319         upgrade_mod_savepoint(true, 2011110400, 'workshop');
00320     }
00321 
00322     // Moodle v2.2.0 release upgrade line
00323     // Put any upgrade step following this
00324 
00325     return true;
00326 }
 All Data Structures Namespaces Files Functions Variables Enumerations