/** * 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()}. */ function xmldb_main_install() { global $CFG, $DB, $SITE, $OUTPUT; // Make sure system context exists $syscontext = context_system::instance(0, MUST_EXIST, false); if ($syscontext->id != SYSCONTEXTID) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new system context id!'); } // Create site course if ($DB->record_exists('course', array())) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create frontpage course, courses already exist.'); } $newsite = new stdClass(); $newsite->fullname = ''; $newsite->shortname = ''; $newsite->summary = NULL; $newsite->newsitems = 3; $newsite->numsections = 1; $newsite->category = 0; $newsite->format = 'site'; // Only for this course $newsite->timecreated = time(); $newsite->timemodified = $newsite->timecreated; 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); } $SITE = get_site(); if ($newsite->id != $SITE->id) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected new site course id!'); } // Make sure site course context exists context_course::instance($SITE->id); // Update the global frontpage cache $SITE = $DB->get_record('course', array('id' => $newsite->id), '*', MUST_EXIST); // Create default course category 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); $defaults = array('rolesactive' => '0', 'auth' => 'email', 'auth_pop3mailbox' => 'INBOX', 'enrol_plugins_enabled' => 'manual,guest,self,cohort', 'theme' => theme_config::DEFAULT_THEME, 'filter_multilang_converted' => 1, 'siteidentifier' => random_string(32) . get_host_from_url($CFG->wwwroot), 'backup_version' => 2008111700, 'backup_release' => '2.0 dev', 'mnet_dispatcher_mode' => 'off', 'sessiontimeout' => 7200, 'stringfilters' => '', 'filterall' => 0, 'texteditors' => 'tinymce,textarea'); foreach ($defaults as $key => $value) { set_config($key, $value); } // Bootstrap mnet $mnethost = new stdClass(); $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); // Initial insert of mnet applications info $mnet_app = new stdClass(); $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'; $mnet_app->sso_jump_url = '/auth/mnet/jump.php'; $moodleapplicationid = $DB->insert_record('mnet_application', $mnet_app); $mnet_app = new stdClass(); $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); // Set up the probably-to-be-removed-soon 'All hosts' record $mnetallhosts = new stdClass(); $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); // Create guest record - do not assign any role, guest user gets the default guest role automatically on the fly if ($DB->record_exists('user', array())) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Can not create default users, users already exist.'); } $guest = new stdClass(); $guest->auth = 'manual'; $guest->username = '******'; $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); if ($guest->id != 1) { echo $OUTPUT->notification('Unexpected id generated for the Guest account. Your database configuration or clustering setup may not be fully supported', 'notifyproblem'); } // Store guest id set_config('siteguest', $guest->id); // Make sure user context exists context_user::instance($guest->id); // Now create admin user $admin = new stdClass(); $admin->auth = 'manual'; $admin->firstname = get_string('admin'); $admin->lastname = get_string('user'); $admin->username = '******'; $admin->password = '******'; $admin->email = ''; $admin->confirmed = 1; $admin->mnethostid = $CFG->mnet_localhost_id; $admin->lang = $CFG->lang; $admin->maildisplay = 1; $admin->timemodified = time(); $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention $admin->id = $DB->insert_record('user', $admin); if ($admin->id != 2) { 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'); } // Store list of admins set_config('siteadmins', $admin->id); // Make sure user context exists context_user::instance($admin->id); // Install the roles system. $managerrole = create_role(get_string('manager', 'role'), 'manager', get_string('managerdescription', 'role'), 'manager'); $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'coursecreator'); $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'editingteacher'); $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'teacher'); $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'student'); $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'guest'); $userrole = create_role(get_string('authenticateduser'), 'user', get_string('authenticateduserdescription'), 'user'); $frontpagerole = create_role(get_string('frontpageuser', 'role'), 'frontpage', get_string('frontpageuserdescription', 'role'), 'frontpage'); // Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles update_capabilities('moodle'); // Default allow assign $defaultallowassigns = array(array($managerrole, $managerrole), array($managerrole, $coursecreatorrole), array($managerrole, $editteacherrole), array($managerrole, $noneditteacherrole), array($managerrole, $studentrole), array($editteacherrole, $noneditteacherrole), array($editteacherrole, $studentrole)); foreach ($defaultallowassigns as $allow) { list($fromroleid, $toroleid) = $allow; allow_assign($fromroleid, $toroleid); } // Default allow override $defaultallowoverrides = array(array($managerrole, $managerrole), array($managerrole, $coursecreatorrole), array($managerrole, $editteacherrole), array($managerrole, $noneditteacherrole), array($managerrole, $studentrole), array($managerrole, $guestrole), array($managerrole, $userrole), array($managerrole, $frontpagerole), array($editteacherrole, $noneditteacherrole), array($editteacherrole, $studentrole), array($editteacherrole, $guestrole)); foreach ($defaultallowoverrides as $allow) { list($fromroleid, $toroleid) = $allow; allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841. } // Default allow switch. $defaultallowswitch = array(array($managerrole, $editteacherrole), array($managerrole, $noneditteacherrole), array($managerrole, $studentrole), array($managerrole, $guestrole), array($editteacherrole, $noneditteacherrole), array($editteacherrole, $studentrole), array($editteacherrole, $guestrole), array($noneditteacherrole, $studentrole), array($noneditteacherrole, $guestrole)); foreach ($defaultallowswitch as $allow) { list($fromroleid, $toroleid) = $allow; allow_switch($fromroleid, $toroleid); } // Set up the context levels where you can assign each role. set_role_contextlevels($managerrole, get_default_contextlevels('manager')); 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')); // Init theme and JS revisions set_config('themerev', time()); set_config('jsrev', time()); // Install licenses require_once $CFG->libdir . '/licenselib.php'; license_manager::install_licenses(); // 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.'); } $mypage = new stdClass(); $mypage->userid = NULL; $mypage->name = '__default'; $mypage->private = 0; $mypage->sortorder = 0; $DB->insert_record('my_pages', $mypage); $mypage->private = 1; $DB->insert_record('my_pages', $mypage); }
protected function set_allow($fromroleid, $targetroleid) { allow_switch($fromroleid, $targetroleid); }
/** * Test allowing of role switching. */ public function test_allow_switch() { global $DB, $CFG; $this->resetAfterTest(); $otherid = create_role('Other role', 'other', 'Some other role', ''); $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); $this->assertFalse($DB->record_exists('role_allow_switch', array('roleid' => $otherid, 'allowswitch' => $student->id))); allow_switch($otherid, $student->id); $this->assertTrue($DB->record_exists('role_allow_switch', array('roleid' => $otherid, 'allowswitch' => $student->id))); // Test event trigger. $allowroleassignevent = \core\event\role_allow_switch_updated::create(array('context' => context_system::instance())); $sink = $this->redirectEvents(); $allowroleassignevent->trigger(); $events = $sink->get_events(); $sink->close(); $event = array_pop($events); $this->assertInstanceOf('\\core\\event\\role_allow_switch_updated', $event); $mode = 'switch'; $baseurl = new moodle_url('/admin/roles/allow.php', array('mode' => $mode)); $expectedlegacylog = array(SITEID, 'role', 'edit allow ' . $mode, str_replace($CFG->wwwroot . '/', '', $baseurl)); $this->assertEventLegacyLogData($expectedlegacylog, $event); }
/** * Test allowing of role switching. * @return void */ public function test_allow_switch() { global $DB; $this->resetAfterTest(); $otherid = create_role('Other role', 'other', 'Some other role', ''); $student = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); $this->assertFalse($DB->record_exists('role_allow_switch', array('roleid' => $otherid, 'allowswitch' => $student->id))); allow_switch($otherid, $student->id); $this->assertTrue($DB->record_exists('role_allow_switch', array('roleid' => $otherid, 'allowswitch' => $student->id))); }
function xmldb_main_install() { global $CFG, $DB, $SITE; /// make sure system context exists $syscontext = get_system_context(false); if ($syscontext->id != 1) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected system context id created!'); } // create site course $newsite = new object(); $newsite->fullname = ""; $newsite->shortname = ""; $newsite->summary = NULL; $newsite->newsitems = 3; $newsite->numsections = 0; $newsite->category = 0; $newsite->format = 'site'; // Only for this course $newsite->teacher = get_string("defaultcourseteacher"); $newsite->teachers = get_string("defaultcourseteachers"); $newsite->student = get_string("defaultcoursestudent"); $newsite->students = get_string("defaultcoursestudents"); $newsite->timemodified = time(); $DB->insert_record('course', $newsite); $SITE = get_site(); if ($SITE->id != 1) { throw new moodle_exception('generalexceptionmessage', 'error', '', 'Unexpected site course id created!'); } /// make sure site course context exists get_context_instance(CONTEXT_COURSE, $SITE->id); /// create default course category $cat = get_course_category(); $defaults = array('rolesactive' => '0', 'auth' => 'email', 'auth_pop3mailbox' => 'INBOX', 'enrol' => 'manual', 'enrol_plugins_enabled' => 'manual', 'style' => 'default', 'template' => 'default', 'theme' => 'standardwhite', 'filter_multilang_converted' => 1, 'siteidentifier' => random_string(32) . get_host_from_url($CFG->wwwroot), 'backup_version' => 2008111700, 'backup_release' => '2.0 dev', 'blocks_version' => 2007081300, 'mnet_dispatcher_mode' => 'off', 'sessiontimeout' => 7200, 'stringfilters' => '', 'filterall' => 0, 'texteditors' => 'tinymce,textarea'); foreach ($defaults as $key => $value) { set_config($key, $value); } /// bootstrap mnet $mnethost = new object(); $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); // Initial insert of mnet applications info $mnet_app = new object(); $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'; $mnet_app->sso_jump_url = '/auth/mnet/land.php'; $DB->insert_record('mnet_application', $mnet_app); $mnet_app = new object(); $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); /// insert log entries - replaces statements section in install.xml update_log_display_entry('user', 'view', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('course', 'user report', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('course', 'view', 'course', 'fullname'); update_log_display_entry('course', 'update', 'course', 'fullname'); update_log_display_entry('course', 'enrol', 'course', 'fullname'); update_log_display_entry('course', 'unenrol', 'course', 'fullname'); update_log_display_entry('course', 'report log', 'course', 'fullname'); update_log_display_entry('course', 'report live', 'course', 'fullname'); update_log_display_entry('course', 'report outline', 'course', 'fullname'); update_log_display_entry('course', 'report participation', 'course', 'fullname'); update_log_display_entry('course', 'report stats', 'course', 'fullname'); update_log_display_entry('message', 'write', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('message', 'read', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('message', 'add contact', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('message', 'remove contact', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('message', 'block contact', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('message', 'unblock contact', 'user', 'CONCAT(firstname,\' \',lastname)'); update_log_display_entry('group', 'view', 'groups', 'name'); /// Create guest record $guest = new object(); $guest->auth = 'manual'; $guest->username = '******'; $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); /// Now create admin user $admin = new object(); $admin->auth = 'manual'; $admin->firstname = get_string('admin'); $admin->lastname = get_string('user'); $admin->username = '******'; $admin->password = '******'; $admin->email = ''; $admin->confirmed = 1; $admin->mnethostid = $CFG->mnet_localhost_id; $admin->lang = $CFG->lang; $admin->maildisplay = 1; $admin->timemodified = time(); $admin->lastip = CLI_SCRIPT ? '0.0.0.0' : getremoteaddr(); // installation hijacking prevention $admin->id = $DB->insert_record('user', $admin); /// Install the roles system. $adminrole = create_role(get_string('administrator'), 'admin', get_string('administratordescription'), 'moodle/legacy:admin'); $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'moodle/legacy:guest'); $userrole = create_role(get_string('authenticateduser'), 'user', get_string('authenticateduserdescription'), 'moodle/legacy:user'); /// Now is the correct moment to install capabilities - after creation of legacy roles, but before assigning of roles assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $syscontext->id); update_capabilities('moodle'); /// assign default roles role_assign($guestrole, $guest->id, 0, $syscontext->id); role_assign($adminrole, $admin->id, 0, $syscontext->id); /// Default allow assign/override/switch. $defaultallows = array($coursecreatorrole => $noneditteacherrole, $coursecreatorrole => $editteacherrole, $coursecreatorrole => $studentrole, $coursecreatorrole => $guestrole, $editteacherrole => $noneditteacherrole, $editteacherrole => $studentrole, $editteacherrole => $guestrole); foreach ($defaultallows as $fromroleid => $toroleid) { allow_assign($fromroleid, $toroleid); allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841. allow_switch($fromroleid, $toroleid); } allow_switch($noneditteacherrole, $studentrole); /// Set up the context levels where you can assign each role. set_role_contextlevels($adminrole, get_default_contextlevels('admin')); 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')); }
function xmldb_core_install() { global $CFG, $DB, $SITE; // switch to new default theme set_config('theme', 'standard'); $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes $systemcontext = context_system::instance(); // add coursetype and icon fields to course table $table = new xmldb_table('course'); $field = new xmldb_field('coursetype'); if (!$dbman->field_exists($table, $field)) { $field->set_attributes(XMLDB_TYPE_INTEGER, '4', null, null, null, null, null); $dbman->add_field($table, $field); } $field = new xmldb_field('icon'); if (!$dbman->field_exists($table, $field)) { $field->set_attributes(XMLDB_TYPE_CHAR, '255', null, null, null, null); $dbman->add_field($table, $field); } // rename the moodle 'manager' fullname to "Site Manager" to make it // distinct from the "Staff Manager" if ($managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager', 'name' => get_string('manager', 'role')))) { $todb = new stdClass(); $todb->id = $managerroleid; $todb->name = get_string('sitemanager', 'local_core'); $DB->update_record('role', $todb); } // Create roles. $manager = $DB->get_record('role', array('shortname' => 'manager')); $managerrole = $manager->id; $staffmanagerrole = create_role('', 'staffmanager', '', 'staffmanager'); $assessorrole = create_role('', 'assessor', '', 'assessor'); $regionalmanagerrole = create_role('', 'regionalmanager', ''); $regionaltrainerrole = create_role('', 'regionaltrainer', ''); $defaultallowassigns = array( array($managerrole, $staffmanagerrole), array($managerrole, $assessorrole), array($managerrole, $regionalmanagerrole), array($managerrole, $regionaltrainerrole) ); foreach ($defaultallowassigns as $allow) { list($fromroleid, $toroleid) = $allow; allow_assign($fromroleid, $toroleid); } $defaultallowoverrides = array( array($managerrole, $staffmanagerrole), array($managerrole, $assessorrole), array($managerrole, $regionalmanagerrole), array($managerrole, $regionaltrainerrole) ); foreach ($defaultallowoverrides as $allow) { list($fromroleid, $toroleid) = $allow; allow_override($fromroleid, $toroleid); // There is a rant about this in MDL-15841. } $defaultallowswitch = array( array($managerrole, $staffmanagerrole), ); foreach ($defaultallowswitch as $allow) { list($fromroleid, $toroleid) = $allow; allow_switch($fromroleid, $toroleid); } set_role_contextlevels($staffmanagerrole, get_default_contextlevels('staffmanager')); assign_capability('moodle/user:viewdetails', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); assign_capability('moodle/user:viewuseractivitiesreport', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); assign_capability('moodle/cohort:view', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); assign_capability('moodle/comment:view', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); assign_capability('moodle/comment:delete', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); assign_capability('moodle/comment:post', CAP_ALLOW, $staffmanagerrole, $systemcontext->id, true); $systemcontext->mark_dirty(); set_role_contextlevels($assessorrole, get_default_contextlevels('teacher')); $role_to_modify = array( 'editingteacher' => 'editingtrainer', 'teacher' => 'trainer', 'student' => 'learner' ); $DB->update_record('role', array('id' => $assessorrole, 'archetype' => 'assessor')); assign_capability('moodle/user:editownprofile', CAP_ALLOW, $assessorrole, $systemcontext->id, true); assign_capability('moodle/user:editownprofile', CAP_ALLOW, $regionalmanagerrole, $systemcontext->id, true); assign_capability('moodle/user:editownprofile', CAP_ALLOW, $regionaltrainerrole, $systemcontext->id, true); foreach ($role_to_modify as $old => $new) { if ($old_role = $DB->get_record('role', array('shortname' => $old))) { $new_role = new stdClass(); $new_role->id = $old_role->id; $new_role->name = ''; $new_role->description = ''; $DB->update_record('role', $new_role); } } // Set up frontpage. set_config('frontpage', ''); set_config('frontpageloggedin', ''); require_once($CFG->dirroot . '/lib/completionlib.php'); if(!completion_info::is_enabled_for_site()){ set_config('coreinstallation', 1); set_config('enablecompletion', 1); set_config('enablecompletion', 1, 'moodlecourse'); set_config('completionstartonenrol', 1, 'moodlecourse'); } // Add completionstartonenrol column to course table. $table = new xmldb_table('course'); // Define field completionstartonenrol to be added to course. $field = new xmldb_field('completionstartonenrol', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0'); // Conditionally launch add field completionstartonenrol. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Add RPL column to course_completions table $table = new xmldb_table('course_completions'); // Define field rpl to be added to course_completions $field = new xmldb_field('rpl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'reaggregate'); // Conditionally launch add field rpl if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Define field rplgrade to be added to course_completions $field = new xmldb_field('rplgrade', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'rpl'); // Conditionally launch add field rpl if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Add RPL column to course_completion_crit_compl table $table = new xmldb_table('course_completion_crit_compl'); // Define field rpl to be added to course_completion_crit_compl $field = new xmldb_field('rpl', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'unenroled'); // Conditionally launch add field rpl if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Define fields status and renewalstatus to be added to course_completions. $table = new xmldb_table('course_completions'); $field = new xmldb_field('status', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0'); // Conditionally launch add field status. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } $field = new xmldb_field('renewalstatus', XMLDB_TYPE_INTEGER, '2', null, null, null, '0'); // Conditionally launch add field renewalstatus. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } rebuild_course_cache($SITE->id, true); // readd specific course completion changes for anyone // upgrading from moodle 2.2.2+ require_once($CFG->dirroot . '/local/core/db/utils.php'); local_readd_course_completion_changes(); // remove any references to "complete on unenrolment" critiera type // these could exist in an upgrade from moodle 2.2 but the criteria $DB->delete_records('course_completion_criteria', array('criteriatype' => 3)); //disable autoupdate notifications from Moodle set_config('disableupdatenotifications', '1'); set_config('disableupdateautodeploy', '1'); set_config('updateautodeploy', false); set_config('updateautocheck', false); set_config('updatenotifybuilds', false); // Disable editing execpaths by default for security. set_config('preventexecpath', '1'); // Then provide default values to prevent them appearing on the upgradesettings page. set_config('geoipfile', $CFG->dataroot . 'geoip/GeoLiteCity.dat'); set_config('location', '', 'enrol_flatfile'); set_config('filter_tex_pathlatex', '/usr/bin/latex'); set_config('filter_tex_pathdvips', '/usr/bin/dvips'); set_config('filter_tex_pathconvert', '/usr/bin/convert'); set_config('pathtodu', ''); set_config('pathtoclam', ''); set_config('aspellpath', ''); set_config('pathtodot', ''); set_config('quarantinedir', ''); set_config('backup_auto_destination', '', 'backup'); // Adding some upgrade code from lib/db/upgrade.php to // avoid conflicts every time we upgrade moodle. // This can be removed once we reach the verion of Moodle that // includes this functionality. E.g. 2.5 for badges, 2.6? for // course completion. // Add openbadges tables. // Define table 'badge' to be created $table = new xmldb_table('badge'); // Adding fields to table 'badge' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'id'); $table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name'); $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'description'); $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated'); $table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timemodified'); $table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'usercreated'); $table->add_field('issuername', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'usermodified'); $table->add_field('issuerurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'issuername'); $table->add_field('issuercontact', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'issuerurl'); $table->add_field('expiredate', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'issuercontact'); $table->add_field('expireperiod', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'expiredate'); $table->add_field('type', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'expireperiod'); $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'type'); $table->add_field('message', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'courseid'); $table->add_field('messagesubject', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'message'); $table->add_field('attachment', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'messagesubject'); $table->add_field('notification', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'attachment'); $table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'notification'); $table->add_field('nextcron', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'status'); // Adding keys to table 'badge' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_courseid', XMLDB_KEY_FOREIGN, array('courseid'), 'course', array('id')); $table->add_key('fk_usermodified', XMLDB_KEY_FOREIGN, array('usermodified'), 'user', array('id')); $table->add_key('fk_usercreated', XMLDB_KEY_FOREIGN, array('usercreated'), 'user', array('id')); // Adding indexes to table 'badge' $table->add_index('type', XMLDB_INDEX_NOTUNIQUE, array('type')); // Set the comment for the table 'badge'. $table->setComment('Defines badge'); // Conditionally launch create table for 'badge' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_criteria' to be created $table = new xmldb_table('badge_criteria'); // Adding fields to table 'badge_criteria' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); $table->add_field('criteriatype', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'badgeid'); $table->add_field('method', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'criteriatype'); // Adding keys to table 'badge_criteria' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id')); // Adding indexes to table 'badge_criteria' $table->add_index('criteriatype', XMLDB_INDEX_NOTUNIQUE, array('criteriatype')); $table->add_index('badgecriteriatype', XMLDB_INDEX_UNIQUE, array('badgeid', 'criteriatype')); // Set the comment for the table 'badge_criteria'. $table->setComment('Defines criteria for issuing badges'); // Conditionally launch create table for 'badge_criteria' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_criteria_param' to be created $table = new xmldb_table('badge_criteria_param'); // Adding fields to table 'badge_criteria_param' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id'); $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'critid'); $table->add_field('value', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'name'); // Adding keys to table 'badge_criteria_param' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id')); // Set the comment for the table 'badge_criteria_param'. $table->setComment('Defines parameters for badges criteria'); // Conditionally launch create table for 'badge_criteria_param' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_issued' to be created $table = new xmldb_table('badge_issued'); // Adding fields to table 'badge_issued' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'badgeid'); $table->add_field('uniquehash', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'userid'); $table->add_field('dateissued', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'uniquehash'); $table->add_field('dateexpire', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'dateissued'); $table->add_field('visible', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'dateexpire'); $table->add_field('issuernotified', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'visible'); // Adding keys to table 'badge_issued' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id')); $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); // Adding indexes to table 'badge_issued' $table->add_index('badgeuser', XMLDB_INDEX_UNIQUE, array('badgeid', 'userid')); // Set the comment for the table 'badge_issued'. $table->setComment('Defines issued badges'); // Conditionally launch create table for 'badge_issued' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_criteria_met' to be created $table = new xmldb_table('badge_criteria_met'); // Adding fields to table 'badge_criteria_met' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('issuedid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'id'); $table->add_field('critid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuedid'); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'critid'); $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'userid'); // Adding keys to table 'badge_criteria_met' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_critid', XMLDB_KEY_FOREIGN, array('critid'), 'badge_criteria', array('id')); $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); $table->add_key('fk_issuedid', XMLDB_KEY_FOREIGN, array('issuedid'), 'badge_issued', array('id')); // Set the comment for the table 'badge_criteria_met'. $table->setComment('Defines criteria that were met for an issued badge'); // Conditionally launch create table for 'badge_criteria_met' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_manual_award' to be created $table = new xmldb_table('badge_manual_award'); // Adding fields to table 'badge_manual_award' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('badgeid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id'); $table->add_field('recipientid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'badgeid'); $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'recipientid'); $table->add_field('issuerrole', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerid'); $table->add_field('datemet', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'issuerrole'); // Adding keys to table 'badge_manual_award' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id')); $table->add_key('fk_recipientid', XMLDB_KEY_FOREIGN, array('recipientid'), 'user', array('id')); $table->add_key('fk_issuerid', XMLDB_KEY_FOREIGN, array('issuerid'), 'user', array('id')); $table->add_key('fk_issuerrole', XMLDB_KEY_FOREIGN, array('issuerrole'), 'role', array('id')); // Set the comment for the table 'badge_manual_award'. $table->setComment('Track manual award criteria for badges'); // Conditionally launch create table for 'badge_manual_award' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define table 'badge_backpack' to be created $table = new xmldb_table('badge_backpack'); // Adding fields to table 'badge_backpack' $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); $table->add_field('email', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'userid'); $table->add_field('backpackurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'email'); $table->add_field('backpackuid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackurl'); $table->add_field('backpackgid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackuid'); $table->add_field('autosync', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'backpackgid'); $table->add_field('password', XMLDB_TYPE_CHAR, '50', null, null, null, null, 'autosync'); // Adding keys to table 'badge_backpack' $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); // Set the comment for the table 'badge_backpack'. $table->setComment('Defines settings for connecting external backpack'); // Conditionally launch create table for 'badge_backpack' if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Create a new 'badge_external' table first. // Define table 'badge_external' to be created. $table = new xmldb_table('badge_external'); // Adding fields to table 'badge_external'. $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('backpackid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id'); $table->add_field('collectionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'backpackid'); // Adding keys to table 'badge_external'. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('fk_backpackid', XMLDB_KEY_FOREIGN, array('backpackid'), 'badge_backpack', array('id')); // Set the comment for the table 'badge_external'. $table->setComment('Setting for external badges display'); // Conditionally launch create table for 'badge_external'. if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Define field backpackgid to be dropped from 'badge_backpack'. $table = new xmldb_table('badge_backpack'); $field = new xmldb_field('backpackgid'); if ($dbman->field_exists($table, $field)) { // Perform user data migration. $usercollections = $DB->get_records('badge_backpack'); foreach ($usercollections as $usercollection) { $collection = new stdClass(); $collection->backpackid = $usercollection->id; $collection->collectionid = $usercollection->backpackgid; $DB->insert_record('badge_external', $collection); } // Launch drop field backpackgid. $dbman->drop_field($table, $field); } // Create missing badgeid foreign key on badge_manual_award. $table = new xmldb_table('badge_manual_award'); $key = new xmldb_key('fk_badgeid', XMLDB_KEY_FOREIGN, array('id'), 'badge', array('id')); $dbman->drop_key($table, $key); $key->set_attributes(XMLDB_KEY_FOREIGN, array('badgeid'), 'badge', array('id')); $dbman->add_key($table, $key); // Drop unused badge image field. $table = new xmldb_table('badge'); $field = new xmldb_field('image', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'description'); // Conditionally launch drop field eventtype. if ($dbman->field_exists($table, $field)) { $dbman->drop_field($table, $field); } // Define field completionprogressonview to be added to course. $table = new xmldb_table('course'); $field = new xmldb_field('completionprogressonview', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, 0, 'enablecompletion'); // Conditionally launch add field completionprogressonview. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } $field = new xmldb_field('audiencevisible', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, 2); // Conditionally launch add field audiencevisible to course table. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Define field invalidatecache to be added to course_completions. $table = new xmldb_table('course_completions'); $field = new xmldb_field('invalidatecache', XMLDB_TYPE_INTEGER, '1', null, null, null, '0'); // Conditionally launch add field invalidatecache. if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } // Backporting MDL-41914 to add new webservice core_user_add_user_device. $table = new xmldb_table('user_devices'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id'); $table->add_field('appid', XMLDB_TYPE_CHAR, '128', null, XMLDB_NOTNULL, null, null, 'userid'); $table->add_field('name', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'appid'); $table->add_field('model', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'name'); $table->add_field('platform', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'model'); $table->add_field('version', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null, 'platform'); $table->add_field('pushid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'version'); $table->add_field('uuid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, 'pushid'); $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'uuid'); $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'timecreated'); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_key('pushid-userid', XMLDB_KEY_UNIQUE, array('pushid', 'userid')); $table->add_key('pushid-platform', XMLDB_KEY_UNIQUE, array('pushid', 'platform')); $table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id')); if (!$dbman->table_exists($table)) { $dbman->create_table($table); } // Add timecompleted for module completion. $table = new xmldb_table('course_modules_completion'); $field = new xmldb_field('timecompleted', XMLDB_TYPE_INTEGER, '10'); if (!$dbman->field_exists($table, $field)) { $dbman->add_field($table, $field); } return true; }