Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * External API to rename content bank content.
  19   *
  20   * @package    core_contentbank
  21   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_contentbank\external;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->libdir . '/externallib.php');
  31  
  32  use external_api;
  33  use external_function_parameters;
  34  use external_single_structure;
  35  use external_value;
  36  use external_warnings;
  37  
  38  /**
  39   * This is the external method for renaming a content.
  40   *
  41   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class rename_content extends external_api {
  45      /**
  46       * rename_content parameters.
  47       *
  48       * @since  Moodle 3.9
  49       * @return external_function_parameters
  50       */
  51      public static function execute_parameters(): external_function_parameters {
  52          return new external_function_parameters(
  53              [
  54                  'contentid' => new external_value(PARAM_INT, 'The content id to rename', VALUE_REQUIRED),
  55                  'name' => new external_value(PARAM_RAW, 'The new name for the content', VALUE_REQUIRED),
  56              ]
  57          );
  58      }
  59  
  60      /**
  61       * Rename content from the contentbank.
  62       *
  63       * @since  Moodle 3.9
  64       * @param  int $contentid The content id to rename.
  65       * @param  string $name The new name.
  66       * @return array True if the content has been renamed; false and the warning, otherwise.
  67       */
  68      public static function execute(int $contentid, string $name): array {
  69          global $DB;
  70  
  71          $result = false;
  72          $warnings = [];
  73  
  74          $params = self::validate_parameters(self::execute_parameters(), [
  75              'contentid' => $contentid,
  76              'name' => $name,
  77          ]);
  78          $params['name'] = clean_param($params['name'], PARAM_TEXT);
  79          try {
  80              $record = $DB->get_record('contentbank_content', ['id' => $params['contentid']], '*', MUST_EXIST);
  81              $contenttypeclass = "\\$record->contenttype\\contenttype";
  82              if (class_exists($contenttypeclass)) {
  83                  $context = \context::instance_by_id($record->contextid, MUST_EXIST);
  84                  self::validate_context($context);
  85                  $contenttype = new $contenttypeclass($context);
  86                  $contentclass = "\\$record->contenttype\\content";
  87                  $content = new $contentclass($record);
  88                  // Check capability.
  89                  if ($contenttype->can_manage($content)) {
  90                      if (trim($params['name']) === '') {
  91                          // If name is empty don't try to rename and return a more detailed message.
  92                          $warnings[] = [
  93                              'item' => $params['contentid'],
  94                              'warningcode' => 'emptynamenotallowed',
  95                              'message' => get_string('emptynamenotallowed', 'core_contentbank')
  96                          ];
  97                      } else {
  98                          // This content can be renamed.
  99                          if ($contenttype->rename_content($content, $params['name'])) {
 100                              $result = true;
 101                          } else {
 102                              $warnings[] = [
 103                                  'item' => $params['contentid'],
 104                                  'warningcode' => 'contentnotrenamed',
 105                                  'message' => get_string('contentnotrenamed', 'core_contentbank')
 106                              ];
 107                          }
 108                      }
 109                  } else {
 110                      // The user has no permission to manage this content.
 111                      $warnings[] = [
 112                          'item' => $params['contentid'],
 113                          'warningcode' => 'nopermissiontomanage',
 114                          'message' => get_string('nopermissiontomanage', 'core_contentbank')
 115                      ];
 116                  }
 117              }
 118          } catch (\moodle_exception $e) {
 119              // The content or the context don't exist.
 120              $warnings[] = [
 121                  'item' => $params['contentid'],
 122                  'warningcode' => 'exception',
 123                  'message' => $e->getMessage()
 124              ];
 125          }
 126  
 127          return [
 128              'result' => $result,
 129              'warnings' => $warnings
 130          ];
 131      }
 132  
 133      /**
 134       * rename_content return.
 135       *
 136       * @since  Moodle 3.9
 137       * @return external_single_structure
 138       */
 139      public static function execute_returns(): external_single_structure {
 140          return new external_single_structure([
 141              'result' => new external_value(PARAM_BOOL, 'The processing result'),
 142              'warnings' => new external_warnings()
 143          ]);
 144      }
 145  }