. /** * Table external API. * * @package core_table * @category external * @copyright 2020 Simey Lameze * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace core_table\external\dynamic; use external_api; use external_function_parameters; use external_multiple_structure; use external_single_structure; use external_value; use external_warnings; use moodle_url; /** * Core table external functions. * * @package core_table * @category external * @copyright 2020 Simey Lameze * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class fetch extends external_api { /** * Describes the parameters for fetching the table html. * * @return external_function_parameters * @since Moodle 3.9 */ public static function execute_parameters(): external_function_parameters { return new external_function_parameters ([ 'handler' => new external_value( // Note: We do not have a PARAM_CLASSNAME which would have been ideal. PARAM_RAW, 'Handler', VALUE_REQUIRED ), 'uniqueid' => new external_value( PARAM_ALPHANUMEXT, 'Unique ID for the container', VALUE_REQUIRED ), 'sortby' => new external_value( PARAM_ALPHANUMEXT, 'The name of a sortable column', VALUE_REQUIRED ), 'sortorder' => new external_value( PARAM_ALPHANUMEXT, 'The sort order', VALUE_REQUIRED ), 'filters' => new external_multiple_structure( new external_single_structure([ 'name' => new external_value(PARAM_ALPHANUM, 'Name of the filter', VALUE_REQUIRED), 'jointype' => new external_value(PARAM_INT, 'Type of join for filter values', VALUE_REQUIRED), 'values' => new external_multiple_structure( new external_value(PARAM_RAW, 'Filter value'), 'The value to filter on', VALUE_REQUIRED ) ]), 'The filters that will be applied in the request', VALUE_OPTIONAL ), 'jointype' => new external_value(PARAM_INT, 'Type of join to join all filters together', VALUE_REQUIRED), ]); } /** * External function to fetch a table view. * * @param string $handler Dynamic table class name. * @param string $uniqueid Unique ID for the container. * @param string $sortby The name of a sortable column. * @param string $sortorder The sort order. * @param array $filters The filters that will be applied in the request. * @param string $jointype The join type. * * @return array */ public static function execute(string $handler, string $uniqueid, string $sortby, string $sortorder, array $filters = [], string $jointype = null) { global $PAGE; if (!class_exists($handler) || !is_subclass_of($handler, \core_table\dynamic::class)) { throw new \UnexpectedValueException('Unknown table handler, or table handler does not support dynamic updating.'); } [ 'handler' => $handler, 'uniqueid' => $uniqueid, 'sortby' => $sortby, 'sortorder' => $sortorder, 'filters' => $filters, 'jointype' => $jointype, ] = self::validate_parameters(self::execute_parameters(), [ 'handler' => $handler, 'uniqueid' => $uniqueid, 'sortby' => $sortby, 'sortorder' => $sortorder, 'filters' => $filters, 'jointype' => $jointype, ]); $filterset = new \core_user\table\participants_filterset(); foreach ($filters as $rawfilter) { $filterset->add_filter_from_params( $rawfilter['name'], $rawfilter['jointype'], $rawfilter['values'] ); } $instance = new $handler($uniqueid); $instance->set_filterset($filterset); $instance->set_sorting($sortby, $sortorder); $context = $instance->get_context(); self::validate_context($context); $PAGE->set_url($instance->get_base_url()); ob_start(); $instance->out(20, true); $participanttablehtml = ob_get_contents(); ob_end_clean(); return [ 'html' => $participanttablehtml, 'warnings' => [] ]; } /** * Describes the data returned from the external function. * * @return external_single_structure * @since Moodle 3.9 */ public static function execute_returns(): external_single_structure { return new external_single_structure([ 'html' => new external_value(PARAM_RAW, 'The raw html of the requested table.'), 'warnings' => new external_warnings() ]); } }