Beispiel #1
0
 /**
  * Generate role export xml file.
  *
  * @param $roleid
  * @return string
  */
 public static function get_export_xml($roleid)
 {
     global $DB;
     $role = $DB->get_record('role', array('id' => $roleid), '*', MUST_EXIST);
     $dom = new DOMDocument('1.0', 'UTF-8');
     $top = $dom->createElement('role');
     $dom->appendChild($top);
     $top->appendChild($dom->createElement('shortname', $role->shortname));
     $top->appendChild($dom->createElement('name', htmlspecialchars($role->name, ENT_COMPAT | ENT_HTML401, 'UTF-8')));
     $top->appendChild($dom->createElement('description', htmlspecialchars($role->description, ENT_COMPAT | ENT_HTML401, 'UTF-8')));
     $top->appendChild($dom->createElement('archetype', $role->archetype));
     $contextlevels = $dom->createElement('contextlevels');
     $top->appendChild($contextlevels);
     foreach (get_role_contextlevels($roleid) as $level) {
         $name = context_helper::get_class_for_level($level);
         $name = preg_replace('/^context_/', '', $name);
         $contextlevels->appendChild($dom->createElement('level', $name));
     }
     foreach (array('assign', 'override', 'switch') as $type) {
         $allows = $dom->createElement('allow' . $type);
         $top->appendChild($allows);
         $records = $DB->get_records('role_allow_' . $type, array('roleid' => $roleid), "allow{$type} ASC");
         foreach ($records as $record) {
             if (!($ar = $DB->get_record('role', array('id' => $record->{'allow' . $type})))) {
                 continue;
             }
             $allows->appendChild($dom->createElement('shortname', $ar->shortname));
         }
     }
     $permissions = $dom->createElement('permissions');
     $top->appendChild($permissions);
     $capabilities = $DB->get_records_sql_menu("SELECT capability, permission\n               FROM {role_capabilities}\n              WHERE contextid = :syscontext AND roleid = :roleid\n           ORDER BY capability ASC", array('syscontext' => context_system::instance()->id, 'roleid' => $roleid));
     $allcapabilities = $DB->get_records('capabilities', array(), 'name ASC');
     foreach ($allcapabilities as $cap) {
         if (!isset($capabilities[$cap->name])) {
             $permissions->appendChild($dom->createElement('inherit', $cap->name));
         }
     }
     foreach ($capabilities as $capability => $permission) {
         if ($permission == CAP_ALLOW) {
             $permissions->appendChild($dom->createElement('allow', $capability));
         }
     }
     foreach ($capabilities as $capability => $permission) {
         if ($permission == CAP_PREVENT) {
             $permissions->appendChild($dom->createElement('prevent', $capability));
         }
     }
     foreach ($capabilities as $capability => $permission) {
         if ($permission == CAP_PROHIBIT) {
             $permissions->appendChild($dom->createElement('prohibit', $capability));
         }
     }
     return $dom->saveXML();
 }
Beispiel #2
0
 public function test_xml()
 {
     global $DB;
     $roles = $DB->get_records('role');
     foreach ($roles as $role) {
         $xml = core_role_preset::get_export_xml($role->id);
         $this->assertTrue(core_role_preset::is_valid_preset($xml));
         $info = core_role_preset::parse_preset($xml);
         $this->assertSame($role->shortname, $info['shortname']);
         $this->assertSame($role->name, $info['name']);
         $this->assertSame($role->description, $info['description']);
         $this->assertSame($role->archetype, $info['archetype']);
         $contextlevels = get_role_contextlevels($role->id);
         $this->assertEquals(array_values($contextlevels), array_values($info['contextlevels']));
         foreach (array('assign', 'override', 'switch') as $type) {
             $records = $DB->get_records('role_allow_' . $type, array('roleid' => $role->id), "allow{$type} ASC");
             $allows = array();
             foreach ($records as $record) {
                 if ($record->{'allow' . $type} == $role->id) {
                     array_unshift($allows, -1);
                 }
                 $allows[] = $record->{'allow' . $type};
             }
             $this->assertEquals($allows, $info['allow' . $type], "{$type} {$role->shortname} does not match");
         }
         $capabilities = $DB->get_records_sql("SELECT *\n                   FROM {role_capabilities}\n                  WHERE contextid = :syscontext AND roleid = :roleid\n               ORDER BY capability ASC", array('syscontext' => context_system::instance()->id, 'roleid' => $role->id));
         foreach ($capabilities as $cap) {
             $this->assertEquals($cap->permission, $info['permissions'][$cap->capability]);
             unset($info['permissions'][$cap->capability]);
         }
         // The remainders should be only inherits.
         foreach ($info['permissions'] as $capability => $permission) {
             if ($permission == CAP_INHERIT) {
                 continue;
             }
             $this->fail('only CAP_INHERIT expected');
         }
     }
 }
Beispiel #3
0
function xmldb_enrol_category_install()
{
    global $CFG, $DB;
    if (!$DB->record_exists_select('role_assignments', "contextid IN (SELECT id FROM {context} WHERE contextlevel = ?)", array(CONTEXT_COURSECAT))) {
        // fresh install or nobody used category enrol
        return;
    }
    // existing sites need a way to keep category role enrols, but we do not want to encourage that on new sites
    // extremely ugly hack, the sync depends on the capabilities so we unfortunately force update of caps here
    // note: this is not officially supported and should not be copied elsewhere! :-(
    update_capabilities('enrol_category');
    $syscontext = get_context_instance(CONTEXT_SYSTEM);
    $archetypes = array('student', 'teacher', 'editingteacher');
    $enableplugin = false;
    foreach ($archetypes as $archetype) {
        $roles = get_archetype_roles($archetype);
        foreach ($roles as $role) {
            if (!$DB->record_exists_select('role_assignments', "roleid = ? AND contextid IN (SELECT id FROM {context} WHERE contextlevel = ?)", array($role->id, CONTEXT_COURSECAT))) {
                continue;
            }
            assign_capability('enrol/category:synchronised', CAP_ALLOW, $role->id, $syscontext->id, true);
            $levels = get_role_contextlevels($role->id);
            $levels[] = CONTEXT_COURSECAT;
            set_role_contextlevels($role->id, $levels);
            $enableplugin = true;
        }
    }
    if (!$enableplugin) {
        return;
    }
    // enable this plugin
    $enabledplugins = explode(',', $CFG->enrol_plugins_enabled);
    $enabledplugins[] = 'category';
    $enabledplugins = array_unique($enabledplugins);
    set_config('enrol_plugins_enabled', implode(',', $enabledplugins));
    // brute force course resync, this may take a while
    require_once "{$CFG->dirroot}/enrol/category/locallib.php";
    enrol_category_sync_full();
}
Beispiel #4
0
 protected function load_current_permissions()
 {
     global $DB;
     if ($this->roleid) {
         if (!($this->role = $DB->get_record('role', array('id' => $this->roleid)))) {
             throw new moodle_exception('invalidroleid');
         }
         $contextlevels = get_role_contextlevels($this->roleid);
         // Put the contextlevels in the array keys, as well as the values.
         if (!empty($contextlevels)) {
             $this->contextlevels = array_combine($contextlevels, $contextlevels);
         } else {
             $this->contextlevels = array();
         }
     } else {
         $this->role = new stdClass();
         $this->role->name = '';
         $this->role->shortname = '';
         $this->role->description = '';
         $this->role->archetype = '';
         $this->contextlevels = array();
     }
     parent::load_current_permissions();
 }
