Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * This script allows to import a course from CLI.
19
 *
20
 * @package    core
21
 * @subpackage cli
22
 * @author     Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
23
 * @copyright  2023 Catalyst IT
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
define('CLI_SCRIPT', 1);
28
 
29
require(__DIR__ . '/../../config.php');
30
require_once($CFG->libdir . '/clilib.php');
31
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32
require_once($CFG->dirroot . "/backup/util/includes/restore_includes.php");
33
 
34
list($options, $unrecognized) = cli_get_params([
35
    'srccourseid' => '',
36
    'srccmid' => '',
37
    'dstcourseid' => '',
38
    'showdebugging' => false,
39
    'help' => false,
40
], [
41
    's' => 'showdebugging',
42
    'h' => 'help',
43
]);
44
 
45
if ($unrecognized) {
46
    $unrecognized = implode("\n  ", $unrecognized);
47
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
48
}
49
 
50
if ($options['help'] ||
51
    !(($options['srccourseid'] && $options['dstcourseid'])
52
        || ($options['srccmid'] && $options['dstcourseid']))) {
53
    $help = <<<EOL
54
Import course or course module into provided course.
55
 
56
Options:
57
    --srccourseid=INT   Source course ID to backup.
58
    --srccmid=INT       Source course module ID to backup.
59
    --dstcourseid=INT   Destination course ID to restore.
60
-s, --showdebugging     Show developer level debugging information
61
-h, --help              Print out this help.
62
 
63
Example1: (Import from a course into another course)
64
\$sudo -u www-data /usr/bin/php admin/cli/import.php --srccourseid=12 --dstcourseid=13\n
65
 
66
Example2: (Import from a course module into a course)
67
\$sudo -u www-data /usr/bin/php admin/cli/import.php --srccmid=21 --dstcourseid=11\n
68
EOL;
69
 
70
    echo $help;
71
    exit(0);
72
}
73
 
74
if ($options['showdebugging']) {
75
    set_debugging(DEBUG_DEVELOPER, true);
76
}
77
 
78
if (!$admin = get_admin()) {
79
    throw new \moodle_exception('noadmins');
80
}
81
 
82
if (!empty($options['srccourseid']) && !empty($options['dstcourseid'])) {
83
    // Import from course into course.
84
    if (!$srccourse = $DB->get_record('course', ['id' => $options['srccourseid']], 'id')) {
85
        throw new \moodle_exception('invalidcourseid');
86
    }
87
    if (!$dstcourse = $DB->get_record('course', ['id' => $options['dstcourseid']], 'id')) {
88
        throw new \moodle_exception('invalidcourseid');
89
    }
90
 
91
    cli_heading(get_string('importfromcoursetocourse', 'backup', (object)$options));
92
    $bc = new backup_controller(backup::TYPE_1COURSE, $srccourse->id, backup::FORMAT_MOODLE,
93
        backup::INTERACTIVE_NO, backup::MODE_IMPORT, $admin->id);
94
    $backupid = $bc->get_backupid();
95
    $bc->execute_plan();
96
    $bc->destroy();
97
    cli_heading(get_string('backupthenrestore', 'backup'));
98
 
99
    $rc = new restore_controller($backupid, $dstcourse->id, backup::INTERACTIVE_NO,
100
        backup::MODE_SAMESITE, $admin->id, backup::TARGET_EXISTING_ADDING);
101
    $rc->execute_precheck();
102
    $rc->execute_plan();
103
    $rc->destroy();
104
    cli_heading(get_string('restoredcourseid', 'backup', $dstcourse->id));
105
} else if (!empty($options['srccmid']) && !empty($options['dstcourseid'])) {
106
    // Import from course module into course.
107
    if (!$cm = $DB->get_record('course_modules', ['id' => $options['srccmid']], 'id')) {
108
        throw new \moodle_exception('invalidcoursemoduleid', 'error', '', $options['srccmid']);
109
    }
110
    if (!$course = $DB->get_record('course', ['id' => $options['dstcourseid']], 'id')) {
111
        throw new \moodle_exception('invalidcourseid');
112
    }
113
 
114
    cli_heading(get_string('importfromccmidtocourse', 'backup', (object)$options));
115
    $bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
116
        backup::INTERACTIVE_NO, backup::MODE_IMPORT, $admin->id);
117
    $backupid = $bc->get_backupid();
118
    $bc->execute_plan();
119
    $bc->destroy();
120
    cli_heading(get_string('backupthenrestore', 'backup'));
121
 
122
    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO,
123
        backup::MODE_SAMESITE, $admin->id, backup::TARGET_EXISTING_ADDING);
124
    $rc->execute_precheck();
125
    $rc->execute_plan();
126
    $rc->destroy();
127
    cli_heading(get_string('restoredcourseid', 'backup', $course->id));
128
} else {
129
    throw new \moodle_exception('invalidoption');
130
}
131
 
132
exit(0);