Skip to content

Commit

Permalink
MDL-78823 core: Add new events when enabling and disabling qbank plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed Aug 2, 2023
1 parent a1d5d1b commit b7b932a
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 5 deletions.
8 changes: 4 additions & 4 deletions admin/qbankplugins.php
Expand Up @@ -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);
Expand All @@ -49,20 +50,19 @@
}

$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);
}
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);
}
Expand Down
2 changes: 2 additions & 0 deletions lang/en/question.php
Expand Up @@ -150,6 +150,8 @@
$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';
Expand Down
56 changes: 56 additions & 0 deletions lib/classes/event/qbank_plugin_base.php
@@ -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],
]);
}
}
36 changes: 36 additions & 0 deletions lib/classes/event/qbank_plugin_disabled.php
@@ -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'];
}
}
36 changes: 36 additions & 0 deletions lib/classes/event/qbank_plugin_enabled.php
@@ -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'];
}
}
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Expand Up @@ -54,6 +54,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 ===

Expand Down
52 changes: 52 additions & 0 deletions question/bank/columnsortorder/classes/event/plugin_observer.php
@@ -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']);
}
}
37 changes: 37 additions & 0 deletions question/bank/columnsortorder/db/events.php
@@ -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',
],
];
28 changes: 28 additions & 0 deletions question/bank/columnsortorder/tests/column_manager_test.php
Expand Up @@ -131,4 +131,32 @@ public function test_enable_disable_columns(): void {
$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);
}
}
2 changes: 1 addition & 1 deletion question/bank/columnsortorder/version.php
Expand Up @@ -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;

0 comments on commit b7b932a

Please sign in to comment.