Beispiel #5
0
 function get_system_roles()
 {
     global $CFG, $DB, $PAGE;
     $roles = $DB->get_records_sql("SELECT id, name, shortname\n                                         FROM {$CFG->prefix}role");
     $rolenames = role_fix_names($roles, NULL, ROLENAME_BOTH, true);
     $data = array();
     foreach ($roles as $role) {
         // Only return roles assignables in course context
         $contextlevels = get_role_contextlevels($role->id);
         if (!in_array(CONTEXT_SYSTEM, $contextlevels)) {
             continue;
         }
         $r['id'] = $role->id;
         $r['name'] = $rolenames[$role->id];
         $data[] = $r;
     }
     return $data;
 }
Beispiel #6
0
    /**
     * Creates a new role in the system.
     *
     * You can fill $record with the role 'name',
     * 'shortname', 'description' and 'archetype'.
     *
     * If an archetype is specified it's capabilities,
     * context where the role can be assigned and
     * all other properties are copied from the archetype;
     * if no archetype is specified it will create an
     * empty role.
     *
     * @param array|stdClass $record
     * @return int The new role id
     */
    public function create_role($record=null) {
        global $DB;

        $this->rolecount++;
        $i = $this->rolecount;

        $record = (array)$record;

        if (empty($record['shortname'])) {
            $record['shortname'] = 'role-' . $i;
        }

        if (empty($record['name'])) {
            $record['name'] = 'Test role ' . $i;
        }

        if (empty($record['description'])) {
            $record['description'] = 'Test role ' . $i . ' description';
        }

        if (empty($record['archetype'])) {
            $record['archetype'] = '';
        } else {
            $archetypes = get_role_archetypes();
            if (empty($archetypes[$record['archetype']])) {
                throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' .
                    'valid archetype shortname (editingteacher, student...)');
            }
        }

        // Creates the role.
        if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) {
            throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role');
        }

        // If no archetype was specified we allow it to be added to all contexts,
        // otherwise we allow it in the archetype contexts.
        if (!$record['archetype']) {
            $contextlevels = array_keys(context_helper::get_all_levels());
        } else {
            // Copying from the archetype default rol.
            $archetyperoleid = $DB->get_field(
                'role',
                'id',
                array('shortname' => $record['archetype'], 'archetype' => $record['archetype'])
            );
            $contextlevels = get_role_contextlevels($archetyperoleid);
        }
        set_role_contextlevels($newroleid, $contextlevels);

        if ($record['archetype']) {

            // We copy all the roles the archetype can assign, override and switch to.
            if ($record['archetype']) {
                $types = array('assign', 'override', 'switch');
                foreach ($types as $type) {
                    $rolestocopy = get_default_role_archetype_allows($type, $record['archetype']);
                    foreach ($rolestocopy as $tocopy) {
                        $functionname = 'allow_' . $type;
                        $functionname($newroleid, $tocopy);
                    }
                }
            }

            // Copying the archetype capabilities.
            $sourcerole = $DB->get_record('role', array('id' => $archetyperoleid));
            role_cap_duplicate($sourcerole, $newroleid);
        }

        return $newroleid;
    }
 /**
  * Call this after the table has been initialised,
  * this resets everything to that role.
  *
  * @param int $roleid role id or 0 for no role
  * @param array $options array with following keys:
  *      'name', 'shortname', 'description', 'permissions', 'archetype',
  *      'contextlevels', 'allowassign', 'allowoverride', 'allowswitch'
  */
 public function force_duplicate($roleid, array $options)
 {
     global $DB;
     if ($roleid == 0) {
         // This means reset to nothing == remove everything.
         if ($options['shortname']) {
             $this->role->shortname = '';
         }
         if ($options['name']) {
             $this->role->name = '';
         }
         if ($options['description']) {
             $this->role->description = '';
         }
         if ($options['archetype']) {
             $this->role->archetype = '';
         }
         if ($options['contextlevels']) {
             $this->contextlevels = array();
         }
         if ($options['allowassign']) {
             $this->allowassign = array();
         }
         if ($options['allowoverride']) {
             $this->allowoverride = array();
         }
         if ($options['allowswitch']) {
             $this->allowswitch = array();
         }
         if ($options['permissions']) {
             foreach ($this->capabilities as $capid => $cap) {
                 $this->permissions[$cap->name] = CAP_INHERIT;
             }
         }
         return;
     }
     $role = $DB->get_record('role', array('id' => $roleid), '*', MUST_EXIST);
     if ($options['shortname']) {
         $this->role->shortname = $role->shortname;
     }
     if ($options['name']) {
         $this->role->name = $role->name;
     }
     if ($options['description']) {
         $this->role->description = $role->description;
     }
     if ($options['archetype']) {
         $this->role->archetype = $role->archetype;
     }
     if ($options['contextlevels']) {
         $this->contextlevels = array();
         $levels = get_role_contextlevels($roleid);
         foreach ($levels as $cl) {
             $this->contextlevels[$cl] = $cl;
         }
     }
     if ($options['allowassign']) {
         $this->allowassign = array_keys($this->get_allow_roles_list('assign', $roleid));
     }
     if ($options['allowoverride']) {
         $this->allowoverride = array_keys($this->get_allow_roles_list('override', $roleid));
     }
     if ($options['allowswitch']) {
         $this->allowswitch = array_keys($this->get_allow_roles_list('switch', $roleid));
     }
     if ($options['permissions']) {
         $this->permissions = $DB->get_records_menu('role_capabilities', array('roleid' => $roleid, 'contextid' => context_system::instance()->id), '', 'capability,permission');
         foreach ($this->capabilities as $capid => $cap) {
             if (!isset($this->permissions[$cap->name])) {
                 $this->permissions[$cap->name] = CAP_INHERIT;
             }
         }
     }
 }
