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 310 and 402] [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  /**
  18   * This is the external method for deleting a content.
  19   *
  20   * @package    mod_glossary
  21   * @since      Moodle 3.10
  22   * @copyright  2020 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_glossary\external;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/mod/glossary/lib.php');
  32  
  33  use core_external\external_api;
  34  use core_external\external_function_parameters;
  35  use core_external\external_single_structure;
  36  use core_external\external_value;
  37  use core_external\external_warnings;
  38  
  39  /**
  40   * This is the external method for deleting a content.
  41   *
  42   * @copyright  2020 Juan Leyva <juan@moodle.com>
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class delete_entry extends external_api {
  46      /**
  47       * Parameters.
  48       *
  49       * @return external_function_parameters
  50       */
  51      public static function execute_parameters(): external_function_parameters {
  52          return new external_function_parameters([
  53              'entryid' => new external_value(PARAM_INT, 'Glossary entry id to delete'),
  54          ]);
  55      }
  56  
  57      /**
  58       * Delete the indicated entry from the glossary.
  59       *
  60       * @param  int $entryid The entry to delete
  61       * @return array with result and warnings
  62       * @throws moodle_exception
  63       */
  64      public static function execute(int $entryid): array {
  65          global $DB;
  66  
  67          $params = self::validate_parameters(self::execute_parameters(), compact('entryid'));
  68          $id = $params['entryid'];
  69  
  70          // Get and validate the glossary.
  71          $entry = $DB->get_record('glossary_entries', ['id' => $id], '*', MUST_EXIST);
  72          list($glossary, $context, $course, $cm) = \mod_glossary_external::validate_glossary($entry->glossaryid);
  73  
  74          // Check and delete.
  75          mod_glossary_can_delete_entry($entry, $glossary, $context, false);
  76          mod_glossary_delete_entry($entry, $glossary, $cm, $context, $course);
  77  
  78          return [
  79              'result' => true,
  80              'warnings' => [],
  81          ];
  82      }
  83  
  84      /**
  85       * Return.
  86       *
  87       * @return external_single_structure
  88       */
  89      public static function execute_returns(): external_single_structure {
  90          return new external_single_structure([
  91              'result' => new external_value(PARAM_BOOL, 'The processing result'),
  92              'warnings' => new external_warnings()
  93          ]);
  94      }
  95  }