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 preparing a entry for edition.
  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_multiple_structure;
  36  use core_external\external_single_structure;
  37  use core_external\external_value;
  38  use core_external\external_warnings;
  39  
  40  /**
  41   * This is the external method for preparing a entry for edition.
  42   *
  43   * @copyright  2020 Juan Leyva <juan@moodle.com>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class prepare_entry extends external_api {
  47      /**
  48       * Parameters.
  49       *
  50       * @return external_function_parameters
  51       */
  52      public static function execute_parameters(): external_function_parameters {
  53          return new external_function_parameters([
  54              'entryid' => new external_value(PARAM_INT, 'Glossary entry id to update'),
  55          ]);
  56      }
  57  
  58      /**
  59       * Prepare for update the indicated entry from the glossary.
  60       *
  61       * @param  int $entryid The entry to update
  62       * @return array with result and warnings
  63       * @throws moodle_exception
  64       */
  65      public static function execute(int $entryid): array {
  66          global $DB;
  67  
  68          $params = self::validate_parameters(self::execute_parameters(), compact('entryid'));
  69          $id = $params['entryid'];
  70  
  71          // Get and validate the glossary.
  72          $entry = $DB->get_record('glossary_entries', ['id' => $id], '*', MUST_EXIST);
  73          list($glossary, $context, $course, $cm) = \mod_glossary_external::validate_glossary($entry->glossaryid);
  74  
  75          // Check permissions.
  76          mod_glossary_can_update_entry($entry, $glossary, $context, $cm, false);
  77  
  78          list($definitionoptions, $attachmentoptions) = glossary_get_editor_and_attachment_options($course, $context, $entry);
  79  
  80          $entry->aliases = '';
  81          $entry->categories = [];
  82          $entry = mod_glossary_prepare_entry_for_edition($entry);
  83          $entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'mod_glossary', 'entry',
  84              $entry->id);
  85          $entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'mod_glossary', 'attachment',
  86              $entry->id);
  87  
  88          // Just get a structure compatible with external API.
  89          array_walk($definitionoptions, function(&$item, $key) use (&$definitionoptions) {
  90              if (!is_scalar($item)) {
  91                  unset($definitionoptions[$key]);
  92                  return;
  93              }
  94              $item = ['name' => $key, 'value' => $item];
  95          });
  96  
  97          array_walk($attachmentoptions, function(&$item, $key) use (&$attachmentoptions) {
  98              if (!is_scalar($item)) {
  99                  unset($attachmentoptions[$key]);
 100                  return;
 101              }
 102              $item = ['name' => $key, 'value' => $item];
 103          });
 104  
 105          return [
 106              'inlineattachmentsid' => $entry->definition_editor['itemid'],
 107              'attachmentsid' => $entry->attachment_filemanager,
 108              'areas' => [
 109                  [
 110                      'area' => 'definition',
 111                      'options' => $definitionoptions,
 112                  ],
 113                  [
 114                      'area' => 'attachment',
 115                      'options' => $attachmentoptions,
 116                  ],
 117              ],
 118              'aliases' => explode("\n", trim($entry->aliases)),
 119              'categories' => $entry->categories,
 120          ];
 121      }
 122  
 123      /**
 124       * Return.
 125       *
 126       * @return external_single_structure
 127       */
 128      public static function execute_returns(): external_single_structure {
 129          return new external_single_structure([
 130              'inlineattachmentsid' => new external_value(PARAM_INT, 'Draft item id for the text editor.'),
 131              'attachmentsid' => new external_value(PARAM_INT, 'Draft item id for the file manager.'),
 132              'areas' => new external_multiple_structure(
 133                  new external_single_structure(
 134                      [
 135                          'area' => new external_value(PARAM_ALPHA, 'File area name.'),
 136                          'options' => new external_multiple_structure(
 137                              new external_single_structure(
 138                                  [
 139                                      'name' => new external_value(PARAM_RAW, 'Name of option.'),
 140                                      'value' => new external_value(PARAM_RAW, 'Value of option.'),
 141                                  ]
 142                              ), 'Draft file area options.'
 143                          )
 144                      ]
 145                  ), 'File areas including options'
 146              ),
 147              'aliases' => new external_multiple_structure(new external_value(PARAM_RAW, 'Alias name.')),
 148              'categories' => new external_multiple_structure(new external_value(PARAM_INT, 'Category id')),
 149              'warnings' => new external_warnings(),
 150          ]);
 151      }
 152  }