Skip to content

Commit

Permalink
MDL-71636 qbank_columnsortorder: Add a columnsortorder settings page
Browse files Browse the repository at this point in the history
This implementation will introduce a feature "columnsortorder"
which will add the column sort order feature in an external page.
Having this feature will give users the flexibility of sorting plugin
columns in the question bank view.
  • Loading branch information
marcghaly authored and timhunt committed Mar 2, 2022
1 parent 7c016b3 commit 6dc1084
Show file tree
Hide file tree
Showing 22 changed files with 1,059 additions and 15 deletions.
8 changes: 8 additions & 0 deletions admin/qbankplugins.php
Expand Up @@ -27,6 +27,8 @@
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

use qbank_columnsortorder\column_manager;

$action = required_param('action', PARAM_ALPHANUMEXT);
$name = required_param('name', PARAM_PLUGIN);

Expand All @@ -46,15 +48,21 @@
throw new moodle_exception('qbanknotfound', 'question', $return, $name);
}

$plugintypename = $plugins[$name]->type . '_' . $plugins[$name]->name;
$columnsortordermanager = new column_manager();

switch ($action) {
case 'disable':
if ($plugins[$name]->is_enabled()) {
$columnsortordermanager->disable_columns($plugintypename);
$class = \core_plugin_manager::resolve_plugininfo_class('qbank');
$class::enable_plugin($name, false);
set_config('disabled', 1, 'qbank_'. $name);
}
break;
case 'enable':
if (!$plugins[$name]->is_enabled()) {
$columnsortordermanager->enable_columns($plugintypename);
$class = \core_plugin_manager::resolve_plugininfo_class('qbank');
$class::enable_plugin($name, true);
}
Expand Down
1 change: 1 addition & 0 deletions admin/settings/plugins.php
Expand Up @@ -431,6 +431,7 @@
$temp->add(new \core_question\admin\manage_qbank_plugins_page());
$ADMIN->add('qbanksettings', $temp);
$plugins = core_plugin_manager::instance()->get_plugins_of_type('qbank');

foreach ($plugins as $plugin) {
/** @var \core\plugininfo\qbank $plugin */
$plugin->load_settings($ADMIN, 'qbanksettings', $hassiteconfig);
Expand Down
8 changes: 6 additions & 2 deletions lib/classes/plugininfo/qbank.php
Expand Up @@ -25,6 +25,8 @@

namespace core\plugininfo;

use qbank_columnsortorder\column_manager;

/**
* Base class for qbank plugins.
*
Expand Down Expand Up @@ -150,8 +152,10 @@ public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $

$settings = null;
if (file_exists($this->full_path('settings.php'))) {
$settings = new \admin_settingpage($section, $this->displayname,
'moodle/site:config', $this->is_enabled() === false);
if ($this->name !== 'columnsortorder') {
$settings = new \admin_settingpage($section, $this->displayname,
'moodle/site:config', $this->is_enabled() === false);
}
include($this->full_path('settings.php')); // This may also set $settings to null.
}
if ($settings) {
Expand Down
10 changes: 10 additions & 0 deletions question/bank/columnsortorder/amd/build/sort_columns.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions question/bank/columnsortorder/amd/src/sort_columns.js
@@ -0,0 +1,81 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Javascript for sorting columns in question bank view.
*
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Ghaly Marc-Alexandre <marc-alexandreghaly@catalyst-ca.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

import {call as fetchMany} from 'core/ajax';
import {exception as displayException} from 'core/notification';
import SortableList from 'core/sortable_list';
import jQuery from 'jquery';

/**
* Sets up sortable list in the column sort order page.
* @param {Element} listRoot
*/
const setupSortableLists = (listRoot) => {
new SortableList(
'.list',
{
moveHandlerSelector: '.item',
}
);

jQuery('.item').on(SortableList.EVENTS.DROP, () => {
const columns = getColumnOrder(listRoot);
setOrder(columns).catch(displayException);
listRoot.querySelectorAll('.item').forEach(item => item.classList.remove('active'));
});

jQuery('.item').on(SortableList.EVENTS.DRAGSTART, (event) => {
event.currentTarget.classList.add('active');
});
};

/**
* Call external function set_order - inserts the updated column in the config_plugins table.
*
* @param {String} columns String that contains column order.
* @returns {Promise}
*/
const setOrder = columns => fetchMany([{
methodname: 'qbank_columnsortorder_set_columnbank_order',
args: {columns: columns},
}])[0];

/**
* Gets the newly reordered columns to display in the question bank view.
* @param {Element} listRoot
* @returns {Array}
*/
const getColumnOrder = (listRoot) => {
const columns = Array.from(listRoot.querySelectorAll('[data-pluginname]')).map(column => column.dataset.pluginname);

return columns.filter((value, index) => columns.indexOf(value) === index);
};

/**
* Initialize module
* @param {int} id unique id for columns.
*/
export const init = (id) => {
const listRoot = document.querySelector(`#${id}`);
setupSortableLists(listRoot);
};

0 comments on commit 6dc1084

Please sign in to comment.