|
Moodle
2.2.1
http://www.collinsharper.com
|
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_table 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 $direction = required_param('direction', PARAM_ALPHA); 00085 $tables =& $structure->getTables(); 00086 if ($direction == 'down') { 00087 $table =& $structure->getTable($tableparam); 00088 $swap =& $structure->getTable($table->getNext()); 00089 } else { 00090 $swap =& $structure->getTable($tableparam); 00091 $table =& $structure->getTable($swap->getPrevious()); 00092 } 00093 00094 // Change the table before the pair 00095 if ($table->getPrevious()) { 00096 $prev =& $structure->getTable($table->getPrevious()); 00097 $prev->setNext($swap->getName()); 00098 $swap->setPrevious($prev->getName()); 00099 $prev->setChanged(true); 00100 } else { 00101 $swap->setPrevious(NULL); 00102 } 00103 // Change the table after the pair 00104 if ($swap->getNext()) { 00105 $next =& $structure->getTable($swap->getNext()); 00106 $next->setPrevious($table->getName()); 00107 $table->setNext($next->getName()); 00108 $next->setChanged(true); 00109 } else { 00110 $table->setNext(NULL); 00111 } 00112 // Swap the tables 00113 $table->setPrevious($swap->getName()); 00114 $swap->setNext($table->getName()); 00115 00116 // Table has changed 00117 $table->setChanged(true); 00118 00119 // Reorder the structure 00120 $structure->orderTables($tables); 00121 // Send tables back to structure (the order above break refs) 00122 $structure->setTables($tables); 00123 // Recalculate the hash 00124 $structure->calculateHash(true); 00125 00126 // If the hash has changed from the original one, change the version 00127 // and mark the structure as changed 00128 $origstructure =& $dbdir->xml_file->getStructure(); 00129 if ($structure->getHash() != $origstructure->getHash()) { 00130 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); 00131 $structure->setChanged(true); 00132 } 00133 00134 // Launch postaction if exists (leave this here!) 00135 if ($this->getPostAction() && $result) { 00136 return $this->launch($this->getPostAction()); 00137 } 00138 00139 // Return ok if arrived here 00140 return $result; 00141 } 00142 } 00143