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 /** 18 * Functions for managing and manipulating question filter conditions 19 * 20 * @package core_question 21 * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net} 22 * @author Mark Johnson <mark.johnson@catalyst-eu.net> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core_question\local\bank; 27 28 /** 29 * Static methods for parsing and formatting data related to filter conditions. 30 */ 31 class filter_condition_manager { 32 33 /** 34 * Extract parameters from args list. 35 * 36 * @param array $args 37 * @return array the param and extra param 38 */ 39 public static function extract_parameters_from_fragment_args(array $args): array { 40 $params = []; 41 if (array_key_exists('filter', $args)) { 42 $params['filter'] = json_decode($args['filter'], true); 43 } 44 if (array_key_exists('cmid', $args)) { 45 $params['cmid'] = $args['cmid']; 46 } 47 if (array_key_exists('courseid', $args)) { 48 $params['courseid'] = $args['courseid']; 49 } 50 $params['jointype'] = $args['jointype'] ?? condition::JOINTYPE_DEFAULT; 51 $params['qpage'] = $args['qpage'] ?? 0; 52 $params['qperpage'] = $args['qperpage'] ?? 100; 53 $params['sortdata'] = json_decode($args['sortdata'] ?? '', true); 54 $extraparams = json_decode($args['extraparams'] ?? '', true); 55 56 return [$params, $extraparams]; 57 } 58 59 /** 60 * List of condition classes 61 * 62 * @return condition[] condition classes: [condition_key] = class 63 */ 64 public static function get_condition_classes(): array { 65 $classes = []; 66 $plugins = \core_component::get_plugin_list_with_class('qbank', 'plugin_feature', 'plugin_feature.php'); 67 foreach ($plugins as $componentname => $plugin) { 68 if (\core\plugininfo\qbank::is_plugin_enabled($componentname)) { 69 $pluginentrypointobject = new $plugin(); 70 $conditions = $pluginentrypointobject->get_question_filters(); 71 foreach ($conditions as $condition) { 72 $classes[$condition->get_condition_key()] = $condition->get_condition_class(); 73 } 74 } 75 } 76 return $classes; 77 } 78 79 /** 80 * Given a JSON-encoded "filter" URL param, create or replace the category filter with the provided category. 81 * 82 * @param string $filterparam The json-encoded filter param from the URL, containing the list of filters. 83 * @param int $newcategoryid The new ID to set for the "category" filter condition's value. 84 * @return string JSON-encoded filter param with the new category. 85 */ 86 public static function update_filter_param_to_category(string $filterparam, int $newcategoryid): string { 87 $returnfilters = json_decode($filterparam, true); 88 if (!$returnfilters) { 89 $returnfilters = [ 90 'category' => [ 91 'name' => 'category', 92 'jointype' => \core_question\local\bank\condition::JOINTYPE_DEFAULT, 93 ] 94 ]; 95 } 96 $returnfilters['category']['values'] = [$newcategoryid]; 97 return json_encode($returnfilters); 98 } 99 100 /** 101 * Unpack filteroptions passed in a request's filter param if required. 102 * 103 * Filteroptions are passed via AJAX as an array of {name:, value:} pairs for compatibility with external functions. 104 * 105 * @param array $filters List of filters, each optionally including an array of filteroptions. 106 * @return array The input array, with filteroptions unpacked from [{name:, value:}, ...] to [name => value, ...]. 107 */ 108 public static function unpack_filteroptions_param(array $filters): array { 109 foreach ($filters as $name => $filter) { 110 if (!empty($filter['filteroptions']) && isset(reset($filter['filteroptions'])['name'])) { 111 $unpacked = []; 112 foreach ($filter['filteroptions'] as $filteroption) { 113 $unpacked[$filteroption['name']] = $filteroption['value']; 114 } 115 $filters[$name]['filteroptions'] = $unpacked; 116 } 117 } 118 return $filters; 119 } 120 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body