[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/util/plan/ -> restore_step.class.php (source)

   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-plan
  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   * Abstract class defining the needed stuf for one restore step
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  abstract class restore_step extends base_step {
  31  
  32      /**
  33       * Constructor - instantiates one object of this class
  34       */
  35      public function __construct($name, $task = null) {
  36          if (!is_null($task) && !($task instanceof restore_task)) {
  37              throw new restore_step_exception('wrong_restore_task_specified');
  38          }
  39          parent::__construct($name, $task);
  40      }
  41  
  42      protected function get_restoreid() {
  43          if (is_null($this->task)) {
  44              throw new restore_step_exception('not_specified_restore_task');
  45          }
  46          return $this->task->get_restoreid();
  47      }
  48  
  49      /**
  50       * Apply course startdate offset based in original course startdate and course_offset_startdate setting
  51       * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
  52       * executions in the same request
  53       *
  54       * @param int $value Time value (seconds since epoch), or empty for nothing
  55       * @return int Time value after applying the date offset, or empty for nothing
  56       */
  57      public function apply_date_offset($value) {
  58  
  59          // Empties don't offset - zeros (int and string), false and nulls return original value.
  60          if (empty($value)) {
  61              return $value;
  62          }
  63  
  64          static $cache = array();
  65          // Lookup cache.
  66          if (isset($cache[$this->get_restoreid()])) {
  67              return $value + $cache[$this->get_restoreid()];
  68          }
  69          // No cache, let's calculate the offset.
  70          $original = $this->task->get_info()->original_course_startdate;
  71          $setting = 0;
  72          if ($this->setting_exists('course_startdate')) { // Seting may not exist (MDL-25019).
  73              $setting  = $this->get_setting_value('course_startdate');
  74          }
  75  
  76          if (empty($original) || empty($setting)) {
  77              // Original course has not startdate or setting doesn't exist, offset = 0.
  78              $cache[$this->get_restoreid()] = 0;
  79  
  80          } else if (abs($setting - $original) < 24 * 60 * 60) {
  81              // Less than 24h of difference, offset = 0 (this avoids some problems with timezones).
  82              $cache[$this->get_restoreid()] = 0;
  83  
  84          } else if (!has_capability('moodle/restore:rolldates',
  85                 context_course::instance($this->get_courseid()), $this->task->get_userid())) {
  86              // Re-enforce 'moodle/restore:rolldates' capability for the user in the course, just in case.
  87              $cache[$this->get_restoreid()] = 0;
  88  
  89          } else {
  90              // Arrived here, let's calculate the real offset.
  91              $cache[$this->get_restoreid()] = $setting - $original;
  92          }
  93  
  94          // Return the passed value with cached offset applied.
  95          return $value + $cache[$this->get_restoreid()];
  96      }
  97  }
  98  
  99  /*
 100   * Exception class used by all the @restore_step stuff
 101   */
 102  class restore_step_exception extends base_step_exception {
 103  
 104      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 105          parent::__construct($errorcode, $a, $debuginfo);
 106      }
 107  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1