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