Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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 delete completely one field
  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 delete_field 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  
  40          // Get needed strings
  41          $this->loadStrings(array(
  42              'confirmdeletefield' => 'tool_xmldb',
  43              'yes' => '',
  44              'no' => ''
  45          ));
  46      }
  47  
  48      /**
  49       * Invoke method, every class will have its own
  50       * returns true/false on completion, setting both
  51       * errormsg and output as necessary
  52       */
  53      function invoke() {
  54          parent::invoke();
  55  
  56          $result = true;
  57  
  58          // Set own core attributes
  59          $this->does_generate = ACTION_GENERATE_HTML;
  60  
  61          // These are always here
  62          global $CFG, $XMLDB;
  63  
  64          // Do the job, setting result as needed
  65  
  66          // Get the dir containing the file
  67          $dirpath = required_param('dir', PARAM_PATH);
  68          $dirpath = $CFG->dirroot . $dirpath;
  69          $tableparam = required_param('table', PARAM_CLEAN);
  70          $fieldparam = required_param('field', PARAM_CLEAN);
  71  
  72          $confirmed = optional_param('confirmed', false, PARAM_BOOL);
  73  
  74          // If  not confirmed, show confirmation box
  75          if (!$confirmed) {
  76              $o = '<table width="60" class="generaltable" border="0" cellpadding="5" cellspacing="0" id="notice">';
  77              $o.= '  <tr><td class="generalboxcontent">';
  78              $o.= '    <p class="centerpara">' . $this->str['confirmdeletefield'] . '<br /><br />' . $fieldparam . '</p>';
  79              $o.= '    <table class="boxaligncenter" cellpadding="20"><tr><td>';
  80              $o.= '      <div class="singlebutton">';
  81              $o.= '        <form action="index.php?action=delete_field&amp;sesskey=' . sesskey() . '&amp;confirmed=yes&amp;postaction=edit_table&amp;field=' . $fieldparam . '&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post"><fieldset class="invisiblefieldset">';
  82              $o.= '          <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
  83              $o.= '      </td><td>';
  84              $o.= '      <div class="singlebutton">';
  85              $o.= '        <form action="index.php?action=edit_table&amp;table=' . $tableparam . '&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '" method="post"><fieldset class="invisiblefieldset">';
  86              $o.= '          <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
  87              $o.= '      </td></tr>';
  88              $o.= '    </table>';
  89              $o.= '  </td></tr>';
  90              $o.= '</table>';
  91  
  92              $this->output = $o;
  93          } else {
  94              // Get the edited dir
  95              if (!empty($XMLDB->editeddirs)) {
  96                  if (isset($XMLDB->editeddirs[$dirpath])) {
  97                      $dbdir = $XMLDB->dbdirs[$dirpath];
  98                      $editeddir = $XMLDB->editeddirs[$dirpath];
  99                      if ($editeddir) {
 100                          $structure = $editeddir->xml_file->getStructure();
 101                          // Move adjacent fields prev and next attributes
 102                          $tables = $structure->getTables();
 103                          $table = $structure->getTable($tableparam);
 104                          $fields = $table->getFields();
 105                          $field = $table->getField($fieldparam);
 106                          if ($field->getPrevious()) {
 107                              $prev = $table->getField($field->getPrevious());
 108                              $prev->setNext($field->getNext());
 109                          }
 110                          if ($field->getNext()) {
 111                              $next = $table->getField($field->getNext());
 112                              $next->setPrevious($field->getPrevious());
 113                          }
 114                          // Remove the field
 115                          $table->deleteField($fieldparam);
 116  
 117                          // Recalculate the hash
 118                          $structure->calculateHash(true);
 119  
 120                          // If the hash has changed from the original one, change the version
 121                          // and mark the structure as changed
 122                          $origstructure = $dbdir->xml_file->getStructure();
 123                          if ($structure->getHash() != $origstructure->getHash()) {
 124                              $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 125                              $structure->setChanged(true);
 126                          }
 127                      }
 128                  }
 129              }
 130          }
 131  
 132          // Launch postaction if exists (leave this here!)
 133          if ($this->getPostAction() && $result) {
 134              return $this->launch($this->getPostAction());
 135          }
 136  
 137          // Return ok if arrived here
 138          return $result;
 139      }
 140  }
 141