Merge branch 'MDL-78823_master' of https://github.com/marxjohnson/moodle

This commit is contained in:
Huong Nguyen 2023-08-02 14:51:46 +07:00
commit 0b3c260e00
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
10 changed files with 255 additions and 5 deletions

View File

@ -27,7 +27,8 @@
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
use qbank_columnsortorder\column_manager;
use core\event\qbank_plugin_enabled;
use core\event\qbank_plugin_disabled;
$action = required_param('action', PARAM_ALPHANUMEXT);
$name = required_param('name', PARAM_PLUGIN);
@ -49,12 +50,11 @@ if (!isset($plugins[$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);
qbank_plugin_disabled::create_for_plugin($plugintypename)->trigger();
$class = \core_plugin_manager::resolve_plugininfo_class('qbank');
$class::enable_plugin($name, false);
set_config('disabled', 1, 'qbank_'. $name);
@ -62,7 +62,7 @@ switch ($action) {
break;
case 'enable':
if (!$plugins[$name]->is_enabled()) {
$columnsortordermanager->enable_columns($plugintypename);
qbank_plugin_enabled::create_for_plugin($plugintypename)->trigger();
$class = \core_plugin_manager::resolve_plugininfo_class('qbank');
$class::enable_plugin($name, true);
}

View File

@ -150,6 +150,8 @@ $string['errorprocess'] = 'Error occurred during processing!';
$string['errorprocessingresponses'] = 'An error occurred while processing your responses ({$a}). Click continue to return to the page you were on and try again.';
$string['errorsavingcomment'] = 'Error saving the comment for question {$a->name} in the database.';
$string['errorupdatingattempt'] = 'Error updating attempt {$a->id} in the database.';
$string['eventqbankdisabled'] = 'Question bank plugin disabled';
$string['eventqbankenabled'] = 'Question bank plugin enabled';
$string['eventquestioncategorycreated'] = 'Question category created';
$string['eventquestioncategorydeleted'] = 'Question category deleted';
$string['eventquestioncategorymoved'] = 'Question category moved';

View File

@ -0,0 +1,56 @@
<?php
// 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/>.
namespace core\event;
/**
* Question bank plugin event.
*
* This describes an administrative event relating to a question bank plugin, in the system context.
* The pluginname will be stored in the other property, and userid will be the user who performed the action on the plugin.
*
* @package core
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class qbank_plugin_base extends base {
protected function init() {
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['crud'] = 'u';
$this->context = \context_system::instance();
}
protected function validate_data() {
if (!str_starts_with($this->data['other']['pluginname'], 'qbank_')) {
throw new \coding_exception('You must provide the full frankenstyle name of a qbank plugin (e.g. qbank_usage)');
}
}
/**
* Return an event instance with $this->other['pluginname'] set to the provided plugin name.
*
* @param string $pluginname
* @return base
* @throws \coding_exception
*/
public static function create_for_plugin(string $pluginname): base {
return self::create([
'other' => ['pluginname' => $pluginname],
]);
}
}

View File

@ -0,0 +1,36 @@
<?php
// 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/>.
namespace core\event;
/**
* Question bank plugin was disabled
*
* @package core
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qbank_plugin_disabled extends qbank_plugin_base {
public static function get_name() {
return get_string('eventqbankdisabled', 'question');
}
public function get_description() {
return 'User ' . $this->data['userid'] . ' disabled question bank plugin ' . $this->data['other']['pluginname'];
}
}

View File

@ -0,0 +1,36 @@
<?php
// 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/>.
namespace core\event;
/**
* Question bank plugin was enabled
*
* @package core
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qbank_plugin_enabled extends qbank_plugin_base {
public static function get_name() {
return get_string('eventqbankenabled', 'question');
}
public function get_description() {
return 'User ' . $this->data['userid'] . ' enabled question bank plugin ' . $this->data['other']['pluginname'];
}
}

View File

@ -59,6 +59,9 @@ information provided here is intended especially for developers.
* New method moodleform::filter_shown_headers() is created to show some expanded headers only and hide the rest.
* count_words() and count_letters() have a new optional parameter called $format to format the text before doing the counting.
* New core_renderer::sr_text method to generate screen reader only inline texts without using html_writter.
* New events \core\event\qbank_plugin_enabled and \core\event\qbank_plugin_disabled are triggered when a qbank plugin is enabled or
disabled respectively, with the plugin's frankenstyle name. Any plugins that need to perform an action in response to a qbank
plugin being enabled or disabled should observe these events.
=== 4.2 ===

View File

@ -0,0 +1,52 @@
<?php
// 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/>.
namespace qbank_columnsortorder\event;
use core\event\qbank_plugin_disabled;
use core\event\qbank_plugin_enabled;
use qbank_columnsortorder\column_manager;
/**
* Observer for qbank plugin enabled/disabled events
*
* @package qbank_columnsortorder
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_observer {
/**
* When a plugin is enabled, enable its columns.
*
* @param qbank_plugin_enabled $event
* @return void
*/
public static function plugin_enabled(qbank_plugin_enabled $event): void {
(new column_manager())->enable_columns($event->other['pluginname']);
}
/**
* When a plugin is disabled, disable its columns.
*
* @param qbank_plugin_disabled $event
* @return void
*/
public static function plugin_disabled(qbank_plugin_disabled $event): void {
(new column_manager())->disable_columns($event->other['pluginname']);
}
}

View File

@ -0,0 +1,37 @@
<?php
// 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/>.
/**
* Event observer registration
*
* @package qbank_columnsortorder
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$observers = [
[
'eventname' => '\core\event\qbank_plugin_enabled',
'callback' => '\qbank_columnsortorder\event\plugin_observer::plugin_enabled',
],
[
'eventname' => '\core\event\qbank_plugin_disabled',
'callback' => '\qbank_columnsortorder\event\plugin_observer::plugin_disabled',
],
];

View File

@ -131,4 +131,32 @@ class column_manager_test extends advanced_testcase {
$this->assertNotFalse($contains);
$this->assertIsInt($contains);
}
/**
* Test enabling and disabling columns through event observers
*
* @covers \qbank_columnsortorder\event\plugin_observer
*/
public function test_plugin_enabled_disabled_observers(): void {
$neworder = $this->columnmanager->get_sorted_columns($this->columns);
shuffle($neworder);
set_columnbank_order::execute($neworder);
// Get the list of enabled columns, excluding core columns (we can't disable those).
$currentconfig = get_config('qbank_columnsortorder', 'enabledcol');
$currentconfig = array_filter(explode(',', $currentconfig), fn($class) => !str_starts_with($class, 'core'));
// Pick a column at random and get its plugin name.
$class = $currentconfig[array_rand($currentconfig, 1)];
$randomplugintodisable = explode('\\', $class)[0];
$olddisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
\core\event\qbank_plugin_disabled::create_for_plugin($randomplugintodisable)->trigger();
$newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
$this->assertNotEquals($olddisabledconfig, $newdisabledconfig);
\core\event\qbank_plugin_enabled::create_for_plugin($randomplugintodisable)->trigger();
$newdisabledconfig = get_config('qbank_columnsortorder', 'disabledcol');
$this->assertEmpty($newdisabledconfig);
$enabledconfig = get_config('qbank_columnsortorder', 'enabledcol');
$contains = strpos($enabledconfig, $randomplugintodisable);
$this->assertNotFalse($contains);
$this->assertIsInt($contains);
}
}

View File

@ -26,6 +26,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qbank_columnsortorder';
$plugin->version = 2023042400;
$plugin->version = 2023042401;
$plugin->requires = 2023041800;
$plugin->maturity = MATURITY_STABLE;