MDL-69521 core: Move all comments in code from 4.1 to 3.11
[moodle.git] / enrol / database / cli / sync.php
CommitLineData
df997f84 1<?php
df997f84
PS
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 *
28bd3d9a
PS
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 *
d6f7480d 29 * @deprecated since Moodle 3.7 MDL-59986 - please do not use this CLI script any more, use scheduled task instead.
74ee34fd 30 * @todo MDL-63266 This will be deleted in Moodle 3.11.
f34518d9 31 * @package enrol_database
9232bceb
PS
32 * @copyright 2010 Petr Skoda {@link http://skodak.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
34 */
35
28bd3d9a 36define('CLI_SCRIPT', true);
df997f84 37
411c3f98
PS
38require(__DIR__.'/../../../config.php');
39require_once("$CFG->libdir/clilib.php");
df997f84 40
f34518d9 41// Now get cli options.
6650f3de
PS
42list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
43
44if ($unrecognized) {
45 $unrecognized = implode("\n ", $unrecognized);
46 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
df997f84
PS
47}
48
6650f3de
PS
49if ($options['help']) {
50 $help =
51"Execute enrol sync with external database.
52The enrol_database plugin must be enabled and properly configured.
53
54Options:
411c3f98 55-v, --verbose Print verbose progress information
6650f3de 56-h, --help Print out this help
df997f84 57
6650f3de 58Example:
411c3f98 59\$ sudo -u www-data /usr/bin/php enrol/database/cli/sync.php
6650f3de
PS
60
61Sample cron entry:
62# 5 minutes past 4am
411c3f98 635 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/enrol/database/cli/sync.php
6650f3de
PS
64";
65
66 echo $help;
67 die;
68}
69
d6f7480d
DNA
70cli_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');
74if (!$task->get_disabled()) {
75 cli_error('[ENROL DATABASE] The scheduled task sync_enrolments is enabled, the cron execution has been aborted.');
76}
77
6650f3de 78if (!enrol_is_enabled('database')) {
411c3f98 79 cli_error('enrol_database plugin is disabled, synchronisation stopped', 2);
6650f3de
PS
80}
81
411c3f98
PS
82if (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 */
df997f84 89$enrol = enrol_get_plugin('database');
6650f3de
PS
90$result = 0;
91
411c3f98
PS
92$result = $result | $enrol->sync_courses($trace);
93$result = $result | $enrol->sync_enrolments($trace);
6650f3de 94
411c3f98 95exit($result);