switch roles: MDL-18132 Refactor role allow assign page

This is ready to eliminate the duplication between this and role allow override, and the soon-to-be-written role allow switch page.
This commit is contained in:
tjhunt 2009-03-25 01:50:55 +00:00
parent 2d9b3ef2d7
commit 9654643e88
2 changed files with 133 additions and 64 deletions

View file

@ -35,86 +35,32 @@
* @package roles * @package roles
*//** */ *//** */
require_once('../../config.php'); require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php'); require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
admin_externalpage_setup('defineroles', '', array(), $CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php');
require_login(); require_login();
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
require_capability('moodle/role:manage', $systemcontext);
/// Get all roles $controller = new role_allow_assign_page();
$roles = get_all_roles(); require_capability('moodle/role:manage', $controller->get_context());
role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
/// Process form submission
if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) { if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) {
/// Delete all records, then add back the ones that should be allowed. $controller->process_submission();
$DB->delete_records('role_allow_assign'); mark_context_dirty($this->systemcontext->path);
foreach ($roles as $fromroleid => $notused) {
foreach ($roles as $targetroleid => $alsonotused) {
if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
allow_assign($fromroleid, $targetroleid);
}
}
}
/// Updated allowassigns sitewide, so force a premissions refresh, and redirect.
mark_context_dirty($systemcontext->path);
add_to_log(SITEID, 'role', 'edit allow assign', 'admin/roles/allowassign.php', '', '', $USER->id); add_to_log(SITEID, 'role', 'edit allow assign', 'admin/roles/allowassign.php', '', '', $USER->id);
redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php'); redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php');
} }
/// Load the current settings $controller->load_current_settings();
$allowed = array();
foreach ($roles as $role) {
// Make an array $role->id => false. This is probalby too clever for its own good.1
$allowed[$role->id] = array_combine(array_keys($roles), array_fill(0, count($roles), false));
}
$raas = $DB->get_recordset('role_allow_assign');
foreach ($raas as $raa) {
$allowed[$raa->roleid][$raa->allowassign] = true;
}
/// Display the editing form. /// Display the editing form.
admin_externalpage_setup('defineroles', '', array(), $CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php');
admin_externalpage_print_header(); admin_externalpage_print_header();
$currenttab='allowassign'; $currenttab='allowassign';
require_once('managetabs.php'); require_once('managetabs.php');
$table->tablealign = 'center'; $table = $controller->get_table();
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->width = '90%';
$table->align[] = 'left';
$table->rotateheaders = true;
$table->head = array(' ');
/// Add role name headers.
foreach ($roles as $targetrole) {
$table->head[] = $targetrole->localname;
$table->align[] = 'left';
}
/// Now the rest of the table.
foreach ($roles as $fromrole) {
$row = array($fromrole->localname);
$a = new stdClass;
$a->fromrole = $fromrole->localname;
foreach ($roles as $targetrole) {
if ($allowed[$fromrole->id][$targetrole->id]) {
$checked = ' checked="checked"';
} else {
$checked = '';
}
$a->targetrole = $targetrole->localname;
$name = 's_' . $fromrole->id . '_' . $targetrole->id;
$tooltip = get_string('allowroletoassign', 'role', $a);
$row[] = '<input type="checkbox" name="' . $name . '" id="' . $name . '" title="' . $tooltip . '" value="1"' . $checked . ' />' .
'<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
}
$table->data[] = $row;
}
print_simple_box(get_string('configallowassign', 'admin'), 'center'); print_simple_box(get_string('configallowassign', 'admin'), 'center');

View file

@ -1250,4 +1250,127 @@ class existing_role_holders_site_admin extends existing_role_holders {
} }
} }
/**
* Base class to hold all the code shared between the role allow assign/override/switch
* pages.
*/
abstract class role_allow_role_page {
protected $tablename;
protected $targetcolname;
protected $systemcontext;
protected $roles;
protected $allowed = null;
public function __construct($tablename, $targetcolname) {
$this->tablename = $tablename;
$this->targetcolname = $targetcolname;
$this->systemcontext = get_context_instance(CONTEXT_SYSTEM);
$this->load_required_roles();
}
/**
* @return object the context we need. (The system context.)
*/
public function get_context() {
return $this->systemcontext;
}
/**
* Load 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, $this->systemcontext, ROLENAME_ORIGINAL);
}
/**
* Update the data with the new settings submitted by the user.
*/
public function process_submission() { /// Delete all records, then add back the ones that should be allowed.
$DB->delete_records($this->tablename);
foreach ($this->roles as $fromroleid => $notused) {
foreach ($this->roles as $targetroleid => $alsonotused) {
if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
$this->set_allow($fromroleid, $targetroleid);
}
}
}
}
/**
* Set one allow in the database.
* @param $fromroleid
* @param $targetroleid
*/
protected abstract function set_allow($fromroleid, $targetroleid);
public function load_current_settings() {
global $DB;
/// Load the current settings
$this->allowed = array();
foreach ($this->roles as $role) {
// Make an array $role->id => false. This is probalby too clever for its own good.
$this->allowed[$role->id] = array_combine(array_keys($this->roles), array_fill(0, count($this->roles), false));
}
$rs = $DB->get_recordset($this->tablename);
foreach ($rs as $allow) {
$this->allowed[$allow->roleid][$allow->{$this->targetcolname}] = true;
}
}
public function get_table() {
$table = new stdClass;
$table->tablealign = 'center';
$table->cellpadding = 5;
$table->cellspacing = 0;
$table->width = '90%';
$table->align[] = 'left';
$table->rotateheaders = true;
$table->head = array('&#xa0;');
/// Add role name headers.
foreach ($this->roles as $targetrole) {
$table->head[] = $targetrole->localname;
$table->align[] = 'left';
}
/// Now the rest of the table.
foreach ($this->roles as $fromrole) {
$row = array($fromrole->localname);
foreach ($this->roles as $targetrole) {
if ($this->allowed[$fromrole->id][$targetrole->id]) {
$checked = ' checked="checked"';
} else {
$checked = '';
}
$name = 's_' . $fromrole->id . '_' . $targetrole->id;
$tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
$row[] = '<input type="checkbox" name="' . $name . '" id="' . $name . '" title="' . $tooltip . '" value="1"' . $checked . ' />' .
'<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
}
$table->data[] = $row;
}
return $table;
}
}
class role_allow_assign_page extends role_allow_role_page {
public function __construct() {
parent::__construct('role_allow_assign', 'allowassign');
}
protected function set_allow($fromroleid, $targetroleid) {
allow_assign($fromroleid, $targetroleid);
}
protected function get_cell_tooltip($fromrole, $targetrole) {
$a = new stdClass;
$a->fromrole = $fromrole->localname;
$a->targetrole = $targetrole->localname;
return get_string('allowroletoassign', 'role', $a);
}
}
?> ?>