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 key 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_key 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          $keyparam = required_param('key', PARAM_CLEAN);
  91          if (!$key = $table->getKey($keyparam)) {
  92              // Arriving here from a name change, looking for the new key name
  93              $keyparam = required_param('name', PARAM_CLEAN);
  94              $key = $table->getKey($keyparam);
  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 ="key" value="' . $keyparam .'" />';
 106          $o.= '    <input type="hidden" name ="sesskey" value="' . sesskey() .'" />';
 107          $o.= '    <input type="hidden" name ="action" value="edit_key_save" />';
 108          $o.= '    <input type="hidden" name ="postaction" value="edit_table" />';
 109          $o.= '    <table id="formelements" class="boxaligncenter">';
 110          // XMLDB key name
 111          // If the key has dependencies, we cannot change its name
 112          $disabled = '';
 113          if ($structure->getKeyUses($table->getName(), $key->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($key->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($key->getComment()) . '</textarea></td></tr>';
 121          // xmldb_key Type
 122          $typeoptions = array (XMLDB_KEY_PRIMARY => $key->getXMLDBKeyName(XMLDB_KEY_PRIMARY),
 123                                XMLDB_KEY_UNIQUE  => $key->getXMLDBKeyName(XMLDB_KEY_UNIQUE),
 124                                XMLDB_KEY_FOREIGN   => $key->getXMLDBKeyName(XMLDB_KEY_FOREIGN),
 125                                XMLDB_KEY_FOREIGN_UNIQUE => $key->getXMLDBKeyName(XMLDB_KEY_FOREIGN_UNIQUE));
 126          // Only show the XMLDB_KEY_FOREIGN_UNIQUE if the Key has that type
 127          // if ($key->getType() != XMLDB_KEY_FOREIGN_UNIQUE) {
 128          // unset ($typeoptions[XMLDB_KEY_FOREIGN_UNIQUE);
 129          // }
 130          $select = html_writer::select($typeoptions, 'type', $key->getType(), false);
 131  
 132          $o.= '      <tr valign="top"><td><label for="menutype" accesskey="t">Type:</label></td>';
 133          $o.= '        <td colspan="2">' . $select . '</td></tr>';
 134          // xmldb_key Fields
 135          $o.= '      <tr valign="top"><td><label for="fields" accesskey="f">Fields:</label></td>';
 136          $o.= '        <td colspan="2"><input name="fields" type="text" size="40" maxlength="80" id="fields" value="' . s(implode(', ', $key->getFields())) . '" /></td></tr>';
 137          // xmldb_key Reftable
 138          $o.= '      <tr valign="top"><td><label for="reftable" accesskey="t">Reftable:</label></td>';
 139          $o.= '        <td colspan="2"><input name="reftable" type="text" size="20" maxlength="40" id="reftable" value="' . s($key->getReftable()) . '" /></td></tr>';
 140          // xmldb_key Reffields
 141          $o.= '      <tr valign="top"><td><label for="reffields" accesskey="t">Reffields:</label></td>';
 142          $o.= '        <td colspan="2"><input name="reffields" type="text" size="40" maxlength="80" id="reffields" value="' . s(implode(', ', $key->getRefFields())) . '" /></td></tr>';
 143          // Change button
 144          $o .= '      <tr valign="top"><td>&nbsp;</td><td colspan="2"><input type="submit" value="' .
 145                       $this->str['change'] . '" class="btn btn-secondary"/></td></tr>';
 146          $o.= '    </table>';
 147          $o.= '</div></form>';
 148          // Calculate the buttons
 149          $b = ' <p class="centerpara buttons">';
 150          // The view original XML button
 151          if ($table->getKey($keyparam)) {
 152              $b .= '&nbsp;<a href="index.php?action=view_key_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=original&amp;table=' . $tableparam . '&amp;key=' . $keyparam . '">[' . $this->str['vieworiginal'] . ']</a>';
 153          } else {
 154              $b .= '&nbsp;[' . $this->str['vieworiginal'] . ']';
 155          }
 156          // The view edited XML button
 157          if ($key->hasChanged()) {
 158              $b .= '&nbsp;<a href="index.php?action=view_key_xml&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;select=edited&amp;table=' . $tableparam . '&amp;key=' . $keyparam . '">[' . $this->str['viewedited'] . ']</a>';
 159          } else {
 160              $b .= '&nbsp;[' . $this->str['viewedited'] . ']';
 161          }
 162          // The back to edit table button
 163          $b .= '&nbsp;<a href="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '">[' . $this->str['back'] . ']</a>';
 164          $b .= '</p>';
 165          $o .= $b;
 166  
 167          $this->output = $o;
 168  
 169          // Launch postaction if exists (leave this here!)
 170          if ($this->getPostAction() && $result) {
 171              return $this->launch($this->getPostAction());
 172          }
 173  
 174          // Return ok if arrived here
 175          return $result;
 176      }
 177  }
 178