Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/new_table/new_table.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 
00032 class new_table extends XMLDBAction {
00033 
00037     function init() {
00038         parent::init();
00039 
00040         // Set own custom attributes
00041 
00042         // Get needed strings
00043         $this->loadStrings(array(
00044             // 'key' => 'module',
00045         ));
00046     }
00047 
00053     function invoke() {
00054         parent::invoke();
00055 
00056         $result = true;
00057 
00058         // Set own core attributes
00059         $this->does_generate = ACTION_NONE;
00060         //$this->does_generate = ACTION_GENERATE_HTML;
00061 
00062         // These are always here
00063         global $CFG, $XMLDB;
00064 
00065         // Do the job, setting result as needed
00066         // Get the dir containing the file
00067         $dirpath = required_param('dir', PARAM_PATH);
00068         $dirpath = $CFG->dirroot . $dirpath;
00069 
00070         // Get the correct dirs
00071         if (!empty($XMLDB->dbdirs)) {
00072             $dbdir =& $XMLDB->dbdirs[$dirpath];
00073         } else {
00074             return false;
00075         }
00076         if (!empty($XMLDB->editeddirs)) {
00077             $editeddir =& $XMLDB->editeddirs[$dirpath];
00078             $structure =& $editeddir->xml_file->getStructure();
00079         }
00080 
00081         // If the changeme table exists, just get it and continue
00082         $changeme_exists = false;
00083         if ($tables =& $structure->getTables()) {
00084             if ($table =& $structure->getTable('changeme')) {
00085                 $changeme_exists = true;
00086             }
00087         }
00088         if (!$changeme_exists) { // Lets create the table
00089             $field = new xmldb_field('id');
00090             $field->setType(XMLDB_TYPE_INTEGER);
00091             $field->setLength(10);
00092             $field->setNotNull(true);
00093             $field->setUnsigned(true);
00094             $field->setSequence(true);
00095             $field->setLoaded(true);
00096             $field->setChanged(true);
00097 
00098             $key = new xmldb_key('primary');
00099             $key->setType(XMLDB_KEY_PRIMARY);
00100             $key->setFields(array('id'));
00101             $key->setLoaded(true);
00102             $key->setChanged(true);
00103 
00104             $table = new xmldb_table('changeme');
00105             $table->setComment('Default comment for the table, please edit me');
00106             $table->addField($field);
00107             $table->addKey($key);
00108 
00109             // Finally, add the whole retrofitted table to the structure
00110             // in the place specified
00111             $structure->addTable($table);
00112         }
00113         // Launch postaction if exists (leave this here!)
00114         if ($this->getPostAction() && $result) {
00115             return $this->launch($this->getPostAction());
00116         }
00117 
00118         // Return ok if arrived here
00119         return $result;
00120     }
00121 }
00122 
 All Data Structures Namespaces Files Functions Variables Enumerations