|
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_field extends XMLDBAction { 00033 00037 function init() { 00038 parent::init(); 00039 00040 // Set own custom attributes 00041 $this->sesskey_protected = false; // This action doesn't need sesskey protection 00042 00043 // Get needed strings 00044 $this->loadStrings(array( 00045 'change' => 'tool_xmldb', 00046 'float2numbernote' => 'tool_xmldb', 00047 'vieworiginal' => 'tool_xmldb', 00048 'viewedited' => 'tool_xmldb', 00049 'yes' => '', 00050 'no' => '', 00051 'back' => 'tool_xmldb' 00052 )); 00053 } 00054 00060 function invoke() { 00061 parent::invoke(); 00062 00063 $result = true; 00064 00065 // Set own core attributes 00066 $this->does_generate = ACTION_GENERATE_HTML; 00067 00068 // These are always here 00069 global $CFG, $XMLDB, $OUTPUT; 00070 00071 // Do the job, setting result as needed 00072 // Get the dir containing the file 00073 $dirpath = required_param('dir', PARAM_PATH); 00074 $dirpath = $CFG->dirroot . $dirpath; 00075 00076 // Get the correct dirs 00077 if (!empty($XMLDB->dbdirs)) { 00078 $dbdir =& $XMLDB->dbdirs[$dirpath]; 00079 } else { 00080 return false; 00081 } 00082 if (!empty($XMLDB->editeddirs)) { 00083 $editeddir =& $XMLDB->editeddirs[$dirpath]; 00084 $structure =& $editeddir->xml_file->getStructure(); 00085 } 00086 00087 // Fetch request data 00088 $tableparam = required_param('table', PARAM_CLEAN); 00089 if (!$table =& $structure->getTable($tableparam)) { 00090 $this->errormsg = 'Wrong table specified: ' . $tableparam; 00091 return false; 00092 } 00093 $fieldparam = required_param('field', PARAM_CLEAN); 00094 if (!$field =& $table->getField($fieldparam)) { 00095 // Arriving here from a name change, looking for the new field name 00096 $fieldparam = required_param('name', PARAM_CLEAN); 00097 $field =& $table->getField($fieldparam); 00098 } 00099 00100 $dbdir =& $XMLDB->dbdirs[$dirpath]; 00101 $origstructure =& $dbdir->xml_file->getStructure(); 00102 00103 $o = ''; // Output starts 00104 00105 // If field is XMLDB_TYPE_FLOAT, comment about to migrate it to XMLDB_TYPE_NUMBER 00106 if ($field->getType() == XMLDB_TYPE_FLOAT) { 00107 $o .= '<p>' . $this->str['float2numbernote'] . '</p>'; 00108 } 00109 00110 // Add the main form 00111 $o.= '<form id="form" action="index.php" method="post">'; 00112 $o.= ' <div>'; 00113 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />'; 00114 $o.= ' <input type="hidden" name ="table" value="' . $tableparam .'" />'; 00115 $o.= ' <input type="hidden" name ="field" value="' . $fieldparam .'" />'; 00116 $o.= ' <input type="hidden" name ="sesskey" value="' . sesskey() .'" />'; 00117 $o.= ' <input type="hidden" name ="action" value="edit_field_save" />'; 00118 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />'; 00119 $o.= ' <table id="formelements" class="boxaligncenter">'; 00120 // XMLDB field name 00121 // If the field has dependencies, we cannot change its name 00122 $disabled = ''; 00123 if ($structure->getFieldUses($table->getName(), $field->getName())) { 00124 $o.= ' <input type="hidden" name ="name" value="' . s($field->getName()) .'" />'; 00125 $o.= ' <tr valign="top"><td>Name:</td><td colspan="2">' . s($field->getName()) . '</td></tr>'; 00126 } else { 00127 $o.= ' <tr valign="top"><td><label for="name" accesskey="n">Name:</label></td><td colspan="2"><input name="name" type="text" size="30" maxlength="30" id="name" value="' . s($field->getName()) . '" /></td></tr>'; 00128 } 00129 // XMLDB field comment 00130 $o.= ' <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td colspan="2"><textarea name="comment" rows="3" cols="80" id="comment">' . s($field->getComment()) . '</textarea></td></tr>'; 00131 // xmldb_field Type 00132 $typeoptions = array (XMLDB_TYPE_INTEGER => $field->getXMLDBTypeName(XMLDB_TYPE_INTEGER), 00133 XMLDB_TYPE_NUMBER => $field->getXMLDBTypeName(XMLDB_TYPE_NUMBER), 00134 XMLDB_TYPE_FLOAT => $field->getXMLDBTypeName(XMLDB_TYPE_FLOAT), 00135 XMLDB_TYPE_DATETIME=> $field->getXMLDBTypeName(XMLDB_TYPE_DATETIME), 00136 XMLDB_TYPE_CHAR => $field->getXMLDBTypeName(XMLDB_TYPE_CHAR), 00137 XMLDB_TYPE_TEXT => $field->getXMLDBTypeName(XMLDB_TYPE_TEXT), 00138 XMLDB_TYPE_BINARY => $field->getXMLDBTypeName(XMLDB_TYPE_BINARY)); 00139 // If current field isnt float, delete such column type to avoid its creation from the interface 00140 // Note that float fields are supported completely but it's possible than in a next future 00141 // we delete them completely from Moodle DB, using, exlusively, number(x,y) types 00142 if ($field->getType() != XMLDB_TYPE_FLOAT) { 00143 unset ($typeoptions[XMLDB_TYPE_FLOAT]); 00144 } 00145 // Also we hide datetimes. Only edition of them is allowed (and retrofit) but not new creation 00146 if ($field->getType() != XMLDB_TYPE_DATETIME) { 00147 unset ($typeoptions[XMLDB_TYPE_DATETIME]); 00148 } 00149 $select = html_writer::select($typeoptions, 'type', $field->getType(), false); 00150 $o.= ' <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>'; 00151 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 00152 // xmldb_field Length 00153 $o.= ' <tr valign="top"><td><label for="length" accesskey="l">Length:</label></td>'; 00154 $o.= ' <td colspan="2"><input name="length" type="text" size="6" maxlength="6" id="length" value="' . s($field->getLength()) . '" /><span id="lengthtip"></span></td></tr>'; 00155 // xmldb_field Decimals 00156 $o.= ' <tr valign="top"><td><label for="decimals" accesskey="d">Decimals:</label></td>'; 00157 $o.= ' <td colspan="2"><input name="decimals" type="text" size="6" maxlength="6" id="decimals" value="' . s($field->getDecimals()) . '" /><span id="decimalstip"></span></td></tr>'; 00158 // xmldb_field Unsigned 00159 $unsignedoptions = array (0 => 'signed', 1 => 'unsigned'); 00160 $select = html_writer::select($unsignedoptions, 'unsigned', $field->getUnsigned(), false); 00161 $o.= ' <tr valign="top"><td><label for="menuunsigned" accesskey="u">Unsigned:</label></td>'; 00162 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 00163 // xmldb_field NotNull 00164 $notnulloptions = array (0 => 'null', 'not null'); 00165 $select = html_writer::select($notnulloptions, 'notnull', $field->getNotNull(), false); 00166 $o.= ' <tr valign="top"><td><label for="menunotnull" accesskey="n">Not Null:</label></td>'; 00167 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 00168 // xmldb_field Sequence 00169 $sequenceoptions = array (0 => $this->str['no'], 1 => 'auto-numbered'); 00170 $select = html_writer::select($sequenceoptions, 'sequence', $field->getSequence(), false); 00171 $o.= ' <tr valign="top"><td><label for="menusequence" accesskey="s">Sequence:</label></td>'; 00172 $o.= ' <td colspan="2">' . $select . '</td></tr>'; 00173 // xmldb_field Default 00174 $o.= ' <tr valign="top"><td><label for="default" accesskey="d">Default:</label></td>'; 00175 $o.= ' <td colspan="2"><input type="text" name="default" size="30" maxlength="80" id="default" value="' . s($field->getDefault()) . '" /></td></tr>'; 00176 // Change button 00177 $o.= ' <tr valign="top"><td> </td><td colspan="2"><input type="submit" value="' .$this->str['change'] . '" /></td></tr>'; 00178 $o.= ' </table>'; 00179 $o.= '</div></form>'; 00180 // Calculate the buttons 00181 $b = ' <p class="centerpara buttons">'; 00182 // The view original XML button 00183 if ($table->getField($fieldparam)) { 00184 $b .= ' <a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=original&table=' . $tableparam . '&field=' . $fieldparam . '">[' . $this->str['vieworiginal'] . ']</a>'; 00185 } else { 00186 $b .= ' [' . $this->str['vieworiginal'] . ']'; 00187 } 00188 // The view edited XML button 00189 if ($field->hasChanged()) { 00190 $b .= ' <a href="index.php?action=view_field_xml&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&select=edited&table=' . $tableparam . '&field=' . $fieldparam . '">[' . $this->str['viewedited'] . ']</a>'; 00191 } else { 00192 $b .= ' [' . $this->str['viewedited'] . ']'; 00193 } 00194 // The back to edit table button 00195 $b .= ' <a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>'; 00196 $b .= '</p>'; 00197 $o .= $b; 00198 00199 $this->output = $o; 00200 00201 // Launch postaction if exists (leave this here!) 00202 if ($this->getPostAction() && $result) { 00203 return $this->launch($this->getPostAction()); 00204 } 00205 00206 // Return ok if arrived here 00207 return $result; 00208 } 00209 } 00210