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-factories
  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  
  25  /**
  26   * Non instantiable factory class providing different backup object instances
  27   *
  28   * This class contains various methods available in order to easily
  29   * create different parts of the backup architecture in an easy way
  30   *
  31   * TODO: Finish phpdocs
  32   */
  33  abstract class backup_factory {
  34  
  35      public static function get_destination_chain($type, $id, $mode, $execution) {
  36          return null;
  37      }
  38  
  39      public static function get_logger_chain($interactive, $execution, $backupid) {
  40          global $CFG;
  41  
  42          $dfltloglevel = backup::LOG_WARNING; // Default logging level
  43          if ($CFG->debugdeveloper) { // Debug developer raises default logging level
  44              $dfltloglevel = backup::LOG_DEBUG;
  45          }
  46  
  47          $enabledloggers = array(); // Array to store all enabled loggers
  48  
  49          // Create error_log_logger, observing $CFG->backup_error_log_logger_level,
  50          // defaulting to $dfltloglevel
  51          $elllevel = isset($CFG->backup_error_log_logger_level) ? $CFG->backup_error_log_logger_level : $dfltloglevel;
  52          $enabledloggers[] = new error_log_logger($elllevel);
  53  
  54          // Create output_indented_logger, observing $CFG->backup_output_indented_logger_level and $CFG->debugdisplay,
  55          // defaulting to LOG_ERROR. Only if interactive and inmediate
  56          if ($CFG->debugdisplay && $interactive == backup::INTERACTIVE_YES && $execution == backup::EXECUTION_INMEDIATE) {
  57              $oillevel = isset($CFG->backup_output_indented_logger_level) ? $CFG->backup_output_indented_logger_level : backup::LOG_ERROR;
  58              $enabledloggers[] = new output_indented_logger($oillevel, false, false);
  59          }
  60  
  61          // Create file_logger, observing $CFG->backup_file_logger_level
  62          // defaulting to $dfltloglevel
  63          $backuptempdir = make_backup_temp_directory(''); // Need to ensure that $CFG->backuptempdir already exists.
  64          $fllevel = isset($CFG->backup_file_logger_level) ? $CFG->backup_file_logger_level : $dfltloglevel;
  65          $enabledloggers[] = new file_logger($fllevel, true, true, $backuptempdir . '/' . $backupid . '.log');
  66  
  67          // Create database_logger, observing $CFG->backup_database_logger_level and defaulting to LOG_WARNING
  68          // and pointing to the backup_logs table
  69          $dllevel = isset($CFG->backup_database_logger_level) ? $CFG->backup_database_logger_level : $dfltloglevel;
  70          $columns = array('backupid' => $backupid);
  71          $enabledloggers[] = new database_logger($dllevel, 'timecreated', 'loglevel', 'message', 'backup_logs', $columns);
  72  
  73          // Create extra file_logger, observing $CFG->backup_file_logger_extra and $CFG->backup_file_logger_extra_level
  74          // defaulting to $fllevel (normal file logger)
  75          if (isset($CFG->backup_file_logger_extra)) {
  76              $flelevel = isset($CFG->backup_file_logger_extra_level) ? $CFG->backup_file_logger_extra_level : $fllevel;
  77              $enabledloggers[] =  new file_logger($flelevel, true, true, $CFG->backup_file_logger_extra);
  78          }
  79  
  80          // Build the chain
  81          $loggers = null;
  82          foreach ($enabledloggers as $currentlogger) {
  83              if ($loggers == null) {
  84                  $loggers = $currentlogger;
  85              } else {
  86                  $lastlogger->set_next($currentlogger);
  87              }
  88              $lastlogger = $currentlogger;
  89          }
  90  
  91          return $loggers;
  92      }
  93  
  94  
  95      /**
  96       * Given one format and one course module id, return the corresponding
  97       * backup_xxxx_activity_task()
  98       */
  99      public static function get_backup_activity_task($format, $moduleid) {
 100          global $CFG, $DB;
 101  
 102          // Check moduleid exists
 103          if (!$coursemodule = get_coursemodule_from_id(false, $moduleid)) {
 104              throw new backup_task_exception('activity_task_coursemodule_not_found', $moduleid);
 105          }
 106          $classname = 'backup_' . $coursemodule->modname . '_activity_task';
 107          return new $classname($coursemodule->name, $moduleid);
 108      }
 109  
 110      /**
 111       * Given one format, one block id and, optionally, one moduleid, return the corresponding backup_xxx_block_task()
 112       */
 113      public static function get_backup_block_task($format, $blockid, $moduleid = null) {
 114          global $CFG, $DB;
 115  
 116          // Check blockid exists
 117          if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
 118              throw new backup_task_exception('block_task_block_instance_not_found', $blockid);
 119          }
 120  
 121          // Set default block backup task
 122          $classname = 'backup_default_block_task';
 123          $testname  = 'backup_' . $block->blockname . '_block_task';
 124          // If the block has custom backup/restore task class (testname), use it
 125          if (class_exists($testname)) {
 126              $classname = $testname;
 127          }
 128          return new $classname($block->blockname, $blockid, $moduleid);
 129      }
 130  
 131      /**
 132       * Given one format and one section id, return the corresponding backup_section_task()
 133       */
 134      public static function get_backup_section_task($format, $sectionid) {
 135          global $DB;
 136  
 137          // Check section exists
 138          if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
 139              throw new backup_task_exception('section_task_section_not_found', $sectionid);
 140          }
 141  
 142          return new backup_section_task((string)$section->name === '' ? $section->section : $section->name, $sectionid);
 143      }
 144  
 145      /**
 146       * Given one format and one course id, return the corresponding backup_course_task()
 147       */
 148      public static function get_backup_course_task($format, $courseid) {
 149          global $DB;
 150  
 151          // Check course exists
 152          if (!$course = $DB->get_record('course', array('id' => $courseid))) {
 153              throw new backup_task_exception('course_task_course_not_found', $courseid);
 154          }
 155  
 156          return new backup_course_task($course->shortname, $courseid);
 157      }
 158  
 159      /**
 160       * Dispatches the creation of the @backup_plan to the proper format builder
 161       */
 162      static public function build_plan($controller) {
 163          backup_plan_builder::build_plan($controller);
 164      }
 165  }