1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @package tool_xmldb
|
|
|
19 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
20 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* This class will save changes in table name and/or comments
|
|
|
25 |
*
|
|
|
26 |
* @package tool_xmldb
|
|
|
27 |
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class edit_table_save extends XMLDBAction {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Init method, every subclass will have its own
|
|
|
34 |
*/
|
|
|
35 |
function init() {
|
|
|
36 |
parent::init();
|
|
|
37 |
|
|
|
38 |
// Set own custom attributes
|
|
|
39 |
|
|
|
40 |
// Get needed strings
|
|
|
41 |
$this->loadStrings(array(
|
|
|
42 |
'tablenameempty' => 'tool_xmldb',
|
|
|
43 |
'incorrecttablename' => 'tool_xmldb',
|
|
|
44 |
'duplicatetablename' => 'tool_xmldb',
|
|
|
45 |
'back' => 'tool_xmldb',
|
|
|
46 |
'administration' => ''
|
|
|
47 |
));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Invoke method, every class will have its own
|
|
|
52 |
* returns true/false on completion, setting both
|
|
|
53 |
* errormsg and output as necessary
|
|
|
54 |
*/
|
|
|
55 |
function invoke() {
|
|
|
56 |
parent::invoke();
|
|
|
57 |
|
|
|
58 |
$result = true;
|
|
|
59 |
|
|
|
60 |
// Set own core attributes
|
|
|
61 |
//$this->does_generate = ACTION_NONE;
|
|
|
62 |
$this->does_generate = ACTION_GENERATE_HTML;
|
|
|
63 |
|
|
|
64 |
// These are always here
|
|
|
65 |
global $CFG, $XMLDB;
|
|
|
66 |
|
|
|
67 |
// Do the job, setting result as needed
|
|
|
68 |
|
|
|
69 |
if (!data_submitted()) { // Basic prevention
|
|
|
70 |
throw new \moodle_exception('wrongcall', 'error');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
// Get parameters
|
|
|
74 |
$dirpath = required_param('dir', PARAM_PATH);
|
|
|
75 |
$dirpath = $CFG->dirroot . $dirpath;
|
|
|
76 |
|
|
|
77 |
$tableparam = strtolower(required_param('table', PARAM_PATH));
|
|
|
78 |
$name = substr(trim(strtolower(required_param('name', PARAM_PATH))),0,xmldb_table::NAME_MAX_LENGTH);
|
|
|
79 |
$comment = required_param('comment', PARAM_CLEAN);
|
|
|
80 |
$comment = $comment;
|
|
|
81 |
|
|
|
82 |
$dbdir = $XMLDB->dbdirs[$dirpath];
|
|
|
83 |
|
|
|
84 |
$editeddir = $XMLDB->editeddirs[$dirpath];
|
|
|
85 |
$structure = $editeddir->xml_file->getStructure();
|
|
|
86 |
$table = $structure->getTable($tableparam);
|
|
|
87 |
|
|
|
88 |
$errors = array(); // To store all the errors found
|
|
|
89 |
|
|
|
90 |
// Perform some checks
|
|
|
91 |
// Check empty name
|
|
|
92 |
if (empty($name)) {
|
|
|
93 |
$errors[] = $this->str['tablenameempty'];
|
|
|
94 |
}
|
|
|
95 |
// Check incorrect name
|
|
|
96 |
if ($name == 'changeme') {
|
|
|
97 |
$errors[] = $this->str['incorrecttablename'];
|
|
|
98 |
}
|
|
|
99 |
// Check duplicatename
|
|
|
100 |
if ($tableparam != $name && $structure->getTable($name)) {
|
|
|
101 |
$errors[] = $this->str['duplicatetablename'];
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
if (!empty($errors)) {
|
|
|
105 |
$temptable = new xmldb_table($name);
|
|
|
106 |
// Prepare the output
|
|
|
107 |
$o = '<p>' .implode(', ', $errors) . '</p>
|
|
|
108 |
<p>' . $temptable->getName() . '</p>';
|
|
|
109 |
$o.= '<a href="index.php?action=edit_table&table=' . $tableparam .
|
|
|
110 |
'&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
|
|
|
111 |
$this->output = $o;
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
// Continue if we aren't under errors
|
|
|
115 |
} else if (empty($errors)) {
|
|
|
116 |
// If there is one name change, do it, changing the prev and next
|
|
|
117 |
// atributes of the adjacent tables
|
|
|
118 |
if ($tableparam != $name) {
|
|
|
119 |
$table->setName($name);
|
|
|
120 |
if ($table->getPrevious()) {
|
|
|
121 |
$prev = $structure->getTable($table->getPrevious());
|
|
|
122 |
$prev->setNext($name);
|
|
|
123 |
$prev->setChanged(true);
|
|
|
124 |
}
|
|
|
125 |
if ($table->getNext()) {
|
|
|
126 |
$next = $structure->getTable($table->getNext());
|
|
|
127 |
$next->setPrevious($name);
|
|
|
128 |
$next->setChanged(true);
|
|
|
129 |
}
|
|
|
130 |
// Table has changed
|
|
|
131 |
$table->setChanged(true);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// Set comment
|
|
|
135 |
if ($table->getComment() != $comment) {
|
|
|
136 |
$table->setComment($comment);
|
|
|
137 |
// Table has changed
|
|
|
138 |
$table->setChanged(true);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
// Recalculate the hash
|
|
|
142 |
$structure->calculateHash(true);
|
|
|
143 |
|
|
|
144 |
// If the hash has changed from the original one, change the version
|
|
|
145 |
// and mark the structure as changed
|
|
|
146 |
$origstructure = $dbdir->xml_file->getStructure();
|
|
|
147 |
if ($structure->getHash() != $origstructure->getHash()) {
|
|
|
148 |
$structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
|
|
|
149 |
$structure->setChanged(true);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
// Launch postaction if exists (leave this here!)
|
|
|
153 |
if ($this->getPostAction() && $result) {
|
|
|
154 |
return $this->launch($this->getPostAction());
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
// Return ok if arrived here
|
|
|
159 |
return $result;
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|