From: skodak Date: Fri, 22 Sep 2006 20:09:47 +0000 (+0000) Subject: role assign and override code fixes + other minor changes X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=305ef971fca44e78d00f223528c4bbbf37cec508;p=moodle.git role assign and override code fixes + other minor changes --- diff --git a/admin/roles/assign.html b/admin/roles/assign.html index b7c2fa09ed..8d63225313 100755 --- a/admin/roles/assign.html +++ b/admin/roles/assign.html @@ -1,11 +1,7 @@ -
- -'); -} -?> + + + @@ -17,7 +13,7 @@ if ($userid) { - + diff --git a/admin/roles/assign.php b/admin/roles/assign.php index bc8e751327..15c9b6052e 100755 --- a/admin/roles/assign.php +++ b/admin/roles/assign.php @@ -1,7 +1,7 @@ dirroot.'/mod/forum/lib.php'); define("MAX_USERS_PER_PAGE", 5000); @@ -22,6 +22,14 @@ $errors = array(); + $baseurl = 'assign.php?contextid='.$contextid; + if (!empty($userid)) { + $baseurl .= '&userid='.$userid; + } + if (!empty($courseid)) { + $baseurl .= '&courseid='.$courseid; + } + if (! $context = get_context_instance_by_id($contextid)) { error("Context ID was incorrect (can't find it)"); } @@ -81,16 +89,16 @@ if ($context->contextlevel == CONTEXT_USER) { /// course header - if ($courseid!= SITEID) { + if ($courseid != SITEID) { print_header("$fullname", "$fullname", - "id\">$course->shortname -> - wwwroot."/user/index.php?id=$course->id\">$strparticipants -> wwwroot."/user/view.php?id=".$userid."&course=".$courseid."\">$fullname ->".$straction, + "wwwroot/course/view.php?id=$course->id\">$course->shortname -> + wwwroot/user/index.php?id=$course->id\">$strparticipants -> wwwroot/user/view.php?id=$userid&course=$courseid\">$fullname ->".$straction, "", "", true, " ", navmenu($course)); /// site header } else { print_header("$course->fullname: $fullname", "$course->fullname", - "wwwroot."/user/view.php?id=".$userid."&course=".$courseid."\">$fullname -> $straction", "", "", true, " ", navmenu($course)); + "wwwroot/user/view.php?id=$userid&course=$courseid\">$fullname -> $straction", "", "", true, " ", navmenu($course)); } $showroles = 1; @@ -204,15 +212,18 @@ choose_from_menu ($assignableroles, 'roleid', $roleid, get_string('listallroles', 'role').'...', $script='rolesform.submit()'); echo '
'; - print_simple_box_start("center"); + print_simple_box_start('center'); include('assign.html'); print_simple_box_end(); if (!empty($errors)) { - print_simple_box_start("center"); - foreach ($errors as $error) { - notify($error); + $msg = '

'; + foreach ($errors as $e) { + $msg .= $e.'
'; } + $msg .= '

