| 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 |  * Table class that cntains the list of memebrs in the selected group.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    block_dash
 | 
        
           |  |  | 21 |  * @copyright  2022 bdecent gmbh <https://bdecent.de>
 | 
        
           |  |  | 22 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | namespace block_dash\table;
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | defined('MOODLE_INTERNAL') || die('No direct access');
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | use html_writer;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | require_once($CFG->dirroot.'/lib/tablelib.php');
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | /**
 | 
        
           |  |  | 34 |  * List of group memebers table.
 | 
        
           |  |  | 35 |  */
 | 
        
           |  |  | 36 | class members_totara extends \table_sql {
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 |     /**
 | 
        
           |  |  | 39 |      * Define table field definitions and filter data
 | 
        
           |  |  | 40 |      *
 | 
        
           |  |  | 41 |      * @param int $pagesize
 | 
        
           |  |  | 42 |      * @param bool $useinitialsbar
 | 
        
           |  |  | 43 |      * @param string $downloadhelpbutton
 | 
        
           |  |  | 44 |      * @return void
 | 
        
           |  |  | 45 |      */
 | 
        
           |  |  | 46 |     public function out($pagesize, $useinitialsbar, $downloadhelpbutton = '') {
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         $this->set_tabledata();
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $columns = ['fullname', 'roles'];
 | 
        
           |  |  | 51 |         $headers = [
 | 
        
           |  |  | 52 |             get_string('fullname'),
 | 
        
           |  |  | 53 |             get_string('roles'),
 | 
        
           |  |  | 54 |         ];
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         $this->define_columns($columns);
 | 
        
           |  |  | 57 |         $this->define_headers($headers);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         if (isset($this->filterset) && $this->filterset->has_filter('group')) {
 | 
        
           |  |  | 60 |             $this->group = $this->filterset->get_filter('group')->get_filter_values();
 | 
        
           |  |  | 61 |             $this->group = current($this->group);
 | 
        
           |  |  | 62 |         }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         $this->collapsible(false);
 | 
        
           |  |  | 65 |         $this->no_sorting('roles');
 | 
        
           |  |  | 66 |         $this->guess_base_url();
 | 
        
           |  |  | 67 |         parent::out($pagesize, $useinitialsbar, $downloadhelpbutton);
 | 
        
           |  |  | 68 |     }
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |     /**
 | 
        
           |  |  | 71 |      * Set the html data attributes in the table tag for the dynamic paginations.
 | 
        
           |  |  | 72 |      *
 | 
        
           |  |  | 73 |      * @return void
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function set_tabledata() {
 | 
        
           |  |  | 76 |         $attrs = [
 | 
        
           |  |  | 77 |             'handler' => 'members_totara',
 | 
        
           |  |  | 78 |             'component' => 'block_dash',
 | 
        
           |  |  | 79 |             'uniqueid' => $this->uniqueid,
 | 
        
           |  |  | 80 |             'context' => $this->get_context()->id,
 | 
        
           |  |  | 81 |             'filter' => isset($this->group) ? $this->group : '',
 | 
        
           |  |  | 82 |         ];
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         foreach ($attrs as $key => $val) {
 | 
        
           |  |  | 85 |             $this->set_attribute('data-table-'.$key, $val);
 | 
        
           |  |  | 86 |         }
 | 
        
           |  |  | 87 |         $this->set_attribute('data-table-dynamic', 'true');
 | 
        
           |  |  | 88 |     }
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |     /**
 | 
        
           |  |  | 91 |      * Setup the filter data for pagination.
 | 
        
           |  |  | 92 |      *
 | 
        
           |  |  | 93 |      * @param int $group
 | 
        
           |  |  | 94 |      * @return void
 | 
        
           |  |  | 95 |      */
 | 
        
           |  |  | 96 |     public function set_filterset($group) {
 | 
        
           |  |  | 97 |         $this->group = $group;
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     /**
 | 
        
           |  |  | 101 |      * Set default sort column.
 | 
        
           |  |  | 102 |      *
 | 
        
           |  |  | 103 |      * @param string $field
 | 
        
           |  |  | 104 |      * @return void
 | 
        
           |  |  | 105 |      */
 | 
        
           |  |  | 106 |     public function set_sort_column($field) {
 | 
        
           |  |  | 107 |         $this->sort_default_column = $field;
 | 
        
           |  |  | 108 |     }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |     /**
 | 
        
           |  |  | 111 |      * Set the context of the current block.
 | 
        
           |  |  | 112 |      *
 | 
        
           |  |  | 113 |      * @return void
 | 
        
           |  |  | 114 |      */
 | 
        
           |  |  | 115 |     public function get_context(): \context {
 | 
        
           |  |  | 116 |         return \context_block::instance($this->uniqueid);
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |     /**
 | 
        
           |  |  | 121 |      * Set the base url of the table, used in the ajax data update.
 | 
        
           |  |  | 122 |      *
 | 
        
           |  |  | 123 |      * @return void
 | 
        
           |  |  | 124 |      */
 | 
        
           |  |  | 125 |     public function guess_base_url(): void {
 | 
        
           |  |  | 126 |         global $PAGE;
 | 
        
           |  |  | 127 |         $this->baseurl = $PAGE->url;
 | 
        
           |  |  | 128 |     }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |     /**
 | 
        
           |  |  | 131 |      * Set the sql query to fetch same user groups.
 | 
        
           |  |  | 132 |      *
 | 
        
           |  |  | 133 |      * @param int $pagesize
 | 
        
           |  |  | 134 |      * @param boolean $useinitialsbar
 | 
        
           |  |  | 135 |      * @return void
 | 
        
           |  |  | 136 |      */
 | 
        
           |  |  | 137 |     public function query_db($pagesize, $useinitialsbar = true) {
 | 
        
           |  |  | 138 |         global $USER;
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |         if (isset($this->currentpage) && !empty($this->currentpage)) {
 | 
        
           |  |  | 141 |             $this->currpage = $this->currentpage;
 | 
        
           |  |  | 142 |         }
 | 
        
           |  |  | 143 |         $select = '*';
 | 
        
           |  |  | 144 |         $from = ' {groups_members} gm JOIN {groups} g ON g.id = gm.groupid
 | 
        
           |  |  | 145 |         JOIN {user} u ON u.id = gm.userid ';
 | 
        
           |  |  | 146 |         $where = ' gm.userid != :userid AND g.id = :groupid AND g.id IN (
 | 
        
           |  |  | 147 |             SELECT groupid FROM {groups_members} WHERE userid = :currentuser
 | 
        
           |  |  | 148 |         )';
 | 
        
           |  |  | 149 |         $this->set_sql($select, $from, $where, ['userid' => $USER->id, 'groupid' => $this->group, 'currentuser' => $USER->id]);
 | 
        
           |  |  | 150 |         parent::query_db($pagesize, false);
 | 
        
           |  |  | 151 |     }
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |     /**
 | 
        
           |  |  | 154 |      * Generate the fullname column.
 | 
        
           |  |  | 155 |      *
 | 
        
           |  |  | 156 |      * @param \stdClass $row
 | 
        
           |  |  | 157 |      * @return string
 | 
        
           |  |  | 158 |      */
 | 
        
           |  |  | 159 |     public function col_fullname($row) {
 | 
        
           |  |  | 160 |         global $OUTPUT;
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 |         return $OUTPUT->user_picture($row, ['size' => 35, 'courseid' => $row->courseid]).' '.fullname($row);
 | 
        
           |  |  | 163 |     }
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |     /**
 | 
        
           |  |  | 166 |      * User lastaccess to the group in the user readable format.
 | 
        
           |  |  | 167 |      *
 | 
        
           |  |  | 168 |      * @param stdclass $row
 | 
        
           |  |  | 169 |      * @return string
 | 
        
           |  |  | 170 |      */
 | 
        
           |  |  | 171 |     public function col_roles($row) {
 | 
        
           |  |  | 172 |         return get_user_roles_in_course($row->userid, $row->courseid);
 | 
        
           |  |  | 173 |     }
 | 
        
           |  |  | 174 | }
 |