Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2011 David Mudrak <david@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for text filters
  32   */
  33  class filter extends base {
  34  
  35      public function init_display_name() {
  36          if (!get_string_manager()->string_exists('filtername', $this->component)) {
  37              $this->displayname = '[filtername,' . $this->component . ']';
  38          } else {
  39              $this->displayname = get_string('filtername', $this->component);
  40          }
  41      }
  42  
  43      /**
  44       * Finds all enabled plugins, the result may include missing plugins.
  45       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  46       */
  47      public static function get_enabled_plugins() {
  48          global $DB, $CFG;
  49          require_once("$CFG->libdir/filterlib.php");
  50  
  51          $enabled = array();
  52          $filters = $DB->get_records_select('filter_active', "active <> :disabled AND contextid = :contextid", array(
  53              'disabled' => TEXTFILTER_DISABLED, 'contextid' => \context_system::instance()->id), 'filter ASC', 'id, filter');
  54          foreach ($filters as $filter) {
  55              $enabled[$filter->filter] = $filter->filter;
  56          }
  57  
  58          return $enabled;
  59      }
  60  
  61      public function get_settings_section_name() {
  62          return 'filtersetting' . $this->name;
  63      }
  64  
  65      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  66          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  67          $ADMIN = $adminroot; // May be used in settings.php.
  68          $plugininfo = $this; // Also can be used inside settings.php.
  69          $filter = $this;     // Also can be used inside settings.php.
  70  
  71          if (!$this->is_installed_and_upgraded()) {
  72              return;
  73          }
  74  
  75          if (!$hassiteconfig) {
  76              return;
  77          }
  78          if (file_exists($this->full_path('settings.php'))) {
  79              $fullpath = $this->full_path('settings.php');
  80          } else if (file_exists($this->full_path('filtersettings.php'))) {
  81              $fullpath = $this->full_path('filtersettings.php');
  82          } else {
  83              return;
  84          }
  85  
  86          $section = $this->get_settings_section_name();
  87          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  88          include($fullpath); // This may also set $settings to null.
  89  
  90          if ($settings) {
  91              $ADMIN->add($parentnodename, $settings);
  92          }
  93      }
  94  
  95      public function is_uninstall_allowed() {
  96          return true;
  97      }
  98  
  99      /**
 100       * Return URL used for management of plugins of this type.
 101       * @return moodle_url
 102       */
 103      public static function get_manage_url() {
 104          return new moodle_url('/admin/filters.php');
 105      }
 106  
 107      /**
 108       * Pre-uninstall hook.
 109       *
 110       * This is intended for disabling of plugin, some DB table purging, etc.
 111       *
 112       * NOTE: to be called from uninstall_plugin() only.
 113       * @private
 114       */
 115      public function uninstall_cleanup() {
 116          global $DB, $CFG;
 117  
 118          $DB->delete_records('filter_active', array('filter' => $this->name));
 119          $DB->delete_records('filter_config', array('filter' => $this->name));
 120  
 121          if (empty($CFG->filterall)) {
 122              $stringfilters = array();
 123          } else if (!empty($CFG->stringfilters)) {
 124              $stringfilters = explode(',', $CFG->stringfilters);
 125              $stringfilters = array_combine($stringfilters, $stringfilters);
 126          } else {
 127              $stringfilters = array();
 128          }
 129  
 130          unset($stringfilters[$this->name]);
 131  
 132          set_config('stringfilters', implode(',', $stringfilters));
 133          set_config('filterall', !empty($stringfilters));
 134  
 135          parent::uninstall_cleanup();
 136      }
 137  }