'; + print_simple_box_start('center'); + notify($msg); print_simple_box_end(); } @@ -222,7 +233,6 @@ if ($inmeta) { sync_metacourse($course); } - $userparam = (!empty($userid)) ? '&userid='.$userid : ''; $table->tablealign = 'center'; $table->cellpadding = 5; @@ -237,7 +247,7 @@ if ($contextusers = get_role_users($roleid, $context)) { $countusers = count($contextusers); } - $table->data[] = array(''.$rolename.'', $countusers); + $table->data[] = array(''.$rolename.'', $countusers); } print_table($table); diff --git a/admin/roles/manage.php b/admin/roles/manage.php index bdb61b5ec7..8b2733149a 100755 --- a/admin/roles/manage.php +++ b/admin/roles/manage.php @@ -70,13 +70,16 @@ $newrole->shortname = $shortname; $newrole->description = $description; } + $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT); + $capabilities = fetch_context_capabilities($sitecontext); // capabilities applicable in this context - foreach ($data as $capname => $value) { - if (!preg_match('|^[a-z_]+/[a-z_]+:[a-z_]+$|', $capname)) { + foreach ($capabilities as $cap) { + if (!isset($data->{$cap->name})) { continue; } - $value = clean_param($value, PARAM_INT); + $capname = $cap->name; + $value = clean_param($data->{$cap->name}, PARAM_INT); if (!in_array($value, $allowed_values)) { continue; } @@ -123,12 +126,14 @@ } $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT); + $capabilities = fetch_context_capabilities($sitecontext); // capabilities applicable in this context - foreach ($data as $capname => $value) { - if (!preg_match('|^[a-z_]+/[a-z_]+:[a-z_]+$|', $capname)) { + foreach ($capabilities as $cap) { + if (!isset($data->{$cap->name})) { continue; } - $value = clean_param($value, PARAM_INT); + $capname = $cap->name; + $value = clean_param($data->{$cap->name}, PARAM_INT); if (!in_array($value, $allowed_values)) { continue; } @@ -231,6 +236,7 @@ foreach ($errors as $e) { $msg .= $e.'
'; } + $msg .= '

