Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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