3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
13 // This program is free software; you can redistribute it and/or modify //
14 // it under the terms of the GNU General Public License as published by //
15 // the Free Software Foundation; either version 2 of the License, or //
16 // (at your option) any later version. //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will provide the interface for all the edit field actions
29 class edit_field
extends XMLDBAction
{
32 * Init method, every subclass will have its own
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
42 'vieworiginal' => 'xmldb',
43 'viewedited' => 'xmldb',
51 * Invoke method, every class will have its own
52 * returns true/false on completion, setting both
53 * errormsg and output as necessary
60 /// Set own core attributes
61 $this->does_generate
= ACTION_GENERATE_HTML
;
63 /// These are always here
66 /// Do the job, setting result as needed
67 /// Get the dir containing the file
68 $dirpath = required_param('dir', PARAM_PATH
);
69 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
71 /// Get the correct dirs
72 if (!empty($XMLDB->dbdirs
)) {
73 $dbdir =& $XMLDB->dbdirs
[$dirpath];
77 if (!empty($XMLDB->editeddirs
)) {
78 $editeddir =& $XMLDB->editeddirs
[$dirpath];
79 $structure =& $editeddir->xml_file
->getStructure();
82 /// ADD YOUR CODE HERE
84 /// Fetch request data
85 $tableparam = required_param('table', PARAM_CLEAN
);
86 if (!$table =& $structure->getTable($tableparam)) {
87 $this->errormsg
= 'Wrong table specified: ' . $tableparm;
90 $fieldparam = required_param('field', PARAM_CLEAN
);
91 if (!$field =& $table->getField($fieldparam)) {
92 /// Arriving here from a name change, looking for the new field name
93 $fieldparam = required_param('name', PARAM_CLEAN
);
94 $field =& $table->getField($fieldparam);
97 $dbdir =& $XMLDB->dbdirs
[$dirpath];
98 $origstructure =& $dbdir->xml_file
->getStructure();
100 /// Add the main form
101 $o = '<form id="form" action="index.php" method="post">';
103 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
104 $o.= ' <input type="hidden" name ="table" value="' . $tableparam .'" />';
105 $o.= ' <input type="hidden" name ="field" value="' . $fieldparam .'" />';
106 $o.= ' <input type="hidden" name ="action" value="edit_field_save" />';
107 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />';
108 $o.= ' <table id="formelements" class="boxaligncenter">';
110 /// If the field has dependencies, we cannot change its name
112 if ($structure->getFieldUses($table->getName(), $field->getName())) {
113 $o.= ' <input type="hidden" name ="name" value="' . s($field->getName()) .'" />';
114 $o.= ' <tr valign="top"><td>Name:</td><td colspan="2">' . s($field->getName()) . '</td></tr>';
116 $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>';
118 /// XMLDB field comment
119 $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>';
121 $typeoptions = array (XMLDB_TYPE_INTEGER
=> $field->getXMLDBTypeName(XMLDB_TYPE_INTEGER
),
122 XMLDB_TYPE_NUMBER
=> $field->getXMLDBTypeName(XMLDB_TYPE_NUMBER
),
123 XMLDB_TYPE_FLOAT
=> $field->getXMLDBTypeName(XMLDB_TYPE_FLOAT
),
124 XMLDB_TYPE_DATETIME
=> $field->getXMLDBTypeName(XMLDB_TYPE_DATETIME
),
125 XMLDB_TYPE_CHAR
=> $field->getXMLDBTypeName(XMLDB_TYPE_CHAR
),
126 XMLDB_TYPE_TEXT
=> $field->getXMLDBTypeName(XMLDB_TYPE_TEXT
),
127 XMLDB_TYPE_BINARY
=> $field->getXMLDBTypeName(XMLDB_TYPE_BINARY
));
128 /// If current field isnt float, delete such column type to avoid its creation from the interface
129 /// Note that float fields are supported completely but it's possible than in a next future
130 /// we delete them completely from Moodle DB, using, exlusively, number(x,y) types
131 if ($field->getType() != XMLDB_TYPE_FLOAT
) {
132 unset ($typeoptions[XMLDB_TYPE_FLOAT
]);
134 /// Also we hide datetimes. Only edition of them is allowed (and retrofit) but not new creation
135 if ($field->getType() != XMLDB_TYPE_DATETIME
) {
136 unset ($typeoptions[XMLDB_TYPE_DATETIME
]);
138 $o.= ' <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>';
139 $o.= ' <td colspan="2">' . choose_from_menu($typeoptions, 'type', $field->getType(), '', '', '', true) . '</td></tr>';
140 /// XMLDBField Length
141 $o.= ' <tr valign="top"><td><label for="length" accesskey="l">Length:</label></td>';
142 $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>';
143 /// XMLDBField Decimals
144 $o.= ' <tr valign="top"><td><label for="decimals" accesskey="d">Decimals:</label></td>';
145 $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>';
146 /// XMLDBField Unsigned
147 $unsignedoptions = array (0 => 'signed', 1 => 'unsigned');
148 $o.= ' <tr valign="top"><td><label for="menuunsigned" accesskey="u">Unsigned:</label></td>';
149 $o.= ' <td colspan="2">' . choose_from_menu($unsignedoptions, 'unsigned', $field->getUnsigned(), '', '', '', true) . '</td></tr>';
150 /// XMLDBField NotNull
151 $notnulloptions = array (0 => 'null', 'not null');
152 $o.= ' <tr valign="top"><td><label for="menunotnull" accesskey="n">Not Null:</label></td>';
153 $o.= ' <td colspan="2">' . choose_from_menu($notnulloptions, 'notnull', $field->getNotNull(), '', '', '', true) . '</td></tr>';
154 /// XMLDBField Sequence
155 $sequenceoptions = array (0 => $this->str
['no'], 1 => 'auto-numbered');
156 $o.= ' <tr valign="top"><td><label for="menusequence" accesskey="s">Sequence:</label></td>';
157 $o.= ' <td colspan="2">' . choose_from_menu($sequenceoptions, 'sequence', $field->getSequence(), '', '', '', true) . '</td></tr>';
158 /// XMLDBField Enum and enumvalues
159 $enumoptions = array (0 => $this->str
['no'], 1 => $this->str
['yes']);
160 $o.= ' <tr valign="top"><td><label for="menuenum" accesskey="s">Enum:</label></td>';
161 $o.= ' <td>' . choose_from_menu($enumoptions, 'enum', $field->getEnum(), '', '', '', true) . '</td>';
162 if (is_array($field->getEnumValues())) {
163 $enumvalues = implode(', ', $field->getEnumValues());
167 $o.= ' <td><textarea name="enumvalues" rows="3" cols="70" id="enumvalues">' . s($enumvalues) . '</textarea></td></tr>';
168 /// XMLDBField Default
169 $o.= ' <tr valign="top"><td><label for="default" accesskey="d">Default:</label></td>';
170 $o.= ' <td colspan="2"><input type="text" name="default" size="30" maxlength="80" id="default" value="' . s($field->getDefault()) . '" /></td></tr>';
172 $o.= ' <tr valign="top"><td> </td><td colspan="2"><input type="submit" value="' .$this->str
['change'] . '" /></td></tr>';
174 $o.= '</div></form>';
175 /// Calculate the buttons
176 $b = ' <p class="centerpara buttons">';
177 /// The view original XML button
178 if ($table->getField($fieldparam)) {
179 $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>';
181 $b .= ' [' . $this->str
['vieworiginal'] . ']';
183 /// The view edited XML button
184 if ($field->hasChanged()) {
185 $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>';
187 $b .= ' [' . $this->str
['viewedited'] . ']';
189 /// The back to edit table button
190 $b .= ' <a href="index.php?action=edit_table&table=' . $tableparam . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '">[' . $this->str
['back'] . ']</a>';
196 /// Launch postaction if exists (leave this here!)
197 if ($this->getPostAction() && $result) {
198 return $this->launch($this->getPostAction());
201 /// Return ok if arrived here