/lib/classes/plugininfo/message.php

https://github.com/markn86/moodle · PHP · 128 lines · 64 code · 21 blank · 43 comment · 10 complexity · d0414460210353af3def030694508e31 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;
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Class for messaging processors
  28. */
  29. class message extends base {
  30. /**
  31. * Finds all enabled plugins, the result may include missing plugins.
  32. * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  33. */
  34. public static function get_enabled_plugins() {
  35. global $DB;
  36. return $DB->get_records_menu('message_processors', array('enabled'=>1), 'name ASC', 'name, name AS val');
  37. }
  38. public static function enable_plugin(string $pluginname, int $enabled): bool {
  39. global $DB;
  40. if (!$plugin = $DB->get_record('message_processors', ['name' => $pluginname])) {
  41. throw new \moodle_exception('invalidplugin', 'message', '', $pluginname);
  42. }
  43. $haschanged = false;
  44. // Only set visibility if it's different from the current value.
  45. if ($plugin->enabled != $enabled) {
  46. $haschanged = true;
  47. $processor = \core_message\api::get_processed_processor_object($plugin);
  48. // Include this information into config changes table.
  49. add_to_config_log($processor->name, $processor->enabled, $enabled, 'core');
  50. // Save processor enabled/disabled status.
  51. \core_message\api::update_processor_status($processor, $enabled);
  52. }
  53. return $haschanged;
  54. }
  55. public function get_settings_section_name() {
  56. return 'messagesetting' . $this->name;
  57. }
  58. public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  59. global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  60. $ADMIN = $adminroot; // May be used in settings.php.
  61. $plugininfo = $this; // Also can be used inside settings.php.
  62. if (!$this->is_installed_and_upgraded()) {
  63. return;
  64. }
  65. if (!$hassiteconfig) {
  66. return;
  67. }
  68. $section = $this->get_settings_section_name();
  69. $settings = null;
  70. $processors = get_message_processors();
  71. if (isset($processors[$this->name])) {
  72. $processor = $processors[$this->name];
  73. if ($processor->available && $processor->hassettings) {
  74. $settings = new admin_settingpage($section, $this->displayname,
  75. 'moodle/site:config', $this->is_enabled() === false);
  76. include($this->full_path('settings.php')); // This may also set $settings to null.
  77. }
  78. }
  79. if ($settings) {
  80. $ADMIN->add($parentnodename, $settings);
  81. }
  82. }
  83. /**
  84. * Return URL used for management of plugins of this type.
  85. * @return moodle_url
  86. */
  87. public static function get_manage_url() {
  88. return new moodle_url('/admin/message.php');
  89. }
  90. public function is_uninstall_allowed() {
  91. return true;
  92. }
  93. /**
  94. * Pre-uninstall hook.
  95. *
  96. * This is intended for disabling of plugin, some DB table purging, etc.
  97. *
  98. * NOTE: to be called from uninstall_plugin() only.
  99. * @private
  100. */
  101. public function uninstall_cleanup() {
  102. global $CFG;
  103. require_once($CFG->libdir.'/messagelib.php');
  104. message_processor_uninstall($this->name);
  105. parent::uninstall_cleanup();
  106. }
  107. }