Search moodle.org's
Developer Documentation

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
  • /admin/ -> filters.php (source)

    Differences Between: [Versions 310 and 34] [Versions 310 and 35] [Versions 34 and 310] [Versions 35 and 310]

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