/lib/classes/plugininfo/filter.php

https://github.com/vadimonus/moodle · PHP · 136 lines · 74 code · 22 blank · 40 comment · 14 complexity · 1c7654e6525b845a74b4e719a34bf2d5 MD5 · raw file

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