Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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