Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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\external; 20 21 use renderer_base; 22 use core\external\exporter; 23 use core_reportbuilder\local\report\base; 24 use core_reportbuilder\local\models\filter; 25 use core_reportbuilder\output\filter_heading_editable; 26 27 /** 28 * Custom report filters exporter class 29 * 30 * @package core_reportbuilder 31 * @copyright 2021 Paul Holden <paulh@moodle.com> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class custom_report_filters_exporter extends exporter { 35 36 /** 37 * Return a list of objects that are related to the exporter 38 * 39 * @return array 40 */ 41 protected static function define_related(): array { 42 return [ 43 'report' => base::class, 44 ]; 45 } 46 47 /** 48 * Return the list of additional properties for read structure and export 49 * 50 * @return array[] 51 */ 52 protected static function define_other_properties(): array { 53 return [ 54 'hasavailablefilters' => [ 55 'type' => PARAM_BOOL, 56 'optional' => true, 57 ], 58 'availablefilters' => [ 59 'type' => [ 60 'optiongroup' => [ 61 'type' => [ 62 'text' => ['type' => PARAM_TEXT], 63 'values' => [ 64 'type' => [ 65 'value' => ['type' => PARAM_TEXT], 66 'visiblename' => ['type' => PARAM_TEXT], 67 ], 68 'multiple' => true, 69 ], 70 ], 71 ], 72 ], 73 'multiple' => true, 74 'optional' => true 75 ], 76 'hasactivefilters' => [ 77 'type' => PARAM_BOOL, 78 'optional' => true, 79 ], 80 'activefilters' => [ 81 'type' => [ 82 'id' => ['type' => PARAM_INT], 83 'heading' => ['type' => PARAM_TEXT], 84 'headingeditable' => ['type' => PARAM_RAW], 85 'sortorder' => ['type' => PARAM_INT], 86 'movetitle' => ['type' => PARAM_TEXT], 87 'entityname' => ['type' => PARAM_TEXT], 88 ], 89 'multiple' => true, 90 'optional' => true 91 ], 92 'helpicon' => [ 93 'type' => PARAM_RAW, 94 'optional' => true, 95 ], 96 ]; 97 } 98 99 /** 100 * Get the additional values to inject while exporting 101 * 102 * @param renderer_base $output 103 * @return array 104 */ 105 protected function get_other_values(renderer_base $output): array { 106 /** @var base $report */ 107 $report = $this->related['report']; 108 109 // Current filters added to the report. 110 $filters = filter::get_filter_records($report->get_report_persistent()->get('id'), 'filterorder'); 111 $filteridentifiers = array_map(static function(filter $filter): string { 112 return $filter->get('uniqueidentifier'); 113 }, $filters); 114 115 $availablefilters = $activefilters = []; 116 117 // Populate available filters. 118 foreach ($report->get_filters() as $filter) { 119 if (in_array($filter->get_unique_identifier(), $filteridentifiers)) { 120 continue; 121 } 122 123 $entityname = $filter->get_entity_name(); 124 if (!array_key_exists($entityname, $availablefilters)) { 125 $availablefilters[$entityname] = [ 126 'optiongroup' => [ 127 'text' => $report->get_entity_title($entityname)->out(), 128 'values' => [], 129 ], 130 ]; 131 } 132 133 $availablefilters[$entityname]['optiongroup']['values'][] = [ 134 'value' => $filter->get_unique_identifier(), 135 'visiblename' => $filter->get_header(), 136 ]; 137 } 138 139 // Populate active filters. 140 $filterinstances = $report->get_filter_instances(); 141 foreach ($filters as $filter) { 142 $filterinstance = $filterinstances[$filter->get('uniqueidentifier')] ?? null; 143 if ($filterinstance === null) { 144 continue; 145 } 146 147 $entityname = $filterinstance->get_entity_name(); 148 $displayvalue = $filterinstance->get_header(); 149 $editable = new filter_heading_editable($filter->get('id')); 150 151 $activefilters[] = [ 152 'id' => $filter->get('id'), 153 'entityname' => $report->get_entity_title($entityname)->out(), 154 'heading' => $displayvalue, 155 'headingeditable' => $editable->render($output), 156 'sortorder' => $filter->get('filterorder'), 157 'movetitle' => get_string('movefilter', 'core_reportbuilder', $displayvalue), 158 ]; 159 } 160 161 return [ 162 'hasavailablefilters' => !empty($availablefilters), 163 'availablefilters' => array_values($availablefilters), 164 'hasactivefilters' => !empty($activefilters), 165 'activefilters' => $activefilters, 166 'helpicon' => $output->help_icon('filters', 'core_reportbuilder'), 167 ]; 168 } 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body