| 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 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 |     abstract protected 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->head = array(' ');
 | 
        
           |  |  | 145 |         $table->colclasses = array('');
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         // Add role name headers.
 | 
        
           |  |  | 148 |         foreach ($this->roles as $targetrole) {
 | 
        
           |  |  | 149 |             $table->head[] = $targetrole->localname;
 | 
        
           |  |  | 150 |             $table->align[] = 'left';
 | 
        
           |  |  | 151 |             if ($this->is_allowed_target($targetrole->id)) {
 | 
        
           |  |  | 152 |                 $table->colclasses[] = '';
 | 
        
           |  |  | 153 |             } else {
 | 
        
           |  |  | 154 |                 $table->colclasses[] = 'dimmed_text';
 | 
        
           |  |  | 155 |             }
 | 
        
           |  |  | 156 |         }
 | 
        
           |  |  | 157 |   | 
        
           |  |  | 158 |         // Now the rest of the table.
 | 
        
           |  |  | 159 |         foreach ($this->roles as $fromrole) {
 | 
        
           |  |  | 160 |             $row = array($fromrole->localname);
 | 
        
           |  |  | 161 |             foreach ($this->roles as $targetrole) {
 | 
        
           |  |  | 162 |                 $checked = '';
 | 
        
           |  |  | 163 |                 $disabled = '';
 | 
        
           |  |  | 164 |                 if ($this->allowed[$fromrole->id][$targetrole->id]) {
 | 
        
           |  |  | 165 |                     $checked = 'checked="checked" ';
 | 
        
           |  |  | 166 |                 }
 | 
        
           |  |  | 167 |                 if (!$this->is_allowed_target($targetrole->id)) {
 | 
        
           |  |  | 168 |                     $disabled = 'disabled="disabled" ';
 | 
        
           |  |  | 169 |                 }
 | 
        
           |  |  | 170 |                 $name = 's_' . $fromrole->id . '_' . $targetrole->id;
 | 
        
           |  |  | 171 |                 $tooltip = $this->get_cell_tooltip($fromrole, $targetrole);
 | 
        
           |  |  | 172 |                 $row[] = '<input type="checkbox" name="' . $name . '" id="' . $name .
 | 
        
           |  |  | 173 |                     '" title="' . $tooltip . '" value="1" ' . $checked . $disabled . '/>' .
 | 
        
           |  |  | 174 |                     '<label for="' . $name . '" class="accesshide">' . $tooltip . '</label>';
 | 
        
           |  |  | 175 |             }
 | 
        
           |  |  | 176 |             $table->data[] = $row;
 | 
        
           |  |  | 177 |         }
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |         return $table;
 | 
        
           |  |  | 180 |     }
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 |     /**
 | 
        
           |  |  | 183 |      * Snippet of text displayed above the table, telling the admin what to do.
 | 
        
           |  |  | 184 |      * @return string
 | 
        
           |  |  | 185 |      */
 | 
        
           |  |  | 186 |     abstract public function get_intro_text();
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |     /**
 | 
        
           |  |  | 189 |      * Returns the allow class respective event class name.
 | 
        
           |  |  | 190 |      * @return string
 | 
        
           |  |  | 191 |      */
 | 
        
           |  |  | 192 |     abstract protected function get_eventclass();
 | 
        
           |  |  | 193 | }
 |