|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 function xmldb_main_install() { 00030 global $CFG, $DB, $SITE, $OUTPUT; 00031 00033 $syscontext = context_system::instance(0, MUST_EXIST, false); 00034 if ($syscontext->id != SYSCONTEXTID) { 00035 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new system context id!'); 00036 } 00037 00038 00040 if ($DB->record_exists('course', array())) { 00041 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create frontpage course, courses already exist.'); 00042 } 00043 $newsite = new stdClass(); 00044 $newsite->fullname = ''; 00045 $newsite->shortname = ''; 00046 $newsite->summary = NULL; 00047 $newsite->newsitems = 3; 00048 $newsite->numsections = 0; 00049 $newsite->category = 0; 00050 $newsite->format = 'site'; // Only for this course 00051 $newsite->timecreated = time(); 00052 $newsite->timemodified = $newsite->timecreated; 00053 00054 if (defined('SITEID')) { 00055 $newsite->id = SITEID; 00056 $DB->import_record('course', $newsite); 00057 $DB->get_manager()->reset_sequence('course'); 00058 } else { 00059 $newsite->id = $DB->insert_record('course', $newsite); 00060 define('SITEID', $newsite->id); 00061 } 00062 $SITE = get_site(); 00063 if ($newsite->id != $SITE->id) { 00064 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!'); 00065 } 00066 // Make sure site course context exists 00067 context_course::instance($SITE->id); 00068 // Update the global frontpage cache 00069 $SITE = $DB->get_record('course', array('id'=>$newsite->id), '*', MUST_EXIST); 00070 00071 00073 if ($DB->record_exists('course_categories', array())) { 00074 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default course category, categories already exist.'); 00075 } 00076 $cat = new stdClass(); 00077 $cat->name = get_string('miscellaneous'); 00078 $cat->depth = 1; 00079 $cat->sortorder = MAX_COURSES_IN_CATEGORY; 00080 $cat->timemodified = time(); 00081 $catid = $DB->insert_record('course_categories', $cat); 00082 $DB->set_field('course_categories', 'path', '/'.$catid, array('id'=>$catid)); 00083 // Make sure category context exists 00084 context_coursecat::instance($catid); 00085 00086 00087 $defaults = array( 00088 'rolesactive' => '0', // marks fully set up system 00089 'auth' => 'email', 00090 'auth_pop3mailbox' => 'INBOX', 00091 'enrol_plugins_enabled' => 'manual,guest,self,cohort', 00092 'theme' => theme_config::DEFAULT_THEME, 00093 'filter_multilang_converted' => 1, 00094 'siteidentifier' => random_string(32).get_host_from_url($CFG->wwwroot), 00095 'backup_version' => 2008111700, 00096 'backup_release' => '2.0 dev', 00097 'mnet_dispatcher_mode' => 'off', 00098 'sessiontimeout' => 7200, // must be present during roles installation 00099 'stringfilters' => '', // These two are managed in a strange way by the filters 00100 'filterall' => 0, // setting page, so have to be initialised here. 00101 'texteditors' => 'tinymce,textarea', 00102 ); 00103 foreach($defaults as $key => $value) { 00104 set_config($key, $value); 00105 } 00106 00107 00109 $mnethost = new stdClass(); 00110 $mnethost->wwwroot = $CFG->wwwroot; 00111 $mnethost->name = ''; 00112 $mnethost->name = ''; 00113 $mnethost->public_key = ''; 00114 00115 if (empty($_SERVER['SERVER_ADDR'])) { 00116 // SERVER_ADDR is only returned by Apache-like webservers 00117 preg_match("@^(?:http[s]?://)?([A-Z0-9\-\.]+).*@i", $CFG->wwwroot, $matches); 00118 $my_hostname = $matches[1]; 00119 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH! 00120 if ($my_ip == $my_hostname) { 00121 $mnethost->ip_address = 'UNKNOWN'; 00122 } else { 00123 $mnethost->ip_address = $my_ip; 00124 } 00125 } else { 00126 $mnethost->ip_address = $_SERVER['SERVER_ADDR']; 00127 } 00128 00129 $mnetid = $DB->insert_record('mnet_host', $mnethost); 00130 set_config('mnet_localhost_id', $mnetid); 00131 00132 // Initial insert of mnet applications info 00133 $mnet_app = new stdClass(); 00134 $mnet_app->name = 'moodle'; 00135 $mnet_app->display_name = 'Moodle'; 00136 $mnet_app->xmlrpc_server_url = '/mnet/xmlrpc/server.php'; 00137 $mnet_app->sso_land_url = '/auth/mnet/land.php'; 00138 $mnet_app->sso_jump_url = '/auth/mnet/jump.php'; 00139 $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app); 00140 00141 $mnet_app = new stdClass(); 00142 $mnet_app->name = 'mahara'; 00143 $mnet_app->display_name = 'Mahara'; 00144 $mnet_app->xmlrpc_server_url = '/api/xmlrpc/server.php'; 00145 $mnet_app->sso_land_url = '/auth/xmlrpc/land.php'; 00146 $mnet_app->sso_jump_url = '/auth/xmlrpc/jump.php'; 00147 $DB->insert_record('mnet_application', $mnet_app); 00148 00149 // Set up the probably-to-be-removed-soon 'All hosts' record 00150 $mnetallhosts = new stdClass(); 00151 $mnetallhosts->wwwroot = ''; 00152 $mnetallhosts->ip_address = ''; 00153 $mnetallhosts->public_key = ''; 00154 $mnetallhosts->public_key_expires = 0; 00155 $mnetallhosts->last_connect_time = 0; 00156 $mnetallhosts->last_log_id = 0; 00157 $mnetallhosts->deleted = 0; 00158 $mnetallhosts->name = 'All Hosts'; 00159 $mnetallhosts->applicationid = $moodleapplicationid; 00160 $mnetallhosts->id = $DB->insert_record('mnet_host', $mnetallhosts, true); 00161 set_config('mnet_all_hosts_id', $mnetallhosts->id); 00162 00164 if ($DB->record_exists('user', array())) { 00165 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.'); 00166 } 00167 $guest = new stdClass(); 00168 $guest->auth = 'manual'; 00169 $guest->username = 'guest'; 00170 $guest->password = hash_internal_user_password('guest'); 00171 $guest->firstname = get_string('guestuser'); 00172 $guest->lastname = ' '; 00173 $guest->email = 'root@localhost'; 00174 $guest->description = get_string('guestuserinfo'); 00175 $guest->mnethostid = $CFG->mnet_localhost_id; 00176 $guest->confirmed = 1; 00177 $guest->lang = $CFG->lang; 00178 $guest->timemodified= time(); 00179 $guest->id = $DB->insert_record('user', $guest); 00180 if ($guest->id != 1) { 00181 echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem'); 00182 } 00183 // Store guest id 00184 set_config('siteguest', $guest->id); 00185 // Make sure user context exists 00186 context_user::instance($guest->id); 00187 00188 00190 $admin = new stdClass(); 00191 $admin->auth = 'manual'; 00192 $admin->firstname = get_string('admin'); 00193 $admin->lastname = get_string('user'); 00194 $admin->username = 'admin'; 00195 $admin->password = 'adminsetuppending'; 00196 $admin->email = ''; 00197 $admin->confirmed = 1; 00198 $admin->mnethostid = $CFG->mnet_localhost_id; 00199 $admin->lang = $CFG->lang; 00200 $admin->maildisplay = 1; 00201 $admin->timemodified = time(); 00202 $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention 00203 $admin->id = $DB->insert_record('user', $admin); 00204 00205 if ($admin->id != 2) { 00206 echo $OUTPUT->notification('Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem'); 00207 } 00208 if ($admin->id != ($guest->id + 1)) { 00209 echo $OUTPUT->notification('Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.', 'notifyproblem'); 00210 } 00211 00213 set_config('siteadmins', $admin->id); 00214 // Make sure user context exists 00215 context_user::instance($admin->id); 00216 00217 00219 $managerrole = create_role(get_string('manager', 'role'), 'manager', get_string('managerdescription', 'role'), 'manager'); 00220 $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'coursecreator'); 00221 $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'editingteacher'); 00222 $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'teacher'); 00223 $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'student'); 00224 $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'guest'); 00225 $userrole = create_role(get_string('authenticateduser'), 'user', get_string('authenticateduserdescription'), 'user'); 00226 $frontpagerole = create_role(get_string('frontpageuser', 'role'), 'frontpage', get_string('frontpageuserdescription', 'role'), 'frontpage'); 00227 00229 update_capabilities('moodle'); 00230 00232 $defaultallowassigns = array( 00233 array($managerrole, $managerrole), 00234 array($managerrole, $coursecreatorrole), 00235 array($managerrole, $editteacherrole), 00236 array($managerrole, $noneditteacherrole), 00237 array($managerrole, $studentrole), 00238 00239 array($editteacherrole, $noneditteacherrole), 00240 array($editteacherrole, $studentrole), 00241 ); 00242 foreach ($defaultallowassigns as $allow) { 00243 list($fromroleid, $toroleid) = $allow; 00244 allow_assign($fromroleid, $toroleid); 00245 } 00246 00248 $defaultallowoverrides = array( 00249 array($managerrole, $managerrole), 00250 array($managerrole, $coursecreatorrole), 00251 array($managerrole, $editteacherrole), 00252 array($managerrole, $noneditteacherrole), 00253 array($managerrole, $studentrole), 00254 array($managerrole, $guestrole), 00255 array($managerrole, $userrole), 00256 array($managerrole, $frontpagerole), 00257 00258 array($editteacherrole, $noneditteacherrole), 00259 array($editteacherrole, $studentrole), 00260 array($editteacherrole, $guestrole), 00261 ); 00262 foreach ($defaultallowoverrides as $allow) { 00263 list($fromroleid, $toroleid) = $allow; 00264 allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841. 00265 } 00266 00268 $defaultallowswitch = array( 00269 array($managerrole, $editteacherrole), 00270 array($managerrole, $noneditteacherrole), 00271 array($managerrole, $studentrole), 00272 array($managerrole, $guestrole), 00273 00274 array($editteacherrole, $noneditteacherrole), 00275 array($editteacherrole, $studentrole), 00276 array($editteacherrole, $guestrole), 00277 00278 array($noneditteacherrole, $studentrole), 00279 array($noneditteacherrole, $guestrole), 00280 ); 00281 foreach ($defaultallowswitch as $allow) { 00282 list($fromroleid, $toroleid) = $allow; 00283 allow_switch($fromroleid, $toroleid); 00284 } 00285 00287 set_role_contextlevels($managerrole, get_default_contextlevels('manager')); 00288 set_role_contextlevels($coursecreatorrole, get_default_contextlevels('coursecreator')); 00289 set_role_contextlevels($editteacherrole, get_default_contextlevels('editingteacher')); 00290 set_role_contextlevels($noneditteacherrole, get_default_contextlevels('teacher')); 00291 set_role_contextlevels($studentrole, get_default_contextlevels('student')); 00292 set_role_contextlevels($guestrole, get_default_contextlevels('guest')); 00293 set_role_contextlevels($userrole, get_default_contextlevels('user')); 00294 00295 // Init themes 00296 set_config('themerev', 1); 00297 00298 // Install licenses 00299 require_once($CFG->libdir . '/licenselib.php'); 00300 license_manager::install_licenses(); 00301 00302 // Init profile pages defaults 00303 if ($DB->record_exists('my_pages', array())) { 00304 throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default profile pages, records already exist.'); 00305 } 00306 $mypage = new stdClass(); 00307 $mypage->userid = NULL; 00308 $mypage->name = '__default'; 00309 $mypage->private = 0; 00310 $mypage->sortorder = 0; 00311 $DB->insert_record('my_pages', $mypage); 00312 $mypage->private = 1; 00313 $DB->insert_record('my_pages', $mypage); 00314 }