Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  
  63          $context = context_system::instance();
  64          $this->load_current_settings();
  65  
  66          // Delete all records, then add back the ones that should be allowed.
  67          $DB->delete_records($this->tablename);
  68          foreach ($this->roles as $fromroleid => $notused) {
  69              foreach ($this->roles as $targetroleid => $alsonotused) {
  70                  $isallowed = $this->allowed[$fromroleid][$targetroleid];
  71                  if (optional_param('s_' . $fromroleid . '_' . $targetroleid, false, PARAM_BOOL)) {
  72                      $this->set_allow($fromroleid, $targetroleid);
  73                      // Only trigger events if this role allow relationship did not exist and the checkbox element
  74                      // has been submitted.
  75                      if (!$isallowed) {
  76                          $eventclass = $this->get_eventclass();
  77                          $eventclass::create([
  78                              'context' => $context,
  79                              'objectid' => $fromroleid,
  80                              'other' => ['targetroleid' => $targetroleid, 'allow' => true]
  81                          ])->trigger();
  82                      }
  83                  } else if ($isallowed) {
  84                      // When the user has deselect an existing role allow checkbox but it is in the list of roles
  85                      // allowances.
  86                      $eventclass = $this->get_eventclass();
  87                      $eventclass::create([
  88                          'context' => $context,
  89                          'objectid' => $fromroleid,
  90                          'other' => ['targetroleid' => $targetroleid, 'allow' => false]
  91                      ])->trigger();
  92                  }
  93              }
  94          }
  95      }
  96  
  97      /**
  98       * Set one allow in the database.
  99       * @param int $fromroleid
 100       * @param int $targetroleid
 101       */
 102      protected abstract function set_allow($fromroleid, $targetroleid);
 103  
 104      /**
 105       * Load the current allows from the database.
 106       */
 107      public function load_current_settings() {
 108          global $DB;
 109          // Load the current settings.
 110          $this->allowed = array();
 111          foreach ($this->roles as $role) {
 112              // Make an array $role->id => false. This is probably too clever for its own good.
 113              $this->allowed[$role->id] = array_combine(array_keys($this->roles), array_fill(0, count($this->roles), false));
 114          }
 115          $rs = $DB->get_recordset($this->tablename);
 116          foreach ($rs as $allow) {
 117              $this->allowed[$allow->roleid][$allow->{$this->targetcolname}] = true;
 118          }
 119          $rs->close();
 120      }
 121  
 122      /**
 123       * Is target allowed?
 124       *
 125       * @param integer $targetroleid a role id.
 126       * @return boolean whether the user should be allowed to select this role as a target role.
 127       */
 128      protected function is_allowed_target($targetroleid) {
 129          return true;
 130      }
 131  
 132      /**
 133       * Returns structure that can be passed to print_table,
 134       * containing one cell for each checkbox.
 135       * @return html_table a table
 136       */
 137      public function get_table() {
 138          $table = new html_table();
 139          $table->tablealign = 'center';
 140          $table->cellpadding = 5;
 141          $table->cellspacing = 0;
 142          $table->width = '90%';
 143          $table->align = array('left');
 144          $table->rotateheaders = true;
 145          $table->head = array('&#xa0;');
 146          $table->colclasses = array('');
 147  
 148          // Add role name headers.
 149          foreach ($this->roles as $targetrole) {
 150              $table->head[] = $targetrole->localname;
 151              $table->align[] = 'left';
 152              if ($this->is_allowed_target($targetrole->id)) {
 153                  $table->colclasses[] = '';
 154              } else {
 155                  $table->colclasses[] = 'dimmed_text';
 156              }
 157          }
 158  
 159          // Now the rest of the table.
 160          foreach ($this->roles as $fromrole) {
 161              $row = array($fromrole->localname);
 162              foreach ($this->roles as $targetrole) {
 163                  $checked = '';
 164                  $disabled = '';
 165                  if ($this->allowed[$fromrole->id][$targetrole->id]) {
 166                      $checked = 'checked="checked" ';
 167                  }
 168                  if (!$this->is_allowed_target($targetrole->id)) {
 169                      $disabled = 'disabled="disabled" ';
 170                  }
 171                  $name = 's_' . $fromrole->id . '_' . $targetrole->id;
 172                  $tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
 173                  $row[] = '<input type="checkbox" name="' . $name . '" id="' . $name .
 174                      '" title="' . $tooltip . '" value="1" ' . $checked . $disabled . '/>' .
 175                      '<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
 176              }
 177              $table->data[] = $row;
 178          }
 179  
 180          return $table;
 181      }
 182  
 183      /**
 184       * Snippet of text displayed above the table, telling the admin what to do.
 185       * @return string
 186       */
 187      public abstract function get_intro_text();
 188  
 189      /**
 190       * Returns the allow class respective event class name.
 191       * @return string
 192       */
 193      protected abstract function get_eventclass();
 194  }