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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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 save the changes performed to the comment of one file
  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_xml_file_save 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              // 'key' => 'module',
  43          ));
  44      }
  45  
  46      /**
  47       * Invoke method, every class will have its own
  48       * returns true/false on completion, setting both
  49       * errormsg and output as necessary
  50       */
  51      function invoke() {
  52          parent::invoke();
  53  
  54          $result = true;
  55  
  56          // Set own core attributes
  57          $this->does_generate = ACTION_NONE;
  58          //$this->does_generate = ACTION_GENERATE_HTML;
  59  
  60          // These are always here
  61          global $CFG, $XMLDB;
  62  
  63          // Do the job, setting result as needed
  64  
  65          if (!data_submitted()) { // Basic prevention
  66              throw new \moodle_exception('wrongcall', 'error');
  67          }
  68  
  69          // Get parameters
  70          $dirpath = required_param('dir', PARAM_PATH);
  71          $dirpath = $CFG->dirroot . $dirpath;
  72  
  73          $comment = required_param('comment', PARAM_CLEAN);
  74          $comment = $comment;
  75  
  76          // Set comment and recalculate hash
  77          $editeddir = $XMLDB->editeddirs[$dirpath];
  78          $structure = $editeddir->xml_file->getStructure();
  79          $structure->setComment($comment);
  80          $structure->calculateHash(true);
  81  
  82  
  83          // If the hash has changed from the original one, change the version
  84          // and mark the structure as changed
  85          $origdir = $XMLDB->dbdirs[$dirpath];
  86          $origstructure = $origdir->xml_file->getStructure();
  87          if ($structure->getHash() != $origstructure->getHash()) {
  88              $structure->setVersion(userdate(time(), '%Y%m%d', 99, false));
  89              $structure->setChanged(true);
  90          }
  91  
  92          // Launch postaction if exists (leave this here!)
  93          if ($this->getPostAction() && $result) {
  94              return $this->launch($this->getPostAction());
  95          }
  96  
  97          // Return ok if arrived here
  98          return $result;
  99      }
 100  }
 101