Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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   * Subplugin info class.
  19   *
  20   * @package   tool_log
  21   * @copyright 2013 Petr Skoda {@link http://skodak.org}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_log\plugininfo;
  25  
  26  use admin_settingpage;
  27  use core\plugininfo\base;
  28  use moodle_url;
  29  use part_of_admin_tree;
  30  
  31  /**
  32   * Plugin info class for logging store plugins.
  33   */
  34  class logstore extends base {
  35  
  36      public static function plugintype_supports_disabling(): bool {
  37          return true;
  38      }
  39  
  40      public function is_enabled() {
  41          $enabled = get_config('tool_log', 'enabled_stores');
  42          if (!$enabled) {
  43              return false;
  44          }
  45  
  46          $enabled = array_flip(explode(',', $enabled));
  47          return isset($enabled['logstore_' . $this->name]);
  48      }
  49  
  50      public static function enable_plugin(string $pluginname, int $enabled): bool {
  51          $haschanged = false;
  52          $plugins = [];
  53          $oldvalue = get_config('tool_log', 'enabled_stores');
  54          if (!empty($oldvalue)) {
  55              $plugins = array_flip(explode(',', $oldvalue));
  56          }
  57          // Only set visibility if it's different from the current value.
  58          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  59              $plugins[$pluginname] = $pluginname;
  60              $haschanged = true;
  61          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  62              unset($plugins[$pluginname]);
  63              $haschanged = true;
  64          }
  65  
  66          if ($haschanged) {
  67              $new = implode(',', array_flip($plugins));
  68              add_to_config_log('tool_logstore_visibility', !$enabled, $enabled, $pluginname);
  69              set_config('enabled_stores', $new, 'tool_log');
  70              // Reset caches.
  71              \core_plugin_manager::reset_caches();
  72          }
  73  
  74          return $haschanged;
  75      }
  76  
  77      public function get_settings_section_name() {
  78          return 'logsetting' . $this->name;
  79      }
  80  
  81      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  82          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  83          /** @var \admin_root $ADMIN */
  84          $ADMIN = $adminroot; // May be used in settings.php.
  85          $section = $this->get_settings_section_name();
  86  
  87          if (!$this->is_installed_and_upgraded()) {
  88              return;
  89          }
  90  
  91          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
  92              return;
  93          }
  94  
  95          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  96          include($this->full_path('settings.php'));
  97  
  98          if ($settings) {
  99              $ADMIN->add($parentnodename, $settings);
 100          }
 101      }
 102  
 103      public static function get_manage_url() {
 104          return new moodle_url('/admin/settings.php', array('section' => 'managelogging'));
 105      }
 106  
 107      public function is_uninstall_allowed() {
 108          return true;
 109      }
 110  
 111      public function uninstall_cleanup() {
 112          $enabled = get_config('tool_log', 'enabled_stores');
 113          if ($enabled) {
 114              $enabled = array_flip(explode(',', $enabled));
 115              unset($enabled['logstore_' . $this->name]);
 116              $enabled = array_flip($enabled);
 117              set_config('enabled_stores', implode(',', $enabled), 'tool_log');
 118          }
 119  
 120          parent::uninstall_cleanup();
 121      }
 122  }