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.
/admin/ -> filters.php (source)

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]

   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   * Filter management page.
  19   *
  20   * @package    core
  21   * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../config.php');
  26  require_once($CFG->libdir . '/adminlib.php');
  27  
  28  $action = optional_param('action', '', PARAM_ALPHA);
  29  $filterpath = optional_param('filterpath', '', PARAM_PLUGIN);
  30  
  31  admin_externalpage_setup('managefilters');
  32  
  33  // Clean up bogus filter states first.
  34  /** @var core\plugininfo\filter[] $plugininfos */
  35  $plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter');
  36  $filters = [];
  37  $states = filter_get_global_states();
  38  foreach ($states as $state) {
  39      if (!isset($plugininfos[$state->filter]) and !get_config('filter_'.$state->filter, 'version')) {
  40          // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installations.
  41          $DB->delete_records('filter_active', ['filter' => $state->filter]);
  42          $DB->delete_records('filter_config', ['filter' => $state->filter]);
  43          error_log('Deleted bogus "filter_'.$state->filter.'" states and config data.');
  44      } else {
  45          $filters[$state->filter] = $state;
  46      }
  47  }
  48  
  49  // Add properly installed and upgraded filters to the global states table.
  50  foreach ($plugininfos as $filter => $info) {
  51      if (isset($filters[$filter])) {
  52          continue;
  53      }
  54      if ($info->is_installed_and_upgraded()) {
  55          filter_set_global_state($filter, TEXTFILTER_DISABLED);
  56          $states = filter_get_global_states();
  57          foreach ($states as $state) {
  58              if ($state->filter === $filter) {
  59                  $filters[$filter] = $state;
  60                  break;
  61              }
  62          }
  63      }
  64  }
  65  
  66  if ($action) {
  67      require_sesskey();
  68  }
  69  
  70  // Process actions.
  71  switch ($action) {
  72  
  73      case 'setstate':
  74          if (isset($filters[$filterpath]) and $newstate = optional_param('newstate', '', PARAM_INT)) {
  75              /** @var \core\plugininfo\filter $class */
  76              $class = core_plugin_manager::resolve_plugininfo_class('filter');
  77              $class::enable_plugin($filterpath, $newstate);
  78          }
  79          break;
  80  
  81      case 'setapplyto':
  82          if (isset($filters[$filterpath])) {
  83              $applytostrings = optional_param('stringstoo', false, PARAM_BOOL);
  84              filter_set_applies_to_strings($filterpath, $applytostrings);
  85              reset_text_filters_cache();
  86              core_plugin_manager::reset_caches();
  87          }
  88          break;
  89  
  90      case 'down':
  91          if (isset($filters[$filterpath])) {
  92              filter_set_global_state($filterpath, $filters[$filterpath]->active, 1);
  93              reset_text_filters_cache();
  94              core_plugin_manager::reset_caches();
  95          }
  96          break;
  97  
  98      case 'up':
  99          if (isset($filters[$filterpath])) {
 100              $oldpos = $filters[$filterpath]->sortorder;
 101              filter_set_global_state($filterpath, $filters[$filterpath]->active, -1);
 102              reset_text_filters_cache();
 103              core_plugin_manager::reset_caches();
 104          }
 105          break;
 106  }
 107  
 108  // Return.
 109  if ($action) {
 110      redirect(new moodle_url('/admin/filters.php'));
 111  }
 112  
 113  // Print the page heading.
 114  echo $OUTPUT->header();
 115  echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
 116  
 117  $states = filter_get_global_states();
 118  $stringfilters = filter_get_string_filters();
 119  
 120  $table = new html_table();
 121  $table->head  = [get_string('filter'), get_string('isactive', 'filters'),
 122          get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('uninstallplugin', 'core_admin')];
 123  $table->colclasses = array ('leftalign', 'leftalign', 'centeralign', 'leftalign', 'leftalign', 'leftalign');
 124  $table->attributes['class'] = 'admintable generaltable';
 125  $table->id = 'filterssetting';
 126  $table->data  = [];
 127  
 128  $lastactive = null;
 129  foreach ($states as $state) {
 130      if ($state->active != TEXTFILTER_DISABLED) {
 131          $lastactive = $state->filter;
 132      }
 133  }
 134  
 135  // Iterate through filters adding to display table.
 136  $firstrow = true;
 137  foreach ($states as $state) {
 138      $filter = $state->filter;
 139      if (!isset($plugininfos[$filter])) {
 140          continue;
 141      }
 142      $plugininfo = $plugininfos[$filter];
 143      $applytostrings = isset($stringfilters[$filter]) && $state->active != TEXTFILTER_DISABLED;
 144      $row = get_table_row($plugininfo, $state, $firstrow, $filter == $lastactive, $applytostrings);
 145      $table->data[] = $row;
 146      if ($state->active == TEXTFILTER_DISABLED) {
 147          $table->rowclasses[] = 'dimmed_text';
 148      } else {
 149          $table->rowclasses[] = '';
 150      }
 151      $firstrow = false;
 152  }
 153  
 154  echo html_writer::table($table);
 155  echo '<p class="filtersettingnote">' . get_string('filterallwarning', 'filters') . '</p>';
 156  echo $OUTPUT->footer();
 157  die;
 158  
 159  
 160  /**
 161   * Return action URL.
 162   *
 163   * @param string $filterpath which filter to get the URL for.
 164   * @param string $action which action to get the URL for.
 165   * @return moodle_url|null the requested URL.
 166   */
 167  function filters_action_url(string $filterpath, string $action): ?moodle_url {
 168      if ($action === 'delete') {
 169          return core_plugin_manager::instance()->get_uninstall_url('filter_'.$filterpath, 'manage');
 170      }
 171      return new moodle_url('/admin/filters.php',
 172              ['sesskey' => sesskey(), 'filterpath' => $filterpath, 'action' => $action]);
 173  }
 174  
 175  /**
 176   * Construct table record.
 177   *
 178   * @param \core\plugininfo\filter $plugininfo
 179   * @param stdClass $state
 180   * @param bool $isfirstrow
 181   * @param bool $islastactive
 182   * @param bool $applytostrings
 183   * @return array data
 184   */
 185  function get_table_row(\core\plugininfo\filter $plugininfo, stdClass $state,
 186          bool $isfirstrow, bool $islastactive, bool $applytostrings): array {
 187      global $OUTPUT;
 188      $row = [];
 189      $filter = $state->filter;
 190      $active = $plugininfo->is_installed_and_upgraded();
 191  
 192      static $activechoices;
 193      static $applytochoices;
 194      if (!isset($activechoices)) {
 195          $activechoices = [
 196              TEXTFILTER_DISABLED => get_string('disabled', 'core_filters'),
 197              TEXTFILTER_OFF => get_string('offbutavailable', 'core_filters'),
 198              TEXTFILTER_ON => get_string('on', 'core_filters'),
 199          ];
 200          $applytochoices = [
 201              0 => get_string('content', 'core_filters'),
 202              1 => get_string('contentandheadings', 'core_filters'),
 203          ];
 204      }
 205  
 206      // Filter name.
 207      $displayname = $plugininfo->displayname;
 208      if (!$plugininfo->rootdir) {
 209          $displayname = '<span class="error">' . $displayname . ' - ' . get_string('status_missing', 'core_plugin') . '</span>';
 210      } else if (!$active) {
 211          $displayname = '<span class="error">' . $displayname . ' - ' . get_string('error') . '</span>';
 212      }
 213      $row[] = $displayname;
 214  
 215      // Disable/off/on.
 216      $select = new single_select(filters_action_url($filter, 'setstate'), 'newstate', $activechoices, $state->active, null, 'active' . $filter);
 217      $select->set_label(get_string('isactive', 'filters'), ['class' => 'accesshide']);
 218      $row[] = $OUTPUT->render($select);
 219  
 220      // Re-order.
 221      $updown = '';
 222      $spacer = $OUTPUT->spacer();
 223      if ($state->active != TEXTFILTER_DISABLED) {
 224          if (!$isfirstrow) {
 225              $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'up'),
 226                      new pix_icon('t/up', get_string('up'), '', ['class' => 'iconsmall']));
 227          } else {
 228              $updown .= $spacer;
 229          }
 230          if (!$islastactive) {
 231              $updown .= $OUTPUT->action_icon(filters_action_url($filter, 'down'),
 232                      new pix_icon('t/down', get_string('down'), '', ['class' => 'iconsmall']));
 233          } else {
 234              $updown .= $spacer;
 235          }
 236      }
 237      $row[] = $updown;
 238  
 239      // Apply to strings.
 240      $select = new single_select(filters_action_url($filter, 'setapplyto'),
 241              'stringstoo', $applytochoices, $applytostrings, null, 'applyto' . $filter);
 242      $select->set_label(get_string('applyto', 'filters'), ['class' => 'accesshide']);
 243      $select->disabled = ($state->active == TEXTFILTER_DISABLED);
 244      $row[] = $OUTPUT->render($select);
 245  
 246      // Settings link, if required.
 247      if ($active and filter_has_global_settings($filter)) {
 248          $row[] = html_writer::link(new moodle_url('/admin/settings.php',
 249                  ['section' => 'filtersetting'.$filter]), get_string('settings'));
 250      } else {
 251          $row[] = '';
 252      }
 253  
 254      // Uninstall.
 255      $row[] = html_writer::link(filters_action_url($filter, 'delete'),
 256              get_string('uninstallplugin', 'core_admin'));
 257  
 258      return $row;
 259  }