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 39 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  use core_external\external_api;
  18  use core_external\external_function_parameters;
  19  use core_external\external_value;
  20  
  21  /**
  22   * tool_xmldb external function
  23   *
  24   * @package    tool_xmldb
  25   * @copyright  2018 Moodle
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class tool_xmldb_external extends external_api {
  29  
  30      /**
  31       * Parameters for the 'tool_xmldb_invoke_move_action' WS
  32       * @return external_function_parameters
  33       */
  34      public static function invoke_move_action_parameters() {
  35          return new external_function_parameters([
  36              'action' => new external_value(PARAM_ALPHAEXT, 'Action'),
  37              'dir' => new external_value(PARAM_PATH, 'Plugin that is being edited'),
  38              'table' => new external_value(PARAM_NOTAGS, 'Table name'),
  39              'field' => new external_value(PARAM_NOTAGS, 'Field name', VALUE_DEFAULT, ''),
  40              'key' => new external_value(PARAM_NOTAGS, 'Key name', VALUE_DEFAULT, ''),
  41              'index' => new external_value(PARAM_NOTAGS, 'Index name', VALUE_DEFAULT, ''),
  42              'position' => new external_value(PARAM_INT, 'How many positions to move by (negative - up, positive - down)'),
  43          ]);
  44      }
  45  
  46      /**
  47       * WS 'tool_xmldb_invoke_move_action' that invokes a move action
  48       *
  49       * @param string $action
  50       * @param string $dir
  51       * @param string $table
  52       * @param string $field
  53       * @param string $key
  54       * @param string $index
  55       * @param int $position
  56       * @throws coding_exception
  57       */
  58      public static function invoke_move_action($action, $dir, $table, $field, $key, $index, $position) {
  59          global $CFG, $XMLDB, $SESSION;
  60          require_once($CFG->libdir.'/ddllib.php');
  61          require_once("$CFG->dirroot/$CFG->admin/tool/xmldb/actions/XMLDBAction.class.php");
  62          require_once("$CFG->dirroot/$CFG->admin/tool/xmldb/actions/XMLDBCheckAction.class.php");
  63          $params = self::validate_parameters(self::invoke_move_action_parameters(), [
  64              'action' => $action,
  65              'dir' => $dir,
  66              'table' => $table,
  67              'field' => $field,
  68              'key' => $key,
  69              'index' => $index,
  70              'position' => $position
  71          ]);
  72  
  73          self::validate_context(context_system::instance());
  74          require_capability('moodle/site:config', context_system::instance());
  75  
  76          if (!in_array($action, ['move_updown_table', 'move_updown_field', 'move_updown_key', 'move_updown_index'])) {
  77              throw new coding_exception('Unsupported action');
  78          }
  79  
  80          $action = $params['action'];
  81          $actionsroot = "$CFG->dirroot/$CFG->admin/tool/xmldb/actions";
  82          $actionclass = $action . '.class.php';
  83          $actionpath = "$actionsroot/$action/$actionclass";
  84  
  85          if (file_exists($actionpath) && is_readable($actionpath)) {
  86              require_once($actionpath);
  87          }
  88          if (!class_exists($action)) {
  89              throw new coding_exception('Action class not found');
  90          }
  91  
  92          if (!isset($SESSION->xmldb)) {
  93              $XMLDB = new stdClass;
  94          } else {
  95              $XMLDB = unserialize($SESSION->xmldb);
  96          }
  97  
  98          $_POST['dir'] = $params['dir'];
  99          $_POST['table'] = $params['table'];
 100          $_POST['field'] = $params['field'];
 101          $_POST['key'] = $params['key'];
 102          $_POST['index'] = $params['index'];
 103          $_POST['direction'] = ($params['position'] > 0) ? 'down' : 'up';
 104          for ($i = 0; $i < abs($params['position']); $i++) {
 105              $a = new $action();
 106              $a->invoke();
 107          }
 108          $SESSION->xmldb = serialize($XMLDB);
 109      }
 110  
 111      /**
 112       * Return structure for the 'tool_xmldb_invoke_move_action' WS
 113       * @return null
 114       */
 115      public static function invoke_move_action_returns() {
 116          return null;
 117      }
 118  
 119  }