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