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   * Add the mandatory fields for persistent to the table.
  25   *
  26   * @package    tool_xmldb
  27   * @copyright  2019 Michael Aherne
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class add_persistent_mandatory extends XMLDBAction {
  31  
  32      function init() {
  33  
  34          parent::init();
  35  
  36          // Get needed strings.
  37          $this->loadStrings(array(
  38              'addpersistent' => 'tool_xmldb',
  39              'persistentfieldsconfirm' => 'tool_xmldb',
  40              'persistentfieldscomplete' => 'tool_xmldb',
  41              'persistentfieldsexist' => 'tool_xmldb',
  42              'back' => 'core'
  43          ));
  44  
  45      }
  46  
  47      function getTitle() {
  48          return $this->str['addpersistent'];
  49      }
  50  
  51      function invoke() {
  52  
  53          parent::invoke();
  54  
  55          $this->does_generate = ACTION_GENERATE_HTML;
  56  
  57          global $CFG, $XMLDB, $OUTPUT;
  58  
  59          $dir = required_param('dir', PARAM_PATH);
  60          $dirpath = $CFG->dirroot . $dir;
  61  
  62          if (empty($XMLDB->dbdirs)) {
  63              return false;
  64          }
  65  
  66          if (!empty($XMLDB->editeddirs)) {
  67              $editeddir = $XMLDB->editeddirs[$dirpath];
  68              $structure = $editeddir->xml_file->getStructure();
  69          }
  70  
  71          $tableparam = required_param('table', PARAM_ALPHANUMEXT);
  72  
  73          /** @var xmldb_table $table */
  74          $table = $structure->getTable($tableparam);
  75  
  76          $result = true;
  77          // Launch postaction if exists (leave this here!)
  78          if ($this->getPostAction() && $result) {
  79              return $this->launch($this->getPostAction());
  80          }
  81  
  82          $confirm = optional_param('confirm', false, PARAM_BOOL);
  83  
  84          $fields = ['usermodified', 'timecreated', 'timemodified'];
  85          $existing = [];
  86          foreach ($fields as $field) {
  87              if ($table->getField($field)) {
  88                  $existing[] = $field;
  89              }
  90          }
  91  
  92          $returnurl = new \moodle_url('/admin/tool/xmldb/index.php', [
  93              'table' => $tableparam,
  94              'dir' => $dir,
  95              'action' => 'edit_table'
  96          ]);
  97  
  98          $backbutton = html_writer::link($returnurl, '[' . $this->str['back'] . ']');
  99          $actionbuttons = html_writer::tag('p', $backbutton, ['class' => 'centerpara buttons']);
 100  
 101          if (!$confirm) {
 102  
 103              if (!empty($existing)) {
 104  
 105                  $message = html_writer::span($this->str['persistentfieldsexist']);
 106                  $message .= html_writer::alist($existing);
 107                  $this->output .= $OUTPUT->notification($message);
 108  
 109                  if (count($existing) == count($fields)) {
 110                      $this->output .= $actionbuttons;
 111                      return true;
 112                  }
 113              }
 114  
 115              $confirmurl = new \moodle_url('/admin/tool/xmldb/index.php', [
 116                  'table' => $tableparam,
 117                  'dir' => $dir,
 118                  'action' => 'add_persistent_mandatory',
 119                  'sesskey' => sesskey(),
 120                  'confirm' => '1'
 121              ]);
 122  
 123              $message = html_writer::span($this->str['persistentfieldsconfirm']);
 124              $message .= html_writer::alist(array_diff($fields, $existing));
 125              $this->output .= $OUTPUT->confirm($message, $confirmurl, $returnurl);
 126  
 127          } else {
 128  
 129              $fieldsadded = [];
 130              foreach ($fields as $field) {
 131                  if (!in_array($field, $existing)) {
 132                      $fieldsadded[] = $field;
 133                      $table->add_field($field, XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, 0);
 134                  }
 135              }
 136  
 137              if (!$table->getKey('usermodified')) {
 138                  $table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']);
 139              }
 140  
 141              $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
 142              $structure->setChanged(true);
 143  
 144              $message = html_writer::span($this->str['persistentfieldscomplete']);
 145              $message .= html_writer::alist(array_diff($fields, $existing));
 146              $this->output .= $OUTPUT->notification($message, 'success');
 147  
 148              $this->output .= $actionbuttons;
 149          }
 150  
 151          return $result;
 152      }
 153  
 154  }