See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401] [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 declare(strict_types=1); 18 19 namespace core_reportbuilder\table; 20 21 use action_menu; 22 use action_menu_filler; 23 use core_table\local\filter\filterset; 24 use html_writer; 25 use moodle_exception; 26 use stdClass; 27 use core_reportbuilder\manager; 28 use core_reportbuilder\local\models\report; 29 use core_reportbuilder\local\report\action; 30 use core_reportbuilder\local\report\column; 31 32 defined('MOODLE_INTERNAL') || die; 33 34 /** 35 * System report dynamic table class 36 * 37 * @package core_reportbuilder 38 * @copyright 2020 Paul Holden <paulh@moodle.com> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class system_report_table extends base_report_table { 42 43 /** @var string Unique ID prefix for the table */ 44 private const UNIQUEID_PREFIX = 'system-report-table-'; 45 46 /** 47 * Table constructor. Note that the passed unique ID value must match the pattern "system-report-table-(\d+)" so that 48 * dynamic updates continue to load the same report 49 * 50 * @param string $uniqueid 51 * @param array $parameters 52 * @throws moodle_exception For invalid unique ID 53 */ 54 public function __construct(string $uniqueid, array $parameters = []) { 55 if (!preg_match('/^' . self::UNIQUEID_PREFIX . '(?<id>\d+)$/', $uniqueid, $matches)) { 56 throw new moodle_exception('invalidsystemreportid', 'core_reportbuilder', '', null, $uniqueid); 57 } 58 59 parent::__construct($uniqueid); 60 61 // If we are loading via a dynamic table AJAX request, defer the report loading until the filterset is added to 62 // the table, as it is used to populate the report $parameters during construction. 63 $serviceinfo = optional_param('info', null, PARAM_RAW); 64 if ($serviceinfo !== 'core_table_get_dynamic_table_content') { 65 $this->load_report_instance((int) $matches['id'], $parameters); 66 } 67 } 68 69 /** 70 * Load the report persistent, and accompanying system report instance. 71 * 72 * @param int $reportid 73 * @param array $parameters 74 */ 75 private function load_report_instance(int $reportid, array $parameters): void { 76 $this->persistent = new report($reportid); 77 $this->report = manager::get_report_from_persistent($this->persistent, $parameters); 78 79 $fields = $this->report->get_base_fields(); 80 $maintable = $this->report->get_main_table(); 81 $maintablealias = $this->report->get_main_table_alias(); 82 $joins = $this->report->get_joins(); 83 [$where, $params] = $this->report->get_base_condition(); 84 85 $this->set_attribute('data-region', 'reportbuilder-table'); 86 $this->set_attribute('class', $this->attributes['class'] . ' reportbuilder-table'); 87 88 // Download options. 89 $this->showdownloadbuttonsat = [TABLE_P_BOTTOM]; 90 $this->is_downloading($parameters['download'] ?? null, $this->report->get_downloadfilename()); 91 92 // Retrieve all report columns. If we are downloading the report, remove as required. 93 $columns = $this->report->get_columns(); 94 if ($this->is_downloading()) { 95 $columns = array_diff_key($columns, 96 array_flip($this->report->get_exclude_columns_for_download())); 97 } 98 99 $columnheaders = $columnsattributes = []; 100 $columnindex = 1; 101 foreach ($columns as $identifier => $column) { 102 $column->set_index($columnindex++); 103 104 $columnheaders[$column->get_column_alias()] = $column->get_title(); 105 106 // Specify whether column should behave as a user fullname column unless the column has a custom title set. 107 if (preg_match('/^user:fullname.*$/', $column->get_unique_identifier()) && !$column->has_custom_title()) { 108 $this->userfullnamecolumns[] = $column->get_column_alias(); 109 } 110 111 // Add each columns fields, joins and params to our report. 112 $fields = array_merge($fields, $column->get_fields()); 113 $joins = array_merge($joins, $column->get_joins()); 114 $params = array_merge($params, $column->get_params()); 115 116 // Disable sorting for some columns. 117 if (!$column->get_is_sortable()) { 118 $this->no_sorting($column->get_column_alias()); 119 } 120 121 // Generate column attributes to be included in each cell. 122 $columnsattributes[$column->get_column_alias()] = $column->get_attributes(); 123 } 124 125 // If the report has any actions then append appropriate column, note that actions are excluded during download. 126 if ($this->report->has_actions() && !$this->is_downloading()) { 127 $columnheaders['actions'] = html_writer::tag('span', get_string('actions', 'core_reportbuilder'), [ 128 'class' => 'sr-only', 129 ]); 130 $this->no_sorting('actions'); 131 } 132 133 $this->define_columns(array_keys($columnheaders)); 134 $this->define_headers(array_values($columnheaders)); 135 136 // Add column attributes to the table. 137 $this->set_columnsattributes($columnsattributes); 138 139 // Initial table sort column. 140 if ($sortcolumn = $this->report->get_initial_sort_column()) { 141 $this->sortable(true, $sortcolumn->get_column_alias(), $this->report->get_initial_sort_direction()); 142 } 143 144 // Table configuration. 145 $this->initialbars(false); 146 $this->collapsible(false); 147 $this->pageable(true); 148 $this->set_default_per_page($this->report->get_default_per_page()); 149 150 // Initialise table SQL properties. 151 $fieldsql = implode(', ', $fields); 152 $this->init_sql($fieldsql, "{{$maintable}} {$maintablealias}", $joins, $where, $params); 153 } 154 155 /** 156 * Return a new instance of the class for given report ID. We include report parameters here so they are present during 157 * initialisation 158 * 159 * @param int $reportid 160 * @param array $parameters 161 * @return static 162 */ 163 public static function create(int $reportid, array $parameters): self { 164 return new static(self::UNIQUEID_PREFIX . $reportid, $parameters); 165 } 166 167 /** 168 * Set the filterset in the table class. We set the report parameters here so that they are persisted while paging 169 * 170 * @param filterset $filterset 171 */ 172 public function set_filterset(filterset $filterset): void { 173 $reportid = $filterset->get_filter('reportid')->current(); 174 $parameters = $filterset->get_filter('parameters')->current(); 175 176 $this->load_report_instance($reportid, json_decode($parameters, true)); 177 178 parent::set_filterset($filterset); 179 } 180 181 /** 182 * Override parent method for retrieving row class with that defined by the system report 183 * 184 * @param array|stdClass $row 185 * @return string 186 */ 187 public function get_row_class($row) { 188 return $this->report->get_row_class((object) $row); 189 } 190 191 /** 192 * Format each row of returned data, executing defined callbacks for the row and each column 193 * 194 * @param array|stdClass $row 195 * @return array 196 */ 197 public function format_row($row) { 198 $this->report->row_callback((object) $row); 199 200 // Walk over the row, and for any key that matches one of our column aliases, call that columns format method. 201 $columnsbyalias = $this->report->get_active_columns_by_alias(); 202 $row = (array) $row; 203 array_walk($row, static function(&$value, $key) use ($columnsbyalias, $row): void { 204 if (array_key_exists($key, $columnsbyalias)) { 205 $value = $columnsbyalias[$key]->format_value($row); 206 } 207 }); 208 209 if ($this->report->has_actions()) { 210 $row['actions'] = $this->format_row_actions((object) $row); 211 } 212 213 return $row; 214 } 215 216 /** 217 * Return formatted actions column for the row 218 * 219 * @param stdClass $row 220 * @return string 221 */ 222 private function format_row_actions(stdClass $row): string { 223 global $OUTPUT; 224 225 $menu = new action_menu(); 226 $menu->set_menu_trigger($OUTPUT->pix_icon('a/setting', get_string('actions', 'core_reportbuilder'))); 227 228 $actions = array_filter($this->report->get_actions(), function($action) use ($row) { 229 // Only return dividers and action items who can be displayed for current users. 230 return $action instanceof action_menu_filler || $action->get_action_link($row); 231 }); 232 233 $totalactions = count($actions); 234 $actionvalues = array_values($actions); 235 foreach ($actionvalues as $position => $action) { 236 if ($action instanceof action_menu_filler) { 237 $ispreviousdivider = array_key_exists($position - 1, $actionvalues) && 238 ($actionvalues[$position - 1] instanceof action_menu_filler); 239 $isnextdivider = array_key_exists($position + 1, $actionvalues) && 240 ($actionvalues[$position + 1] instanceof action_menu_filler); 241 $isfirstdivider = ($position === 0); 242 $islastdivider = ($position === $totalactions - 1); 243 244 // Avoid add divider at last/first position and having multiple fillers in a row. 245 if ($ispreviousdivider || $isnextdivider || $isfirstdivider || $islastdivider) { 246 continue; 247 } 248 $actionlink = $action; 249 } else { 250 // Ensure the action link can be displayed for the current row. 251 $actionlink = $action->get_action_link($row); 252 } 253 254 if ($actionlink) { 255 $menu->add($actionlink); 256 } 257 } 258 return $OUTPUT->render($menu); 259 } 260 261 /** 262 * Get the html for the download buttons 263 * 264 * @return string 265 */ 266 public function download_buttons(): string { 267 global $OUTPUT; 268 269 if ($this->report->can_be_downloaded() && !$this->is_downloading()) { 270 return $OUTPUT->download_dataformat_selector( 271 get_string('downloadas', 'table'), 272 new \moodle_url('/reportbuilder/download.php'), 273 'download', 274 [ 275 'id' => $this->persistent->get('id'), 276 'parameters' => json_encode($this->report->get_parameters()), 277 ] 278 ); 279 } 280 281 return ''; 282 } 283 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body