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 311 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  namespace core_block\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_multiple_structure;
  22  use core_external\external_single_structure;
  23  use core_external\external_value;
  24  
  25  /**
  26   * This is the external method used for fetching the addable blocks in a given page.
  27   *
  28   * @package    core_block
  29   * @since      Moodle 3.11
  30   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class fetch_addable_blocks extends external_api {
  34  
  35      /**
  36       * Describes the parameters for execute.
  37       *
  38       * @return external_function_parameters
  39       */
  40      public static function execute_parameters(): external_function_parameters {
  41          return new external_function_parameters(
  42              [
  43                  'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'),
  44                  'pagetype' => new external_value(PARAM_ALPHANUMEXT, 'The type of the page.'),
  45                  'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.'),
  46                  'subpage' => new external_value(PARAM_TEXT, 'The subpage identifier', VALUE_DEFAULT, ''),
  47                  'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'Page hash', VALUE_DEFAULT, ''),
  48              ]
  49          );
  50      }
  51  
  52      /**
  53       * Fetch the addable blocks in a given page.
  54       *
  55       * @param int $pagecontextid The context ID of the page
  56       * @param string $pagetype The type of the page
  57       * @param string $pagelayout The layout of the page
  58       * @param string $subpage The subpage identifier
  59       * @param string $pagehash Page hash that can be provided instead of all parameters above
  60       * @return array The blocks list
  61       */
  62      public static function execute(int $pagecontextid, string $pagetype, string $pagelayout,
  63              string $subpage = '', string $pagehash = ''): array {
  64          global $PAGE;
  65  
  66          $params = self::validate_parameters(self::execute_parameters(),
  67              [
  68                  'pagecontextid' => $pagecontextid,
  69                  'pagetype' => $pagetype,
  70                  'pagelayout' => $pagelayout,
  71                  'subpage' => $subpage,
  72                  'pagehash' => $pagehash,
  73              ]
  74          );
  75  
  76          if ($params['pagehash']) {
  77              // If pagehash is specified, all other parameters are ignored, all information
  78              // about the page is stored in the session.
  79  
  80              $page = \moodle_page::retrieve_edited_page($params['pagehash'], MUST_EXIST);
  81              self::validate_context($page->context);
  82          } else {
  83              // For backward-compatibility and Mobile App instead of pagehash
  84              // we can specify context, pagelayout, pagetype and subtype.
  85  
  86              $context = \context::instance_by_id($params['pagecontextid']);
  87              // Validate the context. This will also set the context in $PAGE.
  88              self::validate_context($context);
  89  
  90              // We need to manually set the page layout and page type.
  91              $PAGE->set_pagelayout($params['pagelayout']);
  92              $PAGE->set_pagetype($params['pagetype']);
  93              $PAGE->set_subpage($params['subpage']);
  94              $page = $PAGE;
  95          }
  96  
  97          // Firstly, we need to load all currently existing page blocks to later determine which blocks are addable.
  98          $page->blocks->load_blocks(false);
  99          $page->blocks->create_all_block_instances();
 100  
 101          $addableblocks = $page->blocks->get_addable_blocks();
 102  
 103          return array_map(function($block) use ($page) {
 104              $classname = \block_manager::get_block_edit_form_class($block->name);
 105              return [
 106                  'name' => $block->name,
 107                  'title' => get_string('pluginname', "block_{$block->name}"),
 108                  'blockform' => $classname::display_form_when_adding() ? $classname : null,
 109              ];
 110          }, $addableblocks);
 111      }
 112  
 113      /**
 114       * Describes the execute return value.
 115       *
 116       * @return external_multiple_structure
 117       */
 118      public static function execute_returns(): external_multiple_structure {
 119          return new external_multiple_structure(
 120              new external_single_structure(
 121                  [
 122                      'name' => new external_value(PARAM_PLUGIN, 'The name of the block.'),
 123                      'title' => new external_value(PARAM_RAW, 'The title of the block.'),
 124                      'blockform' => new external_value(PARAM_RAW,
 125                          'If this block type has a form when it is being added then the classname of the form')
 126                  ]
 127              ),
 128              'List of addable blocks in a given page.'
 129          );
 130      }
 131  }