|
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 00033 class edit_field_save extends XMLDBAction { 00034 00038 function init() { 00039 parent::init(); 00040 00041 // Set own custom attributes 00042 00043 // Get needed strings 00044 $this->loadStrings(array( 00045 'fieldnameempty' => 'tool_xmldb', 00046 'incorrectfieldname' => 'tool_xmldb', 00047 'duplicatefieldname' => 'tool_xmldb', 00048 'integerincorrectlength' => 'tool_xmldb', 00049 'numberincorrectlength' => 'tool_xmldb', 00050 'floatincorrectlength' => 'tool_xmldb', 00051 'charincorrectlength' => 'tool_xmldb', 00052 'textincorrectlength' => 'tool_xmldb', 00053 'binaryincorrectlength' => 'tool_xmldb', 00054 'numberincorrectdecimals' => 'tool_xmldb', 00055 'floatincorrectdecimals' => 'tool_xmldb', 00056 'defaultincorrect' => 'tool_xmldb', 00057 'back' => 'tool_xmldb', 00058 'administration' => '' 00059 )); 00060 } 00061 00067 function invoke() { 00068 parent::invoke(); 00069 00070 $result = true; 00071 00072 // Set own core attributes 00073 //$this->does_generate = ACTION_NONE; 00074 $this->does_generate = ACTION_GENERATE_HTML; 00075 00076 // These are always here 00077 global $CFG, $XMLDB; 00078 00079 // Do the job, setting result as needed 00080 00081 if (!data_submitted()) { // Basic prevention 00082 print_error('wrongcall', 'error'); 00083 } 00084 00085 // Get parameters 00086 $dirpath = required_param('dir', PARAM_PATH); 00087 $dirpath = $CFG->dirroot . $dirpath; 00088 00089 $tableparam = strtolower(required_param('table', PARAM_PATH)); 00090 $fieldparam = strtolower(required_param('field', PARAM_PATH)); 00091 $name = substr(trim(strtolower(optional_param('name', $fieldparam, PARAM_PATH))),0,30); 00092 00093 $comment = required_param('comment', PARAM_CLEAN); 00094 $comment = trim($comment); 00095 00096 $type = required_param('type', PARAM_INT); 00097 $length = strtolower(optional_param('length', NULL, PARAM_ALPHANUM)); 00098 $decimals = optional_param('decimals', NULL, PARAM_INT); 00099 $unsigned = optional_param('unsigned', false, PARAM_BOOL); 00100 $notnull = optional_param('notnull', false, PARAM_BOOL); 00101 $sequence = optional_param('sequence', false, PARAM_BOOL); 00102 $default = optional_param('default', NULL, PARAM_PATH); 00103 $default = trim($default); 00104 00105 $editeddir =& $XMLDB->editeddirs[$dirpath]; 00106 $structure =& $editeddir->xml_file->getStructure(); 00107 $table =& $structure->getTable($tableparam); 00108 $field =& $table->getField($fieldparam); 00109 $oldhash = $field->getHash(); 00110 00111 $errors = array(); // To store all the errors found 00112 00113 // Perform some automatic assumptions 00114 if ($sequence) { 00115 $unsigned = true; 00116 $notnull = true; 00117 $default = NULL; 00118 } 00119 if ($type != XMLDB_TYPE_NUMBER && $type != XMLDB_TYPE_FLOAT) { 00120 $decimals = NULL; 00121 } 00122 if ($type == XMLDB_TYPE_BINARY) { 00123 $default = NULL; 00124 } 00125 if ($default === '') { 00126 $default = NULL; 00127 } 00128 00129 // Perform some checks 00130 // Check empty name 00131 if (empty($name)) { 00132 $errors[] = $this->str['fieldnameempty']; 00133 } 00134 // Check incorrect name 00135 if ($name == 'changeme') { 00136 $errors[] = $this->str['incorrectfieldname']; 00137 } 00138 // Check duplicate name 00139 if ($fieldparam != $name && $table->getField($name)) { 00140 $errors[] = $this->str['duplicatefieldname']; 00141 } 00142 // Integer checks 00143 if ($type == XMLDB_TYPE_INTEGER) { 00144 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 00145 $length > 0 && $length <= 20)) { 00146 $errors[] = $this->str['integerincorrectlength']; 00147 } 00148 if (!(empty($default) || (is_numeric($default) && 00149 !empty($default) && 00150 intval($default)==floatval($default)))) { 00151 $errors[] = $this->str['defaultincorrect']; 00152 } 00153 } 00154 // Number checks 00155 if ($type == XMLDB_TYPE_NUMBER) { 00156 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 00157 $length > 0 && $length <= 20)) { 00158 $errors[] = $this->str['numberincorrectlength']; 00159 } 00160 if (!(empty($decimals) || (is_numeric($decimals) && 00161 !empty($decimals) && 00162 intval($decimals)==floatval($decimals) && 00163 $decimals >= 0 && 00164 $decimals < $length))) { 00165 $errors[] = $this->str['numberincorrectdecimals']; 00166 } 00167 if (!(empty($default) || (is_numeric($default) && 00168 !empty($default)))) { 00169 $errors[] = $this->str['defaultincorrect']; 00170 } 00171 } 00172 // Float checks 00173 if ($type == XMLDB_TYPE_FLOAT) { 00174 if (!(empty($length) || (is_numeric($length) && 00175 !empty($length) && 00176 intval($length)==floatval($length) && 00177 $length > 0 && 00178 $length <= 20))) { 00179 $errors[] = $this->str['floatincorrectlength']; 00180 } 00181 if (!(empty($decimals) || (is_numeric($decimals) && 00182 !empty($decimals) && 00183 intval($decimals)==floatval($decimals) && 00184 $decimals >= 0 && 00185 $decimals < $length))) { 00186 $errors[] = $this->str['floatincorrectdecimals']; 00187 } 00188 if (!(empty($default) || (is_numeric($default) && 00189 !empty($default)))) { 00190 $errors[] = $this->str['defaultincorrect']; 00191 } 00192 } 00193 // Char checks 00194 if ($type == XMLDB_TYPE_CHAR) { 00195 if (!(is_numeric($length) && !empty($length) && intval($length)==floatval($length) && 00196 $length > 0 && $length <= xmldb_field::CHAR_MAX_LENGTH)) { 00197 $errors[] = $this->str['charincorrectlength']; 00198 } 00199 if ($default !== NULL && $default !== '') { 00200 if (substr($default, 0, 1) == "'" || 00201 substr($default, -1, 1) == "'") { 00202 $errors[] = $this->str['defaultincorrect']; 00203 } 00204 } 00205 } 00206 // Text checks 00207 if ($type == XMLDB_TYPE_TEXT) { 00208 if ($length != 'small' && 00209 $length != 'medium' && 00210 $length != 'big') { 00211 $errors[] = $this->str['textincorrectlength']; 00212 } 00213 if ($default !== NULL && $default !== '') { 00214 if (substr($default, 0, 1) == "'" || 00215 substr($default, -1, 1) == "'") { 00216 $errors[] = $this->str['defaultincorrect']; 00217 } 00218 } 00219 } 00220 // Binary checks 00221 if ($type == XMLDB_TYPE_BINARY) { 00222 if ($length != 'small' && 00223 $length != 'medium' && 00224 $length != 'big') { 00225 $errors[] = $this->str['binaryincorrectlength']; 00226 } 00227 } 00228 00229 if (!empty($errors)) { 00230 $tempfield = new xmldb_field($name); 00231 $tempfield->setType($type); 00232 $tempfield->setLength($length); 00233 $tempfield->setDecimals($decimals); 00234 $tempfield->setUnsigned($unsigned); 00235 $tempfield->setNotNull($notnull); 00236 $tempfield->setSequence($sequence); 00237 $tempfield->setDefault($default); 00238 // Prepare the output 00239 $o = '<p>' .implode(', ', $errors) . '</p> 00240 <p>' . $name . ': ' . $tempfield->readableInfo() . '</p>'; 00241 $o.= '<a href="index.php?action=edit_field&field=' . $field->getName() . '&table=' . $table->getName() . 00242 '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 00243 $this->output = $o; 00244 } 00245 00246 // Continue if we aren't under errors 00247 if (empty($errors)) { 00248 // If there is one name change, do it, changing the prev and next 00249 // atributes of the adjacent fields 00250 if ($fieldparam != $name) { 00251 $field->setName($name); 00252 if ($field->getPrevious()) { 00253 $prev =& $table->getField($field->getPrevious()); 00254 $prev->setNext($name); 00255 $prev->setChanged(true); 00256 } 00257 if ($field->getNext()) { 00258 $next =& $table->getField($field->getNext()); 00259 $next->setPrevious($name); 00260 $next->setChanged(true); 00261 } 00262 } 00263 00264 // Set comment 00265 $field->setComment($comment); 00266 00267 // Set the rest of fields 00268 $field->setType($type); 00269 $field->setLength($length); 00270 $field->setDecimals($decimals); 00271 $field->setUnsigned($unsigned); 00272 $field->setNotNull($notnull); 00273 $field->setSequence($sequence); 00274 $field->setDefault($default); 00275 00276 // If the hash has changed from the old one, change the version 00277 // and mark the structure as changed 00278 $field->calculateHash(true); 00279 if ($oldhash != $field->getHash()) { 00280 $field->setChanged(true); 00281 $table->setChanged(true); 00282 // Recalculate the structure hash 00283 $structure->calculateHash(true); 00284 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false)); 00285 // Mark as changed 00286 $structure->setChanged(true); 00287 } 00288 00289 // Launch postaction if exists (leave this here!) 00290 if ($this->getPostAction() && $result) { 00291 return $this->launch($this->getPostAction()); 00292 } 00293 } 00294 00295 // Return ok if arrived here 00296 return $result; 00297 } 00298 } 00299