Пример #1
0
 /**
  * Definition of this form.
  */
 protected function definition()
 {
     $mform = $this->_form;
     $data = $this->_customdata;
     $options = array();
     $group = get_string('other');
     $options[$group] = array();
     $options[$group][0] = get_string('norole', 'core_role');
     $group = get_string('role', 'core');
     $options[$group] = array();
     foreach (role_get_names(null, ROLENAME_BOTH) as $role) {
         // Allow reset to self too, it may be useful when importing incomplete XML preset.
         $options[$group][$role->id] = $role->localname;
     }
     $group = get_string('archetype', 'core_role');
     $options[$group] = array();
     foreach (get_role_archetypes() as $type) {
         $options[$group][$type] = get_string('archetype' . $type, 'core_role');
     }
     $mform->addElement('header', 'presetheader', get_string('roleresetdefaults', 'core_role'));
     $mform->addElement('selectgroups', 'resettype', get_string('roleresetrole', 'core_role'), $options);
     $mform->addElement('filepicker', 'rolepreset', get_string('rolerepreset', 'core_role'));
     if ($data['roleid']) {
         $mform->addElement('header', 'resetheader', get_string('resetrole', 'core_role'));
         $mform->addElement('advcheckbox', 'shortname', get_string('roleshortname', 'core_role'));
         $mform->addElement('advcheckbox', 'name', get_string('customrolename', 'core_role'));
         $mform->addElement('advcheckbox', 'description', get_string('customroledescription', 'core_role'));
         $mform->addElement('advcheckbox', 'archetype', get_string('archetype', 'core_role'));
         $mform->addElement('advcheckbox', 'contextlevels', get_string('maybeassignedin', 'core_role'));
         $mform->addElement('advcheckbox', 'allowassign', get_string('allowassign', 'core_role'));
         $mform->addElement('advcheckbox', 'allowoverride', get_string('allowoverride', 'core_role'));
         $mform->addElement('advcheckbox', 'allowswitch', get_string('allowswitch', 'core_role'));
         $mform->addElement('advcheckbox', 'permissions', get_string('permissions', 'core_role'));
     }
     $mform->addElement('hidden', 'roleid');
     $mform->setType('roleid', PARAM_INT);
     $mform->addElement('hidden', 'action');
     $mform->setType('action', PARAM_ALPHA);
     $mform->addElement('hidden', 'return');
     $mform->setType('return', PARAM_ALPHA);
     $this->add_action_buttons(true, get_string('continue', 'core'));
     $this->set_data($data);
 }
Пример #2
0
/**
 * Function that creates a role
 *
 * @param string $name role name
 * @param string $shortname role short name
 * @param string $description role description
 * @param string $archetype
 * @return int id or dml_exception
 */
function create_role($name, $shortname, $description, $archetype = '')
{
    global $DB;
    if (strpos($archetype, 'moodle/legacy:') !== false) {
        throw new coding_exception('Use new role archetype parameter in create_role() instead of old legacy capabilities.');
    }
    // verify role archetype actually exists
    $archetypes = get_role_archetypes();
    if (empty($archetypes[$archetype])) {
        $archetype = '';
    }
    // Insert the role record.
    $role = new stdClass();
    $role->name = $name;
    $role->shortname = $shortname;
    $role->description = $description;
    $role->archetype = $archetype;
    //find free sortorder number
    $role->sortorder = $DB->get_field('role', 'MAX(sortorder) + 1', array());
    if (empty($role->sortorder)) {
        $role->sortorder = 1;
    }
    $id = $DB->insert_record('role', $role);
    return $id;
}
Пример #3
0
 /**
  * Parse role preset xml file.
  *
  * @param string $xml
  * @return array role info, null on error
  */
 public static function parse_preset($xml)
 {
     global $DB;
     $info = array();
     if (!self::is_valid_preset($xml)) {
         return null;
     }
     $dom = new DOMDocument();
     $dom->loadXML($xml);
     $info['shortname'] = self::get_node_value($dom, '/role/shortname');
     if (isset($info['shortname'])) {
         $info['shortname'] = strtolower(clean_param($info['shortname'], PARAM_ALPHANUMEXT));
     }
     $info['name'] = self::get_node_value($dom, '/role/name');
     if (isset($value)) {
         $info['name'] = clean_param($info['name'], PARAM_TEXT);
     }
     $info['description'] = self::get_node_value($dom, '/role/description');
     if (isset($value)) {
         $info['description'] = clean_param($info['description'], PARAM_CLEANHTML);
     }
     $info['archetype'] = self::get_node_value($dom, '/role/archetype');
     if (isset($value)) {
         $archetypes = get_role_archetypes();
         if (!isset($archetypes[$info['archetype']])) {
             $info['archetype'] = null;
         }
     }
     $values = self::get_node_children_values($dom, '/role/contextlevels', 'level');
     if (isset($values)) {
         $info['contextlevels'] = array();
         $levelmap = array_flip(context_helper::get_all_levels());
         foreach ($values as $value) {
             $level = 'context_' . $value;
             if (isset($levelmap[$level])) {
                 $cl = $levelmap[$level];
                 $info['contextlevels'][$cl] = $cl;
             }
         }
     }
     foreach (array('assign', 'override', 'switch') as $type) {
         $values = self::get_node_children_values($dom, '/role/allow' . $type, 'shortname');
         if (!isset($values)) {
             $info['allow' . $type] = null;
             continue;
         }
         $info['allow' . $type] = array();
         foreach ($values as $value) {
             if ($value === $info['shortname']) {
                 array_unshift($info['allow' . $type], -1);
                 // Means self.
             }
             if ($role = $DB->get_record('role', array('shortname' => $value))) {
                 $info['allow' . $type][] = $role->id;
                 continue;
             }
         }
     }
     $info['permissions'] = array();
     $values = self::get_node_children_values($dom, '/role/permissions', 'inherit');
     if (isset($values)) {
         foreach ($values as $value) {
             if ($value = clean_param($value, PARAM_CAPABILITY)) {
                 $info['permissions'][$value] = CAP_INHERIT;
             }
         }
     }
     $values = self::get_node_children_values($dom, '/role/permissions', 'allow');
     if (isset($values)) {
         foreach ($values as $value) {
             if ($value = clean_param($value, PARAM_CAPABILITY)) {
                 $info['permissions'][$value] = CAP_ALLOW;
             }
         }
     }
     $values = self::get_node_children_values($dom, '/role/permissions', 'prevent');
     if (isset($values)) {
         foreach ($values as $value) {
             if ($value = clean_param($value, PARAM_CAPABILITY)) {
                 $info['permissions'][$value] = CAP_PREVENT;
             }
         }
     }
     $values = self::get_node_children_values($dom, '/role/permissions', 'prohibit');
     if (isset($values)) {
         foreach ($values as $value) {
             if ($value = clean_param($value, PARAM_CAPABILITY)) {
                 $info['permissions'][$value] = CAP_PROHIBIT;
             }
         }
     }
     return $info;
 }
