Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * @package    tool_xmldb
  25   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  class create_xml_file extends XMLDBAction {
  30  
  31      /**
  32       * Init method, every subclass will have its own
  33       */
  34      function init() {
  35          parent::init();
  36          // Set own core attributes
  37          $this->can_subaction = ACTION_NONE;
  38          //$this->can_subaction = ACTION_HAVE_SUBACTIONS;
  39  
  40          // Set own custom attributes
  41  
  42          // Get needed strings
  43          $this->loadStrings(array(
  44              // 'key' => 'module',
  45          ));
  46      }
  47  
  48      /**
  49       * Invoke method, every class will have its own
  50       * returns true/false on completion, setting both
  51       * errormsg and output as necessary
  52       */
  53      function invoke() {
  54          parent::invoke();
  55  
  56          $result = true;
  57  
  58          // Set own core attributes
  59          $this->does_generate = ACTION_NONE;
  60          //$this->does_generate = ACTION_GENERATE_HTML;
  61  
  62          // These are always here
  63          global $CFG, $XMLDB;
  64  
  65          // Do the job, setting result as needed
  66  
  67          // Get the dir containing the file
  68          $dirpath = required_param('dir', PARAM_PATH);
  69          $plugintype = $this->get_plugin_type($dirpath);
  70          $dirpath = $CFG->dirroot . $dirpath;
  71          $file = $dirpath . '/install.xml';
  72  
  73          // Some variables
  74          $xmlpath = dirname(str_replace($CFG->dirroot . '/', '', $file));
  75          $xmlversion = userdate(time(), '%Y%m%d', 99, false);
  76          $xmlcomment = 'XMLDB file for Moodle ' . dirname($xmlpath);
  77  
  78          $xmltable = strtolower(basename(dirname($xmlpath)));
  79          if ($plugintype && $plugintype != 'mod') {
  80              $xmltable = $plugintype.'_'.$xmltable;
  81          }
  82  
  83          // Initial contents
  84          $c = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
  85          $c.= '  <XMLDB PATH="' . $xmlpath . '" VERSION="' . $xmlversion .'" COMMENT="' . $xmlcomment .'">' . "\n";
  86          $c.= '    <TABLES>' . "\n";
  87          $c.= '      <TABLE NAME="' . $xmltable . '" COMMENT="Default comment for ' . $xmltable .', please edit me">' . "\n";
  88          $c.= '        <FIELDS>' . "\n";
  89          $c.= '          <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" />' . "\n";
  90          $c.= '        </FIELDS>' . "\n";
  91          $c.= '        <KEYS>' . "\n";
  92          $c.= '          <KEY NAME="primary" TYPE="primary" FIELDS="id" />' . "\n";
  93          $c.= '        </KEYS>' . "\n";
  94          $c.= '      </TABLE>' . "\n";
  95          $c.= '    </TABLES>' . "\n";
  96          $c.= '  </XMLDB>' . "\n";
  97  
  98          if (!file_put_contents($file, $c)) {
  99              $errormsg = 'Error creando fichero ' . $file;
 100              $result = false;
 101          }
 102  
 103          // Launch postaction if exists
 104          if ($this->getPostAction() && $result) {
 105              return $this->launch($this->getPostAction());
 106          }
 107  
 108          // Return ok if arrived here
 109          return $result;
 110      }
 111  
 112      /**
 113       * From a given path, work out what type of plugin
 114       * this belongs to
 115       * @param string $dirpath Path to the db file for this plugin
 116       * @return string the type of the plugin or null if not found
 117       */
 118      function get_plugin_type($dirpath) {
 119          global $CFG;
 120          $dirpath = $CFG->dirroot.$dirpath;
 121          // Reverse order so that we get subplugin matches.
 122          $plugintypes = array_reverse(core_component::get_plugin_types());
 123          foreach ($plugintypes as $plugintype => $pluginbasedir) {
 124              if (substr($dirpath, 0, strlen($pluginbasedir)) == $pluginbasedir) {
 125                  return $plugintype;
 126              }
 127          }
 128          return null;
 129      }
 130  }
 131