Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Progress handler that updates a database table with the progress.
  19   *
  20   * @package    core
  21   * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\progress;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Progress handler that updates a database table with the progress.
  31   *
  32   * @package    core
  33   * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class db_updater extends base {
  37  
  38      /**
  39       * The primary key of the database record to update.
  40       *
  41       * @var integer
  42       */
  43      protected $recordid = 0;
  44  
  45      /**
  46       * The database table to insert the progress updates into.
  47       *
  48       * @var string
  49       */
  50      protected $table = '';
  51  
  52      /**
  53       * The table field to update with the progress.
  54       *
  55       * @var string
  56       */
  57      protected $field = '';
  58  
  59      /**
  60       * The maximum frequency in seconds to update the database (default 5 seconds).
  61       * Lower values will increase database calls.
  62       *
  63       * @var integer
  64       */
  65      protected $interval = 5;
  66  
  67  
  68      /**
  69       * The timestamp of when the next progress update to the database will be.
  70       *
  71       * @var integer
  72       */
  73      protected $nextupdate = 0;
  74  
  75      /**
  76       * Constructs the progress reporter.
  77       *
  78       * @param int $recordid The primary key of the database record to update.
  79       * @param string $table The databse table to insert the progress updates into.
  80       * @param string $field The table field to update with the progress.
  81       * @param int $interval The maximum frequency in seconds to update the database (default 5 seconds).
  82       */
  83      public function __construct($recordid, $table, $field, $interval=5) {
  84          $this->recordid = $recordid;
  85          $this->table = $table;
  86          $this->field = $field;
  87          $this->interval = $interval;
  88      }
  89  
  90      /**
  91       * Updates the progress in the database.
  92       * Database update frequency is set by $interval.
  93       *
  94       * @see \core\progress\base::update_progress()
  95       */
  96      public function update_progress() {
  97          global $DB;
  98          $now = $this->get_time();
  99          $lastprogress = $this->lastprogresstime != 0 ? $this->lastprogresstime : $now;
 100  
 101          $progressrecord = new \stdClass();
 102          $progressrecord->id = $this->recordid;
 103          $progressrecord->{$this->field} = '';
 104  
 105          // Update database with progress.
 106          if ($now > $this->nextupdate) {  // Limit database updates based on time.
 107              list ($min, $max) = $this->get_progress_proportion_range();
 108  
 109              $progressrecord->{$this->field} = $min;
 110              $DB->update_record($this->table, $progressrecord);
 111              $this->nextupdate = $lastprogress + $this->interval;
 112          }
 113  
 114          // Set progress to 1 (100%) when there are no more progress updates.
 115          // Their is no guarantee that the final update from the get progress method
 116          // will be 1 even for a successful process. So we explicitly set the final DB
 117          // value to 1 when we are no longer in progress.
 118          if (!$this->is_in_progress_section()) {
 119              $progressrecord->{$this->field} = 1;
 120              $DB->update_record($this->table, $progressrecord);
 121          }
 122      }
 123  }