Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * CLI sync for full external database synchronisation.
  19   *
  20   * Sample cron entry:
  21   * # 5 minutes past 4am
  22   * 5 4 * * * $sudo -u www-data /usr/bin/php /var/www/moodle/enrol/database/cli/sync.php
  23   *
  24   * Notes:
  25   *   - it is required to use the web server account when executing PHP CLI scripts
  26   *   - you need to change the "www-data" to match the apache user account
  27   *   - use "su" if "sudo" not available
  28   *
  29   * @deprecated since Moodle 3.7 MDL-59986 - please do not use this CLI script any more, use scheduled task instead.
  30   * @todo       MDL-63266 This will be deleted in Moodle 3.11.
  31   * @package    enrol_database
  32   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  
  36  define('CLI_SCRIPT', true);
  37  
  38  require(__DIR__.'/../../../config.php');
  39  require_once("$CFG->libdir/clilib.php");
  40  
  41  // Now get cli options.
  42  list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
  43  
  44  if ($unrecognized) {
  45      $unrecognized = implode("\n  ", $unrecognized);
  46      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  47  }
  48  
  49  if ($options['help']) {
  50      $help =
  51  "Execute enrol sync with external database.
  52  The enrol_database plugin must be enabled and properly configured.
  53  
  54  Options:
  55  -v, --verbose         Print verbose progress information
  56  -h, --help            Print out this help
  57  
  58  Example:
  59  \$ sudo -u www-data /usr/bin/php enrol/database/cli/sync.php
  60  
  61  Sample cron entry:
  62  # 5 minutes past 4am
  63  5 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/enrol/database/cli/sync.php
  64  ";
  65  
  66      echo $help;
  67      die;
  68  }
  69  
  70  cli_problem('[ENROL DATABASE] The sync enrolments cron script has been deprecated. Please use the scheduled task instead.');
  71  
  72  // Abort execution of the CLI script if the enrol_database\task\sync_enrolments is enabled.
  73  $task = \core\task\manager::get_scheduled_task('enrol_database\task\sync_enrolments');
  74  if (!$task->get_disabled()) {
  75      cli_error('[ENROL DATABASE] The scheduled task sync_enrolments is enabled, the cron execution has been aborted.');
  76  }
  77  
  78  if (!enrol_is_enabled('database')) {
  79      cli_error('enrol_database plugin is disabled, synchronisation stopped', 2);
  80  }
  81  
  82  if (empty($options['verbose'])) {
  83      $trace = new null_progress_trace();
  84  } else {
  85      $trace = new text_progress_trace();
  86  }
  87  
  88  /** @var enrol_database_plugin $enrol  */
  89  $enrol = enrol_get_plugin('database');
  90  $result = 0;
  91  
  92  $result = $result | $enrol->sync_courses($trace);
  93  $result = $result | $enrol->sync_enrolments($trace);
  94  
  95  exit($result);