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   * one table
  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_table_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          // Get parameters
  83          $tableparam = required_param('table', PARAM_PATH);
  84          if (!$table = $structure->getTable($tableparam)) {
  85              $this->errormsg = 'Wrong table specified: ' . $tableparam;
  86              return false;
  87          }
  88  
  89          // The back to edit table button
  90          $b = ' <p class="centerpara buttons">';
  91          $b .= '<a href="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
  92          $b .= '</p>';
  93          $o = $b;
  94  
  95          $o.= '    <table id="formelements" class="boxaligncenter" cellpadding="5">';
  96          $o .= '      <tr><td><textarea cols="80" rows="32" class="form-control">';
  97  
  98          // Get an array of statements
  99          if ($starr = $DB->get_manager()->generator->getCreateTableSQL($table)) {
 100              $starr = $dbman->generator->getEndedStatements($starr);
 101              $sqltext = '';
 102              foreach ($starr as $st) {
 103                  $sqltext .= s($st) . "\n\n";
 104              }
 105              $sqltext = trim($sqltext);
 106              $o.= $sqltext;
 107          }
 108          $o.= '</textarea></td></tr>';
 109          $o.= '    </table>';
 110  
 111          $this->output = $o;
 112  
 113          // Launch postaction if exists (leave this here!)
 114          if ($this->getPostAction() && $result) {
 115              return $this->launch($this->getPostAction());
 116          }
 117  
 118          // Return ok if arrived here
 119          return $result;
 120      }
 121  }
 122