Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 namespace core_table\external\dynamic; 18 19 use core_external\external_api; 20 use core_external\external_function_parameters; 21 use core_external\external_multiple_structure; 22 use core_external\external_single_structure; 23 use core_external\external_value; 24 use core_external\external_warnings; 25 26 /** 27 * Core table external functions. 28 * 29 * @package core_table 30 * @category external 31 * @copyright 2020 Simey Lameze <simey@moodle.com> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class get extends external_api { 35 36 /** 37 * Describes the parameters for fetching the table html. 38 * 39 * @return external_function_parameters 40 * @since Moodle 3.9 41 */ 42 public static function execute_parameters(): external_function_parameters { 43 return new external_function_parameters ([ 44 'component' => new external_value( 45 PARAM_COMPONENT, 46 'Component', 47 VALUE_REQUIRED 48 ), 49 'handler' => new external_value( 50 // Note: We do not have a PARAM_CLASSNAME which would have been ideal. 51 PARAM_ALPHANUMEXT, 52 'Handler', 53 VALUE_REQUIRED 54 ), 55 'uniqueid' => new external_value( 56 PARAM_ALPHANUMEXT, 57 'Unique ID for the container', 58 VALUE_REQUIRED 59 ), 60 'sortdata' => new external_multiple_structure( 61 new external_single_structure([ 62 'sortby' => new external_value( 63 PARAM_ALPHANUMEXT, 64 'The name of a sortable column', 65 VALUE_REQUIRED 66 ), 67 'sortorder' => new external_value( 68 PARAM_ALPHANUMEXT, 69 'The direction that this column should be sorted by', 70 VALUE_REQUIRED 71 ), 72 ]), 73 'The combined sort order of the table. Multiple fields can be specified.', 74 VALUE_OPTIONAL, 75 [] 76 ), 77 'filters' => new external_multiple_structure( 78 new external_single_structure([ 79 'name' => new external_value(PARAM_ALPHANUM, 'Name of the filter', VALUE_REQUIRED), 80 'jointype' => new external_value(PARAM_INT, 'Type of join for filter values', VALUE_REQUIRED), 81 'values' => new external_multiple_structure( 82 new external_value(PARAM_RAW, 'Filter value'), 83 'The value to filter on', 84 VALUE_REQUIRED 85 ), 86 'filteroptions' => new external_multiple_structure( 87 new external_single_structure([ 88 'name' => new external_value(PARAM_ALPHANUM, 'Name of the filter option', VALUE_REQUIRED), 89 'value' => new external_value(PARAM_RAW, 'Value of the filter option', VALUE_REQUIRED), 90 ]), 91 'Additional options for this filter', 92 VALUE_OPTIONAL, 93 ), 94 ]), 95 'The filters that will be applied in the request', 96 VALUE_OPTIONAL 97 ), 98 'jointype' => new external_value(PARAM_INT, 'Type of join to join all filters together', VALUE_REQUIRED), 99 'firstinitial' => new external_value( 100 PARAM_RAW, 101 'The first initial to sort filter on', 102 VALUE_REQUIRED, 103 null 104 ), 105 'lastinitial' => new external_value( 106 PARAM_RAW, 107 'The last initial to sort filter on', 108 VALUE_REQUIRED, 109 null 110 ), 111 'pagenumber' => new external_value( 112 PARAM_INT, 113 'The page number', 114 VALUE_REQUIRED, 115 null 116 ), 117 'pagesize' => new external_value( 118 PARAM_INT, 119 'The number of records per page', 120 VALUE_REQUIRED, 121 null 122 ), 123 'hiddencolumns' => new external_multiple_structure( 124 new external_value( 125 PARAM_ALPHANUMEXT, 126 'Name of column', 127 VALUE_REQUIRED, 128 null 129 ) 130 ), 131 'resetpreferences' => new external_value( 132 PARAM_BOOL, 133 'Whether the table preferences should be reset', 134 VALUE_REQUIRED, 135 null 136 ), 137 ]); 138 } 139 140 /** 141 * External function to get the table view content. 142 * 143 * @param string $component The component. 144 * @param string $handler Dynamic table class name. 145 * @param string $uniqueid Unique ID for the container. 146 * @param array $sortdata The columns and order to sort by 147 * @param array $filters The filters that will be applied in the request. 148 * @param string $jointype The join type. 149 * @param string $firstinitial The first name initial to filter on 150 * @param string $lastinitial The last name initial to filter on 151 * @param int $pagenumber The page number. 152 * @param int $pagesize The number of records. 153 * @param string $jointype The join type. 154 * @param bool $resetpreferences Whether it is resetting table preferences or not. 155 * 156 * @return array 157 */ 158 public static function execute( 159 string $component, 160 string $handler, 161 string $uniqueid, 162 array $sortdata, 163 ?array $filters = null, 164 ?string $jointype = null, 165 ?string $firstinitial = null, 166 ?string $lastinitial = null, 167 ?int $pagenumber = null, 168 ?int $pagesize = null, 169 ?array $hiddencolumns = null, 170 ?bool $resetpreferences = null 171 ) { 172 global $PAGE; 173 174 [ 175 'component' => $component, 176 'handler' => $handler, 177 'uniqueid' => $uniqueid, 178 'sortdata' => $sortdata, 179 'filters' => $filters, 180 'jointype' => $jointype, 181 'firstinitial' => $firstinitial, 182 'lastinitial' => $lastinitial, 183 'pagenumber' => $pagenumber, 184 'pagesize' => $pagesize, 185 'hiddencolumns' => $hiddencolumns, 186 'resetpreferences' => $resetpreferences, 187 ] = self::validate_parameters(self::execute_parameters(), [ 188 'component' => $component, 189 'handler' => $handler, 190 'uniqueid' => $uniqueid, 191 'sortdata' => $sortdata, 192 'filters' => $filters, 193 'jointype' => $jointype, 194 'firstinitial' => $firstinitial, 195 'lastinitial' => $lastinitial, 196 'pagenumber' => $pagenumber, 197 'pagesize' => $pagesize, 198 'hiddencolumns' => $hiddencolumns, 199 'resetpreferences' => $resetpreferences, 200 ]); 201 202 $tableclass = "\\{$component}\\table\\{$handler}"; 203 if (!class_exists($tableclass)) { 204 throw new \UnexpectedValueException("Table handler class {$tableclass} not found. " . 205 "Please make sure that your table handler class is under the \\{$component}\\table namespace."); 206 } 207 208 if (!is_subclass_of($tableclass, \core_table\dynamic::class)) { 209 throw new \UnexpectedValueException("Table handler class {$tableclass} does not support dynamic updating."); 210 } 211 212 $filtersetclass = $tableclass::get_filterset_class(); 213 if (!class_exists($filtersetclass)) { 214 throw new \UnexpectedValueException("The filter specified ({$filtersetclass}) is invalid."); 215 } 216 217 $filterset = new $filtersetclass(); 218 $filterset->set_join_type($jointype); 219 foreach ($filters as $rawfilter) { 220 $filterset->add_filter_from_params( 221 $rawfilter['name'], 222 $rawfilter['jointype'], 223 $rawfilter['values'] 224 ); 225 } 226 227 $instance = new $tableclass($uniqueid); 228 $instance->set_filterset($filterset); 229 self::validate_context($instance->get_context()); 230 231 $instance->set_sortdata($sortdata); 232 $alphabet = get_string('alphabet', 'langconfig'); 233 234 if ($firstinitial !== null && ($firstinitial === '' || strpos($alphabet, $firstinitial) !== false)) { 235 $instance->set_first_initial($firstinitial); 236 } 237 238 if ($lastinitial !== null && ($lastinitial === '' || strpos($alphabet, $lastinitial) !== false)) { 239 $instance->set_last_initial($lastinitial); 240 } 241 242 if ($pagenumber !== null) { 243 $instance->set_page_number($pagenumber); 244 } 245 246 if ($pagesize === null) { 247 $pagesize = 20; 248 } 249 250 if ($hiddencolumns !== null) { 251 $instance->set_hidden_columns($hiddencolumns); 252 } 253 254 if ($resetpreferences === true) { 255 $instance->mark_table_to_reset(); 256 } 257 258 $PAGE->set_url($instance->baseurl); 259 260 ob_start(); 261 $instance->out($pagesize, true); 262 $tablehtml = ob_get_contents(); 263 ob_end_clean(); 264 265 return [ 266 'html' => $tablehtml, 267 'warnings' => [] 268 ]; 269 } 270 271 /** 272 * Describes the data returned from the external function. 273 * 274 * @return external_single_structure 275 * @since Moodle 3.9 276 */ 277 public static function execute_returns(): external_single_structure { 278 return new external_single_structure([ 279 'html' => new external_value(PARAM_RAW, 'The raw html of the requested table.'), 280 'warnings' => new external_warnings() 281 ]); 282 } 283 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body