2009-11-01 11:31:16 +00:00
< ? php
2010-08-03 08:07:49 +00:00
// This file is part of Moodle - http://moodle.org/
2009-01-11 09:41:48 +00:00
//
2010-08-03 08:07:49 +00:00
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is executed right after the install . xml
*
2012-04-01 22:48:17 +02:00
* For more information , take a look to the documentation available :
* - Upgrade API : { @ link http :// docs . moodle . org / dev / Upgrade_API }
*
* @ package core_install
* @ category upgrade
* @ copyright 2009 Petr Skoda ( http :// skodak . org )
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
2010-08-03 08:07:49 +00:00
*/
defined ( 'MOODLE_INTERNAL' ) || die ();
2009-01-11 09:41:48 +00:00
2012-04-01 22:48:17 +02:00
/**
* Main post - install tasks to be executed after the BD schema is available
*
* This function is automatically executed after Moodle core DB has been
* created at initial install . It ' s in charge of perform the initial tasks
* not covered by the { @ link install . xml } file , like create initial users ,
* roles , templates , moving stuff from other plugins ...
*
* Note that the function is only invoked once , at install time , so if new tasks
* are needed in the future , they will need to be added both here ( for new sites )
* and in the corresponding { @ link upgrade . php } file ( for existing sites ) .
*
* All plugins within Moodle ( modules , blocks , reports ... ) support the existence of
* their own install . php file , using the " Frankenstyle " component name as
* defined at { @ link http :// docs . moodle . org / dev / Frankenstyle }, for example :
* - { @ link xmldb_page_install ()} . ( modules don ' t require the plugintype ( " mod_ " ) to be used .
* - { @ link xmldb_enrol_meta_install ()} .
* - { @ link xmldb_workshopform_accumulative_install ()} .
* - ....
*
* Finally , note that it ' s also supported to have one uninstall . php file that is
* executed also once , each time one plugin is uninstalled ( before the DB schema is
* deleted ) . Those uninstall files will contain one function , using the " Frankenstyle "
* naming conventions , like { @ link xmldb_enrol_meta_uninstall ()} or { @ link xmldb_workshop_uninstall ()} .
*/
2009-01-12 19:36:59 +00:00
function xmldb_main_install () {
2011-09-29 17:07:16 +13:00
global $CFG , $DB , $SITE , $OUTPUT ;
2009-01-11 09:41:48 +00:00
2012-04-01 22:48:17 +02:00
// Make sure system context exists
2011-10-14 12:48:00 +02:00
$syscontext = context_system :: instance ( 0 , MUST_EXIST , false );
if ( $syscontext -> id != SYSCONTEXTID ) {
2010-08-28 11:39:03 +00:00
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Unexpected new system context id!' );
2009-01-11 09:41:48 +00:00
}
2012-04-01 22:48:17 +02:00
// Create site course
2011-10-14 12:48:00 +02:00
if ( $DB -> record_exists ( 'course' , array ())) {
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Can not create frontpage course, courses already exist.' );
}
2010-09-21 08:07:44 +00:00
$newsite = new stdClass ();
2010-08-28 11:39:03 +00:00
$newsite -> fullname = '' ;
$newsite -> shortname = '' ;
$newsite -> summary = NULL ;
$newsite -> newsitems = 3 ;
2012-07-02 14:26:15 +08:00
$newsite -> numsections = 1 ;
2010-08-28 11:39:03 +00:00
$newsite -> category = 0 ;
$newsite -> format = 'site' ; // Only for this course
2011-03-27 18:22:11 +02:00
$newsite -> timecreated = time ();
$newsite -> timemodified = $newsite -> timecreated ;
2009-01-11 09:41:48 +00:00
2011-10-14 12:48:00 +02:00
if ( defined ( 'SITEID' )) {
$newsite -> id = SITEID ;
$DB -> import_record ( 'course' , $newsite );
$DB -> get_manager () -> reset_sequence ( 'course' );
} else {
$newsite -> id = $DB -> insert_record ( 'course' , $newsite );
define ( 'SITEID' , $newsite -> id );
}
2012-10-11 15:08:17 +08:00
// set the field 'numsections'. We can not use format_site::update_format_options() because
// the file is not loaded
$DB -> insert_record ( 'course_format_options' , array ( 'courseid' => SITEID , 'format' => 'site' ,
'sectionid' => 0 , 'name' => 'numsections' , 'value' => $newsite -> numsections ));
2009-01-11 09:41:48 +00:00
$SITE = get_site ();
2011-10-14 12:48:00 +02:00
if ( $newsite -> id != $SITE -> id ) {
2010-08-28 11:39:03 +00:00
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Unexpected new site course id!' );
2009-01-11 09:41:48 +00:00
}
2011-10-14 12:48:00 +02:00
// Make sure site course context exists
context_course :: instance ( $SITE -> id );
2013-08-21 13:42:30 +08:00
// Update the global frontpage cache
2011-10-14 12:48:00 +02:00
$SITE = $DB -> get_record ( 'course' , array ( 'id' => $newsite -> id ), '*' , MUST_EXIST );
2009-01-11 09:41:48 +00:00
2012-04-01 22:48:17 +02:00
// Create default course category
2011-10-14 12:48:00 +02:00
if ( $DB -> record_exists ( 'course_categories' , array ())) {
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Can not create default course category, categories already exist.' );
}
$cat = new stdClass ();
$cat -> name = get_string ( 'miscellaneous' );
$cat -> depth = 1 ;
$cat -> sortorder = MAX_COURSES_IN_CATEGORY ;
$cat -> timemodified = time ();
$catid = $DB -> insert_record ( 'course_categories' , $cat );
$DB -> set_field ( 'course_categories' , 'path' , '/' . $catid , array ( 'id' => $catid ));
// Make sure category context exists
context_coursecat :: instance ( $catid );
2009-01-11 09:41:48 +00:00
2009-04-13 07:04:07 +00:00
$defaults = array (
'rolesactive' => '0' , // marks fully set up system
'auth' => 'email' ,
'auth_pop3mailbox' => 'INBOX' ,
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
'enrol_plugins_enabled' => 'manual,guest,self,cohort' ,
2010-01-12 17:21:52 +00:00
'theme' => theme_config :: DEFAULT_THEME ,
2009-04-13 07:04:07 +00:00
'filter_multilang_converted' => 1 ,
2009-05-25 20:55:27 +00:00
'siteidentifier' => random_string ( 32 ) . get_host_from_url ( $CFG -> wwwroot ),
2009-04-13 07:04:07 +00:00
'backup_version' => 2008111700 ,
'backup_release' => '2.0 dev' ,
'mnet_dispatcher_mode' => 'off' ,
'sessiontimeout' => 7200 , // must be present during roles installation
'stringfilters' => '' , // These two are managed in a strange way by the filters
'filterall' => 0 , // setting page, so have to be initialised here.
2014-02-13 15:57:44 +08:00
'texteditors' => 'atto,tinymce,textarea' ,
2009-04-13 07:04:07 +00:00
);
2009-01-11 09:41:48 +00:00
foreach ( $defaults as $key => $value ) {
set_config ( $key , $value );
}
2012-04-01 22:48:17 +02:00
// Bootstrap mnet
2010-09-21 08:07:44 +00:00
$mnethost = new stdClass ();
2009-01-11 09:41:48 +00:00
$mnethost -> wwwroot = $CFG -> wwwroot ;
$mnethost -> name = '' ;
$mnethost -> name = '' ;
$mnethost -> public_key = '' ;
if ( empty ( $_SERVER [ 'SERVER_ADDR' ])) {
// SERVER_ADDR is only returned by Apache-like webservers
preg_match ( " @^(?:http[s]?://)?([A-Z0-9 \ - \ .]+).*@i " , $CFG -> wwwroot , $matches );
$my_hostname = $matches [ 1 ];
$my_ip = gethostbyname ( $my_hostname ); // Returns unmodified hostname on failure. DOH!
if ( $my_ip == $my_hostname ) {
$mnethost -> ip_address = 'UNKNOWN' ;
} else {
$mnethost -> ip_address = $my_ip ;
}
} else {
$mnethost -> ip_address = $_SERVER [ 'SERVER_ADDR' ];
}
$mnetid = $DB -> insert_record ( 'mnet_host' , $mnethost );
set_config ( 'mnet_localhost_id' , $mnetid );
2009-01-12 19:36:59 +00:00
// Initial insert of mnet applications info
2010-09-21 08:07:44 +00:00
$mnet_app = new stdClass ();
2009-01-12 19:36:59 +00:00
$mnet_app -> name = 'moodle' ;
$mnet_app -> display_name = 'Moodle' ;
$mnet_app -> xmlrpc_server_url = '/mnet/xmlrpc/server.php' ;
$mnet_app -> sso_land_url = '/auth/mnet/land.php' ;
2010-09-17 14:10:20 +00:00
$mnet_app -> sso_jump_url = '/auth/mnet/jump.php' ;
2010-04-06 19:37:59 +00:00
$moodleapplicationid = $DB -> insert_record ( 'mnet_application' , $mnet_app );
2009-01-12 19:36:59 +00:00
2010-09-21 08:07:44 +00:00
$mnet_app = new stdClass ();
2009-01-12 19:36:59 +00:00
$mnet_app -> name = 'mahara' ;
$mnet_app -> display_name = 'Mahara' ;
$mnet_app -> xmlrpc_server_url = '/api/xmlrpc/server.php' ;
$mnet_app -> sso_land_url = '/auth/xmlrpc/land.php' ;
$mnet_app -> sso_jump_url = '/auth/xmlrpc/jump.php' ;
$DB -> insert_record ( 'mnet_application' , $mnet_app );
2010-04-06 19:37:59 +00:00
// Set up the probably-to-be-removed-soon 'All hosts' record
2010-09-21 08:07:44 +00:00
$mnetallhosts = new stdClass ();
2010-04-06 19:37:59 +00:00
$mnetallhosts -> wwwroot = '' ;
$mnetallhosts -> ip_address = '' ;
$mnetallhosts -> public_key = '' ;
$mnetallhosts -> public_key_expires = 0 ;
$mnetallhosts -> last_connect_time = 0 ;
$mnetallhosts -> last_log_id = 0 ;
$mnetallhosts -> deleted = 0 ;
$mnetallhosts -> name = 'All Hosts' ;
$mnetallhosts -> applicationid = $moodleapplicationid ;
$mnetallhosts -> id = $DB -> insert_record ( 'mnet_host' , $mnetallhosts , true );
set_config ( 'mnet_all_hosts_id' , $mnetallhosts -> id );
2009-01-29 22:54:41 +00:00
2012-04-01 22:48:17 +02:00
// Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly
2011-10-14 12:48:00 +02:00
if ( $DB -> record_exists ( 'user' , array ())) {
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Can not create default users, users already exist.' );
}
2010-09-21 08:07:44 +00:00
$guest = new stdClass ();
2009-01-29 22:54:41 +00:00
$guest -> auth = 'manual' ;
$guest -> username = 'guest' ;
$guest -> password = hash_internal_user_password ( 'guest' );
$guest -> firstname = get_string ( 'guestuser' );
$guest -> lastname = ' ' ;
$guest -> email = 'root@localhost' ;
$guest -> description = get_string ( 'guestuserinfo' );
$guest -> mnethostid = $CFG -> mnet_localhost_id ;
$guest -> confirmed = 1 ;
$guest -> lang = $CFG -> lang ;
$guest -> timemodified = time ();
$guest -> id = $DB -> insert_record ( 'user' , $guest );
2010-08-28 11:39:03 +00:00
if ( $guest -> id != 1 ) {
2011-09-29 17:07:16 +13:00
echo $OUTPUT -> notification ( 'Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported' , 'notifyproblem' );
2010-08-28 11:39:03 +00:00
}
// Store guest id
set_config ( 'siteguest' , $guest -> id );
2011-10-14 12:48:00 +02:00
// Make sure user context exists
context_user :: instance ( $guest -> id );
2009-01-29 22:54:41 +00:00
2012-04-01 22:48:17 +02:00
// Now create admin user
2010-09-21 08:07:44 +00:00
$admin = new stdClass ();
2009-01-29 22:54:41 +00:00
$admin -> auth = 'manual' ;
$admin -> firstname = get_string ( 'admin' );
$admin -> lastname = get_string ( 'user' );
$admin -> username = 'admin' ;
$admin -> password = 'adminsetuppending' ;
2009-05-25 21:46:28 +00:00
$admin -> email = '' ;
2009-01-29 22:54:41 +00:00
$admin -> confirmed = 1 ;
$admin -> mnethostid = $CFG -> mnet_localhost_id ;
$admin -> lang = $CFG -> lang ;
$admin -> maildisplay = 1 ;
$admin -> timemodified = time ();
2009-05-25 20:55:27 +00:00
$admin -> lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr (); // installation hijacking prevention
2009-01-29 22:54:41 +00:00
$admin -> id = $DB -> insert_record ( 'user' , $admin );
2011-09-29 17:07:16 +13:00
2010-08-28 11:39:03 +00:00
if ( $admin -> id != 2 ) {
2011-09-29 17:07:16 +13:00
echo $OUTPUT -> notification ( 'Unexpected id generated for the Admin account. Your database configuration or clustering setup may not be fully supported' , 'notifyproblem' );
}
if ( $admin -> id != ( $guest -> id + 1 )) {
echo $OUTPUT -> notification ( 'Nonconsecutive id generated for the Admin account. Your database configuration or clustering setup may not be fully supported.' , 'notifyproblem' );
2010-08-28 11:39:03 +00:00
}
2011-09-29 17:07:16 +13:00
2012-04-01 22:48:17 +02:00
// Store list of admins
2010-03-31 07:41:31 +00:00
set_config ( 'siteadmins' , $admin -> id );
2011-10-14 12:48:00 +02:00
// Make sure user context exists
context_user :: instance ( $admin -> id );
2009-01-29 22:54:41 +00:00
2009-01-11 09:41:48 +00:00
2012-04-01 22:48:17 +02:00
// Install the roles system.
2012-05-18 11:41:16 +02:00
$managerrole = create_role ( '' , 'manager' , '' , 'manager' );
$coursecreatorrole = create_role ( '' , 'coursecreator' , '' , 'coursecreator' );
$editteacherrole = create_role ( '' , 'editingteacher' , '' , 'editingteacher' );
$noneditteacherrole = create_role ( '' , 'teacher' , '' , 'teacher' );
$studentrole = create_role ( '' , 'student' , '' , 'student' );
$guestrole = create_role ( '' , 'guest' , '' , 'guest' );
$userrole = create_role ( '' , 'user' , '' , 'user' );
$frontpagerole = create_role ( '' , 'frontpage' , '' , 'frontpage' );
2009-01-29 22:54:41 +00:00
2012-04-01 22:48:17 +02:00
// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles
2009-01-29 22:54:41 +00:00
update_capabilities ( 'moodle' );
2013-06-17 11:27:42 +02:00
// Default allow role matrices.
foreach ( $DB -> get_records ( 'role' ) as $role ) {
foreach ( array ( 'assign' , 'override' , 'switch' ) as $type ) {
$function = 'allow_' . $type ;
$allows = get_default_role_archetype_allows ( $type , $role -> archetype );
foreach ( $allows as $allowid ) {
$function ( $role -> id , $allowid );
}
}
2009-03-25 04:20:57 +00:00
}
2009-01-29 22:54:41 +00:00
2012-04-01 22:48:17 +02:00
// Set up the context levels where you can assign each role.
2010-03-31 07:41:31 +00:00
set_role_contextlevels ( $managerrole , get_default_contextlevels ( 'manager' ));
2009-01-29 22:54:41 +00:00
set_role_contextlevels ( $coursecreatorrole , get_default_contextlevels ( 'coursecreator' ));
set_role_contextlevels ( $editteacherrole , get_default_contextlevels ( 'editingteacher' ));
set_role_contextlevels ( $noneditteacherrole , get_default_contextlevels ( 'teacher' ));
set_role_contextlevels ( $studentrole , get_default_contextlevels ( 'student' ));
set_role_contextlevels ( $guestrole , get_default_contextlevels ( 'guest' ));
set_role_contextlevels ( $userrole , get_default_contextlevels ( 'user' ));
2009-01-17 23:16:20 +00:00
2012-05-23 11:28:10 +02:00
// Init theme and JS revisions
set_config ( 'themerev' , time ());
set_config ( 'jsrev' , time ());
2010-03-30 19:59:54 +00:00
2013-03-24 22:17:36 +01:00
// No admin setting for this any more, GD is now required, remove in Moodle 2.6.
2013-03-25 14:05:17 +08:00
set_config ( 'gdversion' , 2 );
2013-03-24 22:17:36 +01:00
2010-03-30 19:59:54 +00:00
// Install licenses
require_once ( $CFG -> libdir . '/licenselib.php' );
license_manager :: install_licenses ();
2010-05-04 13:04:35 +00:00
2011-10-14 12:48:00 +02:00
// Init profile pages defaults
if ( $DB -> record_exists ( 'my_pages' , array ())) {
throw new moodle_exception ( 'generalexceptionmessage' , 'error' , '' , 'Can not create default profile pages, records already exist.' );
}
2010-09-21 08:07:44 +00:00
$mypage = new stdClass ();
2010-05-04 13:04:35 +00:00
$mypage -> userid = NULL ;
$mypage -> name = '__default' ;
$mypage -> private = 0 ;
$mypage -> sortorder = 0 ;
2011-10-14 12:48:00 +02:00
$DB -> insert_record ( 'my_pages' , $mypage );
2010-05-04 13:04:35 +00:00
$mypage -> private = 1 ;
2011-10-14 12:48:00 +02:00
$DB -> insert_record ( 'my_pages' , $mypage );
2009-11-01 11:31:16 +00:00
}