function definition() { global $CFG, $DB; $mform = $this->_form; list($instance, $plugin, $course) = $this->_customdata; $coursecontext = context_course::instance($course->id); $enrol = enrol_get_plugin('cohort'); $groups = array(0 => get_string('none')); foreach (groups_get_all_groups($course->id) as $group) { $groups[$group->id] = format_string($group->name, true, array('context' => $coursecontext)); } $mform->addElement('header', 'general', get_string('pluginname', 'enrol_cohort')); $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); $mform->setType('name', PARAM_TEXT); $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), ENROL_INSTANCE_DISABLED => get_string('no')); $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options); if ($instance->id) { if ($cohort = $DB->get_record('cohort', array('id' => $instance->customint1))) { $cohorts = array($instance->customint1 => format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid)))); } else { $cohorts = array($instance->customint1 => get_string('error')); } $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts); $mform->setConstant('customint1', $instance->customint1); $mform->hardFreeze('customint1', $instance->customint1); } else { $cohorts = array('' => get_string('choosedots')); $allcohorts = cohort_get_available_cohorts($coursecontext, 0, 0, 0); foreach ($allcohorts as $c) { $cohorts[$c->id] = format_string($c->name); } $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts); $mform->addRule('customint1', get_string('required'), 'required', null, 'client'); } $roles = get_assignable_roles($coursecontext); $roles[0] = get_string('none'); $roles = array_reverse($roles, true); // Descending default sortorder. $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles); $mform->setDefault('roleid', $enrol->get_config('roleid')); if ($instance->id and !isset($roles[$instance->roleid])) { if ($role = $DB->get_record('role', array('id' => $instance->roleid))) { $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS, true); $roles[$instance->roleid] = role_get_name($role, $coursecontext); } else { $roles[$instance->roleid] = get_string('error'); } } $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups); $mform->addElement('hidden', 'courseid', null); $mform->setType('courseid', PARAM_INT); $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); if ($instance->id) { $this->add_action_buttons(true); } else { $this->add_action_buttons(true, get_string('addinstance', 'enrol')); } $this->set_data($instance); }
/** * Constructor. * @throws Command_Exception. */ public function __construct() { global $DB; // Getting command description. $cmd_name = vmoodle_get_string('cmdsynccapabilityname', 'vmoodleadminset_roles'); $cmd_desc = vmoodle_get_string('cmdsynccapabilitydesc', 'vmoodleadminset_roles'); // Creating platform parameter. $platform_param = new Command_Parameter('platform', 'enum', vmoodle_get_string('platformparamsyncdesc', 'vmoodleadminset_roles'), null, get_available_platforms()); // Getting role parameter. $roles = role_fix_names(get_all_roles(), \context_system::instance(), ROLENAME_ORIGINAL); $rolemenu = array(); foreach ($roles as $r) { $rolemenu[$r->shortname] = $r->name; } $role_param = new Command_Parameter('role', 'enum', vmoodle_get_string('roleparamsyncdesc', 'vmoodleadminset_roles'), null, $rolemenu); // Creating capability parameter. $records = $DB->get_records('capabilities', null, 'name', 'name'); $capabilities = array(); foreach ($records as $record) { $capabilities[$record->name] = get_capability_string($record->name); } asort($capabilities); $capability_param = new Command_Parameter('capability', 'enum', vmoodle_get_string('capabilityparamsyncdesc', 'vmoodleadminset_roles'), null, $capabilities); // Creating command. parent::__construct($cmd_name, $cmd_desc, array($platform_param, $role_param, $capability_param)); }
protected function get_allow_role_control($type) { if ($roles = $this->get_allow_roles_list($type)) { $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL, true); return implode(', ', $roles); } else { return get_string('none'); } }
/** * Constructor. * @param context $context the context this table relates to. * @param string $contextname $context->get_context_name() - to save recomputing. * @param array $allowoverrides * @param array $allowsafeoverrides * @param array $overridableroles */ public function __construct($context, $contextname, $allowoverrides, $allowsafeoverrides, $overridableroles) { parent::__construct($context, 'permissions'); $this->contextname = $contextname; $this->allowoverrides = $allowoverrides; $this->allowsafeoverrides = $allowsafeoverrides; $this->overridableroles = $overridableroles; $roles = get_all_roles($context); $this->roles = role_fix_names(array_reverse($roles, true), $context, ROLENAME_ALIAS, true); }
/** * Constructor. * @throws Command_Exception. */ public function __construct() { global $DB; // Getting command description $cmd_name = vmoodle_get_string('cmdcomparename', 'vmoodleadminset_roles'); $cmd_desc = vmoodle_get_string('cmdcomparedesc', 'vmoodleadminset_roles'); // Getting role parameter $roles = role_fix_names(get_all_roles(), \context_system::instance(), ROLENAME_ORIGINAL); $rolemenu = array(); foreach ($roles as $r) { $rolemenu[$r->shortname] = $r->localname; } $role_param = new Command_Parameter('role', 'enum', vmoodle_get_string('roleparamcomparedesc', 'vmoodleadminset_roles'), null, $rolemenu); // Creating command. parent::__construct($cmd_name, $cmd_desc, $role_param); }
protected function definition() { global $CFG, $DB; $collection = $this->_customdata['collection']; $context = $this->_customdata['context']; $mform = $this->_form; // Text search box. $mform->addElement('text', 'search', get_string('search')); $mform->setType('search', PARAM_TEXT); $options = array(\mod_mediagallery\base::TYPE_ALL => get_string('typeall', 'mediagallery'), \mod_mediagallery\base::TYPE_IMAGE => get_string('typeimage', 'mediagallery'), \mod_mediagallery\base::TYPE_VIDEO => get_string('typevideo', 'mediagallery'), \mod_mediagallery\base::TYPE_AUDIO => get_string('typeaudio', 'mediagallery')); $mform->addElement('select', 'type', get_string('mediatype', 'mediagallery'), $options); // Role select dropdown includes all roles, but using course-specific // names if applied. The reason for not restricting to roles that can // be assigned at course level is that upper-level roles display in the // enrolments table so it makes sense to let users filter by them. $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); if (!empty($CFG->showenrolledusersrolesonly)) { $rolenames = role_fix_names(get_roles_used_in_context($context)); } $mform->addElement('select', 'role', get_string('role'), array(0 => get_string('all')) + $rolenames); // Filter by group. $allgroups = groups_get_all_groups($collection->course); $groupsmenu[0] = get_string('allparticipants'); foreach ($allgroups as $gid => $unused) { $groupsmenu[$gid] = $allgroups[$gid]->name; } if (count($groupsmenu) > 1) { $mform->addElement('select', 'group', get_string('group'), $groupsmenu); } // Submit button does not use add_action_buttons because that adds // another fieldset which causes the CSS style to break in an unfixable // way due to fieldset quirks. $group = array(); $group[] = $mform->createElement('submit', 'submitbutton', get_string('filter')); $group[] = $mform->createElement('submit', 'resetbutton', get_string('reset')); $group[] = $mform->createElement('submit', 'exportbutton', get_string('exportascsv', 'mediagallery')); $mform->addGroup($group, 'buttons', '', ' ', false); $mform->addElement('hidden', 'id', $context->instanceid); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'action', 'search'); $mform->setType('action', PARAM_ALPHA); }
/** * Sets the parameters property of the extended class * * @param USER global object * @param CFG global object */ function set_parameters() { global $USER, $CFG; $site = get_site(); $littlecfg = new object(); // to avoid some notices later $littlecfg->wwwroot = $CFG->wwwroot; $courseparameters = array('label3' => array('langstr' => get_string('course'), 'value' => 'optgroup'), 'courseid' => array('langstr' => 'id', 'value' => $this->course->id), 'coursefullname' => array('langstr' => get_string('fullnamecourse'), 'value' => $this->course->fullname), 'courseshortname' => array('langstr' => get_string('shortnamecourse'), 'value' => $this->course->shortname), 'courseidnumber' => array('langstr' => get_string('idnumbercourse'), 'value' => $this->course->idnumber), 'coursesummary' => array('langstr' => get_string('summary'), 'value' => $this->course->summary), 'courseformat' => array('langstr' => get_string('format'), 'value' => $this->course->format)); $roles = get_all_roles(); $coursecontext = get_context_instance(CONTEXT_COURSE, $this->course->id); $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS); foreach ($roles as $role) { $courseparameters['course' . $role->shortname] = array('langstr' => get_string('yourwordforx', '', $role->name), 'value' => $role->localname); } $courseparameters['label4'] = array('langstr' => '', 'value' => '/optgroup'); $miscparameters = array('label5' => array('langstr' => get_string('miscellaneous'), 'value' => 'optgroup'), 'lang' => array('langstr' => get_string('preferredlanguage'), 'value' => current_language()), 'sitename' => array('langstr' => get_string('fullsitename'), 'value' => format_string($site->fullname)), 'serverurl' => array('langstr' => get_string('serverurl', 'resource', $littlecfg), 'value' => $littlecfg->wwwroot), 'currenttime' => array('langstr' => get_string('time'), 'value' => time()), 'encryptedcode' => array('langstr' => get_string('encryptedcode'), 'value' => $this->set_encrypted_parameter()), 'label6' => array('langstr' => "", 'value' => '/optgroup')); $userparameters = array(); if (!empty($USER->id)) { $userparameters = array('label1' => array('langstr' => get_string('user'), 'value' => 'optgroup'), 'userid' => array('langstr' => 'id', 'value' => $USER->id), 'userusername' => array('langstr' => get_string('username'), 'value' => $USER->username), 'useridnumber' => array('langstr' => get_string('idnumber'), 'value' => $USER->idnumber), 'userfirstname' => array('langstr' => get_string('firstname'), 'value' => $USER->firstname), 'userlastname' => array('langstr' => get_string('lastname'), 'value' => $USER->lastname), 'userfullname' => array('langstr' => get_string('fullnameuser'), 'value' => fullname($USER)), 'useremail' => array('langstr' => get_string('email'), 'value' => $USER->email), 'usericq' => array('langstr' => get_string('icqnumber'), 'value' => $USER->icq), 'userphone1' => array('langstr' => get_string('phone') . ' 1', 'value' => $USER->phone1), 'userphone2' => array('langstr' => get_string('phone2') . ' 2', 'value' => $USER->phone2), 'userinstitution' => array('langstr' => get_string('institution'), 'value' => $USER->institution), 'userdepartment' => array('langstr' => get_string('department'), 'value' => $USER->department), 'useraddress' => array('langstr' => get_string('address'), 'value' => $USER->address), 'usercity' => array('langstr' => get_string('city'), 'value' => $USER->city), 'usertimezone' => array('langstr' => get_string('timezone'), 'value' => get_user_timezone_offset()), 'userurl' => array('langstr' => get_string('webpage'), 'value' => $USER->url), 'label2' => array('langstr' => "", 'value' => '/optgroup')); } $this->parameters = array_merge($userparameters, $courseparameters, $miscparameters); }
public function definition() { $mform =& $this->_form; $category = $this->_customdata['category']; $mform->addElement('header', 'rolerenaming', get_string('rolerenaming')); $mform->addHelpButton('rolerenaming', 'rolerenaming'); if ($roles = get_all_roles()) { $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL); $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSECAT); foreach ($roles as $role) { $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname)); $mform->setType('role_' . $role->id, PARAM_TEXT); if (!in_array($role->id, $assignableroles)) { $mform->setAdvanced('role_' . $role->id); } } } $mform->addElement('hidden', 'id', $category->id); $mform->setType('id', PARAM_INT); $mform->addElement('html', get_string('roleedit_notice', 'local_contextadmin')); $this->add_action_buttons(true, get_string('savechanges')); }
/** * Constructor. * @throws Command_Exception. */ function __construct() { global $DB; // Getting command description. $cmd_name = vmoodle_get_string('cmdallowsyncname', 'vmoodleadminset_roles'); $cmd_desc = vmoodle_get_string('cmdallowsyncdesc', 'vmoodleadminset_roles'); // Creating platform parameter $platform_param = new Command_Parameter('platform', 'enum', vmoodle_get_string('platformparamsyncdesc', 'vmoodleadminset_roles'), null, get_available_platforms()); // Creating table parameter $tables['assign'] = vmoodle_get_string('assigntable', 'vmoodleadminset_roles'); $tables['override'] = vmoodle_get_string('overridetable', 'vmoodleadminset_roles'); $tables['switch'] = vmoodle_get_string('switchtable', 'vmoodleadminset_roles'); $table_param = new Command_Parameter('table', 'enum', vmoodle_get_string('tableparamdesc', 'vmoodleadminset_roles'), null, $tables); // Creating role parameter $roles = role_fix_names(get_all_roles(), \context_system::instance(), ROLENAME_ORIGINAL); $rolemenu = array(); foreach ($roles as $r) { $rolemenu[$r->shortname] = $r->localname; } $role_param = new Command_Parameter('role', 'enum', vmoodle_get_string('roleparamsyncdesc', 'vmoodleadminset_roles'), null, $rolemenu); // Creating command parent::__construct($cmd_name, $cmd_desc, array($platform_param, $table_param, $role_param)); }
// try a year if (strtotime('-1 year', $now) >= $minlog) { $timeoptions[strtotime('-1 year', $now)] = get_string('lastyear'); } $roleoptions = array(); // TODO: we need a new list of roles that are visible here if ($roles = get_roles_used_in_context($context)) { foreach ($roles as $r) { $roleoptions[$r->id] = $r->name; } } $guestrole = get_guest_role(); if (empty($roleoptions[$guestrole->id])) { $roleoptions[$guestrole->id] = $guestrole->name; } $roleoptions = role_fix_names($roleoptions, $context); // print first controls. echo '<form class="participationselectform" action="index.php" method="get"><div>' . "\n" . '<input type="hidden" name="id" value="' . $course->id . '" />' . "\n"; echo '<label for="menuinstanceid">' . get_string('activitymodule') . '</label>' . "\n"; echo html_writer::select($instanceoptions, 'instanceid', $instanceid); echo '<label for="menutimefrom">' . get_string('lookback') . '</label>' . "\n"; echo html_writer::select($timeoptions, 'timefrom', $timefrom); echo '<label for="menuroleid">' . get_string('showonly') . '</label>' . "\n"; echo html_writer::select($roleoptions, 'roleid', $roleid, false); echo '<label for="menuaction">' . get_string('showactions') . '</label>' . "\n"; echo html_writer::select($actionoptions, 'action', $action, false); echo '<input type="submit" value="' . get_string('go') . '" />' . "\n</div></form>\n"; $baseurl = $CFG->wwwroot . '/course/report/participation/index.php?id=' . $course->id . '&roleid=' . $roleid . '&instanceid=' . $instanceid . '&timefrom=' . $timefrom . '&action=' . $action . '&perpage=' . $perpage; if (!empty($instanceid) && !empty($roleid)) { // from here assume we have at least the module we're using. $cm = $modinfo->cms[$instanceid];
function definition() { global $USER, $CFG, $DB; $mform = $this->_form; $course = $this->_customdata['course']; // this contains the data of this form $category = $this->_customdata['category']; $editoroptions = $this->_customdata['editoroptions']; $returnto = $this->_customdata['returnto']; $systemcontext = get_context_instance(CONTEXT_SYSTEM); $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id); if (!empty($course->id)) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $context = $coursecontext; } else { $coursecontext = null; $context = $categorycontext; } $courseconfig = get_config('moodlecourse'); $this->course = $course; $this->context = $context; /// form definition with new course defaults //-------------------------------------------------------------------------------- $mform->addElement('header','general', get_string('general', 'form')); $mform->addElement('hidden', 'returnto', null); $mform->setType('returnto', PARAM_ALPHANUM); $mform->setConstant('returnto', $returnto); // verify permissions to change course category or keep current if (empty($course->id)) { if (has_capability('moodle/course:create', $categorycontext)) { $displaylist = array(); $parentlist = array(); make_categories_list($displaylist, $parentlist, 'moodle/course:create'); $mform->addElement('select', 'category', get_string('category'), $displaylist); $mform->addHelpButton('category', 'category'); $mform->setDefault('category', $category->id); } else { $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $category->id); } } else { if (has_capability('moodle/course:changecategory', $coursecontext)) { $displaylist = array(); $parentlist = array(); make_categories_list($displaylist, $parentlist, 'moodle/course:create'); if (!isset($displaylist[$course->category])) { //always keep current $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category))); } $mform->addElement('select', 'category', get_string('category'), $displaylist); $mform->addHelpButton('category', 'category'); } else { //keep current $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $course->category); } } $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"'); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_MULTILANG); if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) { $mform->hardFreeze('fullname'); $mform->setConstant('fullname', $course->fullname); } $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_MULTILANG); if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) { $mform->hardFreeze('shortname'); $mform->setConstant('shortname', $course->shortname); } $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"'); $mform->addHelpButton('idnumber', 'idnumbercourse'); $mform->setType('idnumber', PARAM_RAW); if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) { $mform->hardFreeze('idnumber'); $mform->setConstants('idnumber', $course->idnumber); } $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions); $mform->addHelpButton('summary_editor', 'coursesummary'); $mform->setType('summary_editor', PARAM_RAW); if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) { $mform->hardFreeze('summary_editor'); } $courseformats = get_plugin_list('format'); $formcourseformats = array(); foreach ($courseformats as $courseformat => $formatdir) { $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat"); } $mform->addElement('select', 'format', get_string('format'), $formcourseformats); $mform->addHelpButton('format', 'format'); $mform->setDefault('format', $courseconfig->format); for ($i=1; $i<=52; $i++) { $sectionmenu[$i] = "$i"; } $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu); $mform->setDefault('numsections', $courseconfig->numsections); $mform->addElement('date_selector', 'startdate', get_string('startdate')); $mform->addHelpButton('startdate', 'startdate'); $mform->setDefault('startdate', time() + 3600 * 24); $choices = array(); $choices['0'] = get_string('hiddensectionscollapsed'); $choices['1'] = get_string('hiddensectionsinvisible'); $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices); $mform->addHelpButton('hiddensections', 'hiddensections'); $mform->setDefault('hiddensections', $courseconfig->hiddensections); $options = range(0, 10); $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options); $mform->addHelpButton('newsitems', 'newsitemsnumber'); $mform->setDefault('newsitems', $courseconfig->newsitems); $mform->addElement('selectyesno', 'showgrades', get_string('showgrades')); $mform->addHelpButton('showgrades', 'showgrades'); $mform->setDefault('showgrades', $courseconfig->showgrades); $mform->addElement('selectyesno', 'showreports', get_string('showreports')); $mform->addHelpButton('showreports', 'showreports'); $mform->setDefault('showreports', $courseconfig->showreports); $choices = get_max_upload_sizes($CFG->maxbytes); $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices); $mform->addHelpButton('maxbytes', 'maximumupload'); $mform->setDefault('maxbytes', $courseconfig->maxbytes); if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) { if (empty($course->legacyfiles)) { //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet $choices = array('0'=>get_string('no'), '2'=>get_string('yes')); } else { $choices = array('1'=>get_string('no'), '2'=>get_string('yes')); } $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices); $mform->addHelpButton('legacyfiles', 'courselegacyfiles'); if (!isset($courseconfig->legacyfiles)) { // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses $courseconfig->legacyfiles = 0; } $mform->setDefault('legacyfiles', $courseconfig->legacyfiles); } if (!empty($CFG->allowcoursethemes)) { $themeobjects = get_list_of_themes(); $themes=array(); $themes[''] = get_string('forceno'); foreach ($themeobjects as $key=>$theme) { $themes[$key] = $theme->name; } $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); } //-------------------------------------------------------------------------------- enrol_course_edit_form($mform, $course, $context); //-------------------------------------------------------------------------------- $mform->addElement('header','', get_string('groups', 'group')); $choices = array(); $choices[NOGROUPS] = get_string('groupsnone', 'group'); $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group'); $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group'); $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices); $mform->addHelpButton('groupmode', 'groupmode', 'group'); $mform->setDefault('groupmode', $courseconfig->groupmode); $choices = array(); $choices['0'] = get_string('no'); $choices['1'] = get_string('yes'); $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices); $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group'); $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce); //default groupings selector $options = array(); $options[0] = get_string('none'); $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options); //-------------------------------------------------------------------------------- $mform->addElement('header','', get_string('availability')); $choices = array(); $choices['0'] = get_string('courseavailablenot'); $choices['1'] = get_string('courseavailable'); $mform->addElement('select', 'visible', get_string('availability'), $choices); $mform->addHelpButton('visible', 'availability'); $mform->setDefault('visible', $courseconfig->visible); if (!has_capability('moodle/course:visibility', $context)) { $mform->hardFreeze('visible'); if (!empty($course->id)) { $mform->setConstant('visible', $course->visible); } else { $mform->setConstant('visible', $category->visible); } } //-------------------------------------------------------------------------------- $mform->addElement('header','', get_string('language')); $languages=array(); $languages[''] = get_string('forceno'); $languages += get_string_manager()->get_list_of_translations(); $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages); $mform->setDefault('lang', $courseconfig->lang); //-------------------------------------------------------------------------------- if (completion_info::is_enabled_for_site()) { $mform->addElement('header','', get_string('progress','completion')); $mform->addElement('select', 'enablecompletion', get_string('completion','completion'), array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion'))); $mform->setDefault('enablecompletion', $courseconfig->enablecompletion); $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion')); $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol); $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0); } else { $mform->addElement('hidden', 'enablecompletion'); $mform->setType('enablecompletion', PARAM_INT); $mform->setDefault('enablecompletion',0); $mform->addElement('hidden', 'completionstartonenrol'); $mform->setType('completionstartonenrol', PARAM_INT); $mform->setDefault('completionstartonenrol',0); } //-------------------------------------------------------------------------------- if (has_capability('moodle/site:config', $systemcontext)) { if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) { $mform->addElement('header', '', get_string('restrictmodules')); $options = array(); $options['0'] = get_string('no'); $options['1'] = get_string('yes'); $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options); if (!empty($CFG->restrictbydefault)) { $mform->setDefault('restrictmodules', 1); } $mods = array(0=>get_string('allownone')); $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name'); $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10')); $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0); // defaults are already in $course } else { // remove any mod restriction $mform->addElement('hidden', 'restrictmodules', 0); $mform->setType('restrictmodules', PARAM_INT); } } else { $mform->addElement('hidden', 'restrictmodules'); $mform->setType('restrictmodules', PARAM_INT); if (empty($course->id)) { $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all')); } else { // keep previous $mform->setConstant('restrictmodules', $course->restrictmodules); } } /// customizable role names in this course //-------------------------------------------------------------------------------- $mform->addElement('header','rolerenaming', get_string('rolerenaming')); $mform->addHelpButton('rolerenaming', 'rolerenaming'); if ($roles = get_all_roles()) { if ($coursecontext) { $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW); } $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE); foreach ($roles as $role) { $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name)); if (isset($role->localname)) { $mform->setDefault('role_'.$role->id, $role->localname); } $mform->setType('role_'.$role->id, PARAM_TEXT); if (!in_array($role->id, $assignableroles)) { $mform->setAdvanced('role_'.$role->id); } } } //-------------------------------------------------------------------------------- $this->add_action_buttons(); //-------------------------------------------------------------------------------- $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); /// finally set the current form data //-------------------------------------------------------------------------------- $this->set_data($course); }
function definition() { global $USER, $CFG, $DB, $PAGE; $mform = $this->_form; $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser', array(array('formid' => $mform->getAttribute('id')))); $course = $this->_customdata['course']; // this contains the data of this form $category = $this->_customdata['category']; $editoroptions = $this->_customdata['editoroptions']; $returnto = $this->_customdata['returnto']; $systemcontext = context_system::instance(); $categorycontext = context_coursecat::instance($category->id); if (!empty($course->id)) { $coursecontext = context_course::instance($course->id); $context = $coursecontext; } else { $coursecontext = null; $context = $categorycontext; } $courseconfig = get_config('moodlecourse'); $this->course = $course; $this->context = $context; /// form definition with new course defaults //-------------------------------------------------------------------------------- $mform->addElement('header', 'general', get_string('general', 'form')); $mform->addElement('hidden', 'returnto', null); $mform->setType('returnto', PARAM_ALPHANUM); $mform->setConstant('returnto', $returnto); $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"'); $mform->addHelpButton('fullname', 'fullnamecourse'); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_TEXT); if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) { $mform->hardFreeze('fullname'); $mform->setConstant('fullname', $course->fullname); } $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); $mform->addHelpButton('shortname', 'shortnamecourse'); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_TEXT); if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) { $mform->hardFreeze('shortname'); $mform->setConstant('shortname', $course->shortname); } // Verify permissions to change course category or keep current. if (empty($course->id)) { if (has_capability('moodle/course:create', $categorycontext)) { $displaylist = coursecat::make_categories_list('moodle/course:create'); $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist); $mform->addHelpButton('category', 'coursecategory'); $mform->setDefault('category', $category->id); } else { $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $category->id); } } else { if (has_capability('moodle/course:changecategory', $coursecontext)) { $displaylist = coursecat::make_categories_list('moodle/course:create'); if (!isset($displaylist[$course->category])) { //always keep current $displaylist[$course->category] = coursecat::get($course->category)->get_formatted_name(); } $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist); $mform->addHelpButton('category', 'coursecategory'); } else { //keep current $mform->addElement('hidden', 'category', null); $mform->setType('category', PARAM_INT); $mform->setConstant('category', $course->category); } } $choices = array(); $choices['0'] = get_string('hide'); $choices['1'] = get_string('show'); $mform->addElement('select', 'visible', get_string('visible'), $choices); $mform->addHelpButton('visible', 'visible'); $mform->setDefault('visible', $courseconfig->visible); if (!has_capability('moodle/course:visibility', $context)) { $mform->hardFreeze('visible'); if (!empty($course->id)) { $mform->setConstant('visible', $course->visible); } else { $mform->setConstant('visible', $courseconfig->visible); } } $mform->addElement('date_selector', 'startdate', get_string('startdate')); $mform->addHelpButton('startdate', 'startdate'); $mform->setDefault('startdate', time() + 3600 * 24); $mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"'); $mform->addHelpButton('idnumber', 'idnumbercourse'); $mform->setType('idnumber', PARAM_RAW); if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) { $mform->hardFreeze('idnumber'); $mform->setConstants('idnumber', $course->idnumber); } // Description. $mform->addElement('header', 'descriptionhdr', get_string('description')); $mform->setExpanded('descriptionhdr'); $mform->addElement('editor', 'summary_editor', get_string('coursesummary'), null, $editoroptions); $mform->addHelpButton('summary_editor', 'coursesummary'); $mform->setType('summary_editor', PARAM_RAW); $summaryfields = 'summary_editor'; if ($overviewfilesoptions = course_overviewfiles_options($course)) { $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions); $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles'); $summaryfields .= ',overviewfiles_filemanager'; } if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) { // Remove the description header it does not contain anything any more. $mform->removeElement('descriptionhdr'); $mform->hardFreeze($summaryfields); } // Course format. $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin')); $courseformats = get_sorted_course_formats(true); $formcourseformats = array(); foreach ($courseformats as $courseformat) { $formcourseformats[$courseformat] = get_string('pluginname', "format_{$courseformat}"); } if (isset($course->format)) { $course->format = course_get_format($course)->get_format(); // replace with default if not found if (!in_array($course->format, $courseformats)) { // this format is disabled. Still display it in the dropdown $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle', get_string('pluginname', 'format_' . $course->format)); } } $mform->addElement('select', 'format', get_string('format'), $formcourseformats); $mform->addHelpButton('format', 'format'); $mform->setDefault('format', $courseconfig->format); // Button to update format-specific options on format change (will be hidden by JavaScript). $mform->registerNoSubmitButton('updatecourseformat'); $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate')); // Just a placeholder for the course format options. $mform->addElement('hidden', 'addcourseformatoptionshere'); $mform->setType('addcourseformatoptionshere', PARAM_BOOL); // Appearance. $mform->addElement('header', 'appearancehdr', get_string('appearance')); if (!empty($CFG->allowcoursethemes)) { $themeobjects = get_list_of_themes(); $themes = array(); $themes[''] = get_string('forceno'); foreach ($themeobjects as $key => $theme) { if (empty($theme->hidefromselector)) { $themes[$key] = get_string('pluginname', 'theme_' . $theme->name); } } $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); } $languages = array(); $languages[''] = get_string('forceno'); $languages += get_string_manager()->get_list_of_translations(); $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages); $mform->setDefault('lang', $courseconfig->lang); $options = range(0, 10); $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options); $mform->addHelpButton('newsitems', 'newsitemsnumber'); $mform->setDefault('newsitems', $courseconfig->newsitems); $mform->addElement('selectyesno', 'showgrades', get_string('showgrades')); $mform->addHelpButton('showgrades', 'showgrades'); $mform->setDefault('showgrades', $courseconfig->showgrades); $mform->addElement('selectyesno', 'showreports', get_string('showreports')); $mform->addHelpButton('showreports', 'showreports'); $mform->setDefault('showreports', $courseconfig->showreports); // Files and uploads. $mform->addElement('header', 'filehdr', get_string('filesanduploads')); if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) { if (empty($course->legacyfiles)) { //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet $choices = array('0' => get_string('no'), '2' => get_string('yes')); } else { $choices = array('1' => get_string('no'), '2' => get_string('yes')); } $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices); $mform->addHelpButton('legacyfiles', 'courselegacyfiles'); if (!isset($courseconfig->legacyfiles)) { // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses $courseconfig->legacyfiles = 0; } $mform->setDefault('legacyfiles', $courseconfig->legacyfiles); } // Handle non-existing $course->maxbytes on course creation. $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes; // Let's prepare the maxbytes popup. $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes); $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices); $mform->addHelpButton('maxbytes', 'maximumupload'); $mform->setDefault('maxbytes', $courseconfig->maxbytes); // Completion tracking. if (completion_info::is_enabled_for_site()) { $mform->addElement('header', 'completionhdr', get_string('completion', 'completion')); $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion')); $mform->setDefault('enablecompletion', $courseconfig->enablecompletion); $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion'); } else { $mform->addElement('hidden', 'enablecompletion'); $mform->setType('enablecompletion', PARAM_INT); $mform->setDefault('enablecompletion', 0); } //-------------------------------------------------------------------------------- enrol_course_edit_form($mform, $course, $context); //-------------------------------------------------------------------------------- $mform->addElement('header', 'groups', get_string('groups', 'group')); $choices = array(); $choices[NOGROUPS] = get_string('groupsnone', 'group'); $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group'); $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group'); $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices); $mform->addHelpButton('groupmode', 'groupmode', 'group'); $mform->setDefault('groupmode', $courseconfig->groupmode); $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group')); $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group'); $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce); //default groupings selector $options = array(); $options[0] = get_string('none'); $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options); //-------------------------------------------------------------------------------- /// customizable role names in this course //-------------------------------------------------------------------------------- $mform->addElement('header', 'rolerenaming', get_string('rolerenaming')); $mform->addHelpButton('rolerenaming', 'rolerenaming'); if ($roles = get_all_roles()) { $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL); $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE); foreach ($roles as $role) { $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname)); $mform->setType('role_' . $role->id, PARAM_TEXT); } } //-------------------------------------------------------------------------------- $this->add_action_buttons(); //-------------------------------------------------------------------------------- $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); /// finally set the current form data //-------------------------------------------------------------------------------- $this->set_data($course); }
/** * Gets all of the roles this course can contain. * * @return array */ public function get_all_roles() { if ($this->_roles === null) { $this->_roles = role_fix_names(get_all_roles($this->context), $this->context); } return $this->_roles; }
// Not needed anymore. unset($contextid); unset($courseid); require_login($course); $systemcontext = context_system::instance(); $isfrontpage = $course->id == SITEID; $frontpagectx = context_course::instance(SITEID); if ($isfrontpage) { $PAGE->set_pagelayout('admin'); require_capability('moodle/site:viewparticipants', $systemcontext); } else { $PAGE->set_pagelayout('incourse'); require_capability('moodle/course:viewparticipants', $context); } $rolenamesurl = new moodle_url("{$CFG->wwwroot}/user/index.php?contextid={$context->id}&sifirst=&silast="); $rolenames = role_fix_names(get_profile_roles($context), $context, ROLENAME_ALIAS, true); if ($isfrontpage) { $rolenames[0] = get_string('allsiteusers', 'role'); } else { $rolenames[0] = get_string('allparticipants'); } // Make sure other roles may not be selected by any means. if (empty($rolenames[$roleid])) { print_error('noparticipants'); } // No roles to display yet? // frontpage course is an exception, on the front page course we should display all users. if (empty($rolenames) && !$isfrontpage) { if (has_capability('moodle/role:assign', $context)) { redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $context->id); } else {
/** * Gets a list of roles that this user can override in this context. * * @param object $context the context. * @param int $rolenamedisplay the type of role name to display. One of the * ROLENAME_X constants. Default ROLENAME_ALIAS. * @param $withcounts if true, count the number of overrides that are set for each role. * @return array if $withcounts is false, then an array $roleid => $rolename. * if $withusercounts is true, returns a list of three arrays, * $rolenames, $rolecounts, and $nameswithcounts. */ function get_overridable_roles($context, $rolenamedisplay = ROLENAME_ALIAS, $withcounts = false) { global $USER, $DB; if (!has_any_capability(array('moodle/role:safeoverride', 'moodle/role:override'), $context)) { if ($withcounts) { return array(array(), array(), array()); } else { return array(); } } $parents = get_parent_contexts($context); $parents[] = $context->id; $contexts = implode(',', $parents); $params = array(); $extrafields = ''; if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT) { $extrafields .= ', ro.shortname'; } $params['userid'] = $USER->id; if ($withcounts) { $extrafields = ', (SELECT count(rc.id) FROM {role_capabilities} rc WHERE rc.roleid = ro.id AND rc.contextid = :conid) AS overridecount'; $params['conid'] = $context->id; } if (has_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM))) { // show all roles to admins $roles = $DB->get_records_sql("\n SELECT ro.id, ro.name{$extrafields}\n FROM {role} ro\n ORDER BY ro.sortorder ASC", $params); } else { $roles = $DB->get_records_sql("\n SELECT ro.id, ro.name{$extrafields}\n FROM {role} ro\n JOIN (SELECT DISTINCT r.id\n FROM {role} r\n JOIN {role_allow_override} rao ON r.id = rao.allowoverride\n JOIN {role_assignments} ra ON rao.roleid = ra.roleid\n WHERE ra.userid = :userid AND ra.contextid IN ({$contexts})\n ) inline_view ON ro.id = inline_view.id\n ORDER BY ro.sortorder ASC", $params); } $rolenames = array(); foreach ($roles as $role) { $rolenames[$role->id] = $role->name; if ($rolenamedisplay == ROLENAME_ORIGINALANDSHORT) { $rolenames[$role->id] .= ' (' . $role->shortname . ')'; } } if ($rolenamedisplay != ROLENAME_ORIGINALANDSHORT) { $rolenames = role_fix_names($rolenames, $context, $rolenamedisplay); } if (!$withcounts) { return $rolenames; } $rolecounts = array(); $nameswithcounts = array(); foreach ($roles as $role) { $nameswithcounts[$role->id] = $rolenames[$role->id] . ' (' . $roles[$role->id]->overridecount . ')'; $rolecounts[$role->id] = $roles[$role->id]->overridecount; } return array($rolenames, $rolecounts, $nameswithcounts); }
function definition() { global $USER, $CFG, $DB; $courseconfig = get_config('moodlecourse'); $mform =& $this->_form; $course = $this->_customdata['course']; $category = $this->_customdata['category']; $systemcontext = get_context_instance(CONTEXT_SYSTEM); $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id); $disable_meta = false; // basic meta course state protection; server-side security checks not needed if (!empty($course)) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); $context = $coursecontext; if (course_in_meta($course)) { $disable_meta = get_string('metaalreadyinmeta'); } else { if ($course->metacourse) { if ($DB->count_records('course_meta', array('parent_course' => $course->id)) > 0) { $disable_meta = get_string('metaalreadyhascourses'); } } else { // if users already enrolled directly into coures, do not allow switching to meta, // users with metacourse manage permission are exception // please note that we do not need exact results - anything unexpected here prevents metacourse $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id'); $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext); if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) { foreach ($users as $user) { if (!isset($managers[$user->id])) { $disable_meta = get_string('metaalreadyhasenrolments'); break; } } } unset($managers); unset($users); unset($enrolroles); } } } else { $coursecontext = null; $context = $categorycontext; } /// form definition with new course defaults //-------------------------------------------------------------------------------- $mform->addElement('header', 'general', get_string('general', 'form')); // Must have create course capability in both categories in order to move course if (has_capability('moodle/course:create', $categorycontext)) { $displaylist = array(); $parentlist = array(); make_categories_list($displaylist, $parentlist, 'moodle/course:create'); $mform->addElement('select', 'category', get_string('category'), $displaylist); } else { $mform->addElement('hidden', 'category', null); } $mform->setHelpButton('category', array('coursecategory', get_string('category'))); $mform->setDefault('category', $category->id); $mform->setType('category', PARAM_INT); $fullname = get_string('defaultcoursefullname'); $shortname = get_string('defaultcourseshortname'); while ($DB->record_exists('course', array('fullname' => $fullname)) or $DB->record_exists('course', array('fullname' => $fullname))) { $fullname++; $shortname++; } $mform->addElement('text', 'fullname', get_string('fullnamecourse'), 'maxlength="254" size="50"'); $mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true); $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); $mform->setType('fullname', PARAM_MULTILANG); if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) { $mform->hardFreeze('fullname'); $mform->setConstant('fullname', $course->fullname); } $mform->setDefault('fullname', $fullname); $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"'); $mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true); $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client'); $mform->setType('shortname', PARAM_MULTILANG); if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) { $mform->hardFreeze('shortname'); $mform->setConstant('shortname', $course->shortname); } $mform->setDefault('shortname', $shortname); $mform->addElement('text', 'idnumber', get_string('idnumbercourse'), 'maxlength="100" size="10"'); $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true); $mform->setType('idnumber', PARAM_RAW); if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) { $mform->hardFreeze('idnumber'); $mform->setConstants('idnumber', $course->idnumber); } $mform->addElement('htmleditor', 'summary', get_string('summary'), array('rows' => '10', 'cols' => '65')); $mform->setHelpButton('summary', array('text2', get_string('helptext')), true); $mform->setType('summary', PARAM_RAW); $courseformats = get_list_of_plugins('course/format'); $formcourseformats = array(); foreach ($courseformats as $courseformat) { $formcourseformats["{$courseformat}"] = get_string("format{$courseformat}", "format_{$courseformat}"); if ($formcourseformats["{$courseformat}"] == "[[format{$courseformat}]]") { $formcourseformats["{$courseformat}"] = get_string("format{$courseformat}"); } } $mform->addElement('select', 'format', get_string('format'), $formcourseformats); $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true); $mform->setDefault('format', $courseconfig->format); for ($i = 1; $i <= 52; $i++) { $sectionmenu[$i] = "{$i}"; } $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu); $mform->setDefault('numsections', $courseconfig->numsections); $mform->addElement('date_selector', 'startdate', get_string('startdate')); $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true); $mform->setDefault('startdate', time() + 3600 * 24); $choices = array(); $choices['0'] = get_string('hiddensectionscollapsed'); $choices['1'] = get_string('hiddensectionsinvisible'); $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices); $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true); $mform->setDefault('hiddensections', $courseconfig->hiddensections); $options = range(0, 10); $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options); $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true); $mform->setDefault('newsitems', $courseconfig->newsitems); $mform->addElement('selectyesno', 'showgrades', get_string('showgrades')); $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true); $mform->setDefault('showgrades', $courseconfig->showgrades); $mform->addElement('selectyesno', 'showreports', get_string('showreports')); $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true); $mform->setDefault('showreports', $courseconfig->showreports); $choices = get_max_upload_sizes($CFG->maxbytes); $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices); $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true); $mform->setDefault('maxbytes', $courseconfig->maxbytes); if (!empty($CFG->allowcoursethemes)) { $themes = array(); $themes[''] = get_string('forceno'); $themes += get_list_of_themes(); $mform->addElement('select', 'theme', get_string('forcetheme'), $themes); } $meta = array(); $meta[0] = get_string('no'); $meta[1] = get_string('yes'); if ($disable_meta === false) { $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta); $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true); $mform->setDefault('metacourse', $courseconfig->metacourse); } else { // no metacourse element - we do not want to change it anyway! $mform->addElement('static', 'nometacourse', get_string('managemeta'), (empty($course->metacourse) ? $meta[0] : $meta[1]) . " - {$disable_meta} "); $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true); } //-------------------------------------------------------------------------------- $mform->addElement('header', 'enrolhdr', get_string('enrolments')); $choices = array(); $modules = explode(',', $CFG->enrol_plugins_enabled); foreach ($modules as $module) { $name = get_string('enrolname', "enrol_{$module}"); $plugin = enrolment_factory::factory($module); if (method_exists($plugin, 'print_entry')) { $choices[$name] = $module; } } asort($choices); $choices = array_flip($choices); $choices = array_merge(array('' => get_string('sitedefault') . ' (' . get_string('enrolname', "enrol_{$CFG->enrol}") . ')'), $choices); $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices); $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true); $mform->setDefault('enrol', $courseconfig->enrol); $roles = get_assignable_roles($context); if (!empty($course)) { // add current default role, so that it is selectable even when user can not assign it if ($current_role = $DB->get_record('role', array('id' => $course->defaultrole))) { $roles[$current_role->id] = strip_tags(format_string($current_role->name, true)); } } $choices = array(); if ($sitedefaultrole = $DB->get_record('role', array('id' => $CFG->defaultcourseroleid))) { $choices[0] = get_string('sitedefault') . ' (' . $sitedefaultrole->name . ')'; } else { $choices[0] = get_string('sitedefault'); } $choices = $choices + $roles; // fix for MDL-9197 foreach ($choices as $choiceid => $choice) { $choices[$choiceid] = format_string($choice); } $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices); $mform->setDefault('defaultrole', 0); $radio = array(); $radio[] =& MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0); $radio[] =& MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1); $radio[] =& MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2); $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false); $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true); $mform->setDefault('enrollable', $courseconfig->enrollable); $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true)); $mform->setDefault('enrolstartdate', 0); $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2); $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true)); $mform->setDefault('enrolenddate', 0); $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2); $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400)); $mform->setDefault('enrolperiod', $courseconfig->enrolperiod); //-------------------------------------------------------------------------------- $mform->addElement('header', 'expirynotifyhdr', get_string('expirynotify')); $choices = array(); $choices['0'] = get_string('no'); $choices['1'] = get_string('yes'); $mform->addElement('select', 'expirynotify', get_string('notify'), $choices); $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true); $mform->setDefault('expirynotify', $courseconfig->expirynotify); $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices); $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true); $mform->setDefault('notifystudents', $courseconfig->notifystudents); $thresholdmenu = array(); for ($i = 1; $i <= 30; $i++) { $seconds = $i * 86400; $thresholdmenu[$seconds] = get_string('numdays', '', $i); } $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu); $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true); $mform->setDefault('expirythreshold', $courseconfig->expirythreshold); //-------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('groups', 'group')); $choices = array(); $choices[NOGROUPS] = get_string('groupsnone', 'group'); $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group'); $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group'); $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices); $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true); $mform->setDefault('groupmode', $courseconfig->groupmode); $choices = array(); $choices['0'] = get_string('no'); $choices['1'] = get_string('yes'); $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices); $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true); $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce); if (!empty($CFG->enablegroupings)) { //default groupings selector $options = array(); $options[0] = get_string('none'); $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options); } //-------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('availability')); $choices = array(); $choices['0'] = get_string('courseavailablenot'); $choices['1'] = get_string('courseavailable'); $mform->addElement('select', 'visible', get_string('availability'), $choices); $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true); $mform->setDefault('visible', $courseconfig->visible); if ($course and !has_capability('moodle/course:visibility', $coursecontext)) { $mform->hardFreeze('visible'); $mform->setConstant('visible', $course->visible); } $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"'); $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true); $mform->setDefault('enrolpassword', ''); $mform->setDefault('enrolpassword', $courseconfig->enrolpassword); $mform->setType('enrolpassword', PARAM_RAW); if (empty($course) or $course->password !== '' and $course->id != SITEID) { // do not require password in existing courses that do not have password yet - backwards compatibility ;-) if (!empty($CFG->enrol_manual_requirekey)) { $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client'); } } $choices = array(); $choices['0'] = get_string('guestsno'); $choices['1'] = get_string('guestsyes'); $choices['2'] = get_string('guestskey'); $mform->addElement('select', 'guest', get_string('opentoguests'), $choices); $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true); $mform->setDefault('guest', $courseconfig->guest); // If we are creating a course, its enrol method isn't yet chosen, BUT the site has a default enrol method which we can use here $enrol_object = $CFG; if (!empty($course)) { $enrol_object = $course; } // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || empty($enrol_object->enrol) && $CFG->enrol == 'manual')) { $costgroup = array(); $currencies = get_list_of_currencies(); $costgroup[] =& MoodleQuickForm::createElement('text', 'cost', '', 'maxlength="6" size="6"'); $costgroup[] =& MoodleQuickForm::createElement('select', 'currency', '', $currencies); $mform->addGroup($costgroup, 'costgrp', get_string('cost'), ' ', false); //defining a rule for a form element within a group : $costgrprules = array(); //set the message to null to tell Moodle to use a default message //available for most rules, fetched from language pack (err_{rulename}). $costgrprules['cost'][] = array(null, 'numeric', null, 'client'); $mform->addGroupRule('costgrp', $costgrprules); $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true); $mform->setDefault('cost', ''); $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency); } //-------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('language')); $languages = array(); $languages[''] = get_string('forceno'); $languages += get_list_of_languages(); $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages); $mform->setDefault('lang', $courseconfig->lang); //-------------------------------------------------------------------------------- require_once $CFG->libdir . '/completionlib.php'; if (completion_info::is_enabled_for_site()) { $mform->addElement('header', '', get_string('progress', 'completion')); $mform->addElement('select', 'enablecompletion', get_string('completion', 'completion'), array(0 => get_string('completiondisabled', 'completion'), 1 => get_string('completionenabled', 'completion'))); $mform->setDefault('enablecompletion', $courseconfig->enablecompletion); } else { $mform->addElement('hidden', 'enablecompletion'); $mform->setDefault('enablecompletion', 0); } //-------------------------------------------------------------------------------- if (has_capability('moodle/site:config', $systemcontext) && (!empty($course->requested) && $CFG->restrictmodulesfor == 'requested' || $CFG->restrictmodulesfor == 'all')) { $mform->addElement('header', '', get_string('restrictmodules')); $options = array(); $options['0'] = get_string('no'); $options['1'] = get_string('yes'); $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options); $mods = array(0 => get_string('allownone')); $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name'); $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple' => 'multiple', 'size' => '10')); $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0); } else { $mform->addElement('hidden', 'restrictmodules', null); } if ($CFG->restrictmodulesfor == 'all') { $mform->setDefault('allowedmods', explode(',', $CFG->defaultallowedmodules)); if (!empty($CFG->restrictbydefault)) { $mform->setDefault('restrictmodules', 1); } } $mform->setType('restrictmodules', PARAM_INT); /// customizable role names in this course //-------------------------------------------------------------------------------- $mform->addElement('header', 'rolerenaming', get_string('rolerenaming')); $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true); if ($roles = get_all_roles()) { if ($coursecontext) { $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW); } $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE); foreach ($roles as $role) { $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->name)); if (isset($role->localname)) { $mform->setDefault('role_' . $role->id, $role->localname); } $mform->setType('role_' . $role->id, PARAM_TEXT); if (!in_array($role->id, $assignableroles)) { $mform->setAdvanced('role_' . $role->id); } } } //-------------------------------------------------------------------------------- $this->add_action_buttons(); //-------------------------------------------------------------------------------- $mform->addElement('hidden', 'id', null); $mform->setType('id', PARAM_INT); }
/** * Returns array of parameters to be passed to legacy add_to_log() function. * * @return array */ protected function get_legacy_logdata() { $roles = get_all_roles(); $rolenames = role_fix_names($roles, $this->get_context(), ROLENAME_ORIGINAL, true); return array($this->courseid, 'role', 'unassign', 'admin/roles/assign.php?contextid=' . $this->contextid . '&roleid=' . $this->objectid, $rolenames[$this->objectid], '', $this->userid); }
/** * Returns an array of role names that have ALL of the the supplied capabilities * Uses get_roles_with_caps_in_context(). Returns $allowed minus $forbidden * * @param stdClass $context * @param array $capabilities An array of capabilities * @return array of roles with all of the required capabilities */ function get_role_names_with_caps_in_context($context, $capabilities) { global $DB; $rolesthatcanrate = get_roles_with_caps_in_context($context, $capabilities); $allroles = $DB->get_records('role', null, 'sortorder DESC'); $roles = array(); foreach ($rolesthatcanrate as $r) { $roles[$r] = $allroles[$r]; } return role_fix_names($roles, $context, ROLENAME_ALIAS, true); }
/** * Return array of trainer roles configured for face-to-face * @param $coursecontext context of the course * @return array */ function facetoface_get_trainer_roles($coursecontext) { global $CFG, $DB; // Check that roles have been selected if (empty($CFG->facetoface_session_roles)) { return false; } // Parse roles $cleanroles = clean_param($CFG->facetoface_session_roles, PARAM_SEQUENCE); list($rolesql, $params) = $DB->get_in_or_equal(explode(',', $cleanroles)); // Load role names $rolenames = $DB->get_records_sql(" SELECT r.id, r.name FROM {role} r WHERE r.id {$rolesql} AND r.id <> 0 ", $params); // Return roles and names if (!$rolenames) { return array(); } $rolenames = role_fix_names($rolenames, $coursecontext); return $rolenames; }
// (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * Block showing course roster * * @package block_roster_tbird * @copyright 2013 onwards Johan Reinalda (http://www.thunderbird.edu) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die; if ($ADMIN->fulltree) { // get all the global roles $choices = role_fix_names(get_all_roles(), null, ROLENAME_ORIGINAL, true); $default = array(); $default[5] = 1; //5 = Student // and then allow each role to be selected for showing in the roster reports. // note we store the variable globally (not block specific), so we can get to it from view.php // note that we do not use admin_setting_pickroles() since we cannot set defaults there. $settings->add(new admin_setting_configmulticheckbox('block_roster_tbird_rolestoshow', get_string('rolestoshow', 'block_roster_tbird'), get_string('rolestoshowdescription', 'block_roster_tbird'), $default, $choices)); $settings->add(new admin_setting_configtext('block_roster_tbird_picsperrow', get_string('picsperrow', 'block_roster_tbird'), get_string('picsperrowdescr', 'block_roster_tbird'), 5, PARAM_INT, 5)); $settings->add(new admin_setting_configtext('block_roster_tbird_picsize', get_string('picsize', 'block_roster_tbird'), get_string('picsizedescr', 'block_roster_tbird'), 200, PARAM_INT, 5)); $settings->add(new admin_setting_configcheckbox('block_roster_tbird_removelastaccessed', get_string('removelastaccessed', 'block_roster_tbird'), get_string('removelastaccesseddescr', 'block_roster_tbird'), 0)); }
/** * Get the parameter values that may be appended to URL * @param object $url module instance * @param object $cm * @param object $course * @param object $config module config options * @return array of parameter values */ function url_get_variable_values($url, $cm, $course, $config) { global $USER, $CFG; $site = get_site(); $coursecontext = context_course::instance($course->id); $values = array('courseid' => $course->id, 'coursefullname' => format_string($course->fullname), 'courseshortname' => format_string($course->shortname, true, array('context' => $coursecontext)), 'courseidnumber' => $course->idnumber, 'coursesummary' => $course->summary, 'courseformat' => $course->format, 'lang' => current_language(), 'sitename' => format_string($site->fullname), 'serverurl' => $CFG->wwwroot, 'currenttime' => time(), 'urlinstance' => $url->id, 'urlcmid' => $cm->id, 'urlname' => format_string($url->name), 'urlidnumber' => $cm->idnumber); if (isloggedin()) { $values['userid'] = $USER->id; $values['userusername'] = $USER->username; $values['useridnumber'] = $USER->idnumber; $values['userfirstname'] = $USER->firstname; $values['userlastname'] = $USER->lastname; $values['userfullname'] = fullname($USER); $values['useremail'] = $USER->email; $values['usericq'] = $USER->icq; $values['userphone1'] = $USER->phone1; $values['userphone2'] = $USER->phone2; $values['userinstitution'] = $USER->institution; $values['userdepartment'] = $USER->department; $values['useraddress'] = $USER->address; $values['usercity'] = $USER->city; $now = new DateTime('now', core_date::get_user_timezone_object()); $values['usertimezone'] = $now->getOffset() / 3600.0; // Value in hours for BC. $values['userurl'] = $USER->url; } // weak imitation of Single-Sign-On, for backwards compatibility only // NOTE: login hack is not included in 2.0 any more, new contrib auth plugin // needs to be createed if somebody needs the old functionality! if (!empty($config->secretphrase)) { $values['encryptedcode'] = url_get_encrypted_parameter($url, $config); } //hmm, this is pretty fragile and slow, why do we need it here?? if ($config->rolesinparams) { $coursecontext = context_course::instance($course->id); $roles = role_fix_names(get_all_roles($coursecontext), $coursecontext, ROLENAME_ALIAS); foreach ($roles as $role) { $values['course' . $role->shortname] = $role->localname; } } return $values; }
/** * Returns text to be displayed to the user which reflects their login status * * @uses $CFG * @uses $USER * @param course $course {@link $COURSE} object containing course information * @param user $user {@link $USER} object containing user information * @return string */ function user_login_string($course = NULL, $user = NULL) { global $USER, $CFG, $SITE; if (empty($user) and !empty($USER->id)) { $user = $USER; } if (empty($course)) { $course = $SITE; } if (!empty($user->realuser)) { if ($realuser = get_record('user', 'id', $user->realuser)) { $fullname = fullname($realuser, true); $realuserinfo = " [<a {$CFG->frametarget}\n href=\"{$CFG->wwwroot}/course/loginas.php?id={$course->id}&return=1&sesskey=" . sesskey() . "\">{$fullname}</a>] "; } } else { $realuserinfo = ''; } if (empty($CFG->loginhttps)) { $wwwroot = $CFG->wwwroot; } else { $wwwroot = str_replace('http:', 'https:', $CFG->wwwroot); } if (empty($course->id)) { // $course->id is not defined during installation return ''; } else { if (!empty($user->id)) { $context = get_context_instance(CONTEXT_COURSE, $course->id); $fullname = fullname($user, true); $username = "******"{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$course->id}\">{$fullname}</a>"; if (is_mnet_remote_user($user) and $idprovider = get_record('mnet_host', 'id', $user->mnethostid)) { $username .= " from <a {$CFG->frametarget} href=\"{$idprovider->wwwroot}\">{$idprovider->name}</a>"; } if (isset($user->username) && $user->username == 'guest') { $loggedinas = $realuserinfo . get_string('loggedinasguest') . " (<a {$CFG->frametarget} href=\"{$wwwroot}/login/index.php\">" . get_string('login') . '</a>)'; } else { if (!empty($user->access['rsw'][$context->path])) { $rolename = ''; if ($role = get_record('role', 'id', $user->access['rsw'][$context->path])) { $rolename = join("", role_fix_names(array($role->id => $role->name), $context)); $rolename = ': ' . format_string($rolename); } $loggedinas = get_string('loggedinas', 'moodle', $username) . $rolename . " (<a {$CFG->frametarget}\n href=\"{$CFG->wwwroot}/course/view.php?id={$course->id}&switchrole=0&sesskey=" . sesskey() . "\">" . get_string('switchrolereturn') . '</a>)'; } else { $loggedinas = $realuserinfo . get_string('loggedinas', 'moodle', $username) . ' ' . " (<a {$CFG->frametarget} href=\"{$CFG->wwwroot}/login/logout.php?sesskey=" . sesskey() . "\">" . get_string('logout') . '</a>)'; } } } else { $loggedinas = get_string('loggedinnot', 'moodle') . " (<a {$CFG->frametarget} href=\"{$wwwroot}/login/index.php\">" . get_string('login') . '</a>)'; } } return '<div class="logininfo">' . $loggedinas . '</div>'; }
/** * Load roles as choices * * @return bool true=>success, false=>error */ public function load_choices() { global $CFG, $DB; if (during_initial_install()) { return false; } if (is_array($this->choices)) { return true; } if ($roles = get_all_roles()) { $this->choices = role_fix_names($roles, null, ROLENAME_ORIGINAL, true); return true; } else { return false; } }
if (!empty($param->line3)) { $graph->y_order[] = 'line3'; $graph->y_format['line3'] = array('colour' => 'red','line' => 'line','legend' => $param->line3); } $graph->y_tick_labels = false; } else { $data = array(); $times = array(); $roles = array(); $missedlines = array(); $rolenames = get_all_roles(); foreach ($rolenames as $r) { $rolenames[$r->id] = $r->name; } $rolenames = role_fix_names($rolenames, get_context_instance(CONTEXT_COURSE, $course->id)); foreach ($stats as $stat) { $data[$stat->roleid][$stat->timeend] = $stat->line1; if (!empty($stat->zerofixed)) { $missedlines[] = $stat->timeend; } if ($stat->roleid != 0) { if (!array_key_exists($stat->roleid,$roles)) { $roles[$stat->roleid] = $rolenames[$stat->roleid]; } } else { if (!array_key_exists($stat->roleid,$roles)) { $roles[$stat->roleid] = get_string('all'); } } if (!array_key_exists($stat->timeend,$times)) {
$ADMIN->add('accounts', new admin_externalpage('cohorts', new lang_string('cohorts', 'cohort'), $CFG->wwwroot . '/cohort/index.php', array('moodle/cohort:manage', 'moodle/cohort:view'))); } // stuff under the "roles" subcategory // "userpolicies" settingpage $temp = new admin_settingpage('userpolicies', new lang_string('userpolicies', 'admin')); if ($ADMIN->fulltree) { if (!during_initial_install()) { $context = context_system::instance(); $otherroles = array(); $guestroles = array(); $userroles = array(); $creatornewroles = array(); $defaultteacherid = null; $defaultuserid = null; $defaultguestid = null; $roles = role_fix_names(get_all_roles(), null, ROLENAME_ORIGINALANDSHORT); foreach ($roles as $role) { $rolename = $role->localname; switch ($role->archetype) { case 'manager': $creatornewroles[$role->id] = $rolename; break; case 'coursecreator': break; case 'editingteacher': $defaultteacherid = isset($defaultteacherid) ? $defaultteacherid : $role->id; $creatornewroles[$role->id] = $rolename; break; case 'teacher': $creatornewroles[$role->id] = $rolename; break;
/** * Load information about all the roles we will need information about. */ protected function load_required_roles() { /// Get all roles $this->roles = get_all_roles(); role_fix_names($this->roles, get_context_instance(CONTEXT_SYSTEM), ROLENAME_ORIGINAL); }
/** * Returns the current setting if it is set * * @return mixed null if null, else an array */ public function get_setting() { $roles = role_fix_names(get_all_roles()); $result = array(); foreach ($roles as $role) { $contexts = $this->config_read('contexts_role' . $role->id); $memberattribute = $this->config_read('memberattribute_role' . $role->id); $result[] = array('id' => $role->id, 'name' => $role->localname, 'contexts' => $contexts, 'memberattribute' => $memberattribute); } return $result; }
/** * Test getting of all overridable roles. */ public function test_get_overridable_roles() { global $DB; $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); $coursecontext = context_course::instance($course->id); $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST); $teacher = $this->getDataGenerator()->create_user(); role_assign($teacherrole->id, $teacher->id, $coursecontext); $teacherename = (object) array('roleid' => $teacherrole->id, 'name' => 'Učitel', 'contextid' => $coursecontext->id); $DB->insert_record('role_names', $teacherename); $this->assertTrue($DB->record_exists('capabilities', array('name' => 'moodle/backup:backupcourse'))); // Any capability is ok. assign_capability('moodle/backup:backupcourse', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id); $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); $student = $this->getDataGenerator()->create_user(); role_assign($studentrole->id, $student->id, $coursecontext); $contexts = $DB->get_records('context'); $users = $DB->get_records('user'); $allroles = $DB->get_records('role'); // Evaluate all results for all users in all contexts. foreach ($users as $user) { $this->setUser($user); foreach ($contexts as $contextid => $unused) { $context = context_helper::instance_by_id($contextid); $roles = get_overridable_roles($context, ROLENAME_SHORT); foreach ($allroles as $roleid => $role) { $hascap = has_any_capability(array('moodle/role:safeoverride', 'moodle/role:override'), $context); if (is_siteadmin()) { $this->assertTrue(isset($roles[$roleid])); } else { $parents = $context->get_parent_context_ids(true); $pcontexts = implode(',', $parents); $allowed = $DB->record_exists_sql("SELECT r.id\n FROM {role} r\n JOIN {role_allow_override} rao ON r.id = rao.allowoverride\n JOIN {role_assignments} ra ON rao.roleid = ra.roleid\n WHERE ra.userid = :userid AND ra.contextid IN ({$pcontexts}) AND r.id = :roleid\n ", array('userid' => $user->id, 'roleid' => $roleid)); if (isset($roles[$roleid])) { $this->assertTrue($hascap); $this->assertTrue($allowed); } else { $this->assertFalse($hascap and $allowed); } } if (isset($roles[$roleid])) { $this->assertEquals($role->shortname, $roles[$roleid]); } } } } // Test parameter defaults. $this->setAdminUser(); $roles1 = get_overridable_roles($coursecontext); $roles2 = get_overridable_roles($coursecontext, ROLENAME_ALIAS, false); $this->assertEquals($roles2, $roles1); $alltypes = array(ROLENAME_ALIAS, ROLENAME_ALIAS_RAW, ROLENAME_BOTH, ROLENAME_ORIGINAL, ROLENAME_ORIGINALANDSHORT, ROLENAME_SHORT); foreach ($alltypes as $type) { $rolenames = role_fix_names($allroles, $coursecontext, $type); $roles = get_overridable_roles($coursecontext, $type, false); foreach ($roles as $roleid => $rolename) { $this->assertSame($rolenames[$roleid]->localname, $rolename); } } // Verify counts. $roles = get_overridable_roles($coursecontext, ROLENAME_ALIAS, false); list($rolenames, $rolecounts, $nameswithcounts) = get_overridable_roles($coursecontext, ROLENAME_ALIAS, true); $this->assertEquals($roles, $rolenames); foreach ($rolenames as $roleid => $name) { if ($roleid == $teacherrole->id) { $this->assertEquals(1, $rolecounts[$roleid]); } else { $this->assertEquals(0, $rolecounts[$roleid]); } $this->assertSame("{$name} ({$rolecounts[$roleid]})", $nameswithcounts[$roleid]); } }
/** * Returns an array of role names that have ALL of the the supplied capabilities * Uses get_roles_with_caps_in_context(). Returns $allowed minus $forbidden * * @param object $context * @param array $capabilities An array of capabilities * @return array of roles with all of the required capabilities */ function get_role_names_with_caps_in_context($context, $capabilities) { global $DB; $rolesthatcanrate = get_roles_with_caps_in_context($context, $capabilities); $allroles = array(); $roles = $DB->get_records('role', null, 'sortorder DESC'); foreach ($roles as $roleid => $role) { $allroles[$roleid] = $role->name; } $rolenames = array(); foreach ($rolesthatcanrate as $r) { $rolenames[$r] = $allroles[$r]; } $rolenames = role_fix_names($rolenames, $context); return $rolenames; }
/** * Load information about all the roles we will need information about. */ protected function load_required_roles() { /// Get all roles $this->roles = role_fix_names(get_all_roles(), context_system::instance(), ROLENAME_ORIGINAL); }