Skip to content

Commit

Permalink
MDL-71516 core_question: Create new plugin type - qbank
Browse files Browse the repository at this point in the history
This commit implements the qbank plugin type which
includes the boilerplate for the qbank plugin, the
qbank plugin management admin page and required core
code addition.
  • Loading branch information
safatshahin committed Aug 15, 2021
1 parent 86bdf87 commit 351176b
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 0 deletions.
64 changes: 64 additions & 0 deletions admin/qbankplugins.php
@@ -0,0 +1,64 @@
<?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/>.

/**
* Question bank plugin settings.
*
* @package core
* @subpackage questionbank
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');

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

$syscontext = context_system::instance();
$PAGE->set_url('/admin/qbankplugins.php');
$PAGE->set_context($syscontext);

require_admin();

$return = new moodle_url('/admin/settings.php', array('section' => 'manageqbanks'));

$plugins = core_plugin_manager::instance()->get_plugins_of_type('qbank');
$sortorder = array_flip(array_keys($plugins));

if (!isset($plugins[$name])) {
throw new moodle_exception('qbanknotfound', 'question', $return, $name);
}

switch ($action) {
case 'disable':
if ($plugins[$name]->is_enabled()) {
set_config('disabled', 1, 'qbank_'. $name);
}
break;
case 'enable':
if (!$plugins[$name]->is_enabled()) {
unset_config('disabled', 'qbank_'. $name);
}
break;
}

core_plugin_manager::reset_caches();

redirect($return);

14 changes: 14 additions & 0 deletions admin/settings/plugins.php
Expand Up @@ -403,6 +403,20 @@
}
}

// Question bank settings.
if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) {
$ADMIN->add('modules', new admin_category('qbanksettings',
new lang_string('questionbanks', 'question')));
$temp = new admin_settingpage('manageqbanks', new lang_string('manageqbanks', 'admin'));
$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);
}
}

// Question type settings
if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) {

Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -1515,6 +1515,7 @@
$string['xmlrpcrecommended'] = 'The XMLRPC extension is useful for web services and Moodle networking.';
$string['yuicomboloading'] = 'YUI combo loading';
$string['ziprequired'] = 'The Zip PHP extension is now required by Moodle, info-ZIP binaries or PclZip library are not used anymore.';
$string['manageqbanks'] = 'Manage question bank plugins';


$string['caching'] = 'Caching';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/plugin.php
Expand Up @@ -192,6 +192,8 @@
$string['type_tool_plural'] = 'Admin tools';
$string['type_webservice'] = 'Webservice protocol';
$string['type_webservice_plural'] = 'Webservice protocols';
$string['type_qbank'] = 'Question bank';
$string['type_qbank_plural'] = 'Question banks';
$string['updateavailable'] = 'There is a new version {$a} available!';
$string['updateavailable_moreinfo'] = 'More info...';
$string['updateavailable_release'] = 'Release {$a}';
Expand Down
3 changes: 3 additions & 0 deletions lang/en/question.php
Expand Up @@ -494,3 +494,6 @@
$string['withselected'] = 'With selected';
$string['xoutofmax'] = '{$a->mark} out of {$a->max}';
$string['yougotnright'] = 'You have correctly selected {$a->num}.';
$string['questionbanks'] = 'Question bank plugins';
$string['qbanknotfound'] = 'The \'{$a}\' question bank doesn\'t exist or is not recognised.';
$string['noquestionbanks'] = 'No question bank plugin found.';
5 changes: 5 additions & 0 deletions lib/classes/plugin_manager.php
Expand Up @@ -1938,6 +1938,10 @@ public static function standard_plugins_list($type) {
'checkbox', 'datetime', 'menu', 'social', 'text', 'textarea'
),

'qbank' => array(
''
),

'qbehaviour' => array(
'adaptive', 'adaptivenopenalty', 'deferredcbm',
'deferredfeedback', 'immediatecbm', 'immediatefeedback',
Expand Down Expand Up @@ -2241,6 +2245,7 @@ protected function reorder_plugin_types(array $types) {
$fix['mod'] = $types['mod'];
$fix['block'] = $types['block'];
$fix['qtype'] = $types['qtype'];
$fix['qbank'] = $types['qbank'];
$fix['qbehaviour'] = $types['qbehaviour'];
$fix['qformat'] = $types['qformat'];
$fix['filter'] = $types['filter'];
Expand Down
147 changes: 147 additions & 0 deletions lib/classes/plugininfo/qbank.php
@@ -0,0 +1,147 @@
<?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/>.

/**
* Defines classes used for plugin info.
*
* @package core
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core\plugininfo;

defined('MOODLE_INTERNAL') || die();

/**
* Base class for qbank plugins.
*
* @package core
* @copyright 2021 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qbank extends base {

public function is_uninstall_allowed(): bool {
return true;
}

public static function get_manage_url(): \moodle_url {
return new \moodle_url('/admin/settings.php', array('section' => 'manageqbanks'));
}

public static function get_plugins($type, $typerootdir, $typeclass, $pluginman): array {
global $CFG;

$qbank = parent::get_plugins($type, $typerootdir, $typeclass, $pluginman);
$order = array_keys($qbank);
$sortedqbanks = array();
foreach ($order as $qbankname) {
$sortedqbanks[$qbankname] = $qbank[$qbankname];
}
return $sortedqbanks;
}

/**
* Finds all enabled plugins, the result may include missing plugins.
* @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
*/
public static function get_enabled_plugins(): ?array {
global $CFG;
$pluginmanager = \core_plugin_manager::instance();
$plugins = $pluginmanager->get_installed_plugins('qbank');

if (!$plugins) {
return array();
}

$plugins = array_keys($plugins);

// Filter to return only enabled plugins.
$enabled = array();
foreach ($plugins as $plugin) {
$qbankinfo = $pluginmanager->get_plugin_info('qbank_'.$plugin);
$qbankavailable = $qbankinfo->get_status();
if ($qbankavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING) {
continue;
}
$disabled = get_config('qbank_' . $plugin, 'disabled');
if (empty($disabled)) {
$enabled[$plugin] = $plugin;
}
}
return $enabled;
}

