Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/move_updown_index/move_updown_index.class.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00032 class move_updown_index extends XMLDBAction {
00033 
00037     function init() {
00038         parent::init();
00039 
00040         // Set own custom attributes
00041 
00042         // Get needed strings
00043         $this->loadStrings(array(
00044             // 'key' => 'module',
00045         ));
00046     }
00047 
00053     function invoke() {
00054         parent::invoke();
00055 
00056         $result = true;
00057 
00058         // Set own core attributes
00059         $this->does_generate = ACTION_NONE;
00060         //$this->does_generate = ACTION_GENERATE_HTML;
00061 
00062         // These are always here
00063         global $CFG, $XMLDB;
00064 
00065         // Do the job, setting result as needed
00066         // Get the dir containing the file
00067         $dirpath = required_param('dir', PARAM_PATH);
00068         $dirpath = $CFG->dirroot . $dirpath;
00069 
00070         // Get the correct dirs
00071         if (!empty($XMLDB->dbdirs)) {
00072             $dbdir =& $XMLDB->dbdirs[$dirpath];
00073         } else {
00074             return false;
00075         }
00076         if (!empty($XMLDB->editeddirs)) {
00077             $editeddir =& $XMLDB->editeddirs[$dirpath];
00078             $structure =& $editeddir->xml_file->getStructure();
00079         }
00080 
00081         $prev = NULL;
00082         $next = NULL;
00083         $tableparam = required_param('table', PARAM_CLEAN);
00084         $indexparam = required_param('index', PARAM_CLEAN);
00085         $direction  = required_param('direction', PARAM_ALPHA);
00086         $tables =& $structure->getTables();
00087         $table =& $structure->getTable($tableparam);
00088         $indexes =& $table->getIndexes();
00089         if ($direction == 'down') {
00090             $index =& $table->getIndex($indexparam);
00091             $swap  =& $table->getIndex($index->getNext());
00092         } else {
00093             $swap  =& $table->getIndex($indexparam);
00094             $index =& $table->getIndex($swap->getPrevious());
00095         }
00096 
00097         // Change the index before the pair
00098         if ($index->getPrevious()) {
00099             $prev =& $table->getIndex($index->getPrevious());
00100             $prev->setNext($swap->getName());
00101             $swap->setPrevious($prev->getName());
00102             $prev->setChanged(true);
00103         } else {
00104             $swap->setPrevious(NULL);
00105         }
00106         // Change the field after the pair
00107         if ($swap->getNext()) {
00108             $next =& $table->getIndex($swap->getNext());
00109             $next->setPrevious($index->getName());
00110             $index->setNext($next->getName());
00111             $next->setChanged(true);
00112         } else {
00113             $index->setNext(NULL);
00114         }
00115         // Swap the indexes
00116         $index->setPrevious($swap->getName());
00117         $swap->setNext($index->getName());
00118 
00119         // Mark indexes as changed
00120         $index->setChanged(true);
00121         $swap->setChanged(true);
00122 
00123         // Table has changed
00124         $table->setChanged(true);
00125 
00126         // Reorder the indexes
00127         $table->orderIndexes($indexes);
00128 
00129         // Recalculate the hash
00130         $structure->calculateHash(true);
00131 
00132         // If the hash has changed from the original one, change the version
00133         // and mark the structure as changed
00134         $origstructure =& $dbdir->xml_file->getStructure();
00135         if ($structure->getHash() != $origstructure->getHash()) {
00136             $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
00137             $structure->setChanged(true);
00138         }
00139 
00140         // Launch postaction if exists (leave this here!)
00141         if ($this->getPostAction() && $result) {
00142             return $this->launch($this->getPostAction());
00143         }
00144 
00145         // Return ok if arrived here
00146         return $result;
00147     }
00148 }
00149 
 All Data Structures Namespaces Files Functions Variables Enumerations