Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / xmldb / actions / new_statement / new_statement.class.php
blobf80e9bc4533e5ec074e7f46f02a86bbdfd819f26
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12 // //
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. //
17 // //
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: //
22 // //
23 // http://www.gnu.org/copyleft/gpl.html //
24 // //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will ask for one statement type and table
28 /// to be able to add sentences of that type
30 class new_statement extends XMLDBAction {
32 /**
33 * Init method, every subclass will have its own
35 function init() {
36 parent::init();
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'statementtype' => 'xmldb',
43 'statementtable' => 'xmldb',
44 'create' => 'xmldb',
45 'back' => 'xmldb'
46 ));
49 /**
50 * Invoke method, every class will have its own
51 * returns true/false on completion, setting both
52 * errormsg and output as necessary
54 function invoke() {
55 parent::invoke();
57 $result = true;
59 /// Set own core attributes
60 $this->does_generate = ACTION_GENERATE_HTML;
62 /// These are always here
63 global $CFG, $XMLDB, $db;
65 /// Do the job, setting result as needed
66 /// Get the dir containing the file
67 $dirpath = required_param('dir', PARAM_PATH);
68 $dirpath = $CFG->dirroot . stripslashes_safe($dirpath);
70 /// Get the correct dirs
71 if (!empty($XMLDB->dbdirs)) {
72 $dbdir =& $XMLDB->dbdirs[$dirpath];
73 } else {
74 return false;
76 if (!empty($XMLDB->editeddirs)) {
77 $editeddir =& $XMLDB->editeddirs[$dirpath];
78 $structure =& $editeddir->xml_file->getStructure();
80 /// ADD YOUR CODE HERE
81 $tableparam = optional_param('table', NULL, PARAM_CLEAN);
82 $typeparam = optional_param('type', NULL, PARAM_CLEAN);
84 /// If no table or type, show form
85 if (!$tableparam || !$typeparam) {
86 /// No postaction here
87 $this->postaction = NULL;
88 /// Get list of tables
89 $dbtables = $db->MetaTables('TABLES');
90 $selecttables = array();
91 foreach ($dbtables as $dbtable) {
92 $dbtable = str_replace($CFG->prefix, '', $dbtable);
93 $selecttables[$dbtable] = $dbtable;
95 /// Get list of statement types
96 $typeoptions = array (XMLDB_STATEMENT_INSERT => XMLDBStatement::getXMLDBStatementName(XMLDB_STATEMENT_INSERT),
97 XMLDB_STATEMENT_UPDATE => XMLDBStatement::getXMLDBStatementName(XMLDB_STATEMENT_UPDATE),
98 XMLDB_STATEMENT_DELETE => XMLDBStatement::getXMLDBStatementName(XMLDB_STATEMENT_DELETE),
99 XMLDB_STATEMENT_CUSTOM => XMLDBStatement::getXMLDBStatementName(XMLDB_STATEMENT_CUSTOM));
100 if (!$selecttables) {
101 $this->errormsg = 'No tables available to create statements';
102 return false;
104 /// Now build the form
105 $o = '<form id="form" action="index.php" method="post">';
106 $o .= '<div>';
107 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
108 $o.= ' <input type="hidden" name ="action" value="new_statement" />';
109 $o.= ' <input type="hidden" name ="postaction" value="edit_statement" />';
110 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
111 $o.= ' <tr><td><label for="type" accesskey="t">' . $this->str['statementtype'] .' </label>' . choose_from_menu($typeoptions, 'type', '', 'choose', '', 0, true) . '<label for="table" accesskey="a">' . $this->str['statementtable'] . ' </label>' .choose_from_menu($selecttables, 'table', '', 'choose', '', 0, true) . '</td></tr>';
112 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['create'] . '" /></td></tr>';
113 $o.= ' <tr><td colspan="2" align="center"><a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a></td></tr>';
114 $o.= ' </table>';
115 $o.= '</div></form>';
117 $this->output = $o;
120 /// If table, retrofit information and, if everything works,
121 /// go to the table edit action
122 } else {
123 /// Get some params (table is mandatory here)
124 $tableparam = required_param('table', PARAM_CLEAN);
125 $typeparam = required_param('type', PARAM_CLEAN);
127 /// Only insert is allowed :-/
128 if ($typeparam != XMLDB_STATEMENT_INSERT) {
129 $this->errormsg = 'Only insert of records is supported';
130 return false;
133 /// Calculate the name of the statement
134 $typename = XMLDBStatement::getXMLDBStatementName($typeparam);
135 $name = trim(strtolower($typename . ' ' . $tableparam));
137 /// Check that this Statement hasn't been created before
138 if ($structure->getStatement($name)) {
139 $this->errormsg = 'The statement "' . $name . '" already exists, please use it to add more sentences';
140 return false;
143 /// Create one new XMLDBStatement
144 $statement = new XMLDBStatement($name);
145 $statement->setType($typeparam);
146 $statement->setTable($tableparam);
147 $statement->setComment('Initial ' . $typename . ' of records on table ' . $tableparam);
148 /// Finally, add the whole retroffited table to the structure
149 /// in the place specified
150 $structure->addStatement($statement);
153 /// Launch postaction if exists (leave this here!)
154 if ($this->getPostAction() && $result) {
155 return $this->launch($this->getPostAction());
158 /// Return ok if arrived here
159 return $result;