| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: new_table_from_mysql.class.php,v 1.8 2007/10/10 05:25:25 nicolasconnault Exp $ 2 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 /////////////////////////////////////////////////////////////////////////// 26 27 /// This class will ask and retrofit all the information from one 28 /// mysql table present in the Moodle DB to one XMLDBTable structure 29 30 class new_table_from_mysql 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 'createtable' => 'xmldb', 43 'aftertable' => 'xmldb', 44 'create' => 'xmldb', 45 'back' => 'xmldb' 46 )); 47 } 48 49 /** 50 * Invoke method, every class will have its own 51 * returns true/false on completion, setting both 52 * errormsg and output as necessary 53 */ 54 function invoke() { 55 parent::invoke(); 56 57 $result = true; 58 59 /// Set own core attributes 60 $this->does_generate = ACTION_GENERATE_HTML; 61 62 /// These are always here 63 global $CFG, $XMLDB, $db; 64 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); 69 70 /// Get the correct dirs 71 if (!empty($XMLDB->dbdirs)) { 72 $dbdir =& $XMLDB->dbdirs[$dirpath]; 73 } else { 74 return false; 75 } 76 if (!empty($XMLDB->editeddirs)) { 77 $editeddir =& $XMLDB->editeddirs[$dirpath]; 78 $structure =& $editeddir->xml_file->getStructure(); 79 } 80 /// ADD YOUR CODE HERE 81 $tableparam = optional_param('table', NULL, PARAM_CLEAN); 82 83 /// If no table, show form 84 if (!$tableparam) { 85 /// No postaction here 86 $this->postaction = NULL; 87 /// Get list of tables 88 $dbtables = $db->MetaTables('TABLES'); 89 $selecttables = array(); 90 foreach ($dbtables as $dbtable) { 91 $dbtable = strtolower(str_replace($CFG->prefix, '', $dbtable)); 92 $i = $structure->findTableInArray($dbtable); 93 if ($i === NULL) { 94 $selecttables[$dbtable] = $dbtable; 95 } 96 } 97 /// Get list of after tables 98 $aftertables = array(); 99 if ($tables =& $structure->getTables()) { 100 foreach ($tables as $aftertable) { 101 $aftertables[$aftertable->getName()] = $aftertable->getName(); 102 } 103 } 104 if (!$selecttables) { 105 $this->errormsg = 'No tables available to be retrofitted'; 106 return false; 107 } 108 /// Now build the form 109 $o = '<form id="form" action="index.php" method="post">'; 110 $o .= '<div>'; 111 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />'; 112 $o.= ' <input type="hidden" name ="action" value="new_table_from_mysql" />'; 113 $o.= ' <input type="hidden" name ="postaction" value="edit_table" />'; 114 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">'; 115 $o.= ' <tr><td><label for="table" accesskey="t">' . $this->str['createtable'] .' </label>' . choose_from_menu($selecttables, 'table', '', 'choose', '', 0, true) . '<label for="after" accesskey="a">' . $this->str['aftertable'] . ' </label>' .choose_from_menu($aftertables, 'after', '', 'choose', '', 0, true) . '</td></tr>'; 116 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['create'] . '" /></td></tr>'; 117 $o.= ' <tr><td colspan="2" align="center"><a href="index.php?action=edit_xml_file&dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a></td></tr>'; 118 $o.= ' </table>'; 119 $o.= '</div></form>'; 120 121 $this->output = $o; 122 123 124 /// If table, retrofit information and, if everything works, 125 /// go to the table edit action 126 } else { 127 /// Get some params (table is mandatory here) 128 $tableparam = required_param('table', PARAM_CLEAN); 129 $afterparam = required_param('after', PARAM_CLEAN); 130 131 /// Create one new XMLDBTable 132 $table = new XMLDBTable(strtolower(trim($tableparam))); 133 $table->setComment($table->getName() . ' table retrofitted from MySQL'); 134 /// Get fields info from ADODb 135 if(!$dbfields = $db->MetaColumns($CFG->prefix . $tableparam)) { 136 ///Try it without prefix if doesn't exist 137 $dbfields = $db->MetaColumns($tableparam); 138 } 139 if ($dbfields) { 140 foreach ($dbfields as $dbfield) { 141 /// Create new XMLDB field 142 $field = new XMLDBField(strtolower($dbfield->name)); 143 /// Set field with info retrofitted 144 $field->setFromADOField($dbfield); 145 /// Add field to the table 146 $table->addField($field); 147 } 148 } 149 /// Get PK, UK and indexes info from ADODb 150 $dbindexes = $db->MetaIndexes($CFG->prefix . $tableparam, true); 151 if ($dbindexes) { 152 $lastkey = NULL; //To temp store the last key processed 153 foreach ($dbindexes as $indexname => $dbindex) { 154 /// Add the indexname to the array 155 $dbindex['name'] = $indexname; 156 /// We are handling one XMLDBKey (primaries + uniques) 157 if ($dbindex['unique']) { 158 $key = new XMLDBKey(strtolower($dbindex['name'])); 159 /// Set key with info retrofitted 160 $key->setFromADOKey($dbindex); 161 /// Set default comment to PKs 162 if ($key->getType() == XMLDB_KEY_PRIMARY) { 163 } 164 /// Add key to the table 165 $table->addKey($key); 166 167 /// We are handling one XMLDBIndex (non-uniques) 168 } else { 169 $index = new XMLDBIndex(strtolower($dbindex['name'])); 170 /// Set index with info retrofitted 171 $index->setFromADOIndex($dbindex); 172 /// Add index to the table 173 $table->addIndex($index); 174 } 175 } 176 } 177 /// Finally, add the whole retroffited table to the structure 178 /// in the place specified 179 $structure->addTable($table, $afterparam); 180 } 181 182 /// Launch postaction if exists (leave this here!) 183 if ($this->getPostAction() && $result) { 184 return $this->launch($this->getPostAction()); 185 } 186 187 /// Return ok if arrived here 188 return $result; 189 } 190 } 191 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |