Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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