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.
   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   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * This class will will move one key up/down
  25   *
  26   * @package    tool_xmldb
  27   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class move_updown_key extends XMLDBAction {
  31  
  32      /**
  33       * Init method, every subclass will have its own
  34       */
  35      function init() {
  36          parent::init();
  37  
  38          // Set own custom attributes
  39  
  40          // Get needed strings
  41          $this->loadStrings(array(
  42              // 'key' => 'module',
  43          ));
  44      }
  45  
  46      /**
  47       * Invoke method, every class will have its own
  48       * returns true/false on completion, setting both
  49       * errormsg and output as necessary
  50       */
  51      function invoke() {
  52          parent::invoke();
  53  
  54          $result = true;
  55  
  56          // Set own core attributes
  57          $this->does_generate = ACTION_NONE;
  58          //$this->does_generate = ACTION_GENERATE_HTML;
  59  
  60          // These are always here
  61          global $CFG, $XMLDB;
  62  
  63          // Do the job, setting result as needed
  64          // Get the dir containing the file
  65          $dirpath = required_param('dir', PARAM_PATH);
  66          $dirpath = $CFG->dirroot . $dirpath;
  67  
  68          // Get the correct dirs
  69          if (!empty($XMLDB->dbdirs)) {
  70              $dbdir = $XMLDB->dbdirs[$dirpath];
  71          } else {
  72              return false;
  73          }
  74          if (!empty($XMLDB->editeddirs)) {
  75              $editeddir = $XMLDB->editeddirs[$dirpath];
  76              $structure = $editeddir->xml_file->getStructure();
  77          }
  78  
  79          $prev = NULL;
  80          $next = NULL;
  81          $tableparam = required_param('table', PARAM_CLEAN);
  82          $keyparam = required_param('key', PARAM_CLEAN);
  83          $direction  = required_param('direction', PARAM_ALPHA);
  84          $tables = $structure->getTables();
  85          $table = $structure->getTable($tableparam);
  86          $keys = $table->getKeys();
  87          if ($direction == 'down') {
  88              $key = $table->getKey($keyparam);
  89              $swap = $table->getKey($key->getNext());
  90          } else {
  91              $swap = $table->getKey($keyparam);
  92              $key = $table->getKey($swap->getPrevious());
  93          }
  94  
  95          // Change the key before the pair
  96          if ($key->getPrevious()) {
  97              $prev = $table->getKey($key->getPrevious());
  98              $prev->setNext($swap->getName());
  99              $swap->setPrevious($prev->getName());
 100              $prev->setChanged(true);
 101          } else {
 102              $swap->setPrevious(NULL);
 103          }
 104          // Change the key after the pair
 105          if ($swap->getNext()) {
 106              $next = $table->getKey($swap->getNext());
 107              $next->setPrevious($key->getName());
 108              $key->setNext($next->getName());
 109              $next->setChanged(true);
 110          } else {
 111              $key->setNext(NULL);
 112          }
 113          // Swap the keys
 114          $key->setPrevious($swap->getName());
 115          $swap->setNext($key->getName());
 116  
 117          // Mark keys as changed
 118          $key->setChanged(true);
 119          $swap->setChanged(true);
 120  
 121          // Table has changed
 122          $table->setChanged(true);
 123  
 124          // Reorder the keys
 125          $table->orderKeys();
 126  
 127          // Recalculate the hash
 128          $structure->calculateHash(true);
 129  
 130          // If the hash has changed from the original one, change the version
 131          // and mark the structure as changed
 132          $origstructure = $dbdir->xml_file->getStructure();
 133          if ($structure->getHash() != $origstructure->getHash()) {
 134              $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 135              $structure->setChanged(true);
 136          }
 137  
 138          // Launch postaction if exists (leave this here!)
 139          if ($this->getPostAction() && $result) {
 140              return $this->launch($this->getPostAction());
 141          }
 142  
 143          // Return ok if arrived here
 144          return $result;
 145      }
 146  }
 147