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.
   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  namespace core_admin\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_single_structure;
  22  use core_external\external_value;
  23  
  24  /**
  25   * Web Service to control the state of a plugin.
  26   *
  27   * @package   core_admin
  28   * @category  external
  29   * @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class set_plugin_state extends external_api {
  33      /**
  34       * Returns description of method parameters
  35       *
  36       * @return external_function_parameters
  37       */
  38      public static function execute_parameters(): external_function_parameters {
  39          return new external_function_parameters([
  40              'plugin' => new external_value(PARAM_PLUGIN, 'The name of the plugin', VALUE_REQUIRED),
  41              'state' => new external_value(PARAM_INT, 'The target state', VALUE_REQUIRED),
  42          ]);
  43      }
  44  
  45      /**
  46       * Set the plugin state.
  47       *
  48       * @param string $plugin The name of the plugin
  49       * @param int $state The target state
  50       * @return null
  51       */
  52      public static function execute(
  53          string $plugin,
  54          int $state,
  55      ): array {
  56          [
  57              'plugin' => $plugin,
  58              'state' => $state,
  59          ] = self::validate_parameters(self::execute_parameters(), [
  60              'plugin' => $plugin,
  61              'state' => $state,
  62          ]);
  63  
  64          $context = \context_system::instance();
  65          self::validate_context($context);
  66          require_capability('moodle/site:config', $context);
  67  
  68          [$plugintype, $pluginname] = explode('_', \core_component::normalize_componentname($plugin), 2);
  69  
  70          $manager = \core_plugin_manager::resolve_plugininfo_class($plugintype);
  71          $displayname = get_string('pluginname', $plugin);
  72  
  73          if ($manager::enable_plugin($pluginname, $state)) {
  74              if (!empty($state)) {
  75                  \core\notification::add(
  76                      get_string('plugin_enabled', 'core_admin', $displayname),
  77                      \core\notification::SUCCESS
  78                  );
  79              } else {
  80                  \core\notification::add(
  81                      get_string('plugin_disabled', 'core_admin', $displayname),
  82                      \core\notification::SUCCESS
  83                  );
  84              }
  85          }
  86  
  87          return [];
  88      }
  89  
  90      /**
  91       * Describe the return structure of the external service.
  92       *
  93       * @return external_single_structure
  94       */
  95      public static function execute_returns(): external_single_structure {
  96          return new external_single_structure([]);
  97      }
  98  }