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 save the changes performed to one sentence
29 class edit_sentence_save
extends XMLDBAction
{
32 * Init method, every subclass will have its own
37 /// Set own custom attributes
39 /// Get needed strings
40 $this->loadStrings(array(
41 'cannotuseidfield' => 'xmldb',
42 'missingfieldsinsentence' => 'xmldb',
43 'missingvaluesinsentence' => 'xmldb',
44 'wrongnumberoffieldsorvalues' => 'xmldb',
45 'administration' => ''
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
59 /// Set own core attributes
60 $this->does_generate
= ACTION_NONE
;
61 //$this->does_generate = ACTION_GENERATE_HTML;
63 /// These are always here
66 /// Do the job, setting result as needed
69 $dirpath = required_param('dir', PARAM_PATH
);
70 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
72 $statementparam = strtolower(required_param('statement', PARAM_CLEAN
));
73 $sentenceparam = strtolower(required_param('sentence', PARAM_ALPHANUM
));
75 $fields = required_param('fields', PARAM_CLEAN
);
76 $fields = trim(stripslashes_safe($fields));
77 $values = required_param('values', PARAM_CLEAN
);
78 $values = trim(stripslashes_safe($values));
80 $editeddir =& $XMLDB->editeddirs
[$dirpath];
81 $structure =& $editeddir->xml_file
->getStructure();
82 $statement =& $structure->getStatement($statementparam);
83 $sentences =& $statement->getSentences();
85 $oldsentence = $sentences[$sentenceparam];
88 $this->errormsg
= 'Wrong statement specified: ' . $statementparam;
92 /// For now, only insert sentences are allowed
93 if ($statement->getType() != XMLDB_STATEMENT_INSERT
) {
94 $this->errormsg
= 'Wrong Statement Type. Only INSERT allowed';
98 $errors = array(); /// To store all the errors found
100 /// Build the whole sentence
101 $sentence = '(' . $fields . ') VALUES (' . $values . ')';
103 /// Perform some checks
104 $fields = $statement->getFieldsFromInsertSentence($sentence);
105 $values = $statement->getValuesFromInsertSentence($sentence);
107 if (in_array('id', $fields)) {
108 $errors[] = $this->str
['cannotuseidfield'];
110 if ($result && count($fields) == 0) {
111 $errors[] = $this->str
['missingfieldsinsentence'];
113 if ($result && count($values) == 0) {
114 $errors[] = $this->str
['missingvaluesinsentence'];
116 if ($result && count($fields) != count($values)) {
117 $errors[] = $this->str
['wrongnumberoffieldsorvalues'];
120 if (!empty($errors)) {
121 /// Prepare the output
123 print_header("$site->shortname: XMLDB",
125 "<a href=\"../index.php\">" . $this->str
['administration'] . "</a> -> <a href=\"index.php\">XMLDB</a>");
126 notice ('<p>' .implode(', ', $errors) . '</p>
128 'index.php?action=edit_sentence&sentence=' .$sentenceparam . '&statement=' . urlencode($statementparam) . '&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)));
132 /// Continue if we aren't under errors
133 if (empty($errors)) {
134 $sentences[$sentenceparam] = $sentence;
136 /// If the sentence has changed from the old one, change the version
137 /// and mark the statement and structure as changed
138 if ($oldsentence != $sentence) {
139 $statement->setChanged(true);
140 $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
142 $structure->setChanged(true);
145 /// Launch postaction if exists (leave this here!)
146 if ($this->getPostAction() && $result) {
147 return $this->launch($this->getPostAction());
151 /// Return ok if arrived here