Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/xmldb/actions/create_xml_file/create_xml_file.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 
00031 class create_xml_file extends XMLDBAction {
00032 
00036     function init() {
00037         parent::init();
00038         // Set own core attributes
00039         $this->can_subaction = ACTION_NONE;
00040         //$this->can_subaction = ACTION_HAVE_SUBACTIONS;
00041 
00042         // Set own custom attributes
00043 
00044         // Get needed strings
00045         $this->loadStrings(array(
00046             // 'key' => 'module',
00047         ));
00048     }
00049 
00055     function invoke() {
00056         parent::invoke();
00057 
00058         $result = true;
00059 
00060         // Set own core attributes
00061         $this->does_generate = ACTION_NONE;
00062         //$this->does_generate = ACTION_GENERATE_HTML;
00063 
00064         // These are always here
00065         global $CFG, $XMLDB;
00066 
00067         // Do the job, setting result as needed
00068 
00069         // Get the dir containing the file
00070         $dirpath = required_param('dir', PARAM_PATH);
00071         $plugintype = $this->get_plugin_type($dirpath);
00072         $dirpath = $CFG->dirroot . $dirpath;
00073         $file = $dirpath . '/install.xml';
00074 
00075         // Some variables
00076         $xmlpath = dirname(str_replace($CFG->dirroot . '/', '', $file));
00077         $xmlversion = userdate(time(), '%Y%m%d', 99, false);
00078         $xmlcomment = 'XMLDB file for Moodle ' . dirname($xmlpath);
00079 
00080         $xmltable = strtolower(basename(dirname($xmlpath)));
00081         if ($plugintype && $plugintype != 'mod') {
00082             $xmltable = $plugintype.'_'.$xmltable;
00083         }
00084 
00085         // Initial contents
00086         $c = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
00087         $c.= '  <XMLDB PATH="' . $xmlpath . '" VERSION="' . $xmlversion .'" COMMENT="' . $xmlcomment .'">' . "\n";
00088         $c.= '    <TABLES>' . "\n";
00089         $c.= '      <TABLE NAME="' . $xmltable . '" COMMENT="Default comment for ' . $xmltable .', please edit me">' . "\n";
00090         $c.= '        <FIELDS>' . "\n";
00091         $c.= '          <FIELD NAME="id" TYPE="int" LENGTH="10" UNSIGNED="true" NOTNULL="true" SEQUENCE="true" />' . "\n";
00092         $c.= '        </FIELDS>' . "\n";
00093         $c.= '        <KEYS>' . "\n";
00094         $c.= '          <KEY NAME="primary" TYPE="primary" FIELDS="id" />' . "\n";
00095         $c.= '        </KEYS>' . "\n";
00096         $c.= '      </TABLE>' . "\n";
00097         $c.= '    </TABLES>' . "\n";
00098         $c.= '  </XMLDB>';
00099 
00100         if (!file_put_contents($file, $c)) {
00101             $errormsg = 'Error creando fichero ' . $file;
00102             $result = false;
00103         }
00104 
00105         // Launch postaction if exists
00106         if ($this->getPostAction() && $result) {
00107             return $this->launch($this->getPostAction());
00108         }
00109 
00110         // Return ok if arrived here
00111         return $result;
00112     }
00113 
00120     function get_plugin_type($dirpath) {
00121         global $CFG;
00122         $dirpath = $CFG->dirroot.$dirpath;
00123         $plugintypes = get_plugin_types();
00124         foreach ($plugintypes as $plugintype => $pluginbasedir) {
00125             if (substr($dirpath, 0, strlen($pluginbasedir)) == $pluginbasedir) {
00126                 return $plugintype;
00127             }
00128         }
00129         return null;
00130     }
00131 }
00132 
 All Data Structures Namespaces Files Functions Variables Enumerations