See Release Notes
Long Term Support Release
Differences Between: [Versions 401 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 qbank_columnsortorder; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 require_once($CFG->libdir . '/questionlib.php'); 22 23 use context_system; 24 use core_question\local\bank\question_edit_contexts; 25 use core_question\local\bank\view; 26 use moodle_url; 27 28 /** 29 * Class column_manager responsible for loading and saving order to the config setting. 30 * 31 * @package qbank_columnsortorder 32 * @copyright 2021 Catalyst IT Australia Pty Ltd 33 * @author Ghaly Marc-Alexandre <marc-alexandreghaly@catalyst-ca.net> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class column_manager { 37 /** 38 * @var array|bool Column order as set in config_plugins 'class' => 'position', ie: question_type_column => 3. 39 */ 40 public $columnorder; 41 42 /** 43 * @var array|bool Disabled columns in config_plugins table. 44 */ 45 public $disabledcolumns; 46 47 /** 48 * Constructor for column_manager class. 49 * 50 */ 51 public function __construct() { 52 $this->columnorder = get_config('qbank_columnsortorder', 'enabledcol'); 53 $this->disabledcolumns = get_config('qbank_columnsortorder', 'disabledcol'); 54 if ($this->columnorder) { 55 $this->columnorder = array_flip(explode(',', $this->columnorder)); 56 } 57 if ($this->disabledcolumns) { 58 $this->disabledcolumns = array_flip(explode(',', $this->disabledcolumns)); 59 } 60 } 61 62 /** 63 * Sets column order in the qbank_columnsortorder plugin config. 64 * 65 * @param array $columns Column order to set. 66 */ 67 public static function set_column_order(array $columns) : void { 68 $columns = implode(',', $columns); 69 set_config('enabledcol', $columns, 'qbank_columnsortorder'); 70 } 71 72 /** 73 * Get qbank. 74 * 75 * @return view 76 */ 77 protected function get_questionbank(): view { 78 $course = (object) ['id' => 0]; 79 $context = context_system::instance(); 80 $contexts = new question_edit_contexts($context); 81 // Dummy call to get the objects without error. 82 $questionbank = new view($contexts, new moodle_url('/question/dummyurl.php'), $course, null); 83 return $questionbank; 84 } 85 86 /** 87 * Get enabled columns. 88 * 89 * @return array 90 */ 91 public function get_columns(): array { 92 $columns = []; 93 foreach ($this->get_questionbank()->get_visiblecolumns() as $key => $column) { 94 if ($column->get_name() === 'checkbox') { 95 continue; 96 } 97 $classelements = explode('\\', $key); 98 $columns[] = (object) [ 99 'class' => get_class($column), 100 'name' => $column->get_title(), 101 'colname' => end($classelements), 102 ]; 103 } 104 return $columns; 105 } 106 107 /** 108 * Get disabled columns. 109 * 110 * @return array 111 */ 112 public function get_disabled_columns(): array { 113 $disabled = []; 114 if ($this->disabledcolumns) { 115 foreach ($this->disabledcolumns as $class => $value) { 116 if (strpos($class, 'qbank_customfields\custom_field_column') !== false) { 117 $class = explode('\\', $class); 118 $disabledname = array_pop($class); 119 $class = implode('\\', $class); 120 $disabled[] = (object) [ 121 'disabledname' => $disabledname, 122 ]; 123 } else { 124 $columnobject = new $class($this->get_questionbank()); 125 $disabled[] = (object) [ 126 'disabledname' => $columnobject->get_title(), 127 ]; 128 } 129 } 130 } 131 return $disabled; 132 } 133 134 /** 135 * Updates enabled and disabled config for 'qbank_columnsortorder' plugin. 136 * 137 * @param array $enabledcolumns Enabled columns to set. 138 * @param array $disabledcolumns Disabled columns to set. 139 */ 140 protected function update_config($enabledcolumns, $disabledcolumns): void { 141 if (!empty($enabledcolumns)) { 142 $configenabled = implode(',', array_flip($enabledcolumns)); 143 set_config('enabledcol', $configenabled, 'qbank_columnsortorder'); 144 } 145 if (!empty($disabledcolumns)) { 146 $configdisabled = implode(',', array_flip($disabledcolumns)); 147 set_config('disabledcol', $configdisabled, 'qbank_columnsortorder'); 148 } else { 149 set_config('disabledcol', null, 'qbank_columnsortorder'); 150 } 151 } 152 153 /** 154 * Enables columns. 155 * 156 * @param string $plugin Plugin type and name ie: qbank_viewcreator. 157 */ 158 public function enable_columns(string $plugin): void { 159 $enabledcolumns = []; 160 $disabledcolumns = []; 161 if ($this->columnorder) { 162 $enabledcolumns = $this->columnorder; 163 } 164 if ($this->disabledcolumns) { 165 $disabledcolumns = $this->disabledcolumns; 166 foreach ($disabledcolumns as $class => $column) { 167 if (strpos($class, $plugin) !== false) { 168 $enabledcolumns[$class] = $class; 169 if (isset($disabledcolumns[$class])) { 170 unset($disabledcolumns[$class]); 171 } 172 } 173 } 174 } 175 $this->update_config($enabledcolumns, $disabledcolumns); 176 } 177 178 /** 179 * Disables columns. 180 * 181 * @param string $plugin Plugin type and name ie: qbank_viewcreator. 182 */ 183 public function disable_columns(string $plugin): void { 184 $disabledcolumns = []; 185 $enabledcolumns = []; 186 $allcolumns = $this->get_columns(); 187 if ($this->disabledcolumns) { 188 $disabledcolumns = $this->disabledcolumns; 189 } 190 if ($this->columnorder) { 191 $enabledcolumns = $this->columnorder; 192 } 193 194 foreach ($allcolumns as $column) { 195 if (strpos($column->class, $plugin) !== false) { 196 if ($column->class === 'qbank_customfields\custom_field_column') { 197 $disabledcolumns[$column->class . '\\' . $column->colname] = $column->class . '\\' . $column->colname; 198 if (isset($enabledcolumns[$column->class . '\\' . $column->colname])) { 199 unset($enabledcolumns[$column->class. '\\' . $column->colname]); 200 } 201 } else { 202 $disabledcolumns[$column->class] = $column->class; 203 if (isset($enabledcolumns[$column->class])) { 204 unset($enabledcolumns[$column->class]); 205 } 206 } 207 } 208 } 209 $this->update_config($enabledcolumns, $disabledcolumns); 210 } 211 212 /** 213 * Orders columns in the question bank view according to config_plugins table 'qbank_columnsortorder' config. 214 * 215 * @param array $ordertosort Unordered array of columns 216 * @return array $properorder|$ordertosort Returns array ordered if 'qbank_columnsortorder' config exists. 217 */ 218 public function get_sorted_columns($ordertosort): array { 219 // Check if db has order set. 220 if (!empty($this->columnorder)) { 221 // Merge new order with old one. 222 $columnsortorder = $this->columnorder; 223 asort($columnsortorder); 224 $columnorder = []; 225 foreach ($columnsortorder as $classname => $colposition) { 226 $colname = explode('\\', $classname); 227 if (strpos($classname, 'qbank_customfields\custom_field_column') !== false) { 228 unset($colname[0]); 229 $classname = implode('\\', $colname); 230 // Checks if custom column still exists. 231 if (array_key_exists($classname, $ordertosort)) { 232 $columnorder[$classname] = $colposition; 233 } else { 234 $configtounset = str_replace('\\', '\\\\', $classname); 235 // Cleans config db. 236 unset_config($configtounset, 'column_sortorder'); 237 } 238 } else { 239 $columnorder[end($colname)] = $colposition; 240 } 241 } 242 $properorder = array_merge($columnorder, $ordertosort); 243 // Always have the checkbox at first column position. 244 if (isset($properorder['checkbox_column'])) { 245 $checkboxfirstelement = $properorder['checkbox_column']; 246 unset($properorder['checkbox_column']); 247 $properorder = array_merge(['checkbox_column' => $checkboxfirstelement], $properorder); 248 } 249 return $properorder; 250 } 251 return $ordertosort; 252 } 253 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body