| 1 | 
           efrain | 
           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 capability table.
  | 
        
        
            | 
            | 
           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 | 
            * This class represents a table with one row for each of a list of capabilities
  | 
        
        
            | 
            | 
           29 | 
            * where the first cell in the row contains the capability name, and there is
  | 
        
        
            | 
            | 
           30 | 
            * arbitrary stuff in the rest of the row. This class is used by
  | 
        
        
            | 
            | 
           31 | 
            * admin/roles/manage.php, override.php and check.php.
  | 
        
        
            | 
            | 
           32 | 
            *
  | 
        
        
            | 
            | 
           33 | 
            * An ajaxy search UI shown at the top, if JavaScript is on.
  | 
        
        
            | 
            | 
           34 | 
            */
  | 
        
        
            | 
            | 
           35 | 
           abstract class core_role_capability_table_base {
  | 
        
        
            | 
            | 
           36 | 
               /** The context this table relates to. */
  | 
        
        
            | 
            | 
           37 | 
               protected $context;
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               /** The capabilities to display. Initialised as $context->get_capabilities(). */
  | 
        
        
            | 
            | 
           40 | 
               protected $capabilities = array();
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /** Added as an id="" attribute to the table on output. */
  | 
        
        
            | 
            | 
           43 | 
               protected $id;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               /** Added to the class="" attribute on output. */
  | 
        
        
           | 1441 | 
           ariadna | 
           46 | 
               protected $classes = ['rolecap table w-auto table-hover'];
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /** Default number of capabilities in the table for the search UI to be shown. */
  | 
        
        
            | 
            | 
           49 | 
               const NUM_CAPS_FOR_SEARCH = 12;
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
               /**
  | 
        
        
            | 
            | 
           52 | 
                * Constructor.
  | 
        
        
            | 
            | 
           53 | 
                * @param context $context the context this table relates to.
  | 
        
        
            | 
            | 
           54 | 
                * @param string $id what to put in the id="" attribute.
  | 
        
        
            | 
            | 
           55 | 
                */
  | 
        
        
            | 
            | 
           56 | 
               public function __construct(context $context, $id) {
  | 
        
        
            | 
            | 
           57 | 
                   $this->context = $context;
  | 
        
        
            | 
            | 
           58 | 
                   $this->capabilities = $context->get_capabilities();
  | 
        
        
            | 
            | 
           59 | 
                   $this->id = $id;
  | 
        
        
            | 
            | 
           60 | 
               }
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
               /**
  | 
        
        
            | 
            | 
           63 | 
                * Use this to add class="" attributes to the table. You get the rolecap by
  | 
        
        
            | 
            | 
           64 | 
                * default.
  | 
        
        
            | 
            | 
           65 | 
                * @param array $classnames of class names.
  | 
        
        
            | 
            | 
           66 | 
                */
  | 
        
        
            | 
            | 
           67 | 
               public function add_classes($classnames) {
  | 
        
        
            | 
            | 
           68 | 
                   $this->classes = array_unique(array_merge($this->classes, $classnames));
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               /**
  | 
        
        
            | 
            | 
           72 | 
                * Display the table.
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
            | 
            | 
           74 | 
               public function display() {
  | 
        
        
            | 
            | 
           75 | 
                   if (count($this->capabilities) > self::NUM_CAPS_FOR_SEARCH) {
  | 
        
        
            | 
            | 
           76 | 
                       global $PAGE;
  | 
        
        
            | 
            | 
           77 | 
                       $jsmodule = array(
  | 
        
        
            | 
            | 
           78 | 
                           'name' => 'rolescapfilter',
  | 
        
        
            | 
            | 
           79 | 
                           'fullpath' => '/admin/roles/module.js',
  | 
        
        
            | 
            | 
           80 | 
                           'strings' => array(
  | 
        
        
            | 
            | 
           81 | 
                               array('filter', 'moodle'),
  | 
        
        
            | 
            | 
           82 | 
                               array('clear', 'moodle'),                ),
  | 
        
        
            | 
            | 
           83 | 
                           'requires' => array('node', 'cookie', 'escape')
  | 
        
        
            | 
            | 
           84 | 
                       );
  | 
        
        
            | 
            | 
           85 | 
                       $PAGE->requires->js_init_call('M.core_role.init_cap_table_filter', array($this->id, $this->context->id), false,
  | 
        
        
            | 
            | 
           86 | 
                           $jsmodule);
  | 
        
        
            | 
            | 
           87 | 
                   }
  | 
        
        
            | 
            | 
           88 | 
                   echo '<table class="' . implode(' ', $this->classes) . '" id="' . $this->id . '">' . "\n<thead>\n";
  | 
        
        
            | 
            | 
           89 | 
                   echo '<tr><th class="name" align="left" scope="col">' . get_string('capability', 'core_role') . '</th>';
  | 
        
        
            | 
            | 
           90 | 
                   $this->add_header_cells();
  | 
        
        
            | 
            | 
           91 | 
                   echo "</tr>\n</thead>\n<tbody>\n";
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   // Loop over capabilities.
  | 
        
        
            | 
            | 
           94 | 
                   $contextlevel = 0;
  | 
        
        
            | 
            | 
           95 | 
                   $component = '';
  | 
        
        
            | 
            | 
           96 | 
                   foreach ($this->capabilities as $capability) {
  | 
        
        
            | 
            | 
           97 | 
                       if ($this->skip_row($capability)) {
  | 
        
        
            | 
            | 
           98 | 
                           continue;
  | 
        
        
            | 
            | 
           99 | 
                       }
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                       // Prints a breaker if component or name or context level has changed.
  | 
        
        
            | 
            | 
           102 | 
                       if (component_level_changed($capability, $component, $contextlevel)) {
  | 
        
        
            | 
            | 
           103 | 
                           $this->print_heading_row($capability);
  | 
        
        
            | 
            | 
           104 | 
                       }
  | 
        
        
            | 
            | 
           105 | 
                       $contextlevel = $capability->contextlevel;
  | 
        
        
            | 
            | 
           106 | 
                       $component = $capability->component;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                       // Start the row.
  | 
        
        
            | 
            | 
           109 | 
                       $rowattributes = $this->get_row_attributes($capability);
  | 
        
        
            | 
            | 
           110 | 
                       // Handle class attributes same as other.
  | 
        
        
            | 
            | 
           111 | 
                       $rowclasses = array_unique(array_merge(array('rolecap'), $this->get_row_classes($capability)));
  | 
        
        
            | 
            | 
           112 | 
                       if (array_key_exists('class', $rowattributes)) {
  | 
        
        
            | 
            | 
           113 | 
                           $rowclasses = array_unique(array_merge($rowclasses, array($rowattributes['class'])));
  | 
        
        
            | 
            | 
           114 | 
                       }
  | 
        
        
            | 
            | 
           115 | 
                       $rowattributes['class']  = implode(' ', $rowclasses);
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                       // Table cell for the capability name.
  | 
        
        
            | 
            | 
           118 | 
                       $contents = '<th scope="row" class="name"><span class="cap-desc">' . get_capability_docs_link($capability) .
  | 
        
        
            | 
            | 
           119 | 
                           '<span class="cap-name">' . $capability->name . '</span></span></th>';
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                       // Add the cells specific to this table.
  | 
        
        
            | 
            | 
           122 | 
                       $contents .= $this->add_row_cells($capability);
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                       echo html_writer::tag('tr', $contents, $rowattributes);
  | 
        
        
            | 
            | 
           125 | 
                   }
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
                   // End of the table.
  | 
        
        
            | 
            | 
           128 | 
                   echo "</tbody>\n</table>\n";
  | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
               /**
  | 
        
        
            | 
            | 
           132 | 
                * Used to output a heading rows when the context level or component changes.
  | 
        
        
            | 
            | 
           133 | 
                * @param stdClass $capability gives the new component and contextlevel.
  | 
        
        
            | 
            | 
           134 | 
                */
  | 
        
        
            | 
            | 
           135 | 
               protected function print_heading_row($capability) {
  | 
        
        
            | 
            | 
           136 | 
                   echo '<tr class="rolecapheading header"><td colspan="' . (1 + $this->num_extra_columns()) . '" class="header"><strong>' .
  | 
        
        
            | 
            | 
           137 | 
                       get_component_string($capability->component, $capability->contextlevel) .
  | 
        
        
            | 
            | 
           138 | 
                       '</strong></td></tr>';
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
               }
  | 
        
        
            | 
            | 
           141 | 
              | 
        
        
            | 
            | 
           142 | 
               /**
  | 
        
        
            | 
            | 
           143 | 
                * For subclasses to override, output header cells, after the initial capability one.
  | 
        
        
            | 
            | 
           144 | 
                */
  | 
        
        
            | 
            | 
           145 | 
               abstract protected function add_header_cells();
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
               /**
  | 
        
        
            | 
            | 
           148 | 
                * For subclasses to override, return the number of cells that add_header_cells/add_row_cells output.
  | 
        
        
            | 
            | 
           149 | 
                */
  | 
        
        
            | 
            | 
           150 | 
               abstract protected function num_extra_columns();
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
               /**
  | 
        
        
            | 
            | 
           153 | 
                * For subclasses to override. Allows certain capabilties
  | 
        
        
            | 
            | 
           154 | 
                * to be left out of the table.
  | 
        
        
            | 
            | 
           155 | 
                *
  | 
        
        
            | 
            | 
           156 | 
                * @param object $capability the capability this row relates to.
  | 
        
        
            | 
            | 
           157 | 
                * @return boolean. If true, this row is omitted from the table.
  | 
        
        
            | 
            | 
           158 | 
                */
  | 
        
        
            | 
            | 
           159 | 
               protected function skip_row($capability) {
  | 
        
        
            | 
            | 
           160 | 
                   return false;
  | 
        
        
            | 
            | 
           161 | 
               }
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
               /**
  | 
        
        
            | 
            | 
           164 | 
                * For subclasses to override. A change to reaturn class names that are added
  | 
        
        
            | 
            | 
           165 | 
                * to the class="" attribute on the <tr> for this capability.
  | 
        
        
            | 
            | 
           166 | 
                *
  | 
        
        
            | 
            | 
           167 | 
                * @param stdClass $capability the capability this row relates to.
  | 
        
        
            | 
            | 
           168 | 
                * @return array of class name strings.
  | 
        
        
            | 
            | 
           169 | 
                */
  | 
        
        
            | 
            | 
           170 | 
               protected function get_row_classes($capability) {
  | 
        
        
            | 
            | 
           171 | 
                   return array();
  | 
        
        
            | 
            | 
           172 | 
               }
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
               /**
  | 
        
        
            | 
            | 
           175 | 
                * For subclasses to override. Additional attributes to be added to
  | 
        
        
            | 
            | 
           176 | 
                * each table row for the capability
  | 
        
        
            | 
            | 
           177 | 
                *
  | 
        
        
            | 
            | 
           178 | 
                * @param stdClass $capability the capability this row relates to.
  | 
        
        
            | 
            | 
           179 | 
                * @return array attribute names and their values.
  | 
        
        
            | 
            | 
           180 | 
                */
  | 
        
        
            | 
            | 
           181 | 
               protected function get_row_attributes($capability) {
  | 
        
        
            | 
            | 
           182 | 
                   return array();
  | 
        
        
            | 
            | 
           183 | 
               }
  | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
               /**
  | 
        
        
            | 
            | 
           186 | 
                * For subclasses to override. Output the data cells for this capability. The
  | 
        
        
            | 
            | 
           187 | 
                * capability name cell will already have been output.
  | 
        
        
            | 
            | 
           188 | 
                *
  | 
        
        
            | 
            | 
           189 | 
                * You can rely on get_row_classes always being called before add_row_cells.
  | 
        
        
            | 
            | 
           190 | 
                *
  | 
        
        
            | 
            | 
           191 | 
                * @param stdClass $capability the capability this row relates to.
  | 
        
        
            | 
            | 
           192 | 
                * @return string html of row cells
  | 
        
        
            | 
            | 
           193 | 
                */
  | 
        
        
            | 
            | 
           194 | 
               abstract protected function add_row_cells($capability);
  | 
        
        
            | 
            | 
           195 | 
           }
  |