Exemplo n.º 1
0
function xmldb_local_upgrade($oldversion)
{
    global $CFG, $db;
    $result = true;
    $reassigncaps = false;
    $resetcustomroles = false;
    $resetstickyblocks = false;
    // learning path approval status changes
    if ($result && $oldversion < 2008091803) {
        // fields added to the course table
        $table = new XMLDBTable('course');
        $field = new XMLDBField('approval_status_id');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, null, null);
        //function setAttributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) {
        if (!field_exists($table, $field)) {
            /// Launch add field shortname
            $result = $result && add_field($table, $field);
        }
    }
    if ($oldversion < 2008092100) {
        // create the roles we need
        $roles = array('superadmin' => array('name' => 'Super Admin', 'description' => 'The highest level site administrator', 'legacy' => 'admin'), 'headteacher' => array('name' => 'Head Teacher', 'description' => 'Oversees the professional development of the teachers at his/her school and other schools that s/he has been assigned to cover.'), 'headeditor' => array('name' => 'Head Editor', 'description' => 'Approves Learning Plans'), 'seniorteacher' => array('name' => 'Senior Teacher', 'description' => 'Has already achieved Participating Teacher and Master Teacher certification and is now pursuing Senior Teacher certification.'), 'masterteacher' => array('name' => 'Master Teacher', 'description' => 'Has already achieved Participating Teacher certification and is now pursuing Master Teacher certification. Is assigned to an Senior Teacher.'), 'participatingteacher' => array('name' => 'Participating Teacher', 'description' => 'Pursuing Participating Teacher certification. Is assigned to an Master Teacher.'), 'translator' => array('name' => 'Translator', 'description' => 'Localizes User Interface'));
        foreach ($roles as $shortname => $roledata) {
            if (!array_key_exists('legacy', $roledata)) {
                $roledata['legacy'] = '';
            }
            $roles[$shortname]['id'] = create_role($roledata['name'], $shortname, $roledata['description'], $roledata['legacy']);
        }
        // boostrap superadmin to the same as admin
        $admin = get_record('role', 'shortname', 'admin');
        role_cap_duplicate($admin, $roles['superadmin']['id']);
    }
    // try to start over!!!
    if ($result && $oldversion < 2008092400) {
        $table = new XMLDBTable('course_status_history');
        if (table_exists($table)) {
            drop_table($table);
        }
        // fields added to the course table
        $table = new XMLDBTable('course');
        $field = new XMLDBField('approval_status_id');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, null, null, null, null, null, null, null);
        /// Launch add field approval_status_id
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
        /// Define table mdl_course_approval_status_history to be created
        $table = new XMLDBTable('course_status_history');
        /// Adding fields to table mdl_course_approval_status
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('timestamp', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('approval_status_id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('reason', XMLDB_TYPE_TEXT, '1000', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2008092401) {
        $roles = get_records_select('role', "shortname IN ('headteacher', 'headeditor', 'seniorteacher', 'masterteacher', 'participatingteacher', 'translator')");
        set_config('messageenabledroles', implode(',', array_keys($roles)));
    }
    if ($oldversion < 2008092500) {
        // add course classification stuff
        $table = new XMLDBTable('classification_type');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_CHAR, 100, null, XMLDB_NOTNULL);
        $table->addFieldInfo('type', XMLDB_TYPE_CHAR, 100, null, XMLDB_NOTNULL, false, true, array('filter', 'topcategory', 'secondcategory'), 'filter');
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('classification_value');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('type', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('value', XMLDB_TYPE_CHAR, 100, null, XMLDB_NOTNULL);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
        $table = new XMLDBTable('course_classification');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('course', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('value', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2008092501) {
        // bootstrap out classification system based on the TAO website.
        $types = array(array('name' => 'Teaching strategies', 'type' => 'filter', 'values' => array('Connected Model', 'Constructivist Model', 'Integrated Model', 'Nested Model', 'Networked/Extended Model', 'Sequenced Model', 'Shared Model')), array('name' => 'Teaching methods', 'type' => 'filter', 'values' => array('Action-Oriented Learning', 'Active video work', 'Ball Bearings', 'Case Study', 'Creative writing', 'Discovery of learning', 'Excursion', 'Experiment', 'Free work', 'Group Puzzle', 'Learning Circle', 'Learning through teaching', 'Letter method', 'Mind mapping', 'Portfolio', 'Project Work', 'SOL method', 'Station Work', 'Traffic Lights', 'Using Tools and Resources', 'Web quest', 'Weekly Plan', 'Workshop', 'Other')), array('name' => 'Learning styles', 'type' => 'filter', 'values' => array('Visual/spatial', 'Verbal/linguistic', 'Logical/mathematical', 'Musical/rhythmic', 'Bodily/kinaesthetic', 'Interpersonal/social', 'Intrapersonal/introspective', 'Communication', 'Information', 'Simulation', 'Presentation', 'Production', 'Visualisation')), array('name' => 'Key Stages', 'type' => 'topcategory', 'values' => array('1 and 2', '3 and 4')), array('name' => 'Subject', 'type' => 'secondcategory', 'values' => array('English', 'Mathematics', 'Science', 'Design and Technology', 'ICT', 'History', 'Geography', 'Art and Design', 'Music', 'Physical Education')));
        foreach ($types as $t) {
            $values = $t['values'];
            $newid = insert_record('classification_type', (object) $t);
            foreach ($values as $v) {
                insert_record('classification_value', (object) array('type' => $newid, 'value' => $v));
            }
        }
    }
    if ($result && $oldversion < 2008100703) {
        // change course status values - seemingly have to drop and recreate the table to reset the serial with xmldb
        $table = new XMLDBTable('course_approval_status');
        if (table_exists($table)) {
            drop_table($table);
        }
        /// Define table mdl_course_approval_status to be created
        $table = new XMLDBTable('course_approval_status');
        /// Adding fields to table mdl_course_approval_status
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('shortname', XMLDB_TYPE_TEXT, 15, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('displayname', XMLDB_TYPE_TEXT, '50', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('description', XMLDB_TYPE_TEXT, '100', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        /// Adding keys to table mdl_course_approval_status
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        /// Launch create table for mdl_course_approval_status
        $result = $result && create_table($table);
        // insert status records
        $record = new object();
        $record->id = 1;
        $record->shortname = 'notsubmitted';
        $record->displayname = 'Not Submitted';
        $record->description = 'Not submitted';
        insert_record('course_approval_status', $record);
        $record->id = 2;
        $record->shortname = 'submitted';
        $record->displayname = 'Submitted';
        $record->description = 'Submitted for approval';
        insert_record('course_approval_status', $record);
        $record->id = 3;
        $record->shortname = 'needschange';
        $record->displayname = 'Needs Changes';
        $record->description = 'Reviewed and needs changes';
        insert_record('course_approval_status', $record);
        $record->id = 4;
        $record->shortname = 'resubmitted';
        $record->displayname = 'Resubmitted';
        $record->description = 'Resubmitted for approval';
        insert_record('course_approval_status', $record);
        $record->id = 5;
        $record->shortname = 'approved';
        $record->displayname = 'Approved';
        $record->description = 'Reviewed and approved';
        insert_record('course_approval_status', $record);
        $record->id = 6;
        $record->shortname = 'published';
        $record->displayname = 'Published';
        $record->description = 'Published';
        insert_record('course_approval_status', $record);
        $record->id = 7;
        $record->shortname = 'suspendeddate';
        $record->displayname = 'Suspended - Out-Dated';
        $record->description = 'Suspended - out-dated';
        insert_record('course_approval_status', $record);
        $record->id = 8;
        $record->shortname = 'suspendedauthor';
        $record->displayname = 'Suspended - By Author ';
        $record->description = 'Suspended - by author request';
        insert_record('course_approval_status', $record);
    }
    if ($oldversion < 2008101300) {
        $table = new XMLDBTable('header_image');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('image', XMLDB_TYPE_TEXT, 'medium', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('url', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('description', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('sortorder', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2008101400) {
        set_config('defaultcustomheader', 'welcome.jpg');
    }
    // Upgrade of Learning Path Author roles
    if ($oldversion < 2008103000) {
        // add new roles
        //TODO: hardcoded Lang strings need to be put into lang!
        $roles = array('lpcreator' => array('name' => 'LP Creator', 'description' => 'Learning Path Creators can create new learning paths.', 'legacy' => 'coursecreator'), 'lpauthor' => array('name' => 'Learning Path Author', 'description' => 'Develops new Learning Plans.'), 'lpeditor' => array('name' => 'Learning Path Editor', 'description' => 'Allowed editing rights on a learning path'));
        foreach ($roles as $shortname => $roledata) {
            if (!array_key_exists('legacy', $roledata)) {
                $roledata['legacy'] = '';
            }
            $roles[$shortname]['id'] = create_role($roledata['name'], $shortname, $roledata['description'], $roledata['legacy']);
        }
        // set default capabilities to each role
        $syscontext = get_context_instance(CONTEXT_SYSTEM);
        $lpc = get_record('role', 'shortname', 'lpcreator');
        $lpa = get_record('role', 'shortname', 'lpauthor');
        $lpe = get_record('role', 'shortname', 'lpeditor');
        // creator
        $allowedcaps = array('moodle/local:islpcreator', 'moodle/course:create');
        foreach ($allowedcaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $lpc->id, $syscontext->id, true);
        }
        // editor
        $allowedcaps = array('moodle/local:islpeditor', 'moodle/local:classifylearningpath', 'moodle/role:assign', 'format/page:addpages', 'format/page:editpages', 'format/page:managepages', 'format/page:viewpagesettings', 'moodle/site:manageblocks', 'moodle/course:manageactivities');
        foreach ($allowedcaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $lpe->id, $syscontext->id, true);
        }
        // author
        $allowedcaps = array('moodle/local:islpauthor');
        foreach ($allowedcaps as $cap) {
            assign_capability($cap, CAP_ALLOW, $lpa->id, $syscontext->id, true);
        }
    }
    // assign new capability
    if ($oldversion < 2008110100) {
        $syscontext = get_context_instance(CONTEXT_SYSTEM);
        $role = get_record('role', 'shortname', 'lpauthor');
        assign_capability('moodle/local:viewunpublishedlearningpath', CAP_ALLOW, $role->id, $syscontext->id, true);
        $role = get_record('role', 'shortname', 'headeditor');
        assign_capability('moodle/local:viewunpublishedlearningpath', CAP_ALLOW, $role->id, $syscontext->id, true);
    }
    if ($oldversion < 2008110300) {
        set_config('lpautomatedcategorisation', 1);
    }
    if ($oldversion < 2008111300) {
        create_role('Certified Participating Teacher', 'certifiedpt', 'Role for a Participating Teacher once they have become certified', '');
    }
    if ($oldversion < 2008111800) {
        // make sure ST doesn't get can message own alumni now that we've split 9 & 10
        delete_records('role_capabilities', 'capability', 'moodle/local:canmessageownalumni', 'roleid', get_field('role', 'id', 'shortname', ROLE_ST));
    }
    if ($oldversion < 2008112100 && get_site()) {
        $b = (object) array('blockid' => get_field('block', 'id', 'name', 'tao_nav'), 'pageid' => SITEID, 'pagetype' => 'course-view', 'position' => 'l', 'weight' => 1, 'visible' => 1, 'configdata' => '');
        insert_record('block_instance', $b);
    }
    if ($oldversion < 2008112101) {
        insert_record('header_image', (object) array('image' => 'header_login.jpg', 'url' => '/login/index.php', 'sortorder' => 1));
    }
    if ($oldversion < 2008112102) {
        //first delete old custom user profile fields
        if ($custid = get_field('user_info_category', 'id', 'name', 'certification')) {
            delete_records('user_info_field', 'categoryid', $custid);
            delete_records('user_info_category', 'name', 'certification');
        }
    }
    if ($oldversion < 2008113000) {
        $table = new XMLDBTable('format_page_user_view');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('format_page_id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2008120801) {
        // field added to the course_modules table
        $table = new XMLDBTable('course_modules');
        $field = new XMLDBField('trackprogress');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, 0, null);
        /// Launch add field shortname
        $result = $result && add_field($table, $field);
    }
    if ($oldversion < 2009022103) {
        // new group invites table
        $table = new XMLDBTable('group_invites');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('fromuserid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('groupid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('timemodified', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009022104) {
        $table = new XMLDBTable('groups');
        $field = new XMLDBField('groupleader');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        $result = $result && add_field($table, $field);
    }
    if ($oldversion < 2009023800) {
        $table = new XMLDBTable('user_friend');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('friendid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($oldversion < 2009031105) {
        $table = new XMLDBTable('certification_status');
        if (table_exists($table)) {
            drop_table($table);
        }
    }
    if ($oldversion < 2009031107) {
        $headerimages = array();
        $headerimages[1]->image = 'header_login.jpg';
        $headerimages[1]->url = '/login/logout.php';
        $headerimages[1]->description = 'logout page';
        $headerimages[1]->sortorder = 1;
        $headerimages[2]->image = 'mycollaboration.jpg';
        $headerimages[2]->url = '/local/my/collaboration.php';
        $headerimages[2]->description = 'My Collaboration';
        $headerimages[2]->sortorder = 1;
        $headerimages[3]->image = 'mylearning.jpg';
        $headerimages[3]->url = '/local/lp/list.php';
        $headerimages[3]->description = 'Learning Paths';
        $headerimages[3]->sortorder = 1;
        $headerimages[4]->image = 'mylearning.jpg';
        $headerimages[4]->url = '/local/my/learning.php';
        $headerimages[4]->description = 'My Learning';
        $headerimages[4]->sortorder = 1;
        $headerimages[5]->image = 'mywork.jpg';
        $headerimages[5]->url = '/local/my/work.php';
        $headerimages[5]->description = 'My Work';
        $headerimages[5]->sortorder = 1;
        $headerimages[6]->image = 'header_login.jpg';
        $headerimages[6]->url = '/index.php';
        $headerimages[6]->description = 'homepage';
        $headerimages[6]->sortorder = 1;
        $headerimages[7]->image = 'header_login.jpg';
        $headerimages[7]->url = '/';
        $headerimages[7]->description = 'homepage';
        $headerimages[7]->sortorder = 1;
        foreach ($headerimages as $himg) {
            //first check to make sure this record doesn't already exist
            if (!record_exists('header_image', 'image', $himg->image, 'url', $himg->url)) {
                insert_record('header_image', $himg);
            }
        }
    }
    if ($oldversion < 2009031108) {
        set_config('defaultblocks_topics', 'tao_nav:course_list');
        set_config('defaultblocks_weeks', 'tao_nav:course_list');
        set_config('defaultblocks_social', 'tao_nav:course_list');
    }
    if ($oldversion < 2009031109) {
        //add courseid to header_image table
        $table = new XMLDBTable('header_image');
        $field = new XMLDBField('courseid');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
    }
    if ($oldversion < 2009031110) {
        //modify url to allow nulls
        $table = new XMLDBTable('header_image');
        $field = new XMLDBField('url');
        $field->setAttributes(XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, null, null, null, null, null);
        $result = $result && change_field_notnull($table, $field);
    }
    if ($oldversion < 2009052201) {
        // add 'custom' flag to roles table
        $table = new XMLDBTable('role');
        $field = new XMLDBField('custom');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
        // make the assumption that everything with an id over 7 (the standard Moodle installed roles) at this stage is a custom role
        execute_sql("UPDATE {$CFG->prefix}role SET custom = 1 WHERE id > 7");
    }
    if ($oldversion < 2009052501) {
        $table = new XMLDBTable('user_friend');
        $field = new XMLDBField('approved');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 1, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
    }
    if ($oldversion < 2009062401) {
        $hostid = get_field('mnet_host', 'id', 'name', 'localmahara');
        $host2service = new stdClass();
        $host2service->serviceid = get_field('mnet_service', 'id', 'name', 'local_mahara');
        if (!empty($host2service->serviceid)) {
            $host2service->publish = 0;
            $host2service->subscribe = 1;
            if ($hostrec = get_record('mnet_host2service', 'hostid', $hostid, 'serviceid', $host2service->serviceid)) {
                $host2service->id = $hostrec->id;
                update_record('mnet_host2service', $host2service);
            } else {
                insert_record('mnet_host2service', $host2service);
            }
        }
    }
    if ($oldversion < 2009062800) {
        $table = new XMLDBTable('learning_path_mode');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('shortname', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addFieldInfo('name', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, null, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
        $lpmodes = array();
        $lpmodes[0]->shortname = 'standard';
        $lpmodes[0]->name = 'Standard';
        $lpmodes[1]->shortname = 'rafl';
        $lpmodes[1]->name = 'RAFL';
        foreach ($lpmodes as $mode) {
            insert_record('learning_path_mode', $mode);
        }
    }
    if ($oldversion < 2009062800) {
        $table = new XMLDBTable('course');
        $field = new XMLDBField('learning_path_mode');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
    }
    if ($oldversion < 2009062903) {
        $ptroleid = get_field('role', 'id', 'shortname', ROLE_PT);
        $lparoleid = get_field('role', 'id', 'shortname', ROLE_LPAUTHOR);
        set_config('defaultcourseroleid', $ptroleid);
        set_config('creatornewroleid', $lparoleid);
    }
    if ($oldversion < 2009070200) {
        // force setup of rafl item fields
        $table = new XMLDBTable('format_page');
        $field = new XMLDBField('rafl_item');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
        $table = new XMLDBTable('format_page_items');
        $field = new XMLDBField('rafl_item');
        $field->setAttributes(XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, null, null, null, null, null);
        if (!field_exists($table, $field)) {
            $result = $result && add_field($table, $field);
        }
    }
    if ($result && $oldversion < 2009071803) {
        //delete old tao_certification table and insert it's rows into the new table
        $table = new XMLDBTable('tao_certification_status');
        if (table_exists($table)) {
            $certrecords = get_records('tao_certification_status');
            if (!empty($certrecords)) {
                foreach ($certrecords as $cert) {
                    $cert->id = null;
                    insert_record('tao_user_certification_status', $cert);
                }
            }
            drop_table($table);
        }
        //now remove old blocks.
        $oldblocknames = array('authored_learning_paths', 'certification_path', 'course_status', 'learning_path_summary', 'lp_brief', 'lp_qa', 'team_groups');
        foreach ($oldblocknames as $oldblockname) {
            if ($oldblockid = get_field('block', 'id', 'name', $oldblockname)) {
                //first get new blocks id
                $newblockid = get_field('block', 'id', 'name', 'tao_' . $oldblockname);
                //now update block instance table replacing oldid with new one
                $oldblocks = get_records('block_instance', 'blockid', $oldblockid);
                if (!empty($oldblocks)) {
                    foreach ($oldblocks as $oldblock) {
                        $oldblock->blockid = $newblockid;
                        update_record('block_instance', $oldblock);
                    }
                }
                //now delete old record from block table.
                delete_records('block', 'name', $oldblockname);
            }
        }
    }
    if ($result && $oldversion < 2009072700) {
        //set up custom scale - in postinst so that the $USER is set.
        $scale = new stdclass();
        $scale->courseid = 0;
        $scale->userid = 0;
        $scale->name = 'TAO: Stars';
        $scale->scale = '★☆☆☆☆, ★★☆☆☆, ★★★☆☆, ★★★★☆, ★★★★★';
        $scale->description = '';
        $scale->timemodified = time();
        insert_record('scale', $scale);
    }
    if ($result && $oldversion < 2009072701) {
        //create new table for storing TAOview ratings
        $table = new XMLDBTable('taoview_ratings');
        $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
        $table->addFieldInfo('userid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('artefactid', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('time', XMLDB_TYPE_INTEGER, 10, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addFieldInfo('rating', XMLDB_TYPE_INTEGER, 4, XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
        $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));
        $result = $result && create_table($table);
    }
    if ($result && $oldversion < 2009072706) {
        //published course category is being moved.
        $cat = get_field('course_categories', 'id', 'name', get_string('taocatlp', 'local'));
        $pubcat = get_field('course_categories', 'id', 'name', 'Published');
        if (!empty($cat) && !empty($pubcat)) {
            //set default publish category to the main one.
            set_config('lppublishedcategory', $cat);
            //now find all the courses in the old published category and put them in the new one.
            $publishedcourses = get_records('course', 'category', $pubcat);
            if (!empty($publishedcourses)) {
                foreach ($publishedcourses as $pc) {
                    $pc->category = $cat;
                    update_record('course', $pc);
                }
            }
            //now remove old course category
            delete_records('course_categories', 'id', $pubcat);
        }
    }
    if ($result && $oldversion < 2009072708) {
        //now prevent the admin user from having the switchrole cap in learning path category
        $lpid = get_field('course_categories', 'id', 'name', get_string('taocatlp', 'local'));
        if (!empty($lpid)) {
            //this will only trigger on upgrades - not for fresh installs as the above category isn't created untill post_inst
            $catcontext = get_context_instance(CONTEXT_COURSECAT, $lpid);
            foreach (get_admin_roles() as $adminrole) {
                assign_capability('moodle/role:switchroles', CAP_PREVENT, $adminrole->id, $catcontext->id);
            }
        }
    }
    if ($result && $oldversion < 2009080300) {
        $reassigncaps = true;
        $resetcustomroles = true;
        $resetstickyblocks = true;
    }
    if ($result && $oldversion < 2009080301) {
        set_config('allowvisiblecoursesinhiddencategories', '1');
    }
    if ($result && $oldversion < 2009080302) {
        //delete old header image records
        delete_records('header_image', 'url', '/local/mahara/taoview.php?view=teaching');
        delete_records('header_image', 'url', '/local/mahara/taoview.php?view=tools');
        $headerimg = new stdclass();
        $headerimg->image = 'myteaching.jpg';
        $headerimg->url = '/local/mahara/taoviewtaoresources.php';
        $headerimg->description = get_string('myteaching', 'local');
        $headerimg->sortorder = 1;
        if (!record_exists('header_image', 'image', $headerimg->image, 'url', $headerimg->url)) {
            insert_record('header_image', $headerimg);
        }
        $headerimg->image = 'mytools.jpg';
        $headerimg->url = '/local/mahara/taoviewtaotools.php';
        $headerimg->description = get_string('mytools', 'local');
        if (!record_exists('header_image', 'image', $headerimg->image, 'url', $headerimg->url)) {
            insert_record('header_image', $headerimg);
        }
    }
    if ($result && $oldversion < 2009080303) {
        set_config('defaulthtmleditor', 'tinymce');
    }
    if ($result && $oldversion < 2009080309) {
        require_once $CFG->dirroot . '/mod/rafl/locallib.php';
        if (!check_rafl_table_encoding()) {
            notify("WARNING: The smartassess rafl tables are not utf8 based tables - they must be manually converted.");
            $result = false;
        }
    }
    if ($result && $oldversion < 2009101407) {
        $table = new XMLDBTable('rafl_webcells');
        if (table_exists($table)) {
            $webcelltext = 'How will the learning path be evaluated and developed?';
            $sql = "UPDATE {$CFG->prefix}rafl_webcells SET webcell_title = '" . $webcelltext . "' WHERE webcell_id =368657";
            if (!execute_sql($sql)) {
                notify('rafl_webcells UPDATE failed');
                $result = false;
            }
            //now update format page
            $fpitems = get_records('format_page', 'nameone', 'How will the learning path be evaluated and develo');
            foreach ($fpitems as $fpi) {
                $fpi->nameone = $webcelltext;
                $fpi->nametwo = $webcelltext;
                update_record('format_page', $fpi);
            }
        }
    }
    if ($result && $oldversion < 2009101410) {
        $reassigncaps = true;
        $resetcustomroles = true;
    }
    if ($result && $oldversion < 2009101413) {
        //MNET change update.
        //first create array of mappings
        $mnetfields = array();
        $mnetfields['address'] = 'address';
        $mnetfields['aim'] = 'aimscreenname';
        $mnetfields['city'] = 'town';
        $mnetfields['country'] = 'country';
        $mnetfields['deleted'] = 'deleted';
        $mnetfields['description'] = 'introduction';
        $mnetfields['firstname'] = 'firstname';
        $mnetfields['htmleditor'] = 'wysiwyg';
        $mnetfields['icq'] = 'icqnumber';
        $mnetfields['idnumber'] = 'studentid';
        $mnetfields['lang'] = 'lang';
        $mnetfields['lastname'] = 'lastname';
        $mnetfields['mns'] = 'mnsnumber';
        $mnetfields['phone1'] = 'businessnumber';
        $mnetfields['phone2'] = 'homenumber';
        $mnetfields['skype'] = 'skypeusername';
        $mnetfields['url'] = 'officialwebsite';
        $mnetfields['yahoo'] = 'yahoochat';
        //first find any active mnet entries.
        $hosts = get_records_select('mnet_host', 'deleted=0 AND applicationid=2');
        if (!empty($hosts)) {
            foreach ($hosts as $host) {
                foreach ($mnetfields as $name => $value) {
                    set_config($name, $value, 'mnet_userprofile_' . $host->id);
                }
            }
        }
        //now add mapping fields to db.
    }
    // check whether we've been asked to reset anything
    if (!empty($reassigncaps)) {
        tao_reassign_capabilities();
    }
    if (!empty($resetcustomroles)) {
        if (empty($CFG->taomode)) {
            // only perform this in 'core' taomode
            tao_reset_custom_roles();
        }
    }
    if (!empty($resetstickyblocks)) {
        if (empty($CFG->taomode)) {
            // only perform this in 'core' taomode
            tao_reset_stickyblocks(true);
        }
    }
    return $result;
}
Exemplo n.º 2
0
function local_postinst()
{
    global $db, $CFG;
    $olddebug = $db->debug;
    $db->debug = $CFG->debug;
    set_config('theme', 'intel');
    $db->debug = $olddebug;
    // set frontpage blocks
    tao_reset_frontpage_blocks();
    // set sticky blocks
    tao_reset_stickyblocks();
    // create the TAO Site Forum
    $forum = local_create_forum(SITEID, NOGROUPS, get_string('siteforumname', 'local'), get_string('siteforumintro', 'local'));
    // remove guest access to the site level forum
    $required = array("mod/forum:addnews", "mod/forum:createattachment", "mod/forum:deleteanypost", "mod/forum:deleteownpost", "mod/forum:editanypost", "mod/forum:initialsubscriptions", "mod/forum:managesubscriptions", "mod/forum:movediscussions", "mod/forum:rate", "mod/forum:replynews", "mod/forum:replypost", "mod/forum:splitdiscussions", "mod/forum:startdiscussion", "mod/forum:throttlingapplies", "mod/forum:viewanyrating", "mod/forum:viewdiscussion", "mod/forum:viewhiddentimedposts", "mod/forum:viewqandawithoutposting", "mod/forum:viewrating", "mod/forum:viewsubscribers");
    if (!($cm = get_coursemodule_from_id("forum", $forum->coursemodule, SITEID))) {
        error("Could not determine which course module this belonged to!");
    }
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    // find the exisiting list of capabilities
    $capabilities = fetch_context_capabilities($context);
    if ($roles = get_records_select('role', 'shortname IN (\'guest\')')) {
        foreach ($roles as $role) {
            foreach ($capabilities as $capability) {
                // check against the list of capabilities that we want to block
                if (!in_array($capability->name, $required)) {
                    continue;
                }
                assign_capability($capability->name, -1000, $role->id, $context->id);
            }
        }
        mark_context_dirty($context->path);
    }
    // give everyone else access to the site level forum
    $required = array("mod/forum:addnews", "mod/forum:createattachment", "mod/forum:deleteownpost", "mod/forum:initialsubscriptions", "mod/forum:rate", "mod/forum:replynews", "mod/forum:replypost", "mod/forum:startdiscussion", "mod/forum:viewanyrating", "mod/forum:viewdiscussion", "mod/forum:viewrating");
    if ($roles = get_records_select('role', 'shortname NOT IN (\'guest\', \'user\')')) {
        foreach ($roles as $role) {
            foreach ($capabilities as $capability) {
                // check against the list of capabilities that we want to block
                if (!in_array($capability->name, $required)) {
                    continue;
                }
                assign_capability($capability->name, 1, $role->id, $context->id);
            }
        }
        mark_context_dirty($context->path);
    }
    // add TAO site wide FAQ
    $glossary = local_create_glossary(SITEID, get_string('defaultglossaryname', 'local'), get_string('defaultglossarydescription', 'local'));
    // set of capabilities that we want to orverride
    $required = array('mod/glossary:approve', 'mod/glossary:comment', 'mod/glossary:import', 'mod/glossary:managecategories', 'mod/glossary:managecomments', 'mod/glossary:manageentries', 'mod/glossary:rate', 'mod/glossary:write');
    if (!($cm = get_coursemodule_from_id("glossary", $glossary->coursemodule, SITEID))) {
        error("Could not determine which course module this belonged to!");
    }
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    // find the exisiting list of capabilities
    $capabilities = fetch_context_capabilities($context);
    if ($roles = get_records_select('role', 'shortname NOT IN (\'admin\', \'superadmin\')')) {
        foreach ($roles as $role) {
            foreach ($capabilities as $capability) {
                // check against the list of capabilities that we want to block
                if (!in_array($capability->name, $required)) {
                    continue;
                }
                assign_capability($capability->name, -1000, $role->id, $context->id);
            }
        }
        mark_context_dirty($context->path);
    }
    //set up default Course Categories
    $course_cat = new stdclass();
    $course_cat->name = get_string('taocatlp', 'local');
    $course_cat->description = '';
    $course_cat->parent = 0;
    $course_cat->sortorder = 2;
    $course_cat->coursecount = 0;
    $course_cat->visible = 1;
    $course_cat->timemodified = time();
    $course_cat->depth = 1;
    $course_cat->path = "''";
    $course_cat->theme = '';
    $lpid = insert_record('course_categories', $course_cat);
    //insert learning path category and keep id for use later.
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $lpid);
    mark_context_dirty($catcontext->path);
    $course_cat->name = get_string('taocatlptemplates', 'local');
    $course_cat->sortorder = 3;
    $course_cat->visible = 0;
    $lptempid = insert_record('course_categories', $course_cat);
    //insert my sections category keep id for use later.
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $lptempid);
    mark_context_dirty($catcontext->path);
    $course_cat->name = get_string('taotrainingcourses', 'local');
    $course_cat->sortorder = 5;
    $course_cat->visible = 1;
    $taotrainingid = insert_record('course_categories', $course_cat);
    //insert my sections category keep id for use later.
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $taotrainingid);
    mark_context_dirty($catcontext->path);
    $course_cat->name = get_string('taocatworkshop', 'local');
    $course_cat->parent = $lpid;
    $course_cat->sortorder = 2;
    $course_cat->visible = 0;
    $catworkid = insert_record('course_categories', $course_cat);
    //insert published category
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $catworkid);
    mark_context_dirty($catcontext->path);
    $course_cat->name = get_string('taocatsuspended', 'local');
    $course_cat->parent = $lpid;
    $course_cat->sortorder = 3;
    $course_cat->visible = 0;
    $suscatid = insert_record('course_categories', $course_cat);
    //insert published category
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $suscatid);
    mark_context_dirty($catcontext->path);
    fix_course_sortorder();
    //set paths correctly.
    //now set default categories for learning path stuff.
    set_config('lptemplatescategory', $lptempid);
    //set template category
    set_config('lpdefaultcategory', $catworkid);
    set_config('lppublishedcategory', $lpid);
    set_config('lpsuspendedcategory', $suscatid);
    //now prevent the admin user from having the switchrole cap in learning path category
    $catcontext = get_context_instance(CONTEXT_COURSECAT, $lpid);
    foreach (get_admin_roles() as $adminrole) {
        assign_capability('moodle/role:switchroles', CAP_PREVENT, $adminrole->id, $catcontext->id);
    }
    //now we need to do some silent restores for:
    // Learning Path Templates
    //silent restore of LP Template into $lptempid
    require_once $CFG->dirroot . '/course/lib.php';
    require_once $CFG->dirroot . '/backup/lib.php';
    require_once $CFG->dirroot . '/backup/restorelib.php';
    if (file_exists($CFG->dirroot . '/local/initialcoursetemplates/lpt.zip')) {
        $course = new StdClass();
        $course->category = $lptempid;
        $course->fullname = get_string('learningpathtemplate', 'local');
        $course->shortname = get_string('learningpathtemplateshortname', 'local');
        $course->format = 'learning';
        //hardcoded for this course format - for some reason silent restore doesn't like restoring this format if not already set
        if ($newcourse = create_course($course)) {
            import_backup_file_silently($CFG->dirroot . '/local/initialcoursetemplates/lpt.zip', $newcourse->id, true, false);
        }
        //now copy the backup file into place to use as template file.
        $fullpath = $CFG->dataroot . "/" . $newcourse->id . "/backupdata";
        if (!is_dir($fullpath)) {
            mkdir($fullpath, $CFG->directorypermissions, true);
        }
        copy($CFG->dirroot . '/local/initialcoursetemplates/lpt.zip', $fullpath . '/lpt.zip');
    }
    //now restore Training courses:
    if (file_exists($CFG->dirroot . '/local/initialcoursetemplates/taodocs1.zip')) {
        import_backup_file_silently($CFG->dirroot . '/local/initialcoursetemplates/taodocs1.zip', SITEID, true, false, array(), RESTORETO_NEW_COURSE);
    }
    if (file_exists($CFG->dirroot . '/local/initialcoursetemplates/taodocs2.1.zip')) {
        import_backup_file_silently($CFG->dirroot . '/local/initialcoursetemplates/taodocs2.1.zip', SITEID, true, false, array(), RESTORETO_NEW_COURSE);
    }
    if (file_exists($CFG->dirroot . '/local/initialcoursetemplates/taodocs2.2.zip')) {
        import_backup_file_silently($CFG->dirroot . '/local/initialcoursetemplates/taodocs2.2.zip', SITEID, true, false, array(), RESTORETO_NEW_COURSE);
    }
    //site frontpage stuff - default settings and some initial content.
    $sitecourse = get_record('course', 'id', SITEID);
    $sitecourse->numsections = 1;
    update_record('course', $sitecourse);
    $a = new stdclass();
    $a->myteachinglink = $CFG->wwwroot . '/local/mahara/taoviewtaoresources.php';
    $a->mytoolslink = $CFG->wwwroot . '/local/mahara/taoviewtaotools.php';
    $a->mylearninglink = $CFG->wwwroot . '/local/my/learning.php';
    $a->myworklink = $CFG->wwwroot . '/local/my/work.php';
    $a->mycollablink = $CFG->wwwroot . '/local/my/collaboration.php';
    $a->myteachingimg = $CFG->wwwroot . '/theme/intel/pix/path/teaching.jpg';
    $a->mylearningimg = $CFG->wwwroot . '/theme/intel/pix/path/learning.jpg';
    $a->myworkimg = $CFG->wwwroot . '/theme/intel/pix/path/work.jpg';
    $a->mytoolsimg = $CFG->wwwroot . '/theme/intel/pix/path/tools.jpg';
    $a->mycollabimg = $CFG->wwwroot . '/theme/intel/pix/path/collaboration.jpg';
    $sitesection = new stdclass();
    $sitesection->course = SITEID;
    $sitesection->section = 1;
    $sitesection->summary = addslashes(get_string('initialfrontpagecontent', 'local', $a));
    $sitesection->sequence = '';
    $sitesection->visible = 1;
    $sitesectionid = insert_record('course_sections', $sitesection);
    //add a link to the training courses on the frontpage: - there's probably a cleaner way to do this!!!
    $resource = new stdclass();
    $resource->course = SITEID;
    $resource->name = get_string('taotrainingcourses', 'local');
    $resource->type = 'file';
    $resource->reference = $CFG->wwwroot . '/course/category.php?id=' . $taotrainingid;
    $resource->summary = '';
    $resource->timemodified = time();
    $trainresid = insert_record("resource", $resource);
    //add record to course_modules for the resource
    $resm = new stdclass();
    $resm->course = SITEID;
    $resm->module = get_field('modules', 'id', 'name', 'resource');
    $resm->instance = $trainresid;
    $resm->section = $sitesectionid;
    $resm->added = time();
    $resm->score = 0;
    $resm->indent = 0;
    $resm->visible = 1;
    $resm->visibleold = 1;
    $resm->groupmode = 0;
    $resm->groupingid = 0;
    $resm->groupmembersonly = 0;
    $resm->trackprogress = 0;
    $resmid = insert_record('course_modules', $resm);
    //now update the sequence for the section just created.
    $sitesection->sequence = $resmid;
    $sitesection->id = $sitesectionid;
    update_record('course_sections', $sitesection);
    //set no other stuff to display in main content section of homepage
    set_config('frontpage', '');
    set_config('frontpageloggedin', '');
    set_config('allowvisiblecoursesinhiddencategories', '1');
    // ensure custom roles are set correctly
    tao_reset_custom_roles();
    rebuild_course_cache(SITEID);
    return true;
}
Exemplo n.º 3
0
/**
* When called resets all custom roles as per definition set down in /local/roles.php
*
* Note that this uses the non-core role.custom field to isolate roles to remove.
*
* Utilise the $path parameter to allow for localisation (i.e. different roles defintion than core).
*
* Sort order is reset based on the order listed in the defintion.
*
* WARNING: as long as you retain the same shortname existing user role assigments will
*             be retained.  if you change the shortname they will be lost.
*
* KNOWN ISSUE: we rely on shortname being unique, but this is not enforced by the db.
*                       this is more a problem with moodle.
*
* @param text $path
*
*/
function tao_reset_custom_roles($path = 'local')
{
    global $CFG;
    if (!get_site()) {
        // not finished installing, skip
        return true;
    }
    // get latest role definition from roles file
    $rolespath = $CFG->dirroot . '/' . $path . '/roles.php';
    if (!file_exists($rolespath)) {
        debugging("Local caps reassignment called with invalid path {$path}");
        return false;
    }
    require_once $rolespath;
    if (!isset($customroles)) {
        return true;
        // nothing to do.
    }
    $undeletableroles = array();
    $undeletableroles[$CFG->notloggedinroleid] = 1;
    $undeletableroles[$CFG->guestroleid] = 1;
    $undeletableroles[$CFG->defaultuserroleid] = 1;
    $undeletableroles[$CFG->defaultcourseroleid] = 1;
    // If there is only one admin role, add that to $undeletableroles too.
    $adminroles = get_admin_roles();
    if (count($adminroles) == 1) {
        $undeletableroles[reset($adminroles)->id] = 1;
    }
    // get recordset of existing custom roles
    $sql = "SELECT id, name, shortname, description, sortorder, custom\n              FROM {$CFG->prefix}role\n              WHERE custom IS NOT NULL";
    $roles = get_records_sql($sql);
    // remove custom roles that are not in the latest definition
    foreach ($roles as $role) {
        // check whether this role is in the latest definition
        if (array_key_exists($role->shortname, $customroles)) {
            continue;
        }
        // extra safety: check undeletable roles
        if (isset($undeletableroles[$role->id])) {
            continue;
        }
        delete_role($role->id);
    }
    // hack to avoid sortorder unique constraint
    execute_sql("UPDATE {$CFG->prefix}role SET sortorder = (sortorder+1000) WHERE custom IS NOT NULL");
    // set sortorder to current highest value
    $sortorder = get_field_sql("SELECT " . sql_max('sortorder') . " FROM {$CFG->prefix}role WHERE custom IS NULL");
    // now loop through the new settings
    foreach ($customroles as $shortname => $role) {
        $sortorder++;
        // get the roleid
        $roleid = get_field('role', 'id', 'shortname', $shortname);
        // if exists then make updates
        if (!empty($roleid)) {
            // only update fields that have been set
            if (isset($role['name'])) {
                set_field('role', 'name', $role['name'], 'shortname', $shortname);
            }
            if (isset($role['description'])) {
                set_field('role', 'description', $role['description'], 'shortname', $shortname);
            }
            // reset sortorder
            set_field('role', 'sortorder', $sortorder, 'shortname', $shortname);
            // else create record
        } else {
            $newrole = new stdclass();
            $newrole->name = $role['name'];
            $newrole->shortname = $shortname;
            $newrole->description = $role['description'];
            $newrole->sortorder = $sortorder;
            $newrole->custom = 1;
            $roleid = insert_record('role', $newrole);
        }
        // remove any previously set legacy roles
        $legacyroles = get_legacy_roles();
        foreach ($legacyroles as $ltype => $lcap) {
            unassign_capability($lcap, $roleid);
        }
        // reset legacy role
        if (isset($role['legacy'])) {
            $legacycap = $legacyroles[$role['legacy']];
            $context = get_context_instance(CONTEXT_SYSTEM);
            assign_capability($legacycap, CAP_ALLOW, $roleid, $context->id);
        }
        // update the context settings
        set_role_contextlevels($roleid, $role['context']);
        //  e.g. array(CONTEXT_SYSTEM, CONTEXT_COURSECAT)
        // set allow assigns
        if (is_array($role['canassign'])) {
            // delete existing
            delete_records('role_allow_assign', 'allowassign', $roleid);
            foreach ($role['canassign'] as $canassign) {
                $canassignid = get_field('role', 'id', 'shortname', $canassign);
                allow_assign($canassignid, $roleid);
            }
        }
    }
    // reset custom capabilities to keep up with changes
    return tao_reset_capabilities();
}
Exemplo n.º 4
0
$defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
/// Check access permissions.
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
require_login();
require_capability('moodle/role:manage', $systemcontext);
admin_externalpage_setup('defineroles');
/// Get some basic data we are going to need.
$roles = get_all_roles();
role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
$undeletableroles = array();
$undeletableroles[$CFG->notloggedinroleid] = 1;
$undeletableroles[$CFG->guestroleid] = 1;
$undeletableroles[$CFG->defaultuserroleid] = 1;
$undeletableroles[$CFG->defaultcourseroleid] = 1;
// If there is only one admin role, add that to $undeletableroles too.
$adminroles = get_admin_roles();
if (count($adminroles) == 1) {
    $undeletableroles[reset($adminroles)->id] = 1;
}
///.Process submitted data.
$confirmed = optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey();
switch ($action) {
    case 'delete':
        if (isset($undeletableroles[$roleid])) {
            print_error('cannotdeletethisrole', '', $baseurl);
        }
        if (!$confirmed) {
            // show confirmation
            admin_externalpage_print_header();
            $optionsyes = array('action' => 'delete', 'roleid' => $roleid, 'sesskey' => sesskey(), 'confirm' => 1);
            $a = new object();
Exemplo n.º 5
0
/**
 * function that creates a role
 * @param name - role name
 * @param shortname - role short name
 * @param description - role description
 * @param legacy - optional legacy capability
 * @return id or false
 */
function create_role($name, $shortname, $description, $legacy = '')
{
    global $CFG;
    // check for duplicate role name
    if ($role = get_record('role', 'name', $name)) {
        error('there is already a role with this name!');
    }
    if ($role = get_record('role', 'shortname', $shortname)) {
        error('there is already a role with this shortname!');
    }
    // Get the system context.
    if (!($context = get_context_instance(CONTEXT_SYSTEM))) {
        return false;
    }
    // Insert the role record.
    $role = new object();
    $role->name = $name;
    $role->shortname = $shortname;
    $role->description = $description;
    //find free sortorder number
    if (!($role->sortorder = get_field_sql('SELECT MAX(sortorder) + 1 FROM ' . $CFG->prefix . 'role'))) {
        $role->sortorder = 1;
    }
    $id = insert_record('role', $role);
    if (!$id) {
        return false;
    }
    if ($legacy) {
        assign_capability($legacy, CAP_ALLOW, $id, $context->id);
    }
    // By default, users with role:manage at site level should be able to assign
    // users to this new role, and override this new role's capabilities.
    if ($adminroles = get_admin_roles()) {
        foreach ($adminroles as $arole) {
            allow_assign($arole->id, $id);
            allow_override($arole->id, $id);
        }
    }
    return $id;
}
Exemplo n.º 6
0
 public function getAllUsers()
 {
     $sql = sprintf("SELECT admin_id, username, first_name, last_name, company_id, email_addr, email_addr2, is_super FROM %s WHERE 1", self::T_NAME);
     $query = $this->db->query($sql);
     $this->load->model('admin_permission_m', 'adminPermission');
     $permissions = $this->adminPermission->getAllUserPermissions();
     $rows = $query->result_array();
     if (sizeof($rows) > 0) {
         $return = array();
         $allRoles = get_admin_roles();
         foreach ($rows as $row) {
             $userRoles = $permissions[$row['admin_id']];
             if (is_array($userRoles) == false) {
                 $userRoles = array();
             }
             $role = "";
             if ($row['is_super']) {
                 $role = get_supper_admin_rolename();
                 $type = ucfirst($role);
             } else {
                 if (identical_values($allRoles, $userRoles)) {
                     $role = get_allinclude_rolename();
                     $type = ucfirst($role);
                 } else {
                     $role = implode(',', $userRoles);
                     foreach ($userRoles as &$item) {
                         $item = ucfirst($item);
                     }
                     $type = implode(',', $userRoles);
                 }
             }
             $return[] = array_merge($row, array('role' => $role, 'usertype' => $type));
         }
         return $return;
     } else {
         return array();
     }
 }