'; admin_externalpage_print_header($adminroot); notify($msg); print_continue('manage.php'); diff --git a/admin/roles/override.html b/admin/roles/override.html index 106f5f2443..476d3f0acf 100755 --- a/admin/roles/override.html +++ b/admin/roles/override.html @@ -1,110 +1,109 @@ -
- - -'); -} -if ($courseid) { - print (''); -}?> - - - - - - - - - - + + + + + +
+ + + + + + + + + component != $component or $capability->contextlevel != $contextlevel) { - echo (''); - } + foreach ($capabilities as $capability) { + // prints a breaker if component or name or context level + if ($capability->component != $component or $capability->contextlevel != $contextlevel) { + echo (''); + } - // these 2 are used to see to group same mod/core capabilities together - $contextlevel = $capability->contextlevel; - $component = $capability->component; + // these 2 are used to see to group same mod/core capabilities together + $contextlevel = $capability->contextlevel; + $component = $capability->component; - // check the capability override for this cap, this role in this context - if (isset($localoverrides[$capability->name])) { - $localpermission = $localoverrides[$capability->name]->permission; - } else { - $localpermission = 0; // Just inherit - } + // check the capability override for this cap, this role in this context + if (isset($localoverrides[$capability->name])) { + $localpermission = $localoverrides[$capability->name]->permission; + } else { + $localpermission = 0; // Just inherit + } - if (isset($r_caps[$capability->name])) { - $isallow = $r_caps[$capability->name] > 0; - $isprevent = $r_caps[$capability->name] < 0 && $r_caps[$capability->name] > -500; - $isprohibit = $r_caps[$capability->name] <= -500; - } else { - $isallow = 0; - $isprevent = 0; - $isprohibit = 0; - } + if (isset($r_caps[$capability->name])) { + $isallow = $r_caps[$capability->name] > 0; + $isprevent = $r_caps[$capability->name] < 0 && $r_caps[$capability->name] > -500; + $isprohibit = $r_caps[$capability->name] <= -500; + } else { + $isallow = 0; + $isprevent = 0; + $isprohibit = 0; + } - $isdisabled = $isprohibit || islegacy($capability->name); + $isdisabled = $isprohibit || islegacy($capability->name); - ?> + ?> - - + + - + - + - + - - - + + + - -
'.get_component_string($capability->component, $capability->contextlevel).'
'.get_component_string($capability->component, $capability->contextlevel).'
name); ?>
name); ?> - /> - + /> + > - /> - > + /> + > - /> - > + /> + > - /> - riskbitmask) { - echo "T"; - } - if (RISK_CONFIG & (int)$capability->riskbitmask) { - echo "C"; - } - if (RISK_XSS & (int)$capability->riskbitmask) { - echo "X"; - } - if (RISK_PERSONAL & (int)$capability->riskbitmask) { - echo "P"; - } - if (RISK_SPAM & (int)$capability->riskbitmask) { - echo "S"; - } - ?>
> + /> + riskbitmask) { + echo "T"; + } + if (RISK_CONFIG & (int)$capability->riskbitmask) { + echo "C"; + } + if (RISK_XSS & (int)$capability->riskbitmask) { + echo "X"; + } + if (RISK_PERSONAL & (int)$capability->riskbitmask) { + echo "P"; + } + if (RISK_SPAM & (int)$capability->riskbitmask) { + echo "S"; + } + ?>
-
-
-
+ + +
+
+ + +
+ diff --git a/admin/roles/override.php b/admin/roles/override.php index b21be28d82..35434063fe 100755 --- a/admin/roles/override.php +++ b/admin/roles/override.php @@ -1,11 +1,12 @@ -id) { - error ('can not override base role capabilities'); + error ('Can not override base role capabilities'); + } + + if ($context->contextlevel == CONTEXT_COURSE) { + require_login($context->instanceid); + } else { + require_login(); + } + + $baseurl = 'override.php?contextid='.$contextid; + if (!empty($userid)) { + $baseurl .= '&userid='.$userid; + } + if (!empty($courseid)) { + $baseurl .= '&courseid='.$courseid; } + if ($cancel) { + redirect($baseurl); + } + + /// Get some language strings $strroletooverride = get_string('roletooverride', 'role'); - $stroverrideusers = get_string('overrideusers', 'role'); - $strpotentialusers = get_string('potentialusers', 'role'); - $strexistingusers = get_string('existingusers', 'role'); - $straction = get_string('overrideroles', 'role'); - $strcurrentrole = get_string('currentrole', 'role'); + $stroverrideusers = get_string('overrideusers', 'role'); + $straction = get_string('overrideroles', 'role'); + $strcurrentrole = get_string('currentrole', 'role'); $strcurrentcontext = get_string('currentcontext', 'role'); - $strsearch = get_string('search'); - $strshowall = get_string('showall'); - $strparticipants = get_string("participants"); - $straction = get_string('overrideroles', 'role'); - - $context = get_record('context', 'id', $contextid); - $overridableroles = get_overridable_roles($context); + $strparticipants = get_string('participants'); /// Make sure this user can override that role if ($roleid) { @@ -47,6 +60,58 @@ $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context)); } +/// Process incoming role override + if ($data = data_submitted() and confirm_sesskey()) { + $allowed_values = array(CAP_INHERIT, CAP_ALLOW, CAP_PREVENT, CAP_PROHIBIT); + $capabilities = fetch_context_capabilities($context); // capabilities applicable in this context + + $localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id", + '', 'capability, permission, id'); + + foreach ($capabilities as $cap) { + if (!isset($data->{$cap->name})) { + continue; + } + $capname = $cap->name; + $value = clean_param($data->{$cap->name}, PARAM_INT); + if (!in_array($value, $allowed_values)) { + continue; + } + + if (isset($localoverrides[$capname])) { // Something exists, so update it + if ($value == CAP_INHERIT) { // inherit = delete + delete_records('role_capabilities', 'roleid', $roleid, 'contextid', $contextid, + 'capability', $capname); + } else { + $localoverride = new object(); + $localoverride->id = $localoverrides[$capname]->id; + $localoverride->permission = $value; + $localoverride->timemodified = time(); + $localoverride->modifierid = $USER->id; + if (!update_record('role_capabilities', $localoverride)) { + error('Could not update a capability!'); + } + } + + } else { // insert a record + if ($value != CAP_INHERIT) { // Ignore inherits + $localoverride = new object(); + $localoverride->capability = $capname; + $localoverride->contextid = $contextid; + $localoverride->roleid = $roleid; + $localoverride->permission = $value; + $localoverride->timemodified = time(); + $localoverride->modifierid = $USER->id; + if (!insert_record('role_capabilities', $localoverride)) { + error('Could not insert a capability!'); + } + } + } + } + redirect($baseurl); + } + + /// Print the header and tabs if ($context->contextlevel == CONTEXT_USER) { @@ -54,14 +119,14 @@ /// course header if ($course->id != SITEID) { print_header("$fullname", "$fullname", - "id\">$course->shortname -> - wwwroot."/user/index.php?id=$course->id\">$strparticipants -> wwwroot."/user/view.php?id=".$userid."&course=".$course->id."\">$fullname -> $straction", + "wwwroot/course/view.php?id=$course->id\">$course->shortname -> + wwwroot/user/index.php?id=$course->id\">$strparticipants -> wwwroot/user/view.php?id=$userid&course=$course->id\">$fullname -> $straction", "", "", true, " ", navmenu($course)); /// site header } else { print_header("$course->fullname: $fullname", "$course->fullname", - "wwwroot."/user/view.php?id=".$userid."&course=".$course->id."\">$fullname -> $straction", "", "", true, " ", navmenu($course)); + "wwwroot/user/view.php?id=$userid&course=$course->id\">$fullname -> $straction", "", "", true, " ", navmenu($course)); } $showroles = 1; $currenttab = 'override'; @@ -73,55 +138,11 @@ } -/// Process incoming role override - if ($data = data_submitted()) { - - $localoverrides = get_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id", - '', 'capability, permission, id'); - - foreach ($data as $capname => $value) { - if ($capname == 'contextid' || $capname == 'roleid' || $capname == 'userid') { // ignore contextid and roleid - continue; - } - - if (isset($localoverrides[$capname])) { // Something exists, so update it - - if ($value == CAP_INHERIT) { // inherit = delete - delete_records('role_capabilities', 'roleid', $roleid, 'contextid', $contextid, - 'capability', $capname); - } else { - $localoverride = new object; - $localoverride->id = $localoverrides[$capname]->id; - $localoverride->permission = $value; - $localoverride->timemodified = time(); - $localoverride->modifierid = $USER->id; - - if (!update_record('role_capabilities', $localoverride)) { - debugging('Could not update a capability!'); - } - } - - } else { // insert a record - - if ($value != CAP_INHERIT) { // Ignore inherits - $override->capability = $capname; - $override->contextid = $contextid; - $override->roleid = $roleid; - $override->permission = $value; - $override->timemodified = time(); - $override->modifierid = $USER->id; - if (!insert_record('role_capabilities', $override)) { - debugging('Could not insert a capability!'); - } - } - } - } - } - + $overridableroles = get_overridable_roles($context); if ($roleid) { /// prints a form to swap roles - echo '
'; + echo ''; echo '
'.$strcurrentcontext.': '.print_context_name($context).'
'; if ($userid) { echo ''; @@ -148,18 +169,16 @@ // Get the capabilities overrideable in this context if ($capabilities = fetch_context_capabilities($context)) { - print_simple_box_start("center"); + print_simple_box_start('center'); include_once('override.html'); print_simple_box_end(); } else { notice(get_string('nocapabilitiesincontext', 'role'), - $CFG->wwwroot.'/'.$CFG->admin.'/roles/override.php?contextid='.$contextid); + $CFG->wwwroot.'/'.$CFG->admin.'/roles/'.$baseurl); } } else { // Print overview table - $userparam = (!empty($userid)) ? '&userid='.$userid : ''; - $table->tablealign = 'center'; $table->cellpadding = 5; $table->cellspacing = 0; @@ -171,7 +190,7 @@ foreach ($overridableroles as $roleid => $rolename) { $countusers = 0; $overridecount = count_records_select('role_capabilities', "roleid = $roleid AND contextid = $context->id"); - $table->data[] = array(''.$rolename.'', $overridecount); + $table->data[] = array(''.$rolename.'', $overridecount); } print_table($table);