| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * Manage question banks page.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core_question
 | 
        
           |  |  | 21 |  * @copyright  2021 Catalyst IT Australia Pty Ltd
 | 
        
           |  |  | 22 |  * @author     Safat Shahin <safatshahin@catalyst-au.net>
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 24 |  */
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | namespace core_question\admin;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | /**
 | 
        
           |  |  | 29 |  * Class manage_qbank_plugins_page.
 | 
        
           |  |  | 30 |  *
 | 
        
           |  |  | 31 |  * @copyright  2021 Catalyst IT Australia Pty Ltd
 | 
        
           |  |  | 32 |  * @author     Safat Shahin <safatshahin@catalyst-au.net>
 | 
        
           |  |  | 33 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  */
 | 
        
           |  |  | 35 | class manage_qbank_plugins_page extends \admin_setting {
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * Class admin_page_manageqbanks constructor.
 | 
        
           |  |  | 39 |      */
 | 
        
           |  |  | 40 |     public function __construct() {
 | 
        
           |  |  | 41 |         $this->nosave = true;
 | 
        
           |  |  | 42 |         parent::__construct('manageqbanks',
 | 
        
           |  |  | 43 |                 new \lang_string('manageqbanks', 'admin'), '', '');
 | 
        
           |  |  | 44 |     }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     public function get_setting(): bool {
 | 
        
           |  |  | 47 |         return true;
 | 
        
           |  |  | 48 |     }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |     public function get_defaultsetting(): bool {
 | 
        
           |  |  | 51 |         return true;
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     public function write_setting($data): string {
 | 
        
           |  |  | 55 |         // Do not write any setting.
 | 
        
           |  |  | 56 |         return '';
 | 
        
           |  |  | 57 |     }
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |     public function is_related($query): bool {
 | 
        
           |  |  | 60 |         if (parent::is_related($query)) {
 | 
        
           |  |  | 61 |             return true;
 | 
        
           |  |  | 62 |         }
 | 
        
           |  |  | 63 |         $types = \core_plugin_manager::instance()->get_plugins_of_type('qbank');
 | 
        
           |  |  | 64 |         foreach ($types as $type) {
 | 
        
           |  |  | 65 |             if (strpos($type->component, $query) !== false ||
 | 
        
           |  |  | 66 |                     strpos(\core_text::strtolower($type->displayname), $query) !== false) {
 | 
        
           |  |  | 67 |                 return true;
 | 
        
           |  |  | 68 |             }
 | 
        
           |  |  | 69 |         }
 | 
        
           |  |  | 70 |         return false;
 | 
        
           |  |  | 71 |     }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |     public function output_html($data, $query = ''): string {
 | 
        
           |  |  | 74 |         global $CFG, $OUTPUT;
 | 
        
           |  |  | 75 |         $return = '';
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         $pluginmanager = \core_plugin_manager::instance();
 | 
        
           |  |  | 78 |         $types = $pluginmanager->get_plugins_of_type('qbank');
 | 
        
           |  |  | 79 |         if (empty($types)) {
 | 
        
           |  |  | 80 |             return get_string('noquestionbanks', 'question');
 | 
        
           |  |  | 81 |         }
 | 
        
           |  |  | 82 |         $txt = get_strings(['settings', 'name', 'enable', 'disable', 'default']);
 | 
        
           |  |  | 83 |         $txt->uninstall = get_string('uninstallplugin', 'core_admin');
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 |         $table = new \html_table();
 | 
        
           |  |  | 86 |         $table->head  = [$txt->name, $txt->enable, $txt->settings, $txt->uninstall];
 | 
        
           |  |  | 87 |         $table->align = ['left', 'center', 'center', 'center', 'center'];
 | 
        
           |  |  | 88 |         $table->attributes['class'] = 'manageqbanktable generaltable admintable';
 | 
        
           |  |  | 89 |         $table->data  = [];
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         $totalenabled = 0;
 | 
        
           |  |  | 92 |         $count = 0;
 | 
        
           |  |  | 93 |         foreach ($types as $type) {
 | 
        
           |  |  | 94 |             if ($type->is_enabled() && $type->is_installed_and_upgraded()) {
 | 
        
           |  |  | 95 |                 $totalenabled++;
 | 
        
           |  |  | 96 |             }
 | 
        
           |  |  | 97 |         }
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |         foreach ($types as $type) {
 | 
        
           |  |  | 100 |             $url = new \moodle_url('/admin/qbankplugins.php', ['sesskey' => sesskey(), 'name' => $type->name]);
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |             $class = '';
 | 
        
           |  |  | 103 |             if ($pluginmanager->get_plugin_info('qbank_'.$type->name)->get_status() ===
 | 
        
           |  |  | 104 |                     \core_plugin_manager::PLUGIN_STATUS_MISSING) {
 | 
        
           |  |  | 105 |                 $strtypename = $type->displayname.' ('.get_string('missingfromdisk').')';
 | 
        
           |  |  | 106 |             } else {
 | 
        
           |  |  | 107 |                 $strtypename = $type->displayname;
 | 
        
           |  |  | 108 |             }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 |             if ($type->is_enabled()) {
 | 
        
           |  |  | 111 |                 $hideshow = \html_writer::link($url->out(false, ['action' => 'disable']),
 | 
        
           |  |  | 112 |                         $OUTPUT->pix_icon('t/hide', $txt->disable, 'moodle', ['class' => 'iconsmall']));
 | 
        
           |  |  | 113 |             } else {
 | 
        
           |  |  | 114 |                 $class = 'dimmed_text';
 | 
        
           |  |  | 115 |                 $hideshow = \html_writer::link($url->out(false, ['action' => 'enable']),
 | 
        
           |  |  | 116 |                         $OUTPUT->pix_icon('t/show', $txt->enable, 'moodle', ['class' => 'iconsmall']));
 | 
        
           |  |  | 117 |             }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |             $settings = '';
 | 
        
           |  |  | 120 |             if ($type->get_settings_url()) {
 | 
        
           |  |  | 121 |                 $settings = \html_writer::link($type->get_settings_url(), $txt->settings);
 | 
        
           |  |  | 122 |             }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |             $uninstall = '';
 | 
        
           |  |  | 125 |             if ($uninstallurl = \core_plugin_manager::instance()->get_uninstall_url(
 | 
        
           |  |  | 126 |                     'qbank_'.$type->name, 'manage')) {
 | 
        
           |  |  | 127 |                 $uninstall = \html_writer::link($uninstallurl, $txt->uninstall);
 | 
        
           |  |  | 128 |             }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |             $row = new \html_table_row([$strtypename, $hideshow, $settings, $uninstall]);
 | 
        
           |  |  | 131 |             if ($class) {
 | 
        
           |  |  | 132 |                 $row->attributes['class'] = $class;
 | 
        
           |  |  | 133 |             }
 | 
        
           |  |  | 134 |             $table->data[] = $row;
 | 
        
           |  |  | 135 |             $count++;
 | 
        
           |  |  | 136 |         }
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |         // Sort table data.
 | 
        
           |  |  | 139 |         usort($table->data, function($a, $b) {
 | 
        
           |  |  | 140 |             $aid = $a->cells[0]->text;
 | 
        
           |  |  | 141 |             $bid = $b->cells[0]->text;
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |             if ($aid == $bid) {
 | 
        
           |  |  | 144 |                 return 0;
 | 
        
           |  |  | 145 |             }
 | 
        
           |  |  | 146 |             return $aid < $bid ? -1 : 1;
 | 
        
           |  |  | 147 |         });
 | 
        
           |  |  | 148 |   | 
        
           |  |  | 149 |         $return .= \html_writer::table($table);
 | 
        
           |  |  | 150 |         return highlight($query, $return);
 | 
        
           |  |  | 151 |     }
 | 
        
           |  |  | 152 | }
 |