Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/db/upgradelib.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 
00032 defined('MOODLE_INTERNAL') || die();
00033 
00034 function upgrade_fix_category_depths() {
00035     global $CFG, $DB;
00036 
00037     // first fix incorrect parents
00038     $sql = "SELECT c.id
00039               FROM {course_categories} c
00040              WHERE c.parent > 0 AND c.parent NOT IN (SELECT pc.id FROM {course_categories} pc)";
00041     $rs = $DB->get_recordset_sql($sql);
00042     foreach ($rs as $cat) {
00043         $cat->depth  = 1;
00044         $cat->path   = '/'.$cat->id;
00045         $cat->parent = 0;
00046         $DB->update_record('course_categories', $cat);
00047     }
00048     $rs->close();
00049 
00050     // now add path and depth to top level categories
00051     $sql = "UPDATE {course_categories}
00052                SET depth = 1, path = ".$DB->sql_concat("'/'", "id")."
00053              WHERE parent = 0";
00054     $DB->execute($sql);
00055 
00056     // now fix all other levels - slow but works in all supported dbs
00057     $parentdepth = 1;
00058     while ($DB->record_exists('course_categories', array('depth'=>0))) {
00059         $sql = "SELECT c.id, pc.path
00060                   FROM {course_categories} c, {course_categories} pc
00061                  WHERE c.parent=pc.id AND c.depth=0 AND pc.depth=?";
00062         $rs = $DB->get_recordset_sql($sql, array($parentdepth));
00063         foreach ($rs as $cat) {
00064             $cat->depth = $parentdepth+1;
00065             $cat->path  = $cat->path.'/'.$cat->id;
00066             $DB->update_record('course_categories', $cat);
00067         }
00068         $rs->close();
00069         $parentdepth++;
00070         if ($parentdepth > 100) {
00071             //something must have gone wrong - nobody can have more than 100 levels of categories, right?
00072             debugging('Unknown error fixing category depths');
00073             break;
00074         }
00075     }
00076 }
00077 
00083 function upgrade_migrate_files_courses() {
00084     global $DB, $CFG;
00085     require_once($CFG->libdir.'/filelib.php');
00086 
00087     set_config('upgradenewfilemirgation', 1);
00088 
00089     $count = $DB->count_records('course');
00090     $pbar = new progress_bar('migratecoursefiles', 500, true);
00091 
00092     $rs = $DB->get_recordset('course');
00093     $i = 0;
00094     foreach ($rs as $course) {
00095         $i++;
00096         upgrade_set_timeout(60*5); // set up timeout, may also abort execution
00097         $context = get_context_instance(CONTEXT_COURSE, $course->id);
00098         upgrade_migrate_files_course($context, '/', true);
00099         $pbar->update($i, $count, "Migrated course files - course $i/$count.");
00100     }
00101     $rs->close();
00102 
00103     return true;
00104 }
00105 
00111 function upgrade_simplify_overkill_pool_structure() {
00112     global $CFG, $OUTPUT;
00113 
00114     if (isset($CFG->upgradenewfilemirgation)) {
00115         // newer upgrade, directory structure is in the form xx/xx already
00116         unset_config('upgradenewfilemirgation');
00117         return;
00118     }
00119 
00120     $filedir = $CFG->dataroot.'/filedir'; // hardcoded hack, do not use elsewhere!!
00121 
00122     echo $OUTPUT->notification("Changing file pool directory structure, this may take a while...", 'notifysuccess');
00123 
00124     $dir_l1 = new DirectoryIterator($filedir);
00125     foreach ($dir_l1 as $d1) {
00126         if ($d1->isDot() or $d1->isLink() or !$d1->isDir()) {
00127             continue;
00128         }
00129         $name1 = $d1->getFilename();
00130         if (strlen($name1) != 2) {
00131             continue; //weird
00132         }
00133         $dir_l2 = new DirectoryIterator("$filedir/$name1");
00134         foreach ($dir_l2 as $d2) {
00135             if ($d2->isDot() or $d2->isLink() or !$d2->isDir()) {
00136                 continue;
00137             }
00138             $name2 = $d2->getFilename();
00139             if (strlen($name2) != 2) {
00140                 continue; //weird
00141             }
00142             $dir_l3 = new DirectoryIterator("$filedir/$name1/$name2");
00143             foreach ($dir_l3 as $d3) {
00144                 if ($d3->isDot() or $d3->isLink() or !$d3->isDir()) {
00145                     continue;
00146                 }
00147                 $name3 = $d3->getFilename();
00148                 if (strlen($name3) != 2) {
00149                     continue; //weird
00150                 }
00151                 $dir_l4 = new DirectoryIterator("$filedir/$name1/$name2/$name3");
00152                 foreach ($dir_l4 as $d4) {
00153                     if (!$d4->isFile()) {
00154                         continue; //. or ..
00155                     }
00156                     upgrade_set_timeout(60*5); // set up timeout, may also abort execution
00157                     $newfile = "$filedir/$name1/$name2/".$d4->getFilename();
00158                     $oldfile = "$filedir/$name1/$name2/$name3/".$d4->getFilename();
00159                     if (!file_exists($newfile)) {
00160                         rename($oldfile, $newfile);
00161                     }
00162                 }
00163                 unset($d4);
00164                 unset($dir_l4);
00165                 rmdir("$filedir/$name1/$name2/$name3");
00166             }
00167             unset($d3);
00168             unset($dir_l3); // release file handles
00169         }
00170         unset($d2);
00171         unset($dir_l2); // release file handles
00172     }
00173 }
00174 
00178 function upgrade_migrate_user_icons() {
00179     global $CFG, $OUTPUT, $DB;
00180 
00181     $fs = get_file_storage();
00182 
00183     $icon = array('component'=>'user', 'filearea'=>'icon', 'itemid'=>0, 'filepath'=>'/');
00184 
00185     $count = $DB->count_records('user', array('picture'=>1, 'deleted'=>0));
00186     $pbar = new progress_bar('migrateusericons', 500, true);
00187 
00188     $rs = $DB->get_recordset('user', array('picture'=>1, 'deleted'=>0), 'id ASC', 'id, picture');
00189     $i = 0;
00190     foreach ($rs as $user) {
00191         $i++;
00192         upgrade_set_timeout(60); 
00193         $pbar->update($i, $count, "Migrated user icons $i/$count.");
00194 
00195         if (!$context = get_context_instance(CONTEXT_USER, $user->id)) {
00196             // deleted user
00197             continue;
00198         }
00199 
00200         if ($fs->file_exists($context->id, 'user', 'icon', 0, '/', 'f1.jpg')) {
00201             // already converted!
00202             continue;
00203         }
00204 
00205         $level1 = floor($user->id / 1000) * 1000;
00206         $userdir = "$CFG->dataroot/user/$level1/$user->id";
00207         if (!file_exists("$userdir/f1.jpg") or !file_exists("$userdir/f2.jpg")) {
00208             $userdir = "$CFG->dataroot/users/$user->id";
00209             if (!file_exists("$userdir/f1.jpg") or !file_exists("$userdir/f2.jpg")) {
00210                 // no image found, sorry
00211                 $user->picture = 0;
00212                 $DB->update_record('user', $user);
00213                 continue;
00214             }
00215         }
00216 
00217         $icon['contextid'] = $context->id;
00218         $icon['filename']  = 'f1.jpg';
00219         $fs->create_file_from_pathname($icon, "$userdir/f1.jpg");
00220         $icon['filename']  = 'f2.jpg';
00221         $fs->create_file_from_pathname($icon, "$userdir/f2.jpg");
00222     }
00223     $rs->close();
00224 
00225     // purge all old user image dirs
00226     remove_dir("$CFG->dataroot/user");
00227     remove_dir("$CFG->dataroot/users");
00228 }
00229 
00233 function upgrade_migrate_group_icons() {
00234     global $CFG, $OUTPUT, $DB;
00235 
00236     $fs = get_file_storage();
00237 
00238     $icon = array('component'=>'group', 'filearea'=>'icon', 'filepath'=>'/');
00239 
00240     $count = $DB->count_records('groups', array('picture'=>1));
00241     $pbar = new progress_bar('migrategroupfiles', 500, true);
00242 
00243     $rs = $DB->get_recordset('groups', array('picture'=>1), 'courseid ASC', 'id, picture, courseid');
00244     $i = 0;
00245     foreach ($rs as $group) {
00246         $i++;
00247         upgrade_set_timeout(60); 
00248         $pbar->update($i, $count, "Migrated group icons  $i/$count.");
00249 
00250         if (!$context = get_context_instance(CONTEXT_COURSE, $group->courseid)) {
00251             debugging('Invalid group record (id=' . $group->id . ') found.');
00252             continue;
00253         }
00254 
00255         if ($fs->file_exists($context->id, 'group', 'icon', $group->id, '/', 'f1.jpg')) {
00256             // already converted!
00257             continue;
00258         }
00259 
00260         $groupdir = "$CFG->dataroot/groups/$group->id";
00261         if (!file_exists("$groupdir/f1.jpg") or !file_exists("$groupdir/f2.jpg")) {
00262             // no image found, sorry
00263             $group->picture = 0;
00264             $DB->update_record('groups', $group);
00265             continue;
00266         }
00267 
00268         $icon['contextid'] = $context->id;
00269         $icon['itemid']    = $group->id;
00270         $icon['filename']  = 'f1.jpg';
00271         $fs->create_file_from_pathname($icon, "$groupdir/f1.jpg");
00272         $icon['filename']  = 'f2.jpg';
00273         $fs->create_file_from_pathname($icon, "$groupdir/f2.jpg");
00274     }
00275     $rs->close();
00276 
00277     // purge all old group image dirs
00278     remove_dir("$CFG->dataroot/groups");
00279 }
00280 
00284 function upgrade_migrate_files_course($context, $path, $delete) {
00285     global $CFG, $OUTPUT;
00286 
00287     $fullpathname = $CFG->dataroot.'/'.$context->instanceid.$path;
00288     if (!file_exists($fullpathname)) {
00289         return;
00290     }
00291     $items = new DirectoryIterator($fullpathname);
00292     $fs = get_file_storage();
00293 
00294     $textlib = textlib_get_instance();
00295 
00296     foreach ($items as $item) {
00297         if ($item->isDot()) {
00298             continue;
00299         }
00300 
00301         if ($item->isLink()) {
00302             // do not delete symbolic links or its children
00303             $delete_this = false;
00304         } else {
00305             $delete_this = $delete;
00306         }
00307 
00308         if (strpos($path, '/backupdata/') === 0) {
00309             $component = 'backup';
00310             $filearea  = 'course';
00311             $filepath  = substr($path, strlen('/backupdata'));
00312         } else {
00313             $component = 'course';
00314             $filearea  = 'legacy';
00315             $filepath  = $path;
00316         }
00317 
00318         if ($item->isFile()) {
00319             if (!$item->isReadable()) {
00320                 $notification = "File not readable, skipping: ".$fullpathname.$item->getFilename();
00321                 echo $OUTPUT->notification($notification);
00322                 upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
00323                 continue;
00324             }
00325 
00326             $filepath = clean_param($filepath, PARAM_PATH);
00327             $filename = clean_param($item->getFilename(), PARAM_FILE);
00328 
00329             if ($filename === '') {
00330                 //unsupported chars, sorry
00331                 continue;
00332             }
00333 
00334             if ($textlib->strlen($filepath) > 255) {
00335                 // we need something unique and reproducible, sorry no shortening possible
00336                 $filepath = '/directory_over_255_chars/'.md5($filepath).'/';
00337                 $oldfile = $fullpathname.$item->getFilename();
00338                 $newfile = $filepath.$item->getFilename();
00339                 $notification = "File path longer than 255 chars '$oldfile', file path truncated to '$newfile'";
00340                 echo $OUTPUT->notification($notification);
00341                 upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
00342             }
00343 
00344             if ($textlib->strlen($filename) > 255) {
00345                 //try to shorten, but look for collisions
00346                 $oldfile = $fullpathname.$item->getFilename();
00347                 $parts = explode('.', $filename);
00348                 $ext = array_pop($parts);
00349                 $name = implode('.', $parts);
00350                 $name = $textlib->substr($name, 0, 254-$textlib->strlen($ext));
00351                 $newfilename = $name . '.' . $ext;
00352                 if (file_exists($fullpathname.$newfilename) or $fs->file_exists($context->id, $component, $filearea, '0', $filepath, $newfilename)) {
00353                     $filename = 'file_name_over_255_chars'.md5($filename).$ext; // bad luck, file with shortened name exists
00354                 } else {
00355                     $filename = $newfilename; // shortened name should not cause collisions
00356                 }
00357                 $notification = "File name longer than 255 chars '$oldfile', file name truncated to '$filename'";
00358                 echo $OUTPUT->notification($notification);
00359                 upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
00360             }
00361 
00362             if (!$fs->file_exists($context->id, $component, $filearea, '0', $filepath, $filename)) {
00363                 $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
00364                                      'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
00365                 if ($fs->create_file_from_pathname($file_record, $fullpathname.$item->getFilename())) {
00366                     if ($delete_this) {
00367                         @unlink($fullpathname.$item->getFilename());
00368                     }
00369                 }
00370             }
00371 
00372         } else {
00373             if ($path == '/' and $item->getFilename() == 'moddata') {
00374                 continue; // modules are responsible
00375             }
00376 
00377             $dirname = clean_param($item->getFilename(), PARAM_PATH);
00378             if ($dirname === '') {
00379                 //unsupported chars, sorry
00380                 continue;
00381             }
00382             $filepath = ($filepath.$dirname.'/');
00383             if ($filepath !== '/backupdata/' and $textlib->strlen($filepath) <= 255) {
00384                 $fs->create_directory($context->id, $component, $filearea, 0, $filepath);
00385             }
00386 
00387             //migrate recursively all subdirectories
00388             upgrade_migrate_files_course($context, $path.$item->getFilename().'/', $delete_this);
00389             if ($delete_this) {
00390                 // delete dir if empty
00391                 @rmdir($fullpathname.$item->getFilename());
00392             }
00393         }
00394     }
00395     unset($items); //release file handles
00396 }
00397 
00403 function upgrade_migrate_files_blog() {
00404     global $DB, $CFG, $OUTPUT;
00405 
00406     $fs = get_file_storage();
00407 
00408     $count = $DB->count_records_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'");
00409 
00410     $rs = $DB->get_recordset_select('post', "module='blog' AND attachment IS NOT NULL AND attachment <> '1'");
00411 
00412     if ($rs->valid()) {
00413 
00414         upgrade_set_timeout(60*20); // set up timeout, may also abort execution
00415 
00416         $pbar = new progress_bar('migrateblogfiles', 500, true);
00417 
00418         $i = 0;
00419         foreach ($rs as $entry) {
00420             $i++;
00421             $pathname = "$CFG->dataroot/blog/attachments/$entry->id/$entry->attachment";
00422             if (!file_exists($pathname)) {
00423                 $entry->attachment = NULL;
00424                 $DB->update_record('post', $entry);
00425                 continue;
00426             }
00427 
00428             $filename = clean_param($entry->attachment, PARAM_FILE);
00429             if ($filename === '') {
00430                 // weird file name, ignore it
00431                 $entry->attachment = NULL;
00432                 $DB->update_record('post', $entry);
00433                 continue;
00434             }
00435 
00436             if (!is_readable($pathname)) {
00437                 echo $OUTPUT->notification(" File not readable, skipping: ".$pathname);
00438                 continue;
00439             }
00440 
00441             if (!$fs->file_exists(SYSCONTEXTID, 'blog', 'attachment', $entry->id, '/', $filename)) {
00442                 $file_record = array('contextid'=>SYSCONTEXTID, 'component'=>'blog', 'filearea'=>'attachment', 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename,
00443                                      'timecreated'=>filectime($pathname), 'timemodified'=>filemtime($pathname), 'userid'=>$entry->userid);
00444                 $fs->create_file_from_pathname($file_record, $pathname);
00445             }
00446             @unlink($pathname);
00447             @rmdir("$CFG->dataroot/blog/attachments/$entry->id/");
00448 
00449             $entry->attachment = 1; // file name not needed there anymore
00450             $DB->update_record('post', $entry);
00451             $pbar->update($i, $count, "Migrated blog attachments - $i/$count.");
00452         }
00453     }
00454     $rs->close();
00455 
00456     @rmdir("$CFG->dataroot/blog/attachments/");
00457     @rmdir("$CFG->dataroot/blog/");
00458 }
00459 
00470 function upgrade_fix_incorrect_mnethostids() {
00471 
00472     global $CFG, $DB;
00473 
00475     $old_mnet_localhost_id = !empty($CFG->mnet_localhost_id) ? $CFG->mnet_localhost_id : 0;
00476     $old_mnet_all_hosts_id = !empty($CFG->mnet_all_hosts_id) ? $CFG->mnet_all_hosts_id : 0;
00477 
00478     $current_mnet_localhost_host = $DB->get_record('mnet_host', array('wwwroot' => $CFG->wwwroot)); 
00479     $current_mnet_all_hosts_host = $DB->get_record_select('mnet_host', $DB->sql_isempty('mnet_host', 'wwwroot', false, false)); 
00480 
00481     if (!$moodleapplicationid = $DB->get_field('mnet_application', 'id', array('name' => 'moodle'))) {
00482         $m = (object)array(
00483             'name'              => 'moodle',
00484             'display_name'      => 'Moodle',
00485             'xmlrpc_server_url' => '/mnet/xmlrpc/server.php',
00486             'sso_land_url'      => '/auth/mnet/land.php',
00487             'sso_jump_url'      => '/auth/mnet/jump.php',
00488         );
00489         $moodleapplicationid = $DB->insert_record('mnet_application', $m);
00490     }
00491 
00494     if (!$current_mnet_localhost_host) {
00495         $current_mnet_localhost_host                     = new stdClass();
00496         $current_mnet_localhost_host->wwwroot            = $CFG->wwwroot;
00497         $current_mnet_localhost_host->ip_address         = '';
00498         $current_mnet_localhost_host->public_key         = '';
00499         $current_mnet_localhost_host->public_key_expires = 0;
00500         $current_mnet_localhost_host->last_connect_time  = 0;
00501         $current_mnet_localhost_host->last_log_id        = 0;
00502         $current_mnet_localhost_host->deleted            = 0;
00503         $current_mnet_localhost_host->name               = '';
00504         $current_mnet_localhost_host->applicationid      = $moodleapplicationid;
00506         if (empty($_SERVER['SERVER_ADDR'])) {
00508             $count = preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $current_mnet_localhost_host->wwwroot, $matches);
00509             $my_hostname = $count > 0 ? $matches[1] : false;
00510             $my_ip       = gethostbyname($my_hostname);  // Returns unmodified hostname on failure. DOH!
00511             if ($my_ip == $my_hostname) {
00512                 $current_mnet_localhost_host->ip_address = 'UNKNOWN';
00513             } else {
00514                 $current_mnet_localhost_host->ip_address = $my_ip;
00515             }
00516         } else {
00517             $current_mnet_localhost_host->ip_address = $_SERVER['SERVER_ADDR'];
00518         }
00519         $current_mnet_localhost_host->id = $DB->insert_record('mnet_host', $current_mnet_localhost_host, true);
00520     }
00521 
00524     if (!$current_mnet_all_hosts_host) {
00525         $current_mnet_all_hosts_host                     = new stdClass();
00526         $current_mnet_all_hosts_host->wwwroot            = '';
00527         $current_mnet_all_hosts_host->ip_address         = '';
00528         $current_mnet_all_hosts_host->public_key         = '';
00529         $current_mnet_all_hosts_host->public_key_expires = 0;
00530         $current_mnet_all_hosts_host->last_connect_time  = 0;
00531         $current_mnet_all_hosts_host->last_log_id        = 0;
00532         $current_mnet_all_hosts_host->deleted            = 0;
00533         $current_mnet_all_hosts_host->name               = 'All Hosts';
00534         $current_mnet_all_hosts_host->applicationid      = $moodleapplicationid;
00535         $current_mnet_all_hosts_host->id                 = $DB->insert_record('mnet_host', $current_mnet_all_hosts_host, true);
00536     }
00537 
00539 
00540     if ($old_mnet_localhost_id != $current_mnet_localhost_host->id) { 
00541 
00542         set_config('mnet_localhost_id', $current_mnet_localhost_host->id);
00543 
00545         $DB->delete_records('mnet_host', array('id' => $old_mnet_localhost_id));
00546     }
00547 
00549 
00550     if ($old_mnet_all_hosts_id != $current_mnet_all_hosts_host->id) { 
00551 
00552         set_config('mnet_all_hosts_id', $current_mnet_all_hosts_host->id);
00553 
00555         $DB->delete_records('mnet_host', array('id' => $old_mnet_all_hosts_id));
00556     }
00557 
00559     $hosts = $DB->get_records_menu('mnet_host', null, '', 'id, id AS id2');
00560     list($in_sql, $in_params) = $DB->get_in_or_equal($hosts, SQL_PARAMS_QM, null, false);
00561 
00562     $sql = "SELECT id
00563             FROM {user} u1
00564             WHERE u1.mnethostid $in_sql
00565               AND NOT EXISTS (
00566                   SELECT 'x'
00567                     FROM {user} u2
00568                    WHERE u2.username = u1.username
00569                      AND u2.mnethostid = ?)";
00570 
00571     $params = array_merge($in_params, array($current_mnet_localhost_host->id));
00572 
00573     $rs = $DB->get_recordset_sql($sql, $params);
00574     foreach ($rs as $rec) {
00575         $DB->set_field('user', 'mnethostid', $current_mnet_localhost_host->id, array('id' => $rec->id));
00576         upgrade_set_timeout(60); 
00577     }
00578     $rs->close();
00579 
00580     // fix up any host records that have incorrect ids
00581     $DB->set_field_select('mnet_host', 'applicationid', $moodleapplicationid, 'id = ? or id = ?', array($current_mnet_localhost_host->id, $current_mnet_all_hosts_host->id));
00582 
00583 }
00584 
00603 function upgrade_cleanup_unwanted_block_contexts($contextidarray) {
00604     global $DB;
00605 
00606     if (!is_array($contextidarray) || count($contextidarray)===0) {
00607         // Ummmm no instances?
00608         return;
00609     }
00610 
00611     $contextidstring = join(',', $contextidarray);
00612 
00613     $blockcontexts = $DB->get_recordset_select('context', 'contextlevel = '.CONTEXT_BLOCK.' AND id IN ('.$contextidstring.')', array(), '', 'id, contextlevel');
00614     $blockcontextids = array();
00615     foreach ($blockcontexts as $blockcontext) {
00616         $blockcontextids[] = $blockcontext->id;
00617     }
00618 
00619     if (count($blockcontextids)===0) {
00620         // None of the instances have unique contexts
00621         return;
00622     }
00623 
00624     $blockcontextidsstring = join(',', $blockcontextids);
00625 
00626     $DB->delete_records_select('role_assignments', 'contextid IN ('.$blockcontextidsstring.')');
00627     $DB->delete_records_select('role_capabilities', 'contextid IN ('.$blockcontextidsstring.')');
00628     $DB->delete_records_select('role_names', 'contextid IN ('.$blockcontextidsstring.')');
00629     $DB->delete_records_select('context', 'id IN ('.$blockcontextidsstring.')');
00630 }
00631 
00638 function update_fix_automated_backup_config() {
00639     $mappings = array(
00640         // Old setting      => new setting
00641         'backup_sche_active'            => 'backup_auto_active',
00642         'backup_sche_hour'              => 'backup_auto_hour',
00643         'backup_sche_minute'            => 'backup_auto_minute',
00644         'backup_sche_destination'       => 'backup_auto_destination',
00645         'backup_sche_keep'              => 'backup_auto_keep',
00646         'backup_sche_userfiles'         => 'backup_auto_user_files',
00647         'backup_sche_modules'           => 'backup_auto_activities',
00648         'backup_sche_logs'              => 'backup_auto_logs',
00649         'backup_sche_messages'          => 'backup_auto_messages',
00650         'backup_sche_blocks'            => 'backup_auto_blocks',
00651         'backup_sche_weekdays'          => 'backup_auto_weekdays',
00652         'backup_sche_users'             => 'backup_auto_users',
00653         'backup_sche_blogs'             => 'backup_auto_blogs',
00654         'backup_sche_coursefiles'       => null,
00655         'backup_sche_sitefiles'         => null,
00656         'backup_sche_withuserdata'      => null,
00657         'backup_sche_metacourse'        => null,
00658         'backup_sche_running'           => null,
00659     );
00660 
00661     $oldconfig = get_config('backup');
00662     foreach ($mappings as $oldsetting=>$newsetting) {
00663         if (!isset($oldconfig->$oldsetting)) {
00664             continue;
00665         }
00666         if ($newsetting !== null) {
00667             $oldvalue = $oldconfig->$oldsetting;
00668             set_config($newsetting, $oldvalue, 'backup');
00669         }
00670         unset_config($oldsetting, 'backup');
00671     }
00672 
00673     unset_config('backup_sche_gradebook_history');
00674     unset_config('disablescheduleddbackups');
00675 }
00676 
00681 function upgrade_populate_default_messaging_prefs() {
00682     global $DB;
00683 
00684     $providers = $DB->get_records('message_providers');
00685     $processors = $DB->get_records('message_processors');
00686     $defaultpreferences = (object)$DB->get_records_menu('config_plugins', array('plugin'=>'message'), '', 'name,value');
00687 
00688     $transaction = $DB->start_delegated_transaction();
00689 
00690     $setting = new stdClass();
00691     $setting->plugin = 'message';
00692 
00693     foreach ($providers as $provider) {
00694         $componentproviderbase = $provider->component.'_'.$provider->name;
00695         // set MESSAGE_PERMITTED to all combinations of message types
00696         // (providers) and outputs (processors)
00697         foreach ($processors as $processor) {
00698             $preferencename = $processor->name.'_provider_'.$componentproviderbase.'_permitted';
00699             if (!isset($defaultpreferences->{$preferencename})) {
00700                 $setting->name = $preferencename;
00701                 $setting->value = 'permitted';
00702                 $DB->insert_record('config_plugins', $setting);
00703             }
00704         }
00705         // for email output we also have to set MESSAGE_DEFAULT_OFFLINE + MESSAGE_DEFAULT_ONLINE
00706         foreach(array('loggedin', 'loggedoff') as $state) {
00707             $preferencename = 'message_provider_'.$componentproviderbase.'_'.$state;
00708             if (!isset($defaultpreferences->{$preferencename})) {
00709                 $setting->name = $preferencename;
00710                 $setting->value = 'email';
00711                 // except instant message where default for popup should be
00712                 // MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF and for email
00713                 // MESSAGE_DEFAULT_LOGGEDOFF.
00714                 if ($componentproviderbase == 'moodle_instantmessage') {
00715                     if  ($state == 'loggedoff') {
00716                         $setting->value = 'email,popup';
00717                     } else {
00718                         $setting->value = 'popup';
00719                     }
00720                 }
00721                 $DB->insert_record('config_plugins', $setting);
00722             }
00723         }
00724     }
00725     $transaction->allow_commit();
00726 }
 All Data Structures Namespaces Files Functions Variables Enumerations