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   * This is the external method for deleting a content.
  19   *
  20   * @package    core_contentbank
  21   * @since      Moodle 3.9
  22   * @copyright  2020 Sara Arjona <sara@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_contentbank\external;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->libdir . '/externallib.php');
  32  
  33  use core_contentbank\contentbank;
  34  use external_api;
  35  use external_function_parameters;
  36  use external_multiple_structure;
  37  use external_single_structure;
  38  use external_value;
  39  use external_warnings;
  40  
  41  /**
  42   * This is the external method for deleting a content.
  43   *
  44   * @copyright  2020 Sara Arjona <sara@moodle.com>
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class delete_content extends external_api {
  48      /**
  49       * Parameters.
  50       *
  51       * @return external_function_parameters
  52       */
  53      public static function execute_parameters(): external_function_parameters {
  54          return new external_function_parameters([
  55              'contentids' => new external_multiple_structure(
  56                  new external_value(PARAM_INT, 'The content id to delete', VALUE_REQUIRED)
  57              )
  58          ]);
  59      }
  60  
  61      /**
  62       * Delete content from the contentbank.
  63       *
  64       * @param  array $contentids List of content ids to delete.
  65       * @return array True if the content has been deleted; false and the warning, otherwise.
  66       */
  67      public static function execute(array $contentids): array {
  68          global $DB;
  69  
  70          $result = false;
  71          $warnings = [];
  72  
  73          $params = self::validate_parameters(self::execute_parameters(), ['contentids' => $contentids]);
  74          $cb = new contentbank();
  75          foreach ($params['contentids'] as $contentid) {
  76              try {
  77                  $record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST);
  78                  $content = $cb->get_content_from_id($record->id);
  79                  $contenttype = $content->get_content_type_instance();
  80                  $context = \context::instance_by_id($record->contextid, MUST_EXIST);
  81                  self::validate_context($context);
  82                  // Check capability.
  83                  if ($contenttype->can_delete($content)) {
  84                      // This content can be deleted.
  85                      if (!$contenttype->delete_content($content)) {
  86                          $warnings[] = [
  87                              'item' => $contentid,
  88                              'warningcode' => 'contentnotdeleted',
  89                              'message' => get_string('contentnotdeleted', 'core_contentbank')
  90                          ];
  91                      }
  92                  } else {
  93                      // The user has no permission to delete this content.
  94                      $warnings[] = [
  95                          'item' => $contentid,
  96                          'warningcode' => 'nopermissiontodelete',
  97                          'message' => get_string('nopermissiontodelete', 'core_contentbank')
  98                      ];
  99                  }
 100              } catch (\moodle_exception $e) {
 101                  // The content or the context don't exist.
 102                  $warnings[] = [
 103                      'item' => $contentid,
 104                      'warningcode' => 'exception',
 105                      'message' => $e->getMessage()
 106                  ];
 107              }
 108          }
 109  
 110          if (empty($warnings)) {
 111              $result = true;
 112          }
 113  
 114          return [
 115              'result' => $result,
 116              'warnings' => $warnings
 117          ];
 118      }
 119  
 120      /**
 121       * Return.
 122       *
 123       * @return external_single_structure
 124       */
 125      public static function execute_returns(): external_single_structure {
 126          return new external_single_structure([
 127              'result' => new external_value(PARAM_BOOL, 'The processing result'),
 128              'warnings' => new external_warnings()
 129          ]);
 130      }
 131  }