Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/new_table_from_mysql/new_table_from_mysql.class.php
Go to the documentation of this file.
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 
00033 class new_table_from_mysql extends XMLDBAction {
00034 
00038     function init() {
00039         parent::init();
00040 
00041         // Set own custom attributes
00042 
00043         // Get needed strings
00044         $this->loadStrings(array(
00045             'createtable' => 'tool_xmldb',
00046             'aftertable' => 'tool_xmldb',
00047             'create' => 'tool_xmldb',
00048             'back' => 'tool_xmldb'
00049         ));
00050     }
00051 
00057     function invoke() {
00058         parent::invoke();
00059 
00060         $result = true;
00061 
00062         // Set own core attributes
00063         $this->does_generate = ACTION_GENERATE_HTML;
00064 
00065         // These are always here
00066         global $CFG, $XMLDB, $DB, $OUTPUT;
00067 
00068         // Do the job, setting result as needed
00069         // Get the dir containing the file
00070         $dirpath = required_param('dir', PARAM_PATH);
00071         $dirpath = $CFG->dirroot . $dirpath;
00072 
00073         // Get the correct dirs
00074         if (!empty($XMLDB->dbdirs)) {
00075             $dbdir =& $XMLDB->dbdirs[$dirpath];
00076         } else {
00077             return false;
00078         }
00079         if (!empty($XMLDB->editeddirs)) {
00080             $editeddir =& $XMLDB->editeddirs[$dirpath];
00081             $structure =& $editeddir->xml_file->getStructure();
00082         }
00083 
00084         $tableparam = optional_param('table', NULL, PARAM_CLEAN);
00085 
00086         // If no table, show form
00087         if (!$tableparam) {
00088             // No postaction here
00089             $this->postaction = NULL;
00090             // Get list of tables
00091             $dbtables = $DB->get_tables();
00092             $selecttables = array();
00093             foreach ($dbtables as $dbtable) {
00094                 $i = $structure->findTableInArray($dbtable);
00095                 if ($i === NULL) {
00096                     $selecttables[$dbtable] = $dbtable;
00097                 }
00098             }
00099             // Get list of after tables
00100             $aftertables = array();
00101             if ($tables =& $structure->getTables()) {
00102                 foreach ($tables as $aftertable) {
00103                     $aftertables[$aftertable->getName()] = $aftertable->getName();
00104                 }
00105             }
00106             if (!$selecttables) {
00107                 $this->errormsg = 'No tables available to be retrofitted';
00108                 return false;
00109             }
00110             // Now build the 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 ="action" value="new_table_from_mysql" />';
00115             $o.= '    <input type="hidden" name ="postaction" value="edit_table" />';
00116             $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() . '" />';
00117             $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
00118             $o.= '      <tr><td><label for="table" accesskey="t">' . $this->str['createtable'] .' </label>' . html_writer::select($selecttables, 'table') . '<label for="after" accesskey="a">' . $this->str['aftertable'] . ' </label>' .html_writer::select($aftertables, 'after') . '</td></tr>';
00119             $o.= '      <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['create'] . '" /></td></tr>';
00120             $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>';
00121             $o.= '    </table>';
00122             $o.= '</div></form>';
00123 
00124             $this->output = $o;
00125 
00126 
00127         // If table, retrofit information and, if everything works,
00128         // go to the table edit action
00129         } else {
00130             // Get some params (table is mandatory here)
00131             $tableparam = required_param('table', PARAM_CLEAN);
00132             $afterparam = required_param('after', PARAM_CLEAN);
00133 
00134             // Create one new xmldb_table
00135             $table = new xmldb_table(strtolower(trim($tableparam)));
00136             $table->setComment($table->getName() . ' table retrofitted from MySQL');
00137             // Get fields info from ADODb
00138             $dbfields = $DB->get_columns($tableparam);
00139             if ($dbfields) {
00140                 foreach ($dbfields as $dbfield) {
00141                     // Create new XMLDB field
00142                     $field = new xmldb_field($dbfield->name);
00143                     // Set field with info retrofitted
00144                     $field->setFromADOField($dbfield);
00145                     // Add field to the table
00146                     $table->addField($field);
00147                 }
00148             }
00149             // Get PK, UK and indexes info from ADODb
00150             $dbindexes = $DB->get_indexes($tableparam);
00151             if ($dbindexes) {
00152                 $lastkey = NULL; //To temp store the last key processed
00153                 foreach ($dbindexes as $indexname => $dbindex) {
00154                     // Add the indexname to the array
00155                     $dbindex['name'] = $indexname;
00156                     // We are handling one xmldb_key (primaries + uniques)
00157                     if ($dbindex['unique']) {
00158                         $key = new xmldb_key(strtolower($dbindex['name']));
00159                         // Set key with info retrofitted
00160                         $key->setFromADOKey($dbindex);
00161                         // Set default comment to PKs
00162                         if ($key->getType() == XMLDB_KEY_PRIMARY) {
00163                         }
00164                         // Add key to the table
00165                         $table->addKey($key);
00166 
00167                     // We are handling one xmldb_index (non-uniques)
00168                     } else {
00169                         $index = new xmldb_index(strtolower($dbindex['name']));
00170                         // Set index with info retrofitted
00171                         $index->setFromADOIndex($dbindex);
00172                         // Add index to the table
00173                         $table->addIndex($index);
00174                     }
00175                 }
00176             }
00177             // Finally, add the whole retroffited table to the structure
00178             // in the place specified
00179             $structure->addTable($table, $afterparam);
00180         }
00181 
00182         // Launch postaction if exists (leave this here!)
00183         if ($this->getPostAction() && $result) {
00184             return $this->launch($this->getPostAction());
00185         }
00186 
00187         // Return ok if arrived here
00188         return $result;
00189     }
00190 }
00191 
 All Data Structures Namespaces Files Functions Variables Enumerations