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 provide the interface for all the edit index actions
  25   *
  26   * @package    tool_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   */
  30  class edit_index extends XMLDBAction {
  31  
  32      /**
  33       * Init method, every subclass will have its own
  34       */
  35      function init() {
  36          parent::init();
  37  
  38          // Set own custom attributes
  39          $this->sesskey_protected = false; // This action doesn't need sesskey protection
  40  
  41          // Get needed strings
  42          $this->loadStrings(array(
  43              'change' => 'tool_xmldb',
  44              'vieworiginal' => 'tool_xmldb',
  45              'viewedited' => 'tool_xmldb',
  46              'yes' => '',
  47              'no' => '',
  48              'back' => 'tool_xmldb'
  49          ));
  50      }
  51  
  52      /**
  53       * Invoke method, every class will have its own
  54       * returns true/false on completion, setting both
  55       * errormsg and output as necessary
  56       */
  57      function invoke() {
  58          parent::invoke();
  59  
  60          $result = true;
  61  
  62          // Set own core attributes
  63          $this->does_generate = ACTION_GENERATE_HTML;
  64  
  65          // These are always here
  66          global $CFG, $XMLDB, $OUTPUT;
  67  
  68          // Do the job, setting result as needed
  69          // Get the dir containing the file
  70          $dirpath = required_param('dir', PARAM_PATH);
  71          $dirpath = $CFG->dirroot . $dirpath;
  72  
  73          // Get the correct dirs
  74          if (!empty($XMLDB->dbdirs)) {
  75              $dbdir = $XMLDB->dbdirs[$dirpath];
  76          } else {
  77              return false;
  78          }
  79          if (!empty($XMLDB->editeddirs)) {
  80              $editeddir = $XMLDB->editeddirs[$dirpath];
  81              $structure = $editeddir->xml_file->getStructure();
  82          }
  83  
  84          // Fetch request data
  85          $tableparam = required_param('table', PARAM_CLEAN);
  86          if (!$table = $structure->getTable($tableparam)) {
  87              $this->errormsg = 'Wrong table specified: ' . $tableparam;
  88              return false;
  89          }
  90          $indexparam = required_param('index', PARAM_CLEAN);
  91          if (!$index = $table->getIndex($indexparam)) {
  92              // Arriving here from a name change, looking for the new key name
  93              $indexparam = required_param('name', PARAM_CLEAN);
  94              $index = $table->getIndex($indexparam);
  95          }
  96  
  97          $dbdir = $XMLDB->dbdirs[$dirpath];
  98          $origstructure = $dbdir->xml_file->getStructure();
  99  
 100          // Add the main form
 101          $o = '<form id="form" action="index.php" method="post">';
 102          $o.= '<div>';
 103          $o.= '    <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
 104          $o.= '    <input type="hidden" name ="table" value="' . $tableparam .'" />';
 105          $o.= '    <input type="hidden" name ="index" value="' . $indexparam .'" />';
 106          $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() .'" />';
 107          $o.= '    <input type="hidden" name ="action" value="edit_index_save" />';
 108          $o.= '    <input type="hidden" name ="postaction" value="edit_table" />';
 109          $o.= '    <table id="formelements" class="boxaligncenter">';
 110          // XMLDB index name
 111          // If the index has dependencies, we cannot change its name
 112          $disabled = '';
 113          if ($structure->getIndexUses($table->getName(), $index->getName())) {
 114              $disabled = ' disabled="disabled " ';
 115          }
 116          $o.= '      <tr valign="top"><td><label for="name" accesskey="n">Name:</label></td><td colspan="2"><input name="name" type="text" size="'.xmldb_field::NAME_MAX_LENGTH.'" id="name"' . $disabled . ' value="' . s($index->getName()) . '" /></td></tr>';
 117          // XMLDB key comment
 118          $o .= '      <tr valign="top"><td><label for="comment" accesskey="c">Comment:</label></td><td colspan="2">
 119                       <textarea name="comment" rows="3" cols="80" id="comment" class="form-control">' .
 120                       s($index->getComment()) . '</textarea></td></tr>';
 121          // xmldb_index Type
 122          $typeoptions = array (0 => 'not unique',
 123                                1 => 'unique');
 124          $o.= '      <tr valign="top"><td><label for="menuunique" accesskey="t">Type:</label></td>';
 125          $select = html_writer::select($typeoptions, 'unique', $index->getUnique(), false);
 126          $o.= '        <td colspan="2">' . $select . '</td></tr>';
 127          // xmldb_index Fields
 128          $o.= '      <tr valign="top"><td><label for="fields" accesskey="f">Fields:</label></td>';
 129          $o.= '        <td colspan="2"><input name="fields" type="text" size="40" maxlength="80" id="fields" value="' . s(implode(', ', $index->getFields())) . '" /></td></tr>';
 130          // xmldb_index hints
 131          $o.= '      <tr valign="top"><td><label for="hints" accesskey="h">Hints:</label></td>';
 132          $o.= '        <td colspan="2"><input name="hints" type="text" size="40" maxlength="80" id="hints" value="' . s(implode(', ', $index->getHints())) . '" /></td></tr>';
 133          // Change button
 134          $o .= '      <tr valign="top"><td>&nbsp;</td><td colspan="2"><input type="submit" value="' .
 135                       $this->str['change'] . '" class="btn btn-secondary"/></td></tr>';
 136          $o.= '    </table>';
 137          $o.= '</div></form>';
 138          // Calculate the buttons
 139          $b = ' <p class="centerpara buttons">';
 140          // The view original XML button
 141          if ($table->getIndex($indexparam)) {
 142              $b .= '&nbsp;<a href="index.php?action=view_index_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original&amp;table=' . $tableparam . '&amp;index=' . $indexparam . '">[' . $this->str['vieworiginal'] . ']</a>';
 143          } else {
 144              $b .= '&nbsp;[' . $this->str['vieworiginal'] . ']';
 145          }
 146          // The view edited XML button
 147          if ($index->hasChanged()) {
 148              $b .= '&nbsp;<a href="index.php?action=view_index_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited&amp;table=' . $tableparam . '&amp;index=' . $indexparam . '">[' . $this->str['viewedited'] . ']</a>';
 149          } else {
 150              $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
 151          }
 152          // The back to edit table button
 153          $b .= '&nbsp;<a href="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
 154          $b .= '</p>';
 155          $o .= $b;
 156  
 157          $this->output = $o;
 158  
 159          // Launch postaction if exists (leave this here!)
 160          if ($this->getPostAction() && $result) {
 161              return $this->launch($this->getPostAction());
 162          }
 163  
 164          // Return ok if arrived here
 165          return $result;
 166      }
 167  }
 168