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   * This class will show the SQL generated for the selected RDBMS for
  25   * the entire XMLDB file
  26   *
  27   * @package    tool_xmldb
  28   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class view_structure_sql extends XMLDBAction {
  32  
  33      /**
  34       * Init method, every subclass will have its own
  35       */
  36      function init() {
  37          parent::init();
  38  
  39          // Set own custom attributes
  40          $this->sesskey_protected = false; // This action doesn't need sesskey protection
  41  
  42          // Get needed strings
  43          $this->loadStrings(array(
  44              'selectdb' => 'tool_xmldb',
  45              'back' => 'tool_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          $dbman = $DB->get_manager();
  65  
  66          // Do the job, setting result as needed
  67          // Get the dir containing the file
  68          $dirpath = required_param('dir', PARAM_PATH);
  69          $dirpath = $CFG->dirroot . $dirpath;
  70  
  71          // Get the correct dirs
  72          if (!empty($XMLDB->dbdirs)) {
  73              $dbdir = $XMLDB->dbdirs[$dirpath];
  74          } else {
  75              return false;
  76          }
  77          if (!empty($XMLDB->editeddirs)) {
  78              $editeddir = $XMLDB->editeddirs[$dirpath];
  79              $structure = $editeddir->xml_file->getStructure();
  80          }
  81  
  82          // The back to edit table button
  83          $b = ' <p class="centerpara buttons">';
  84          $b .= '<a href="index.php?action=edit_xml_file&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
  85          $b .= '</p>';
  86          $o = $b;
  87  
  88          $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
  89          $o .= '      <tr><td><textarea cols="80" rows="32" class="form-control">';
  90          // Get an array of statements
  91          if ($starr = $DB->get_manager()->generator->getCreateStructureSQL($structure)) {
  92              $starr = $dbman->generator->getEndedStatements($starr);
  93              $sqltext = '';
  94              foreach ($starr as $st) {
  95                  $sqltext .= s($st) . "\n\n";
  96              }
  97              $sqltext = trim($sqltext);
  98              $o.= $sqltext;
  99          }
 100          $o.= '</textarea></td></tr>';
 101          $o.= '    </table>';
 102  
 103          $this->output = $o;
 104  
 105          // Launch postaction if exists (leave this here!)
 106          if ($this->getPostAction() && $result) {
 107              return $this->launch($this->getPostAction());
 108          }
 109  
 110          // Return ok if arrived here
 111          return $result;
 112      }
 113  }
 114