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  
   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   * @package    moodlecore
  20   * @subpackage backup-structure
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   *
  24   * TODO: Finish phpdocs
  25   */
  26  
  27  /**
  28   * Instantiable class representing one optigroup element for conditional branching
  29   *
  30   * Objects of this class are internally nested elements, so they support having both
  31   * final elements and children (more nested elements) and are able to have one source
  32   * and all the stuff supported by nested elements. Their main differences are:
  33   *
  34   * - Support for conditional execution, using simple equality checks with outer values.
  35   * - Don't have representation in the hierarchy, so:
  36   *     - Their level is the level of the parent of their enclosing optigroup.
  37   *     - Act as one "path bridge" when looking for parent path values
  38   *     - They don't support attributes
  39   *
  40   * Their main use is to allow conditional branching, basically for optional submodules
  41   * like question types, assignment subtypes... where different subtrees of information
  42   * must be exported. It's correct to assume that each submodule will define its own
  43   * optigroup_element for backup purposes.
  44   */
  45  class backup_optigroup extends base_optigroup implements processable {
  46  
  47      public function add_child($element) {
  48          if (!($element instanceof backup_optigroup_element)) { // parameter must be backup_optigroup_element
  49              if (is_object($element)) {
  50                  $found = get_class($element);
  51              } else {
  52                  $found = 'non object';
  53              }
  54              throw new base_optigroup_exception('optigroup_element_incorrect', $found);
  55          }
  56          parent::add_child($element);
  57      }
  58  
  59      public function process($processor) {
  60          if (!$processor instanceof base_processor) { // No correct processor, throw exception
  61              throw new base_element_struct_exception('incorrect_processor');
  62          }
  63          // Iterate over all the children backup_optigroup_elements, delegating the process
  64          // an knowing it only handles final elements, so we'll delegate process of nested
  65          // elements below. Tricky but we need to priorize finals BEFORE nested always.
  66          foreach ($this->get_children() as $child) {
  67              if ($child->condition_matches()) { // Only if the optigroup_element condition matches
  68                  $child->process($processor);
  69                  if (!$this->is_multiple()) {
  70                      break; // one match found and this optigroup is not multiple => break loop
  71                  }
  72              }
  73          }
  74          // Now iterate again, but looking for nested elements what will go AFTER all the finals
  75          // that have been processed above
  76          foreach ($this->get_children() as $child) {
  77              if ($child->condition_matches()) { // Only if the optigroup_element condition matches
  78                  foreach ($child->get_children() as $nested_element) {
  79                       $nested_element->process($processor);
  80                  }
  81                  if (!$this->is_multiple()) {
  82                      break; // one match found and this optigroup is not multiple => break loop
  83                  }
  84              }
  85          }
  86      }
  87  }