\ No newline at end of file
diff --git a/admin/roles/manage.php b/admin/roles/manage.php
index abdea578543..edde142bb4a 100755
--- a/admin/roles/manage.php
+++ b/admin/roles/manage.php
@@ -9,65 +9,143 @@
$roleid = optional_param('roleid', 0, PARAM_INT); // if set, we are editing a role
$name = optional_param('name', '', PARAM_MULTILANG); // new role name
- $shortname = optional_param('shortname', '', PARAM_SAFEDIR); // new role shortname
+ $shortname = optional_param('shortname', '', PARAM_RAW); // new role shortname, special cleaning before storage
$description = optional_param('description', '', PARAM_CLEAN); // new role desc
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
+ $cancel = optional_param('cancel', 0, PARAM_BOOL);
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
- $strmanageroles = get_string('manageroles');
- $strdelete = get_string('delete');
-
- if ($roleid && $action!='delete') {
- $role = get_record('role', 'id', $roleid);
- $editingstr = '-> '.get_string('editinga', '', $role->name);
- } else {
- $editingstr ='';
+ if ($cancel) {
+ redirect('manage.php');
}
- admin_externalpage_print_header($adminroot);
+ $errors = array();
+ $newrole = false;
- $currenttab = 'manage';
- include_once('managetabs.php');
+ $roles = get_records('role', '', '', 'sortorder ASC, id ASC');
+ $rolescount = count($roles);
- // form processing, editing a role, adding a role or deleting a role
- if ($action && confirm_sesskey()) {
+/// fix sort order if needed
+ $rolesort = array();
+ $i = 0;
+ foreach ($roles as $rolex) {
+ $rolesort[] = $rolex->id;
+ if ($rolex->sortorder != $i) {
+ $r = new object();
+ $r->id = $rolex->id;
+ $r->sortorder = $i;
+ update_record('role', $r);
+ $roles[$rolex->id]->sortorder = $i;
+ }
+ $i++;
+ }
- switch ($action) {
- case 'add':
- $newrole = create_role($name, $shortname, $description);
+/// form processing, editing a role, adding a role, deleting a role etc.
+ switch ($action) {
+ case 'add':
+ if ($data = data_submitted() and confirm_sesskey()) {
- $ignore = array('roleid', 'sesskey', 'action', 'name', 'description', 'contextid');
+ $shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR)); // only lowercase safe ASCII characters
- $data = data_submitted();
+ if (empty($name)) {
+ $errors['name'] = get_string('errorbadrolename', 'role');
+ } else if (count_records('role', 'name', $name)) {
+ $errors['name'] = get_string('errorexistsrolename', 'role');
+ }
+
+ if (empty($shortname)) {
+ $errors['shortname'] = get_string('errorbadroleshortname', 'role');
+ } else if (count_records('role', 'shortname', $shortname)) {
+ $errors['shortname'] = get_string('errorexistsroleshortname', 'role');
+ }
+
+ if (empty($errors)) {
+ $newrole = create_role($name, $shortname, $description, '', $rolescount);
+ } else {
+ $newrole = new object();
+ $newrole->name = $name;
+ $newrole->shortname = $shortname;
+ $newrole->description = $description;
+ }
+ $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT);
foreach ($data as $capname => $value) {
- if (in_array($capname, $ignore)) {
+ if (!preg_match('|^[a-z_]+/[a-z_]+:[a-z_]+$|', $capname)) {
+ continue;
+ }
+ $value = (int)$value;
+ if (!in_array($value, $allowed_values)) {
continue;
}
- assign_capability($capname, $value, $newrole, $sitecontext->id);
+ if (empty($errors)) {
+ assign_capability($capname, $value, $newrole, $sitecontext->id);
+ } else {
+ $newrole->$capname = $value;
+ }
+ }
+ if (empty($errors)) {
+ redirect('manage.php');
+ }
+ }
+ break;
+ case 'edit':
+ if ($data = data_submitted() and confirm_sesskey()) {
+
+ $shortname = moodle_strtolower(clean_param(clean_filename($shortname), PARAM_SAFEDIR)); // only lowercase safe ASCII characters
+
+ if (empty($name)) {
+ $errors['name'] = get_string('errorbadrolename', 'role');
+ } else {
+ if ($rs = get_records('role', 'name', $name)) {
+ unset($rs[$roleid]);
+ if (!empty($rs)) {
+ $errors['name'] = get_string('errorexistsrolename', 'role');
+ }
+ }
}
- break;
+ if (empty($shortname)) {
+ $errors['shortname'] = get_string('errorbadroleshortname', 'role');
+ } else {
+ if ($rs = get_records('role', 'shortname', $shortname)) {
+ unset($rs[$roleid]);
+ if (!empty($rs)) {
+ $errors['shortname'] = get_string('errorexistsroleshortname', 'role');
+ }
+ }
+ }
+ if (!empty($errors)) {
+ $newrole = new object();
+ $newrole->name = $name;
+ $newrole->shortname = $shortname;
+ $newrole->description = $description;
+ }
- case 'edit':
-
- $ignore = array('roleid', 'sesskey', 'action', 'name', 'description', 'contextid');
-
- $data = data_submitted();
+ $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT);
foreach ($data as $capname => $value) {
- if (in_array($capname, $ignore)) {
+ if (!preg_match('|^[a-z_]+/[a-z_]+:[a-z_]+$|', $capname)) {
+ continue;
+ }
+ $value = (int)$value;
+ if (!in_array($value, $allowed_values)) {
+ continue;
+ }
+
+ if (!empty($errors)) {
+ $newrole->$capname = $value;
continue;
}
// edit default caps
- $SQL = "select * from {$CFG->prefix}role_capabilities where
- roleid = $roleid and capability = '$capname' and contextid = $sitecontext->id";
+ $SQL = "SELECT * FROM {$CFG->prefix}role_capabilities
+ WHERE roleid = $roleid AND capability = '$capname'
+ AND contextid = $sitecontext->id";
$localoverride = get_record_sql($SQL);
@@ -88,74 +166,161 @@
// update normal role settings
- $role->id = $roleid;
- $role->name = $name;
- $role->description = $description;
+ if (empty($errors)) {
+ $role->id = $roleid;
+ $role->name = $name;
+ $role->description = $description;
- if (!update_record('role', $role)) {
- error('Could not update role!');
+ if (!update_record('role', $role)) {
+ error('Could not update role!');
+ }
+ redirect('manage.php');
+ }
+ }
+ break;
+
+ case 'delete':
+ if ($confirm and data_submitted() and confirm_sesskey()) {
+
+ // first unssign all users
+ if (!role_unassign($roleid)) {
+ error("Error while unassigning all users from role with ID $roleid!");
}
- break;
+ if (!delete_records('role', 'id', $roleid)) {
+ error("Could not delete role with ID $roleid!");
+ }
- case 'delete':
- if ($confirm) { // deletes a role
+ } else if (confirm_sesskey()){
+ // show confirmation
+ admin_externalpage_print_header($adminroot);
+ $optionsyes = new object();
+ $optionsyes->action = 'delete';
+ $optionsyes->roleid = $roleid;
+ $optionsyes->sesskey = sesskey();
+ $optionsyes->confirm = 1;
+ $a = new object();
+ $a->id = $roleid;
+ $a->name = $roles[$roleid]->name;
+ $a->shortname = $roles[$roleid]->shortname;
+ $a->count = (int)count_records('role_assignments', 'roleid', $roleid);
+ notice_yesno(get_string('deleterolesure', 'role', $a), 'manage.php', 'manage.php', $optionsyes, NULL, 'post', 'get');
+ admin_externalpage_print_footer($adminroot);
+ die;
+ }
- // check for depedencies XXX TODO
+ redirect('manage.php');
+ break;
- // delete all associated role-assignments? XXX TODO
+ case 'moveup':
+ if (array_key_exists($roleid, $roles) and confirm_sesskey()) {
+ $role = $roles[$roleid];
+ if ($role->sortorder > 0) {
+ $above = $roles[$rolesort[$role->sortorder - 1]];
+ $r = new object();
- if (!delete_records('role', 'id', $roleid)) {
- error('Could not delete role!');
+ $r->id = $role->id;
+ $r->sortorder = $above->sortorder;
+ if (!update_record('role', $r)) {
+ $errors[] = "Can not update role with ID $r->id!";
}
- } else {
- echo ('');
-
// this is the array holding capabilities of this role sorted till this context
$r_caps = role_context_capabilities($roleid, $sitecontext);
@@ -163,7 +328,19 @@
$capabilities = fetch_context_capabilities($sitecontext);
$usehtmleditor = can_use_html_editor();
- print_simple_box_start();
+
+ switch ($action) {
+ case 'add':
+ print_heading(get_string('addrole', 'role'));
+ break;
+ case 'view':
+ print_heading(get_string('viewrole', 'role'));
+ break;
+ case 'edit':
+ print_heading(get_string('editrole', 'role'));
+ break;
+ }
+ print_simple_box_start('center');
include_once('manage.html');
print_simple_box_end();
@@ -176,15 +353,17 @@
$table = new object;
$table->tablealign = 'center';
- $table->align = array('right', 'left', 'left');
+ $table->align = array('right', 'left', 'middle');
$table->wrap = array('nowrap', '', 'nowrap');
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->width = '90%';
+ $table->data = array();
- $table->head = array(get_string('roles', 'role'),
+ $table->head = array(get_string('name'),
get_string('description'),
- get_string('delete'));
+ get_string('shortname'),
+ get_string('edit'));
/*************************
* List all current roles *
@@ -192,15 +371,40 @@
foreach ($roles as $role) {
- $table->data[] = array(''.format_string($role->name).'', format_text($role->description, FORMAT_HTML), ''.$strdelete.'');
+ $stredit = get_string('edit');
+ $strdelete = get_string('delete');
+ $strmoveup = get_string('moveup');
+ $strmovedown = get_string('movedown');
+
+ $row = array();
+ $row[0] = ''.format_string($role->name).'';
+ $row[1] = format_text($role->description, FORMAT_HTML);
+ $row[2] = s($role->shortname);
+ $row[3] = ''.
+ ' ';
+ $row[3] .= ''.
+ ' ';
+ if ($role->sortorder != 0) {
+ $row[3] .= ''.
+ ' ';
+ } else {
+ $row[3] .= ' ';
+ }
+ if ($role->sortorder+1 < $rolescount) {
+ $row[3] .= ''.
+ ' ';
+ } else {
+ $row[3] .= ' ';
+ }
+
+ $table->data[] = $row;
}
print_table($table);
- $options = new object;
- $options->sesskey = sesskey();
- $options->action = 'new';
- print_single_button('manage.php', $options, get_string('addrole', 'role'), 'POST');
+ $options = new object();
+ $options->action = 'add';
+ print_single_button('manage.php', $options, get_string('addrole', 'role'), 'get');
}
admin_externalpage_print_footer($adminroot);
diff --git a/admin/roles/override.php b/admin/roles/override.php
index 9f7e097a1fc..d61952f305b 100755
--- a/admin/roles/override.php
+++ b/admin/roles/override.php
@@ -130,7 +130,7 @@
echo '';
}
echo ''.$strroletooverride.': ';
- choose_from_menu ($overridableroles, 'roleid', $roleid, get_string('listallroles', 'role'), $script='rolesform.submit()');
+ choose_from_menu ($overridableroles, 'roleid', $roleid, get_string('listallroles', 'role').'...', $script='rolesform.submit()');
echo '';
$parentcontexts = get_parent_contexts($context);
diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php
index 8fb85e013a1..14b063d0b57 100644
--- a/lang/en_utf8/role.php
+++ b/lang/en_utf8/role.php
@@ -11,9 +11,15 @@ $string['capabilities'] = 'Capabilities';
$string['currentrole'] = 'Current role';
$string['currentcontext'] = 'Current context';
$string['defineroles'] = 'Define roles';
+$string['deleterolesure'] = '
Are you sure, that you want to delete role \"$a->name ($a->shortname)\"?
Currently this role is assigned to $a->count users.
';
+$string['editrole'] = 'Edit role';
+$string['errorbadrolename'] = 'Incorrect role name';
+$string['errorexistsrolename'] = 'Role name already exists';
+$string['errorbadroleshortname'] = 'Incorrect role name';
+$string['errorexistsroleshortname'] = 'Role name already exists';
$string['existingusers'] = '$a existing users';
$string['inherit'] = 'Inherit';
-$string['listallroles'] = 'List all roles...';
+$string['listallroles'] = 'List all roles';
$string['manageroles'] = 'Manage roles';
$string['metaassignerror'] = 'Can not assign this role to user \"$a\" because Manage metacourse capability is needed.';
$string['metaunassignerror'] = 'Role of user \"$a\" was automatically reassigned, please unassign the role in child courses instead.';
@@ -26,13 +32,11 @@ $string['prevent'] = 'Prevent';
$string['prohibit'] = 'Prohibit';
$string['risks'] = 'Risks';
$string['roleassignments'] = 'Role assignments';
-$string['roledescription'] = 'Role description';
-$string['rolename'] = 'Role name';
-$string['roleshortname'] = 'Role short name (ASCII)';
$string['roletoassign'] = 'Role to assign';
$string['roletooverride'] = 'Role to override';
$string['roles'] = 'Roles';
$string['selectrole'] = 'Select a role';
+$string['viewrole'] = 'View role details';
$string['site:doanything'] = 'Allowed to do everything';
$string['legacy:guest'] = 'LEGACY ROLE: Guest';
@@ -100,4 +104,4 @@ $string['question:export'] = 'Export questions';
$string['question:managecategory'] = 'Manage question category';
$string['question:manage'] = 'Manage questions';
-?>
+?>
\ No newline at end of file
diff --git a/lib/accesslib.php b/lib/accesslib.php
index 407033a6525..2d1c6a254c8 100755
--- a/lib/accesslib.php
+++ b/lib/accesslib.php
@@ -1188,7 +1188,7 @@ function get_local_override($roleid, $contextid, $capability) {
* @param legacy - optional legacy capability
* @return id or false
*/
-function create_role($name, $shortname, $description, $legacy='') {
+function create_role($name, $shortname, $description, $legacy='', $sortorder = -1) {
// check for duplicate role name
@@ -1200,10 +1200,17 @@ function create_role($name, $shortname, $description, $legacy='') {
error('there is already a role with this shortname!');
}
+ $role = new object();
$role->name = $name;
$role->shortname = $shortname;
$role->description = $description;
+ if ($sortorder = -1) {
+ $role->sortorder = count_records('role');
+ } else {
+ $role->sortorder = $sortorder;
+ }
+
$context = get_context_instance(CONTEXT_SYSTEM, SITEID);
if ($id = insert_record('role', $role)) {
@@ -2626,21 +2633,21 @@ function get_user_capability_course($capability, $userid='') {
* @return array
*/
function get_roles_on_exact_context($context) {
-
+
global $CFG;
- return get_records_sql("SELECT DISTINCT r.*
+ return get_records_sql("SELECT DISTINCT r.*
FROM {$CFG->prefix}role_assignments ra,
{$CFG->prefix}role r
WHERE ra.roleid = r.id
AND ra.contextid = $context->id");
-
+
}
-/*
+/*
* Switches the current user to another role for the current session and only
- * in the given context. If roleid is not valid (eg 0) or the current user
- * doesn't have permissions to be switching roles then the user's session
+ * in the given context. If roleid is not valid (eg 0) or the current user
+ * doesn't have permissions to be switching roles then the user's session
* is compltely reset to have their normal roles.
* @param integer $roleid
* @param object $context
@@ -2652,7 +2659,7 @@ function role_switch($roleid, $context) {
global $db;
/// If we can't use this or are already using it or no role was specified then bail completely and reset
- if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
+ if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
|| !empty($USER->switchrole[$context->id]) || !confirm_sesskey()) {
load_user_capability('', $context); // Reset all permissions for this context to normal
unset($USER->switchrole[$context->id]); // Delete old capabilities
@@ -2695,9 +2702,9 @@ function role_switch($roleid, $context) {
// get any role that has an override on exact context
function get_roles_with_override_on_context($context) {
-
+
global $CFG;
-
+
return get_records_sql("SELECT DISTINCT r.*
FROM {$CFG->prefix}role_capabilities rc,
{$CFG->prefix}role r
@@ -2707,10 +2714,10 @@ function get_roles_with_override_on_context($context) {
// get all capabilities for this role on this context (overrids)
function get_capabilities_from_role_on_context($role, $context) {
-
+
global $CFG;
-
- return get_records_sql("SELECT *
+
+ return get_records_sql("SELECT *
FROM {$CFG->prefix}role_capabilities
WHERE contextid = $context->id
AND roleid = $role->id");
@@ -2719,13 +2726,13 @@ function get_capabilities_from_role_on_context($role, $context) {
/* find all user assignemnt of users for this role, on this context
*/
function get_users_from_role_on_context($role, $context) {
-
+
global $CFG;
-
+
return get_records_sql("SELECT *
FROM {$CFG->prefix}role_assignments
WHERE contextid = $context->id
- AND roleid = $role->id");
+ AND roleid = $role->id");
}
?>
diff --git a/lib/adminlib.php b/lib/adminlib.php
index 3dafb3114c7..7841dd0e27e 100644
--- a/lib/adminlib.php
+++ b/lib/adminlib.php
@@ -381,7 +381,6 @@ function upgrade_activity_modules($return) {
if ($updated_modules) {
print_continue($return);
- print_footer();
die;
}
}
diff --git a/lib/weblib.php b/lib/weblib.php
index a60ec085a04..13da6c8b2f9 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -4600,7 +4600,7 @@ function notice ($message, $link='') {
* @param string $linkyes The link to take the user to if they choose "Yes"
* @param string $linkno The link to take the user to if they choose "No"
*/
-function notice_yesno ($message, $linkyes, $linkno) {
+function notice_yesno ($message, $linkyes, $linkno, $optionsyes=NULL, $optionsno=NULL, $methodyes='post', $methodno='post') {
global $CFG;
@@ -4611,9 +4611,9 @@ function notice_yesno ($message, $linkyes, $linkno) {
print_simple_box_start('center', '60%', '', 5, 'generalbox', 'notice');
echo '