[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/classes/ -> allow_role_page.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Base class for allow matrices.
  19   *
  20   * @package    core_role
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Base class for managing the data in the grid of checkboxes on the role allow
  29   * allow/overrides/switch editing pages (allow.php).
  30   */
  31  abstract class core_role_allow_role_page {
  32      protected $tablename;
  33      protected $targetcolname;
  34      protected $roles;
  35      protected $allowed = null;
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @param string $tablename the table where our data is stored.
  41       * @param string $targetcolname the name of the target role id column.
  42       */
  43      public function __construct($tablename, $targetcolname) {
  44          $this->tablename = $tablename;
  45          $this->targetcolname = $targetcolname;
  46          $this->load_required_roles();
  47      }
  48  
  49      /**
  50       * Load information about all the roles we will need information about.
  51       */
  52      protected function load_required_roles() {
  53          // Get all roles.
  54          $this->roles = role_fix_names(get_all_roles(), context_system::instance(), ROLENAME_ORIGINAL);
  55      }
  56  
  57      /**
  58       * Update the data with the new settings submitted by the user.
  59       */
  60      public function process_submission() {
  61          global $DB;
  62          // Delete all records, then add back the ones that should be allowed.
  63          $DB->delete_records($this->tablename);
  64          foreach ($this->roles as $fromroleid => $notused) {
  65              foreach ($this->roles as $targetroleid => $alsonotused) {
  66                  if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
  67                      $this->set_allow($fromroleid, $targetroleid);
  68                  }
  69              }
  70          }
  71      }
  72  
  73      /**
  74       * Set one allow in the database.
  75       * @param int $fromroleid
  76       * @param int $targetroleid
  77       */
  78      protected abstract function set_allow($fromroleid, $targetroleid);
  79  
  80      /**
  81       * Load the current allows from the database.
  82       */
  83      public function load_current_settings() {
  84          global $DB;
  85          // Load the current settings.
  86          $this->allowed = array();
  87          foreach ($this->roles as $role) {
  88              // Make an array $role->id => false. This is probably too clever for its own good.
  89              $this->allowed[$role->id] = array_combine(array_keys($this->roles), array_fill(0, count($this->roles), false));
  90          }
  91          $rs = $DB->get_recordset($this->tablename);
  92          foreach ($rs as $allow) {
  93              $this->allowed[$allow->roleid][$allow->{$this->targetcolname}] = true;
  94          }
  95          $rs->close();
  96      }
  97  
  98      /**
  99       * Is target allowed?
 100       *
 101       * @param integer $targetroleid a role id.
 102       * @return boolean whether the user should be allowed to select this role as a target role.
 103       */
 104      protected function is_allowed_target($targetroleid) {
 105          return true;
 106      }
 107  
 108      /**
 109       * Returns structure that can be passed to print_table,
 110       * containing one cell for each checkbox.
 111       * @return html_table a table
 112       */
 113      public function get_table() {
 114          $table = new html_table();
 115          $table->tablealign = 'center';
 116          $table->cellpadding = 5;
 117          $table->cellspacing = 0;
 118          $table->width = '90%';
 119          $table->align = array('left');
 120          $table->rotateheaders = true;
 121          $table->head = array('&#xa0;');
 122          $table->colclasses = array('');
 123  
 124          // Add role name headers.
 125          foreach ($this->roles as $targetrole) {
 126              $table->head[] = $targetrole->localname;
 127              $table->align[] = 'left';
 128              if ($this->is_allowed_target($targetrole->id)) {
 129                  $table->colclasses[] = '';
 130              } else {
 131                  $table->colclasses[] = 'dimmed_text';
 132              }
 133          }
 134  
 135          // Now the rest of the table.
 136          foreach ($this->roles as $fromrole) {
 137              $row = array($fromrole->localname);
 138              foreach ($this->roles as $targetrole) {
 139                  $checked = '';
 140                  $disabled = '';
 141                  if ($this->allowed[$fromrole->id][$targetrole->id]) {
 142                      $checked = 'checked="checked" ';
 143                  }
 144                  if (!$this->is_allowed_target($targetrole->id)) {
 145                      $disabled = 'disabled="disabled" ';
 146                  }
 147                  $name = 's_' . $fromrole->id . '_' . $targetrole->id;
 148                  $tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
 149                  $row[] = '<input type="checkbox" name="' . $name . '" id="' . $name .
 150                      '" title="' . $tooltip . '" value="1" ' . $checked . $disabled . '/>' .
 151                      '<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
 152              }
 153              $table->data[] = $row;
 154          }
 155  
 156          return $table;
 157      }
 158  
 159      /**
 160       * Snippet of text displayed above the table, telling the admin what to do.
 161       * @return string
 162       */
 163      public abstract function get_intro_text();
 164  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1