mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-71516 core_question: Create new plugin type - qbank
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
1 parent
86bdf87
commit 351176b
Showing
10 changed files
with
382 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This file describes core qbank plugin changes in /question/bank/*, | ||
information provided here is intended especially for developers. |
Oops, something went wrong.