|
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 edit_table_save 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 'tablenameempty' => 'tool_xmldb', 00045 'incorrecttablename' => 'tool_xmldb', 00046 'duplicatetablename' => 'tool_xmldb', 00047 'back' => 'tool_xmldb', 00048 'administration' => '' 00049 )); 00050 } 00051 00057 function invoke() { 00058 parent::invoke(); 00059 00060 $result = true; 00061 00062 // Set own core attributes 00063 //$this->does_generate = ACTION_NONE; 00064 $this->does_generate = ACTION_GENERATE_HTML; 00065 00066 // These are always here 00067 global $CFG, $XMLDB; 00068 00069 // Do the job, setting result as needed 00070 00071 if (!data_submitted()) { // Basic prevention 00072 print_error('wrongcall', 'error'); 00073 } 00074 00075 // Get parameters 00076 $dirpath = required_param('dir', PARAM_PATH); 00077 $dirpath = $CFG->dirroot . $dirpath; 00078 00079 $tableparam = strtolower(required_param('table', PARAM_PATH)); 00080 $name = substr(trim(strtolower(required_param('name', PARAM_PATH))),0,28); 00081 $comment = required_param('comment', PARAM_CLEAN); 00082 $comment = $comment; 00083 00084 $dbdir =& $XMLDB->dbdirs[$dirpath]; 00085 00086 $editeddir =& $XMLDB->editeddirs[$dirpath]; 00087 $structure =& $editeddir->xml_file->getStructure(); 00088 $table =& $structure->getTable($tableparam); 00089 00090 $errors = array(); // To store all the errors found 00091 00092 // Perform some checks 00093 // Check empty name 00094 if (empty($name)) { 00095 $errors[] = $this->str['tablenameempty']; 00096 } 00097 // Check incorrect name 00098 if ($name == 'changeme') { 00099 $errors[] = $this->str['incorrecttablename']; 00100 } 00101 // Check duplicatename 00102 if ($tableparam != $name && $structure->getTable($name)) { 00103 $errors[] = $this->str['duplicatetablename']; 00104 } 00105 00106 if (!empty($errors)) { 00107 $temptable = new xmldb_table($name); 00108 // Prepare the output 00109 $o = '<p>' .implode(', ', $errors) . '</p> 00110 <p>' . $temptable->getName() . '</p>'; 00111 $o.= '<a href="index.php?action=edit_table&table=' . $tableparam . 00112 '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 00113 $this->output = $o; 00114 00115 00116 // Continue if we aren't under errors 00117 } else if (empty($errors)) { 00118 // If there is one name change, do it, changing the prev and next 00119 // atributes of the adjacent tables 00120 if ($tableparam != $name) { 00121 $table->setName($name); 00122 if ($table->getPrevious()) { 00123 $prev =& $structure->getTable($table->getPrevious()); 00124 $prev->setNext($name); 00125 $prev->setChanged(true); 00126 } 00127 if ($table->getNext()) { 00128 $next =& $structure->getTable($table->getNext()); 00129 $next->setPrevious($name); 00130 $next->setChanged(true); 00131 } 00132 // Table has changed 00133 $table->setChanged(true); 00134 } 00135 00136 // Set comment 00137 if ($table->getComment() != $comment) { 00138 $table->setComment($comment); 00139 // Table has changed 00140 $table->setChanged(true); 00141 } 00142 00143 // Recalculate the hash 00144 $structure->calculateHash(true); 00145 00146 // If the hash has changed from the original one, change the version 00147 // and mark the structure as changed 00148 $origstructure =& $dbdir->xml_file->getStructure(); 00149 if ($structure->getHash() != $origstructure->getHash()) { 00150 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); 00151 $structure->setChanged(true); 00152 } 00153 00154 // Launch postaction if exists (leave this here!) 00155 if ($this->getPostAction() && $result) { 00156 return $this->launch($this->getPostAction()); 00157 } 00158 } 00159 00160 // Return ok if arrived here 00161 return $result; 00162 } 00163 } 00164