PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/table/classes/external/dynamic/fetch.php

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