Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Allows the admin to manage question behaviours.
  20   *
  21   * @package    moodlecore
  22   * @subpackage questionengine
  23   * @copyright  2011 The Open University
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  
  28  require_once(__DIR__ . '/../config.php');
  29  require_once($CFG->libdir . '/questionlib.php');
  30  require_once($CFG->libdir . '/adminlib.php');
  31  require_once($CFG->libdir . '/tablelib.php');
  32  
  33  // Check permissions.
  34  require_login(null, false);
  35  $systemcontext = context_system::instance();
  36  require_capability('moodle/question:config', $systemcontext);
  37  
  38  admin_externalpage_setup('manageqbehaviours');
  39  $thispageurl = new moodle_url('/admin/qbehaviours.php');
  40  
  41  $behaviours = core_component::get_plugin_list('qbehaviour');
  42  $pluginmanager = core_plugin_manager::instance();
  43  
  44  // Get some data we will need - question counts and which types are needed.
  45  $counts = $DB->get_records_sql_menu("
  46          SELECT behaviour, COUNT(1)
  47          FROM {question_attempts} GROUP BY behaviour");
  48  $needed = array();
  49  $archetypal = array();
  50  foreach ($behaviours as $behaviour => $notused) {
  51      if (!array_key_exists($behaviour, $counts)) {
  52          $counts[$behaviour] = 0;
  53      }
  54      $needed[$behaviour] = ($counts[$behaviour] > 0) ||
  55              $pluginmanager->other_plugins_that_require('qbehaviour_' . $behaviour);
  56      $archetypal[$behaviour] = question_engine::is_behaviour_archetypal($behaviour);
  57  }
  58  foreach ($counts as $behaviour => $count) {
  59      if (!array_key_exists($behaviour, $behaviours)) {
  60          $counts['missing'] += $count;
  61      }
  62  }
  63  $needed['missing'] = true;
  64  
  65  // Work of the correct sort order.
  66  $config = get_config('question');
  67  $sortedbehaviours = array();
  68  foreach ($behaviours as $behaviour => $notused) {
  69      $sortedbehaviours[$behaviour] = question_engine::get_behaviour_name($behaviour);
  70  }
  71  if (!empty($config->behavioursortorder)) {
  72      $sortedbehaviours = question_engine::sort_behaviours($sortedbehaviours,
  73              $config->behavioursortorder, '');
  74  }
  75  
  76  if (!empty($config->disabledbehaviours)) {
  77      $disabledbehaviours = explode(',', $config->disabledbehaviours);
  78  } else {
  79      $disabledbehaviours = array();
  80  }
  81  
  82  // Process actions ============================================================
  83  
  84  // Disable.
  85  if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  86      if (!isset($behaviours[$disable])) {
  87          print_error('unknownbehaviour', 'question', $thispageurl, $disable);
  88      }
  89  
  90      if (array_search($disable, $disabledbehaviours) === false) {
  91          $class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
  92          $class::enable_plugin($disable, false);
  93      }
  94      redirect($thispageurl);
  95  }
  96  
  97  // Enable.
  98  if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
  99      if (!isset($behaviours[$enable])) {
 100          print_error('unknownbehaviour', 'question', $thispageurl, $enable);
 101      }
 102  
 103      if (!$archetypal[$enable]) {
 104          print_error('cannotenablebehaviour', 'question', $thispageurl, $enable);
 105      }
 106  
 107      if (($key = array_search($enable, $disabledbehaviours)) !== false) {
 108          $class = \core_plugin_manager::resolve_plugininfo_class('qbehaviour');
 109          $class::enable_plugin($enable, true);
 110      }
 111      redirect($thispageurl);
 112  }
 113  
 114  // Move up in order.
 115  if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
 116      if (!isset($behaviours[$up])) {
 117          print_error('unknownbehaviour', 'question', $thispageurl, $up);
 118      }
 119  
 120      // This function works fine for behaviours, as well as qtypes.
 121      $neworder = question_reorder_qtypes($sortedbehaviours, $up, -1);
 122      set_config('behavioursortorder', implode(',', $neworder), 'question');
 123      redirect($thispageurl);
 124  }
 125  
 126  // Move down in order.
 127  if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
 128      if (!isset($behaviours[$down])) {
 129          print_error('unknownbehaviour', 'question', $thispageurl, $down);
 130      }
 131  
 132      // This function works fine for behaviours, as well as qtypes.
 133      $neworder = question_reorder_qtypes($sortedbehaviours, $down, +1);
 134      set_config('behavioursortorder', implode(',', $neworder), 'question');
 135      redirect($thispageurl);
 136  }
 137  
 138  // End of process actions ==================================================
 139  
 140  // Print the page heading.
 141  echo $OUTPUT->header();
 142  echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin'));
 143  
 144  // Set up the table.
 145  $table = new flexible_table('qbehaviouradmintable');
 146  $table->define_baseurl($thispageurl);
 147  $table->define_columns(array('behaviour', 'numqas', 'version', 'requires',
 148          'available', 'uninstall'));
 149  $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'),
 150          get_string('version'), get_string('requires', 'admin'),
 151          get_string('availableq', 'question'), get_string('uninstallplugin', 'core_admin')));
 152  $table->set_attribute('id', 'qbehaviours');
 153  $table->set_attribute('class', 'generaltable admintable');
 154  $table->setup();
 155  
 156  // Add a row for each question type.
 157  foreach ($sortedbehaviours as $behaviour => $behaviourname) {
 158      $row = array();
 159  
 160      // Question icon and name.
 161      $row[] = $behaviourname;
 162  
 163      // Count
 164      $row[] = $counts[$behaviour];
 165  
 166      // Question version number.
 167      $version = get_config('qbehaviour_' . $behaviour, 'version');
 168      if ($version) {
 169          $row[] = $version;
 170      } else {
 171          $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'text-muted'));
 172      }
 173  
 174      // Other question types required by this one.
 175      $plugin = $pluginmanager->get_plugin_info('qbehaviour_' . $behaviour);
 176      $required = $plugin->get_other_required_plugins();
 177      if (!empty($required)) {
 178          $strrequired = array();
 179          foreach ($required as $component => $notused) {
 180              $strrequired[] = $pluginmanager->plugin_name($component);
 181          }
 182          $row[] = implode(', ', $strrequired);
 183      } else {
 184          $row[] = '';
 185      }
 186  
 187      // Are people allowed to select this behaviour?
 188      $rowclass = '';
 189      if ($archetypal[$behaviour]) {
 190          $enabled = array_search($behaviour, $disabledbehaviours) === false;
 191          $icons = question_behaviour_enable_disable_icons($behaviour, $enabled);
 192          if (!$enabled) {
 193              $rowclass = 'dimmed_text';
 194          }
 195      } else {
 196          $icons = $OUTPUT->spacer(array('class' => 'iconsmall'));
 197      }
 198  
 199      // Move icons.
 200      $icons .= question_behaviour_icon_html('up', $behaviour, 't/up', get_string('up'), null);
 201      $icons .= question_behaviour_icon_html('down', $behaviour, 't/down', get_string('down'), null);
 202      $row[] = $icons;
 203  
 204      // Delete link, if available.
 205      if ($needed[$behaviour]) {
 206          $row[] = '';
 207      } else {
 208          $uninstallurl = core_plugin_manager::instance()->get_uninstall_url('qbehaviour_'.$behaviour, 'manage');
 209          if ($uninstallurl) {
 210              $row[] = html_writer::link($uninstallurl, get_string('uninstallplugin', 'core_admin'),
 211                  array('title' => get_string('uninstallbehaviour', 'question')));
 212          }
 213      }
 214  
 215      $table->add_data($row, $rowclass);
 216  }
 217  
 218  $table->finish_output();
 219  
 220  echo $OUTPUT->footer();
 221  
 222  function question_behaviour_enable_disable_icons($behaviour, $enabled) {
 223      if ($enabled) {
 224          return question_behaviour_icon_html('disable', $behaviour, 't/hide',
 225                  get_string('enabled', 'question'), get_string('disable'));
 226      } else {
 227          return question_behaviour_icon_html('enable', $behaviour, 't/show',
 228                  get_string('disabled', 'question'), get_string('enable'));
 229      }
 230  }
 231  
 232  function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) {
 233      global $OUTPUT;
 234      return $OUTPUT->action_icon(new moodle_url('/admin/qbehaviours.php',
 235              array($action => $behaviour, 'sesskey' => sesskey())),
 236              new pix_icon($icon, $alt, 'moodle', array('title' => '', 'class' => 'iconsmall')),
 237              null, array('title' => $tip));
 238  }