Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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  namespace mod_data\external;
  18  
  19  use core\notification;
  20  use mod_data\local\importer\preset_importer;
  21  use mod_data\manager;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->libdir . '/externallib.php');
  27  
  28  /**
  29   * This is the external method for deleting a saved preset.
  30   *
  31   * @package    mod_data
  32   * @since      Moodle 4.1
  33   * @copyright  2022 Amaia Anabitarte <amaia@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class get_mapping_information extends \external_api {
  37      /**
  38       * Parameters.
  39       *
  40       * @return \external_function_parameters
  41       */
  42      public static function execute_parameters(): \external_function_parameters {
  43          return new \external_function_parameters([
  44              'cmid' => new \external_value(PARAM_INT, 'Id of the data activity', VALUE_REQUIRED),
  45              'importedpreset' => new \external_value(PARAM_TEXT, 'Preset to be imported'),
  46          ]);
  47      }
  48  
  49      /**
  50       * Get importing information for the given database course module.
  51       *
  52       * @param  int $cmid Id of the course module where to import the preset.
  53       * @param  string $importedpreset Plugin or saved preset to be imported.
  54       * @return array Information needed to decide whether to show the dialogue or not.
  55       */
  56      public static function execute(int $cmid, string $importedpreset): array {
  57  
  58          $params = self::validate_parameters(
  59              self::execute_parameters(),
  60              ['cmid' => $cmid, 'importedpreset' => $importedpreset]
  61          );
  62  
  63          try {
  64              // Let's get the manager.
  65              list($course, $cm) = get_course_and_cm_from_cmid($params['cmid'], manager::MODULE);
  66              $manager = manager::create_from_coursemodule($cm);
  67  
  68              $importer = preset_importer::create_from_plugin_or_directory($manager, $params['importedpreset']);
  69              $result['data'] = $importer->get_mapping_information();
  70          } catch (\moodle_exception $e) {
  71              $result['warnings'][] = [
  72                  'item' => $importedpreset,
  73                  'warningcode' => 'exception',
  74                  'message' => $e->getMessage()
  75              ];
  76              notification::error($e->getMessage());
  77          }
  78          return $result;
  79      }
  80  
  81      /**
  82       * Return.
  83       *
  84       * @return \external_single_structure
  85       */
  86      public static function execute_returns(): \external_single_structure {
  87          return new \external_single_structure([
  88              'data' => new \external_single_structure([
  89                  'needsmapping' => new \external_value(PARAM_BOOL, 'Whether the importing needs mapping or not'),
  90                  'presetname' => new \external_value(PARAM_TEXT, 'Name of the applied preset'),
  91                  'fieldstocreate' => new \external_value(PARAM_TEXT, 'List of field names to create'),
  92                  'fieldstoremove' => new \external_value(PARAM_TEXT, 'List of field names to remove'),
  93              ], 'Information to import if everything went fine', VALUE_OPTIONAL),
  94              'warnings' => new \external_warnings(),
  95          ]);
  96      }
  97  }