From: tjhunt Date: Wed, 25 Mar 2009 01:50:55 +0000 (+0000) Subject: switch roles: MDL-18132 Refactor role allow assign page X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9654643e8822a4aceda77f62da212777ff11c21b;p=moodle.git 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. --- diff --git a/admin/roles/allowassign.php b/admin/roles/allowassign.php index 81e6f83357..2620697507 100755 --- a/admin/roles/allowassign.php +++ b/admin/roles/allowassign.php @@ -35,86 +35,32 @@ * @package roles *//** */ - require_once('../../config.php'); - require_once($CFG->libdir.'/adminlib.php'); + require_once(dirname(__FILE__) . '/../../config.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(); - $systemcontext = get_context_instance(CONTEXT_SYSTEM); - require_capability('moodle/role:manage', $systemcontext); -/// Get all roles - $roles = get_all_roles(); - role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL); + $controller = new role_allow_assign_page(); + require_capability('moodle/role:manage', $controller->get_context()); -/// Process form submission if (optional_param('submit', false, PARAM_BOOL) && data_submitted() && confirm_sesskey()) { - /// Delete all records, then add back the ones that should be allowed. - $DB->delete_records('role_allow_assign'); - 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); + $controller->process_submission(); + mark_context_dirty($this->systemcontext->path); add_to_log(SITEID, 'role', 'edit allow assign', 'admin/roles/allowassign.php', '', '', $USER->id); redirect($CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php'); } -/// Load the 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; - } + $controller->load_current_settings(); /// Display the editing form. - admin_externalpage_setup('defineroles', '', array(), $CFG->wwwroot . '/' . $CFG->admin . '/roles/allowassign.php'); admin_externalpage_print_header(); $currenttab='allowassign'; require_once('managetabs.php'); - $table->tablealign = 'center'; - $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[] = '' . - ''; - } - $table->data[] = $row; - } + $table = $controller->get_table(); print_simple_box(get_string('configallowassign', 'admin'), 'center'); diff --git a/admin/roles/lib.php b/admin/roles/lib.php index 0b8ef62a69..a32ddba683 100644 --- a/admin/roles/lib.php +++ b/admin/roles/lib.php @@ -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(' '); + + /// 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[] = '' . + ''; + } + $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); + } +} + ?> \ No newline at end of file