-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathduplicate_course.php
More file actions
127 lines (117 loc) · 5.02 KB
/
duplicate_course.php
File metadata and controls
127 lines (117 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Handle duplicated courses
*
* @package local_lsf_unification
* @copyright 2025 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
require_once("$CFG->libdir/adminlib.php");
require_once("$CFG->dirroot/backup/util/includes/restore_includes.php");
require_once("./lib_features.php");
require_login();
$PAGE->set_url('/local/lsf_unification/duplicate_course.php');
// Where we came from. Used in a number of redirects.
$returnurl = $CFG->wwwroot . '/course/index.php';
// Check permissions.
require_login();
if (isguestuser()) {
throw new moodle_exception('guestsarenotallowed', '', $returnurl);
}
$context = context_system::instance();
$PAGE->set_context($context);
require_capability('moodle/course:request', $context);
establish_secondary_DB_connection();
$strtitle = get_string('courserequest');
$PAGE->set_title($strtitle);
$PAGE->set_heading($strtitle);
$PAGE->navbar->add($strtitle);
echo $OUTPUT->header();
$courseid = required_param('courseid', PARAM_INT);
$acceptorid = get_course_acceptor($courseid);
$filesbackups = get_backup_files($acceptorid);
$filestemplates = get_template_files();
$filetype = optional_param('filetype', null, PARAM_RAW);
$fileid = optional_param('fileid', null, PARAM_RAW);
$fileinfo = null;
if (!in_array($courseid, get_my_courses_as_teacher()) && !is_course_imported_by($courseid, $USER)) {
die("context not found");
}
$course = $DB->get_record('course', ["id" => $courseid]);
if (time() - $course->timecreated > 60 * 60 * get_config('local_lsf_unification', 'duplication_timeframe')) {
$duplicationconfig = get_config('local_lsf_unification', 'duplication_timeframe');
$duplicationstr = get_string('duplication_timeframe_error', 'local_lsf_unification', $duplicationconfig);
echo "<b>" . $duplicationstr . "</b><br>";
} else {
if (!empty($fileid)) {
// Get rights.
$creatorroleid = $DB->get_record('role', ['shortname' => 'lsfunificationcourseimporter'])->id;
$context = context_course::instance($courseid, MUST_EXIST);
if (!enrol_try_internal_enrol($course->id, $USER->id, $creatorroleid)) {
die("error ##");
}
// Restore backup.
if ($filetype == "t" && get_config('local_lsf_unification', 'restore_templates')) {
if (empty($filestemplates[$fileid])) {
die("error #0");
}
$fileinfo = $filestemplates[$fileid];
} else if ($filetype == "b" && get_config('local_lsf_unification', 'restore_old_courses')) {
if (empty($filesbackups[$fileid])) {
die("error #0");
}
$fileinfo = $filesbackups[$fileid];
} else {
die("error #x");
}
$tmpdir = $CFG->tempdir . '/backup';
$foldername = restore_controller::get_tempdir_name(empty($fileinfo->course) ? 42 : $fileinfo->course->id, $USER->id);
$pathname = $tmpdir . '/' . $foldername;
if (is_dir($pathname)) {
die("error #1");
}
if (!mkdir($pathname, 0777, true)) {
die("error #2: " . $pathname);
}
if (!empty($fileinfo->category)) {
if (!mkdir($pathname . "/" . $fileinfo->category, 0777, true)) {
die("error #2: " . $pathname);
}
}
if (!copy($fileinfo->path . "/" . $fileinfo->name, $pathname . "/" . $fileinfo->name)) {
die("error #3");
}
if (!lsf_unification_unzip($pathname . "/" . $fileinfo->name, $pathname)) {
die("error #4");
}
restore_dbops::delete_course_content($courseid, ["keep_roles_and_enrolments" => true]);
// Log is deleted by restore_dbops::delete_course_content.
$event = \local_lsf_unification\event\course_duplicated::create([
'objectid' => $courseid,
'context' => context_system::instance(0, IGNORE_MISSING),
'other' => empty($fileinfo->course) ? ('template_' . $fileinfo->name) : $fileinfo->course->id,
]);
$event->trigger();
duplicate_course($courseid, $foldername);
// Dump rights.
role_unassign($creatorroleid, $USER->id, $context->id);
}
}
echo "<a href='request.php?courseid=" . $courseid . "&answer=7'>" . get_string('continue') . "</a><br>";
echo $OUTPUT->footer();
close_secondary_DB_connection();