Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Defines backup_plugin class
  20   *
  21   * @package     core_backup
  22   * @subpackage  moodle2
  23   * @category    backup
  24   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class implementing the plugins support for moodle2 backups
  32   *
  33   * TODO: Finish phpdocs
  34   */
  35  abstract class backup_plugin {
  36  
  37      /** @var string */
  38      protected $plugintype;
  39      /** @var string */
  40      protected $pluginname;
  41      /** @var string */
  42      protected $connectionpoint;
  43      /** @var backup_optigroup_element */
  44      protected $optigroup; // Optigroup, parent of all optigroup elements
  45      /** @var backup_structure_step */
  46      protected $step;
  47      /** @var backup_course_task|backup_activity_task */
  48      protected $task;
  49  
  50      /**
  51       * backup_plugin constructor.
  52       *
  53       * @param string $plugintype
  54       * @param string $pluginname
  55       * @param backup_optigroup_element $optigroup
  56       * @param backup_structure_step $step
  57       */
  58      public function __construct($plugintype, $pluginname, $optigroup, $step) {
  59          $this->plugintype = $plugintype;
  60          $this->pluginname = $pluginname;
  61          $this->optigroup  = $optigroup;
  62          $this->connectionpoint = '';
  63          $this->step       = $step;
  64          $this->task       = $step->get_task();
  65      }
  66  
  67      public function define_plugin_structure($connectionpoint) {
  68  
  69          $this->connectionpoint = $connectionpoint;
  70  
  71          $methodname = 'define_' . $connectionpoint . '_plugin_structure';
  72  
  73          if (method_exists($this, $methodname)) {
  74              $this->$methodname();
  75          }
  76      }
  77  
  78  // Protected API starts here
  79  
  80  // backup_step/structure_step/task wrappers
  81  
  82      /**
  83       * Returns the value of one (task/plan) setting
  84       */
  85      protected function get_setting_value($name) {
  86          if (is_null($this->task)) {
  87              throw new backup_step_exception('not_specified_backup_task');
  88          }
  89          return $this->task->get_setting_value($name);
  90      }
  91  
  92  // end of backup_step/structure_step/task wrappers
  93  
  94      /**
  95       * Factory method that will return one backup_plugin_element (backup_optigroup_element)
  96       * with its name automatically calculated, based one the plugin being handled (type, name)
  97       */
  98      protected function get_plugin_element($final_elements = null, $conditionparam = null, $conditionvalue = null) {
  99          // Something exclusive for this backup_plugin_element (backup_optigroup_element)
 100          // because it hasn't XML representation
 101          $name = 'optigroup_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
 102          $optigroup_element = new backup_plugin_element($name, $final_elements, $conditionparam, $conditionvalue);
 103          $this->optigroup->add_child($optigroup_element);  // Add optigroup_element to stay connected since beginning
 104          return $optigroup_element;
 105      }
 106  
 107      /**
 108       * Simple helper function that suggests one name for the main nested element in plugins
 109       * It's not mandatory to use it but recommended ;-)
 110       */
 111      protected function get_recommended_name() {
 112          return 'plugin_' . $this->plugintype . '_' . $this->pluginname . '_' . $this->connectionpoint;
 113      }
 114  
 115  }