MDL-31527 XMLDB Editor - make sure default tables are created with correct plugin...
[moodle.git] / admin / tool / xmldb / actions / create_xml_file / create_xml_file.class.php
CommitLineData
0df0df23 1<?php
b007a5d0
PS
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/>.
8c15ae99 16
b007a5d0
PS
17/**
18 * @package tool
19 * @subpackage xmldb
20 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
8c15ae99 23
b007a5d0
PS
24/**
25 * @package tool
26 * @subpackage xmldb
27 * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
8c15ae99 30
31class create_xml_file extends XMLDBAction {
32
eef868d1 33 /**
8c15ae99 34 * Init method, every subclass will have its own
35 */
36 function init() {
37 parent::init();
35c4f1e2 38 // Set own core attributes
8c15ae99 39 $this->can_subaction = ACTION_NONE;
40 //$this->can_subaction = ACTION_HAVE_SUBACTIONS;
41
35c4f1e2 42 // Set own custom attributes
8c15ae99 43
35c4f1e2 44 // Get needed strings
8c15ae99 45 $this->loadStrings(array(
35c4f1e2 46 // 'key' => 'module',
8c15ae99 47 ));
48 }
49
50 /**
51 * Invoke method, every class will have its own
52 * returns true/false on completion, setting both
53 * errormsg and output as necessary
54 */
55 function invoke() {
56 parent::invoke();
57
58 $result = true;
59
35c4f1e2 60 // Set own core attributes
8c15ae99 61 $this->does_generate = ACTION_NONE;
62 //$this->does_generate = ACTION_GENERATE_HTML;
63
35c4f1e2 64 // These are always here
8c15ae99 65 global $CFG, $XMLDB;
66
35c4f1e2 67 // Do the job, setting result as needed
8c15ae99 68
35c4f1e2 69 // Get the dir containing the file
c080aa88 70 $dirpath = required_param('dir', PARAM_PATH);
1cdadf51 71 $plugintype = $this->get_plugin_type($dirpath);
294ce987 72 $dirpath = $CFG->dirroot . $dirpath;
8c15ae99 73 $file = $dirpath . '/install.xml';
74
35c4f1e2 75 // Some variables
8c15ae99 76 $xmlpath = dirname(str_replace($CFG->dirroot . '/', '', $file));
77 $xmlversion = userdate(time(), '%Y%m%d', 99, false);
78 $xmlcomment = 'XMLDB file for Moodle ' . dirname($xmlpath);
79
80 $xmltable = strtolower(basename(dirname($xmlpath)));
1cdadf51
DS
81 if ($plugintype && $plugintype != 'mod') {
82 $xmltable = $plugintype.'_'.$xmltable;
83 }
eef868d1 84
35c4f1e2 85 // Initial contents
8c15ae99 86 $c = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
87 $c.= ' <XMLDB PATH="' . $xmlpath . '" VERSION="' . $xmlversion .'" COMMENT="' . $xmlcomment .'">' . "\n";
88 $c.= ' <TABLES>' . "\n";
89 $c.= ' <TABLE NAME="' . $xmltable . '" COMMENT="Default comment for ' . $xmltable .', please edit me">' . "\n";
90 $c.= ' <FIELDS>' . "\n";
4a78d22f 91 $c.= ' <FIELD NAME="id" TYPE="int" LENGTH="10" UNSIGNED="true" NOTNULL="true" SEQUENCE="true" />' . "\n";
8c15ae99 92 $c.= ' </FIELDS>' . "\n";
93 $c.= ' <KEYS>' . "\n";
4a78d22f 94 $c.= ' <KEY NAME="primary" TYPE="primary" FIELDS="id" />' . "\n";
8c15ae99 95 $c.= ' </KEYS>' . "\n";
96 $c.= ' </TABLE>' . "\n";
97 $c.= ' </TABLES>' . "\n";
98 $c.= ' </XMLDB>';
99
100 if (!file_put_contents($file, $c)) {
101 $errormsg = 'Error creando fichero ' . $file;
102 $result = false;
103 }
104
35c4f1e2 105 // Launch postaction if exists
8c15ae99 106 if ($this->getPostAction() && $result) {
107 return $this->launch($this->getPostAction());
108 }
109
35c4f1e2 110 // Return ok if arrived here
8c15ae99 111 return $result;
112 }
1cdadf51
DS
113
114 /**
115 * From a given path, work out what type of plugin
116 * this belongs to
117 * @param string $dirpath Path to the db file for this plugin
118 * @return string the type of the plugin or null if not found
119 */
120 function get_plugin_type($dirpath) {
121 global $CFG;
122 $dirpath = $CFG->dirroot.$dirpath;
123 $plugintypes = get_plugin_types();
124 foreach ($plugintypes as $plugintype => $pluginbasedir) {
125 if (substr($dirpath, 0, strlen($pluginbasedir)) == $pluginbasedir) {
126 return $plugintype;
127 }
128 }
129 return null;
130 }
8c15ae99 131}
0df0df23 132