/**
* Checks if a qbank plugin is ready to be used.
* It checks the plugin status as well as the plugin is missing or not.
*
* @param string $fullpluginname the name of the plugin
* @return bool
*/
public static function is_ready($fullpluginname): bool {
$pluginmanager = \core_plugin_manager::instance();
$qbankinfo = $pluginmanager->get_plugin_info($fullpluginname);
if (empty($qbankinfo)) {
return false;
}
$qbankavailable = $qbankinfo->get_status();
if ($qbankavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING ||
!empty(get_config($fullpluginname, 'disabled'))) {
return false;
}
return true;
}

/**
* Loads plugin settings to the settings tree
*
* This function usually includes settings.php file in plugins folder.
* Alternatively it can create a link to some settings page (instance of admin_externalpage)
*
* @param \part_of_admin_tree $adminroot
* @param string $parentnodename
* @param bool $hassiteconfig whether the current user has moodle/site:config capability
*/
public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig): void {
global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
$ADMIN = $adminroot; // May be used in settings.php.
$plugininfo = $this; // Also can be used inside settings.php.

if (!$this->is_installed_and_upgraded()) {
return;
}

if (!$hassiteconfig) {
return;
}

$section = $this->get_settings_section_name();

$settings = null;
if (file_exists($this->full_path('settings.php'))) {
$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) {
$ADMIN->add($parentnodename, $settings);
}
}
}
1 change: 1 addition & 0 deletions lib/components.json
Expand Up @@ -28,6 +28,7 @@
"repository": "repository",
"portfolio": "portfolio",
"search": "search\/engine",
"qbank": "question\/bank",
"qbehaviour": "question\/behaviour",
"qformat": "question\/format",
"plagiarism": "plagiarism",
Expand Down
2 changes: 2 additions & 0 deletions question/bank/upgrade.txt
@@ -0,0 +1,2 @@
This file describes core qbank plugin changes in /question/bank/*,
information provided here is intended especially for developers.

0 comments on commit 351176b

Please sign in to comment.