function xmldb_local_iomad_upgrade($oldversion)
{
    global $CFG, $DB;
    $result = true;
    $dbman = $DB->get_manager();
    if ($oldversion < 2011090600) {
        // Define table department to be created.
        $table = new xmldb_table('department');
        // Adding fields to table department.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
        $table->add_field('company', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('parent', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
        // Adding keys to table department.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table department_users to be created.
        $table = new xmldb_table('department_users');
        // Adding fields to table department_users.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('departmentid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table department_users.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department_users.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table department_courses to be created.
        $table = new xmldb_table('department_courses');
        // Adding fields to table department_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('departmentid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table department_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for department_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011090600, 'local', 'iomad');
    }
    // Licensing added.
    if ($oldversion < 2011091500) {
        // Define table companylicense to be created.
        $table = new xmldb_table('companylicense');
        // Adding fields to table companylicense.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
        $table->add_field('allocation', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('validlength', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('expirydate', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('used', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null);
        // Adding keys to table companylicense.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table companylicense_users to be created.
        $table = new xmldb_table('companylicense_users');
        // Adding fields to table companylicense_users.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('licenseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table companylicense_users.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense_users.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table companylicense_courses to be created.
        $table = new xmldb_table('companylicense_courses');
        // Adding fields to table companylicense_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('licenseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table companylicense_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companylicense_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011091500, 'local', 'iomad');
    }
    if ($oldversion < 2011092300) {
        // Define field id to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('isusing', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011092300, 'local', 'iomad');
    }
    if ($oldversion < 2011092600) {
        // Define field timecompleted to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('timecompleted', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, null, 'isusing');
        // Conditionally launch add field timecompleted.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field score to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('score', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null, 'timecompleted');
        // Conditionally launch add field score.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field result to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('result', XMLDB_TYPE_TEXT, 'small', null, null, null, null, 'score');
        // Conditionally launch add field result.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011092600, 'local', 'iomad');
    }
    if ($oldversion < 2011103000) {
        // Define table company_course_groups to be created.
        $table = new xmldb_table('company_course_groups');
        // Adding fields to table company_course_groups.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('groupid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_course_groups.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_course_groups.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table iomad_courses to be created.
        $table = new xmldb_table('iomad_courses');
        // Adding fields to table iomad_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('licensed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        $table->add_field('shared', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
        // Adding keys to table iomad_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for iomad_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011103000, 'local', 'iomad');
    }
    if ($oldversion < 2011111000) {
        // Define table iomad_courses to be created.
        $table = new xmldb_table('iomad_courses');
        // Adding fields to table iomad_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('licensed', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
        $table->add_field('shared', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, null, null, '0');
        // Adding keys to table iomad_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for iomad_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111000, 'local', 'iomad');
    }
    if ($oldversion < 2011111401) {
        // Define table classroom to be created.
        $table = new xmldb_table('classroom');
        // Adding fields to table classroom.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('name', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
        $table->add_field('address', XMLDB_TYPE_CHAR, '70', null, null, null, null);
        $table->add_field('city', XMLDB_TYPE_CHAR, '120', null, null, null, null);
        $table->add_field('country', XMLDB_TYPE_CHAR, '2', null, null, null, null);
        $table->add_field('postcode', XMLDB_TYPE_CHAR, '20', null, null, null, null);
        $table->add_field('capacity', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table classroom.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for classroom.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        upgrade_plugin_savepoint(true, 2011111401, 'local', 'iomad');
    }
    if ($oldversion < 2011111800) {
        // Define field validlength to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('validlength', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, null, null, '0', 'shared');
        // Conditionally launch add field validlength.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111800, 'local', 'iomad');
    }
    if ($oldversion < 2011111801) {
        // Define field warnexpire to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('warnexpire', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'validlength');
        // Conditionally launch add field warnexpire.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field warncompletion to be added to iomad_courses.
        $table = new xmldb_table('iomad_courses');
        $field = new xmldb_field('warncompletion', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'warnexpire');
        // Conditionally launch add field warncompletion.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011111801, 'local', 'iomad');
    }
    if ($oldversion < 2011112000) {
        // Define field category to be added to company.
        $table = new xmldb_table('company');
        $field = new xmldb_field('category', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'theme');
        // Conditionally launch add field category.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2011112000, 'local', 'iomad');
    }
    if ($oldversion < 2012012500) {
        // Define table company_course_groups to be created.
        // ADDED AGAIN DUE TO git branching timelines.
        $table = new xmldb_table('company_course_groups');
        // Adding fields to table company_course_groups.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('groupid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_course_groups.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_course_groups.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Define table company_shared_courses to be created.
        $table = new xmldb_table('company_shared_courses');
        // Adding fields to table company_shared_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_shared_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_shared_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012012500, 'local', 'iomad');
    }
    if ($oldversion < 2012051500) {
        // Change the role permissions for company and create the department manager role.
        $systemcontext = context_system::instance();
        // Create the Company Manager role.
        if (!($companymanager = $DB->get_record('role', array('shortname' => 'companymanager')))) {
            $companymanagerid = create_role('Company Manager', 'companymanager', '(Iomad) Manages individual companies - can upload users etc.');
        } else {
            $companymanagerid = $companymanager->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companymanagerid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companymanagerid;
            $level->contextlevel = CONTEXT_SYSTEM;
            $DB->insert_record('role_context_levels', $level);
        }
        // Create new Company Department Manager role.
        if (!($companydepartmentmanager = $DB->get_record('role', array('shortname' => 'companydepartmentmanager')))) {
            $companydepartmentmanagerid = create_role('Company Department Manager', 'companydepartmentmanager', '(Iomad) Manages departments within companies - can upload users etc.');
        } else {
            $companydepartmentmanagerid = $companydepartmentmanager->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companydepartmentmanagerid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companydepartmentmanagerid;
            $level->contextlevel = CONTEXT_SYSTEM;
            $DB->insert_record('role_context_levels', $level);
        }
        $companydepartmentmanagercaps = array('block/iomad_reports:view', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:allocate_licenses', 'local/iomad_dashboard:view');
        if ($DB->get_records('role_capabilities', array('roleid' => $companydepartmentmanagerid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companydepartmentmanagerid));
        }
        foreach ($companydepartmentmanagercaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $companydepartmentmanagerid, $systemcontext->id);
        }
        $companymanagercaps = array('block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:allocate_licenses', 'block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:classrooms', 'block/iomad_company_admin:classrooms_delete', 'block/iomad_company_admin:classrooms_edit', 'block/iomad_company_admin:company_edit', 'block/iomad_company_admin:company_course_unenrol', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:company_user_profiles', 'block/iomad_company_admin:createcourse', 'local/iomad_dashboard:view');
        if ($DB->get_records('role_capabilities', array('roleid' => $companymanagerid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companymanagerid));
        }
        foreach ($companymanagercaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $companymanagerid, $systemcontext->id);
        }
        //  Deal with the database.
        // Define field id to be added to companymanager.
        $table = new xmldb_table('companymanager');
        $field = new xmldb_field('departmentmanager', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field departmentmanager.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Conditionally launch add field id.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $DB->set_field('companymanager', 'departmentmanager', 0);
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012051500, 'local', 'iomad');
    }
    if ($oldversion < 2012052200) {
        // Define table company_created_courses to be created.
        $table = new xmldb_table('company_created_courses');
        // Adding fields to table company_created_courses.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_created_courses.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_created_courses.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Change the role permissions for company and create the department manager role..
        $systemcontext = context_system::instance();
        // Create the Company Course Editor.
        if (!($companycourseeditor = $DB->get_record('role', array('shortname' => 'companycourseeditor')))) {
            $companycourseeditorid = create_role('Company Course Editor', 'companycourseeditor', '(Iomad) Teacher style role for Company manager provided to them when they create their own course.');
        } else {
            $companycourseeditorid = $companycourseeditor->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companycourseeditorid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companycourseeditorid;
            $level->contextlevel = CONTEXT_COURSE;
            $DB->insert_record('role_context_levels', $level);
        }
        // Create new Company Course Non Editor role.
        if (!($companycoursenoneditor = $DB->get_record('role', array('shortname' => 'companycoursenoneditor')))) {
            $companycoursenoneditorid = create_role('Company Course Non Editor', 'companycoursenoneditor', '(Iomad) Non editing teacher style role form Company and department managers');
        } else {
            $companycoursenoneditorid = $companycoursenoneditor->id;
        }
        // If not done already, allow assignment at system context.
        $levels = get_role_contextlevels($companycoursenoneditorid);
        if (empty($levels)) {
            $level = null;
            $level->roleid = $companycoursenoneditorid;
            $level->contextlevel = CONTEXT_COURSE;
            $DB->insert_record('role_context_levels', $level);
        }
        if ($DB->get_records('role_capabilities', array('roleid' => $companycourseeditorid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companycourseeditorid));
        }
        $companycourseeditorcaps = array('block/side_bar_block:editblock', 'block/side_bar_block:viewblock', 'booktool/importhtml:import', 'booktool/print:print', 'enrol/authorize:manage', 'enrol/license:manage', 'enrol/license:unenrol', 'enrol/manual:enrol', 'enrol/manual:unenrol', 'gradereport/grader:view', 'gradereport/overview:view', 'gradereport/user:view', 'mod/assignment:exportownsubmission', 'mod/assignment:grade', 'mod/assignment:view', 'mod/book:edit', 'mod/book:read', 'mod/book:viewhiddenchapters', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:choose', 'mod/choice:deleteresponses', 'mod/choice:downloadresponses', 'mod/choice:readresponses', 'mod/courseclassroom:grade', 'mod/courseclassroom:invite', 'mod/courseclassroom:viewattendees', 'mod/forum:addnews', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteanypost', 'mod/forum:deleteownpost', 'mod/forum:editanypost', 'mod/forum:exportdiscussion', 'mod/forum:exportownpost', 'mod/forum:exportpost', 'mod/forum:managesubscriptions', 'mod/forum:movediscussions', 'mod/forum:postwithoutthrottling', 'mod/forum:rate', 'mod/forum:replynews', 'mod/forum:replypost', 'mod/forum:splitdiscussions', 'mod/forum:startdiscussion', 'mod/forum:viewallratings', 'mod/forum:viewanyrating', 'mod/forum:viewdiscussion', 'mod/forum:viewhiddentimedposts', 'mod/forum:viewqandawithoutposting', 'mod/forum:viewrating', 'mod/forum:viewsubscribers', 'mod/page:view', 'mod/resource:view', 'mod/scorm:deleteresponses', 'mod/scorm:savetrack', 'mod/scorm:viewreport', 'mod/scorm:viewscores', 'mod/url:view', 'moodle/block:edit', 'moodle/block:view', 'moodle/calendar:manageentries', 'moodle/calendar:managegroupentries', 'moodle/calendar:manageownentries', 'moodle/course:activityvisibility', 'moodle/course:changefullname', 'moodle/course:changesummary', 'moodle/course:manageactivities', 'moodle/course:managefiles', 'moodle/course:managegroups', 'moodle/course:markcomplete', 'moodle/course:reset', 'moodle/course:sectionvisibility', 'moodle/course:setcurrentsection', 'moodle/course:update', 'moodle/course:viewhiddenactivities', 'moodle/course:viewhiddensections', 'moodle/course:viewparticipants', 'moodle/grade:edit', 'moodle/grade:hide', 'moodle/grade:lock', 'moodle/grade:manage', 'moodle/grade:managegradingforms', 'moodle/grade:manageletters', 'moodle/grade:manageoutcomes', 'moodle/grade:unlock', 'moodle/grade:viewall', 'moodle/grade:viewhidden', 'moodle/notes:manage', 'moodle/notes:view', 'moodle/rating:rate', 'moodle/rating:view', 'moodle/rating:viewall', 'moodle/rating:viewany', 'moodle/role:switchroles', 'moodle/site:accessallgroups', 'moodle/site:manageblocks', 'moodle/site:trustcontent', 'moodle/site:viewfullnames', 'moodle/site:viewreports', 'moodle/site:viewuseridentity', 'moodle/user:viewdetails', 'report/courseoverview:view', 'report/log:view', 'report/log:viewtoday', 'report/loglive:view', 'report/outline:view', 'report/participation:view', 'report/progress:view');
        foreach ($companycourseeditorcaps as $rolecapability) {
            // Assign_capability will update rather than insert if capability exists.
            assign_capability($rolecapability, CAP_ALLOW, $companycourseeditorid, $systemcontext->id);
        }
        if ($DB->get_records('role_capabilities', array('roleid' => $companycoursenoneditorid))) {
            $DB->delete_records('role_capabilities', array('roleid' => $companycoursenoneditorid));
        }
        $companycoursenoneditorcaps = array('block/side_bar_block:viewblock', 'gradereport/grader:view', 'gradereport/user:view', 'mod/assignment:view', 'mod/book:read', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:readresponses', 'mod/feedback:view', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteownpost', 'mod/forum:replypost', 'mod/forum:startdiscussion', 'mod/forum:viewdiscussion', 'mod/forum:viewqandawithoutposting', 'mod/page:view', 'mod/quiz:attempt', 'mod/quiz:view', 'mod/resource:view', 'mod/survey:participate', 'moodle/block:view', 'moodle/grade:viewall', 'moodle/site:viewfullnames', 'moodle/site:viewuseridentity');
        foreach ($companycoursenoneditorcaps as $rolecapability) {
            // Assign_capability will update rather than insert if capability exists.
            assign_capability($rolecapability, CAP_ALLOW, $companycoursenoneditorid, $systemcontext->id);
        }
        // Deal with role assignments.
        // Get the list of company courses.
        $companycourses = $DB->get_records('companycourse');
        // Get the managers.
        foreach ($companycourses as $companycourse) {
            $companymanagers = $DB->get_records('companymanager', array('companyid' => $companycourse->companyid));
            if ($DB->record_exists('course', array('id' => $companycourse->courseid))) {
                if ($DB->record_exists('scorm', array('course' => $companycourse->courseid))) {
                    // This is a scorm course so only noneditor role to be applied.
                    foreach ($companymanagers as $companymanager) {
                        if ($user = $DB->get_record('user', array('id' => $companymanager->userid, 'deleted' => 0))) {
                            company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorid);
                        }
                    }
                } else {
                    // Add it to the company created courses.
                    $DB->insert_record('company_created_courses', array('companyid' => $companycourse->companyid, 'courseid' => $companycourse->courseid));
                    // Set up the manager roles.
                    foreach ($companymanagers as $companymanager) {
                        if ($user = $DB->get_record('user', array('id' => $companymanager->userid, 'deleted' => 0))) {
                            if ($companymanager->departmentmanager) {
                                // Lowly department manager, no more than that.
                                company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycoursenoneditorid);
                            } else {
                                company_user::enrol($user, array($companycourse->courseid), $companycourse->companyid, $companycourseeditorid);
                            }
                        }
                    }
                }
            }
        }
        // Restrict the default modules.
        $allowedmodules = '1,3,5,7,10,12,14,15,17,20,21,22';
        set_config('restrictbydefault', 1);
        set_config('restrictmodulesfor', 'all');
        set_config('defaultallowedmodules', $allowedmodules);
        // Restrict the modules for every course.
        $sitecourses = $DB->get_records_select('course', "id != " . SITEID);
        foreach ($sitecourses as $sitecourse) {
            foreach (explode(',', $allowedmodules) as $module) {
                $DB->insert_record('course_allowed_modules', array('course' => $sitecourse->id, 'module' => $module));
                $DB->set_field('course', 'restrictmodules', '1', array('id' => $sitecourse->id));
            }
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2012052200, 'local', 'iomad');
    }
    if ($oldversion < 2013050100) {
        // Define table companyusers to be created.
        $table = new xmldb_table('companyusers');
        // Adding fields to table companyusers.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('usserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        // Adding keys to table companyusers.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for companyusers.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Need to deal with current company allocations.
        /*if ($companyfield = $DB->get_record('user_info_field', array('shortname' => 'company'))) {
            if ($companyusers = $DB->get_records('user_info_data', array('fieldid' => $companyfield->id))) {
                foreach($companyusers as $companyuser) {
          */
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2013050100, 'local', 'iomad');
    }
    if ($oldversion < 2014012400) {
        $systemcontext = context_system::instance();
        // Get the Company Manager role.
        if ($companymanager = $DB->get_record('role', array('shortname' => 'companymanager'))) {
            $companymanagerid = $companymanager->id;
            $companymanagercaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($companymanagercaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $companymanagerid, $systemcontext->id);
            }
        }
        // Get the Company Department Manager role.
        if ($companydepartmentmanager = $DB->get_record('role', array('shortname' => 'companydepartmentmanager'))) {
            $companydepartmentmanagerid = $companydepartmentmanager->id;
            $companydepartmentmanagercaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($companydepartmentmanagercaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $companydepartmentmanagerid, $systemcontext->id);
            }
        }
        // Get the Client Administrator role.
        if ($clientadministrator = $DB->get_record('role', array('shortname' => 'clientadministrator'))) {
            $clientadministratorid = $clientadministrator->id;
            $clientadministratorcaps = array('local/iomad_dashboard:view', 'block/iomad_reports:view', 'local_report_attendance:view', 'local_report_completion:view', 'local_report_users:view', 'local_report_scorm_overview:view');
            foreach ($clientadministratorcaps as $cap) {
                assign_capability($cap, CAP_ALLOW, $clientadministratorid, $systemcontext->id);
            }
        }
    }
    if ($oldversion < 2014022600) {
        // Change the site to force user allowed themes.
        set_config('allowuserthemes', 1);
    }
    if ($oldversion < 2014052700) {
        // Define field suspended to be added to company_users.
        $table = new xmldb_table('company_users');
        $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'departmentid');
        // Conditionally launch add field suspended.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Define field suspended to be added to company.
        $table = new xmldb_table('company');
        $field = new xmldb_field('suspended', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'profileid');
        // Conditionally launch add field suspended.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014052700, 'local', 'iomad');
    }
    if ($oldversion < 2014052702) {
        // Define new table company_role_restriction
        $table = new xmldb_table('company_role_restriction');
        // Adding fields to table department.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('roleid', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
        $table->add_field('capability', XMLDB_TYPE_CHAR, '255', null, null, null, null);
        // Adding keys to table department.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        $table->add_key('company_roleidcompanyid', XMLDB_KEY_UNIQUE, array('roleid', 'companyid', 'capability'));
        // Conditionally launch create table for company_role_restriction.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
    }
    if ($oldversion < 2014120400) {
        // Define field licensecourseid to be added to companylicense_users.
        $table = new xmldb_table('companylicense_users');
        $field = new xmldb_field('licensecourseid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'userid');
        // Conditionally launch add field licensecourseid.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014120400, 'local', 'iomad');
    }
    if ($oldversion < 2014121900) {
        // Deal with licenseses which have already been allocated.
        $licenseusers = $DB->get_records('companylicense_users', array('licensecourseid' => 0));
        foreach ($licenseusers as $licenseuser) {
            if ($licenseuser->timecompleted != null) {
                continue;
            }
            // Are they using the license?
            if ($licenseuser->isusing == 1) {
                // Get the course.
                $enrolrecords = $DB->get_records_sql("SELECT e.courseid,ue.userid\n                                                    FROM {enrol} e JOIN {user_enrolments} ue\n                                                    ON e.id=ue.enrolid\n                                                    WHERE userid = :userid\n                                                    AND courseid IN\n                                                     (SELECT courseid FROM {companylicense_courses}\n                                                      WHERE licenseid = :licenseid)", array('userid' => $licenseuser->userid, 'licenseid' => $licenseuser->licenseid));
                // Do we have more than one record?
                if (count($enrolrecords > 1)) {
                    foreach ($enrolrecords as $enrolrecord) {
                        // Check if we already have a record for this course.
                        if ($DB->get_record('companylicense_users', array('userid' => $licenseuser->userid, 'licenseid' => $licenseuser->licenseid, 'licensecourseid' => $enrolrecord->courseid))) {
                            continue;
                        } else {
                            $licenseuser->licensecourseid = $enrolrecord->courseid;
                            $DB->update_record('companylicense_users', $licenseuser);
                        }
                    }
                } else {
                    list($enrolcourseid, $enroluserid) = each($enrolrecords);
                    $licenseuser->licensecourseid = $enrolcourseid;
                    $DB->update_record('companylicense_users', $licenseuser);
                }
            } else {
                // Get the courses.
                $licensecourses = $DB->get_records('companylicense_courses', array('licenseid' => $licenseuser->licenseid));
                if (count($licensecourses) == 1) {
                    // Only one course so add it.
                    $licensecourse = array_pop($licensecourses);
                    $licenseuser->licensecourseid = $licensecourse->id;
                    $DB->update_record('companylicense_users', $licenseuser);
                } else {
                    //  Dont know which course to assign so we are going to remove the record as its not being used.
                    $DB->delete_records('companylicense_users', array('id' => $licenseuser->id));
                }
            }
        }
        //  Update the used counts for each license.
        $licenses = $DB->get_records('companylicense');
        foreach ($licenses as $license) {
            $licensecount = $DB->count_records('companylicense_users', array('licenseid' => $license->id));
            $license->used = $licensecount;
            $DB->update_record('companylicense', $license);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2014121900, 'local', 'iomad');
    }
    if ($oldversion < 2015020800) {
        // Define table company_domains to be created.
        $table = new xmldb_table('company_domains');
        // Adding fields to table company_domains.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('companyid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
        $table->add_field('domain', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
        // Adding keys to table company_domains.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
        // Conditionally launch create table for company_domains.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }
        // Iomad savepoint reached.
        upgrade_plugin_savepoint(true, 2015020800, 'local', 'iomad');
    }
    return $result;
}
Beispiel #9
0
 protected function load_current_permissions()
 {
     if ($this->roleid) {
         if (!($this->role = get_record('role', 'id', $this->roleid))) {
             print_error('invalidroleid');
         }
         $this->role->legacytype = get_legacy_type($this->roleid);
         $contextlevels = get_role_contextlevels($this->roleid);
         // Put the contextlevels in the array keys, as well as the values.
         if (!empty($contextlevels)) {
             $this->contextlevels = array_combine($contextlevels, $contextlevels);
         } else {
             $this->contextlevels = array();
         }
     } else {
         $this->role = new stdClass();
         $this->role->name = '';
         $this->role->shortname = '';
         $this->role->description = '';
         $this->role->legacytype = '';
         $this->contextlevels = array();
     }
     parent::load_current_permissions();
 }
Beispiel #10
0
function backup_general_info($bf, $preferences)
{
    global $CFG, $DB;
    fwrite($bf, start_tag("INFO", 1, true));
    //The name of the backup
    fwrite($bf, full_tag("NAME", 2, false, $preferences->backup_name));
    //The moodle_version
    fwrite($bf, full_tag("MOODLE_VERSION", 2, false, $preferences->moodle_version));
    fwrite($bf, full_tag("MOODLE_RELEASE", 2, false, $preferences->moodle_release));
    //The backup_version
    fwrite($bf, full_tag("BACKUP_VERSION", 2, false, $preferences->backup_version));
    fwrite($bf, full_tag("BACKUP_RELEASE", 2, false, $preferences->backup_release));
    //The date
    fwrite($bf, full_tag("DATE", 2, false, $preferences->backup_unique_code));
    //The original site wwwroot
    fwrite($bf, full_tag("ORIGINAL_WWWROOT", 2, false, $CFG->wwwroot));
    //The original site identifier. MD5 hashed for security.
    fwrite($bf, full_tag("ORIGINAL_SITE_IDENTIFIER_HASH", 2, false, md5(get_site_identifier())));
    //Indicate if it includes external MNET users
    $sql = "SELECT b.old_id\n                  FROM {backup_ids} b\n                  JOIN {user} u ON b.old_id=u.id\n                 WHERE b.backup_code = ?\n                       AND b.table_name = 'user' AND u.mnethostid != ?";
    if ($DB->record_exists_sql($sql, array($preferences->backup_unique_code, $CFG->mnet_localhost_id))) {
        fwrite($bf, full_tag("MNET_REMOTEUSERS", 2, false, 'true'));
    }
    //Te includes tag
    fwrite($bf, start_tag("DETAILS", 2, true));
    //Now, go to mod element of preferences to print its status
    foreach ($preferences->mods as $element) {
        //Calculate info
        $included = "false";
        $userinfo = "false";
        if ($element->backup) {
            $included = "true";
            if ($element->userinfo) {
                $userinfo = "true";
            }
        }
        //Prints the mod start
        fwrite($bf, start_tag("MOD", 3, true));
        fwrite($bf, full_tag("NAME", 4, false, $element->name));
        fwrite($bf, full_tag("INCLUDED", 4, false, $included));
        fwrite($bf, full_tag("USERINFO", 4, false, $userinfo));
        if (isset($preferences->mods[$element->name]->instances) && is_array($preferences->mods[$element->name]->instances) && count($preferences->mods[$element->name]->instances)) {
            fwrite($bf, start_tag("INSTANCES", 4, true));
            foreach ($preferences->mods[$element->name]->instances as $id => $object) {
                if (!empty($object->backup)) {
                    //Calculate info
                    $included = "false";
                    $userinfo = "false";
                    if ($object->backup) {
                        $included = "true";
                        if ($object->userinfo) {
                            $userinfo = "true";
                        }
                    }
                    fwrite($bf, start_tag("INSTANCE", 5, true));
                    fwrite($bf, full_tag("ID", 5, false, $id));
                    fwrite($bf, full_tag("NAME", 5, false, $object->name));
                    fwrite($bf, full_tag("INCLUDED", 5, false, $included));
                    fwrite($bf, full_tag("USERINFO", 5, false, $userinfo));
                    fwrite($bf, end_tag("INSTANCE", 5, true));
                }
            }
            fwrite($bf, end_tag("INSTANCES", 4, true));
        }
        //Print the end
        fwrite($bf, end_tag("MOD", 3, true));
    }
    //The metacourse in backup
    if ($preferences->backup_metacourse == 1) {
        fwrite($bf, full_tag("METACOURSE", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("METACOURSE", 3, false, "false"));
    }
    //The user in backup
    if ($preferences->backup_users == 1) {
        fwrite($bf, full_tag("USERS", 3, false, "course"));
    } else {
        if ($preferences->backup_users == 0) {
            fwrite($bf, full_tag("USERS", 3, false, "all"));
        } else {
            fwrite($bf, full_tag("USERS", 3, false, "none"));
        }
    }
    //The logs in backup
    if ($preferences->backup_logs == 1) {
        fwrite($bf, full_tag("LOGS", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("LOGS", 3, false, "false"));
    }
    //The user files
    if ($preferences->backup_user_files == 1) {
        fwrite($bf, full_tag("USERFILES", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("USERFILES", 3, false, "false"));
    }
    //The course files
    if ($preferences->backup_course_files == 1) {
        fwrite($bf, full_tag("COURSEFILES", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("COURSEFILES", 3, false, "false"));
    }
    //The site files
    if ($preferences->backup_site_files == 1) {
        fwrite($bf, full_tag("SITEFILES", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("SITEFILES", 3, false, "false"));
    }
    //The gradebook histories
    if (empty($CFG->disablegradehistory) && $preferences->backup_gradebook_history == 1) {
        fwrite($bf, full_tag("GRADEBOOKHISTORIES", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("GRADEBOOKHISTORIES", 3, false, "false"));
    }
    //The messages in backup
    if ($preferences->backup_messages == 1 && $preferences->backup_course == SITEID) {
        fwrite($bf, full_tag("MESSAGES", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("MESSAGES", 3, false, "false"));
    }
    //The blogs in backup
    if ($preferences->backup_blogs == 1 && $preferences->backup_course == SITEID) {
        fwrite($bf, full_tag("BLOGS", 3, false, "true"));
    } else {
        fwrite($bf, full_tag("BLOGS", 3, false, "false"));
    }
    //The mode of writing the block data
    fwrite($bf, full_tag('BLOCKFORMAT', 3, false, 'instances'));
    fwrite($bf, end_tag("DETAILS", 2, true));
    $status = fwrite($bf, end_tag("INFO", 1, true));
    ///Roles stuff goes in here
    fwrite($bf, start_tag('ROLES', 1, true));
    $roles = backup_fetch_roles($preferences);
    $sitecontext = get_context_instance(CONTEXT_SYSTEM);
    $coursecontext = get_context_instance(CONTEXT_COURSE, $preferences->backup_course);
    foreach ($roles as $role) {
        fwrite($bf, start_tag('ROLE', 2, true));
        fwrite($bf, full_tag('ID', 3, false, $role->id));
        fwrite($bf, full_tag('NAME', 3, false, $role->name));
        fwrite($bf, full_tag('SHORTNAME', 3, false, $role->shortname));
        /// Calculate $role name in course
        $nameincourse = role_get_name($role, $coursecontext);
        if ($nameincourse != $role->name) {
            fwrite($bf, full_tag('NAMEINCOURSE', 3, false, $nameincourse));
        }
        /// List of context level where this role can be assigned.
        fwrite($bf, start_tag('CONTEXTLEVELS', 3, true));
        $contextlevels = get_role_contextlevels($role->id);
        foreach ($contextlevels as $cl) {
            fwrite($bf, full_tag('CONTEXTLEVEL', 4, false, $cl));
        }
        fwrite($bf, end_tag('CONTEXTLEVELS', 3, true));
        /// find and write all default capabilities
        fwrite($bf, start_tag('CAPABILITIES', 3, true));
        // pull out all default (site context) capabilities
        if ($capabilities = role_context_capabilities($role->id, $sitecontext)) {
            foreach ($capabilities as $capability => $value) {
                fwrite($bf, start_tag('CAPABILITY', 4, true));
                fwrite($bf, full_tag('NAME', 5, false, $capability));
                fwrite($bf, full_tag('PERMISSION', 5, false, $value));
                // use this to pull out the other info (timemodified and modifierid)
                $cap = $DB->get_record_sql("SELECT *\n                                                  FROM {role_capabilities}\n                                                 WHERE capability = ?\n                                                       AND contextid = ?\n                                                       AND roleid = ?", array($capability, $sitecontext->id, $role->id));
                fwrite($bf, full_tag("TIMEMODIFIED", 5, false, $cap->timemodified));
                fwrite($bf, full_tag("MODIFIERID", 5, false, $cap->modifierid));
                fwrite($bf, end_tag('CAPABILITY', 4, true));
            }
        }
        fwrite($bf, end_tag('CAPABILITIES', 3, true));
        fwrite($bf, end_tag('ROLE', 2, true));
    }
    fwrite($bf, end_tag('ROLES', 1, true));
    return $status;
}
Beispiel #11
0
 function get_roles()
 {
     global $CFG, $DB, $PAGE;
     $roles = $DB->get_records_sql("SELECT id, name, shortname\n                                         FROM {$CFG->prefix}role");
     $data = array();
     foreach ($roles as $role) {
         // Only return roles assignables in course context
         $contextlevels = get_role_contextlevels($role->id);
         if (!in_array(CONTEXT_COURSE, $contextlevels)) {
             continue;
         }
         $r['id'] = $role->id;
         $r['name'] = $role->name;
         $r['name'] = get_string($role->shortname);
         $data[] = $r;
     }
     return $data;
 }
Beispiel #12
0
function xmldb_local_iomad_install()
{
    global $CFG, $DB;
    $systemcontext = context_system::instance();
    // Create new Company Manager role.
    if (!($companymanager = $DB->get_record('role', array('shortname' => 'companymanager')))) {
        $companymanagerid = create_role('Company Manager', 'companymanager', '(Iomad) Manages individual companies - can upload users etc.');
    } else {
        $companymanagerid = $companymanager->id;
    }
    // If not done already, allow assignment at system context.
    $levels = get_role_contextlevels($companymanagerid);
    if (empty($levels)) {
        $level = new stdClass();
        $level->roleid = $companymanagerid;
        $level->contextlevel = CONTEXT_SYSTEM;
        $DB->insert_record('role_context_levels', $level);
    }
    // Create new Company Department Manager role.
    if (!($companydepartmentmanager = $DB->get_record('role', array('shortname' => 'companydepartmentmanager')))) {
        $companydepartmentmanagerid = create_role('Company Department Manager', 'companydepartmentmanager', 'Iomad Manages departments within companies - can upload users etc.');
    } else {
        $companydepartmentmanagerid = $companydepartmentmanager->id;
    }
    // If not done already, allow assignment at system context.
    $levels = get_role_contextlevels($companydepartmentmanagerid);
    if (empty($levels)) {
        $level = new stdclass();
        $level->roleid = $companydepartmentmanagerid;
        $level->contextlevel = CONTEXT_SYSTEM;
        $DB->insert_record('role_context_levels', $level);
    }
    // Create new Client Administrator role.
    if (!($clientadministrator = $DB->get_record('role', array('shortname' => 'clientadministrator')))) {
        $clientadministratorid = create_role('Client Administrator', 'clientadministrator', '(Iomad) Manages site - can create new companies and add managers etc.');
    } else {
        $clientadministratorid = $clientadministrator->id;
    }
    // If not done already, allow assignment at system context.
    $levels = get_role_contextlevels($clientadministratorid);
    if (empty($levels)) {
        $level = new stdclass();
        $level->roleid = $clientadministratorid;
        $level->contextlevel = CONTEXT_SYSTEM;
        $DB->insert_record('role_context_levels', $level);
    }
    // Create new Client Course Editor role.
    if (!($companycourseeditor = $DB->get_record('role', array('shortname' => 'companycourseeditor')))) {
        $companycourseeditorid = create_role('Client Course Editor', 'companycourseeditor', 'Iomad Client Course Editor - can edit course content; add, delete, modify etc..');
    } else {
        $companycourseeditorid = $companycourseeditor->id;
    }
    // If not done already, allow assignment at system context.
    $levels = get_role_contextlevels($companycourseeditorid);
    if (empty($levels)) {
        $level = new stdclass();
        $level->roleid = $companycourseeditorid;
        $level->contextlevel = CONTEXT_SYSTEM;
        $DB->insert_record('role_context_levels', $level);
    }
    // Create new Client Course Access role.
    if (!($companycoursenoneditor = $DB->get_record('role', array('shortname' => 'companycoursenoneditor')))) {
        $companycoursenoneditorid = create_role('Client Course Access', 'companycoursenoneditor', 'Iomad Client Course Access - similar to the non-editing teacher role for client admin');
    } else {
        $companycoursenoneditorid = $companycoursenoneditor->id;
    }
    // If not done already, allow assignment at system context.
    $levels = get_role_contextlevels($clientadministratorid);
    if (empty($levels)) {
        $level = null;
        $level->roleid = $clientadministratorid;
        $level->contextlevel = CONTEXT_SYSTEM;
        $DB->insert_record('role_context_levels', $level);
    }
    // Add capabilities to above.
    $clientadministratorcaps = array('local/iomad_dashboard:view', 'block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:company_add', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:company_delete', 'block/iomad_company_admin:company_edit', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:company_user', 'block/iomad_company_admin:createcourse', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:view', 'block/iomad_company_admin:restrict_capabilities', 'block/iomad_link:view', 'block/iomad_online_users:viewlist', 'block/iomad_reports:view', 'local/report_attendance:view', 'local/report_completion:view', 'local/report_users:view', 'local/report_scorm_overview:view');
    foreach ($clientadministratorcaps as $cap) {
        assign_capability($cap, CAP_ALLOW, $clientadministratorid, $systemcontext->id);
    }
    $companydepartmentmanagercaps = array('block/iomad_report:view', 'local/iomad_dashboard:view', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:allocate_licenses', 'block/iomad_reports:view', 'local/report_attendance:view', 'local/report_completion:view', 'local/report_users:view', 'local/report_scorm_overview:view');
    foreach ($companydepartmentmanagercaps as $cap) {
        assign_capability($cap, CAP_ALLOW, $companydepartmentmanagerid, $systemcontext->id);
    }
    $companymanagercaps = array('local/iomad_dashboard:view', 'block/iomad_online_users:viewlist', 'block/iomad_link:view', 'block/iomad_company_admin:view_licenses', 'block/iomad_company_admin:view', 'block/iomad_company_admin:user_upload', 'block/iomad_company_admin:user_create', 'block/iomad_company_admin:editusers', 'block/iomad_company_admin:edit_departments', 'block/iomad_company_admin:company_view', 'block/iomad_company_admin:company_course_users', 'block/iomad_company_admin:assign_department_manager', 'block/iomad_company_admin:allocate_licenses', 'block/iomad_company_admin:assign_company_manager', 'block/iomad_company_admin:classrooms', 'block/iomad_company_admin:classrooms_add', 'block/iomad_company_admin:classrooms_delete', 'block/iomad_company_admin:classrooms_edit', 'block/iomad_company_admin:company_edit', 'block/iomad_company_admin:company_course_unenrol', 'block/iomad_company_admin:company_manager', 'block/iomad_company_admin:company_user_profiles', 'block/iomad_company_admin:createcourse', 'block/iomad_reports:view', 'local/report_attendance:view', 'local/report_completion:view', 'local/report_users:view', 'local/report_scorm_overview:view');
    $companycoursenoneditorcaps = array('block/side_bar_block:viewblock', 'gradereport/grader:view', 'gradereport/user:view', 'mod/assignment:view', 'mod/book:read', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:readresponses', 'mod/feedback:view', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteownpost', 'mod/forum:replypost', 'mod/forum:startdiscussion', 'mod/forum:viewdiscussion', 'mod/forum:viewqandawithoutposting', 'mod/page:view', 'mod/quiz:attempt', 'mod/quiz:view', 'mod/resource:view', 'mod/survey:participate', 'mod/trainingevent:add`', 'mod/trainingevent:grade', 'mod/trainingevent:viewattendees', 'moodle/block:view', 'moodle/grade:viewall', 'moodle/site:viewfullnames', 'moodle/site:viewuseridentity');
    $companycourseeditorcaps = array('block/side_bar_block:editblock', 'block/side_bar_block:viewblock', 'booktool/importhtml:import', 'booktool/print:print', 'enrol/authorize:manage', 'enrol/license:manage', 'enrol/license:unenrol', 'enrol/manual:enrol', 'enrol/manual:unenrol', 'gradereport/grader:view', 'gradereport/overview:view', 'gradereport/user:view', 'mod/assignment:exportownsubmission', 'mod/assignment:grade', 'mod/assignment:view', 'mod/book:edit', 'mod/book:read', 'mod/book:viewhiddenchapters', 'mod/certificate:manage', 'mod/certificate:view', 'mod/choice:choose', 'mod/choice:deleteresponses', 'mod/choice:downloadresponses', 'mod/choice:readresponses', 'mod/trainingevent:add`', 'mod/trainingevent:grade', 'mod/trainingevent:invite', 'mod/trainingevent:viewattendees', 'mod/forum:addnews', 'mod/forum:addquestion', 'mod/forum:createattachment', 'mod/forum:deleteanypost', 'mod/forum:deleteownpost', 'mod/forum:editanypost', 'mod/forum:exportdiscussion', 'mod/forum:exportownpost', 'mod/forum:exportpost', 'mod/forum:managesubscriptions', 'mod/forum:movediscussions', 'mod/forum:postwithoutthrottling', 'mod/forum:rate', 'mod/forum:replynews', 'mod/forum:replypost', 'mod/forum:splitdiscussions', 'mod/forum:startdiscussion', 'mod/forum:viewallratings', 'mod/forum:viewanyrating', 'mod/forum:viewdiscussion', 'mod/forum:viewhiddentimedposts', 'mod/forum:viewqandawithoutposting', 'mod/forum:viewrating', 'mod/forum:viewsubscribers', 'mod/page:view', 'mod/resource:view', 'mod/scorm:deleteresponses', 'mod/scorm:savetrack', 'mod/scorm:viewreport', 'mod/scorm:viewscores', 'mod/url:view', 'moodle/block:edit', 'moodle/block:view', 'moodle/calendar:manageentries', 'moodle/calendar:managegroupentries', 'moodle/calendar:manageownentries', 'moodle/course:activityvisibility', 'moodle/course:changefullname', 'moodle/course:changesummary', 'moodle/course:manageactivities', 'moodle/course:managefiles', 'moodle/course:managegroups', 'moodle/course:markcomplete', 'moodle/course:reset', 'moodle/course:sectionvisibility', 'moodle/course:setcurrentsection', 'moodle/course:update', 'moodle/course:viewhiddenactivities', 'moodle/course:viewhiddensections', 'moodle/course:viewparticipants', 'moodle/grade:edit', 'moodle/grade:hide', 'moodle/grade:lock', 'moodle/grade:manage', 'moodle/grade:managegradingforms', 'moodle/grade:manageletters', 'moodle/grade:manageoutcomes', 'moodle/grade:unlock', 'moodle/grade:viewall', 'moodle/grade:viewhidden', 'moodle/notes:manage', 'moodle/notes:view', 'moodle/rating:rate', 'moodle/rating:view', 'moodle/rating:viewall', 'moodle/rating:viewany', 'moodle/role:switchroles', 'moodle/site:accessallgroups', 'moodle/site:manageblocks', 'moodle/site:trustcontent', 'moodle/site:viewfullnames', 'moodle/site:viewreports', 'moodle/site:viewuseridentity', 'moodle/user:viewdetails', 'report/courseoverview:view', 'report/log:view', 'report/log:viewtoday', 'report/loglive:view', 'report/outline:view', 'report/participation:view', 'report/progress:view');
    foreach ($companymanagercaps as $cap) {
        assign_capability($cap, CAP_ALLOW, $companymanagerid, $systemcontext->id);
    }
    foreach ($companycourseeditorcaps as $rolecapability) {
        // Assign_capability will update rather than insert if capability exists.
        assign_capability($rolecapability, CAP_ALLOW, $companycourseeditorid, $systemcontext->id);
    }
    foreach ($companycoursenoneditorcaps as $rolecapability) {
        // Assign_capability will update rather than insert if capability exists.
        assign_capability($rolecapability, CAP_ALLOW, $companycoursenoneditorid, $systemcontext->id);
    }
    // Create custom user field for company (if not already existing).
    if (!$DB->get_record('user_info_field', array('shortname' => 'company'))) {
        $field = new stdClass();
        $field->shortname = 'company';
        $field->name = 'Company';
        $field->datatype = 'text';
        $field->description = 'User Company';
        $field->descriptionformat = 1;
        $field->categoryid = 0;
        $field->required = 0;
        $field->locked = 1;
        $field->visible = 0;
        $field->param1 = 30;
        $field->param2 = 2048;
        $DB->insert_record('user_info_field', $field);
    }
    // Even worse - change the theme.
    $theme = theme_config::load('iomad');
    set_config('theme', $theme->name);
    set_config('allowuserthemes', 1);
    // Enable completion tracking.
    set_config('enablecompletion', 1);
    // Set the default blocks in courses.
    $defblocks = ':iomad_link,iomad_company_selector,iomad_online_users,completionstatus';
    set_config('defaultblocks_topics', $defblocks);
    set_config('defaultblocks_weeks', $defblocks);
}