Пример #4
0
 protected function get_archetype_field($id)
 {
     $options = array();
     $options[''] = get_string('none');
     foreach (get_role_archetypes() as $type) {
         $options[$type] = get_string('archetype' . $type, 'role');
     }
     return html_writer::select($options, 'archetype', $this->role->archetype, false);
 }
 /**
  * Test default enrol roles.
  */
 public function test_get_default_enrol_roles()
 {
     $this->resetAfterTest();
     $course = $this->getDataGenerator()->create_course();
     $coursecontext = context_course::instance($course->id);
     $id2 = create_role('New student role', 'student2', 'New student description', 'student');
     set_role_contextlevels($id2, array(CONTEXT_COURSE));
     $allroles = get_all_roles();
     $expected = array($id2 => $allroles[$id2]);
     foreach (get_role_archetypes() as $archetype) {
         $defaults = get_default_contextlevels($archetype);
         if (in_array(CONTEXT_COURSE, $defaults)) {
             $roles = get_archetype_roles($archetype);
             foreach ($roles as $role) {
                 $expected[$role->id] = $role;
             }
         }
     }
     $roles = get_default_enrol_roles($coursecontext);
     foreach ($allroles as $role) {
         $this->assertEquals(isset($expected[$role->id]), isset($roles[$role->id]));
         if (isset($roles[$role->id])) {
             $this->assertSame(role_get_name($role, $coursecontext), $roles[$role->id]);
         }
     }
 }
Пример #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;
    }
Пример #7
0
 //
 $settings->add(new admin_setting_heading('equella_lti_settings', ecs('lti.heading'), ecs('lti.help')));
 $settings->add(new admin_setting_configcheckbox('equella_enable_lti', ecs('enablelti'), ecs('enablelti.desc'), 0));
 $settings->add(new admin_setting_configtext('equella_lti_oauth_key', ecs('lti.key.title'), ecs('lti.key.help'), ''));
 $settings->add(new admin_setting_configtext('equella_lti_oauth_secret', ecs('lti.secret.title'), ecs('lti.secret.help'), ''));
 // ///////////////////////////////////////////////////////////////////////////////
 //
 // SHARED SECRETS
 //
 $settings->add(new admin_setting_heading('equella_sharedsecrets_settings', ecs('sharedsecrets.heading'), ecs('sharedsecrets.help')));
 $defaultvalue = '';
 $description = '';
 $settings->add(new equella_setting_left_heading('equella_default_group', ecs('group', ecs('group.default')), ''));
 $settings->add(new admin_setting_configtext('equella_shareid', ecs('sharedid.title'), $description, $defaultvalue));
 $settings->add(new admin_setting_configtext('equella_sharedsecret', ecs('sharedsecret.title'), $description, $defaultvalue));
 $rolearchetypes = get_role_archetypes();
 foreach (get_all_editing_roles() as $role) {
     $shortname = clean_param($role->shortname, PARAM_ALPHANUM);
     if (in_array($shortname, $rolearchetypes)) {
         $heading = ecs('group.' . $shortname);
     } else {
         $heading = ecs('group.noname', $shortname);
         if (!empty($role->name)) {
             $heading = ecs('group', $role->name);
         }
     }
     $sectionname = 'equella_' . $shortname . '_role_group';
     $settings->add(new equella_setting_left_heading($sectionname, $heading, ''));
     $settings->add(new admin_setting_configtext("equella_{$shortname}_shareid", ecs('sharedid.title'), $description, $defaultvalue, PARAM_TEXT));
     $settings->add(new admin_setting_configtext("equella_{$shortname}_sharedsecret", ecs('sharedsecret.title'), $description, $defaultvalue, PARAM_TEXT));
 }