forked from Opencast-Moodle/moodle-block_opencast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importvideos.php
366 lines (309 loc) · 15 KB
/
importvideos.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
/**
* Page to manually import videos from other Moodle courses.
*
* @package block_opencast
* @copyright 2020 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_opencast\local\activitymodulemanager;
require_once('../../config.php');
global $PAGE, $OUTPUT, $CFG, $DB;
// Handle submitted parameters of the form.
// This course id of the target course.
$courseid = required_param('courseid', PARAM_INT);
$ocinstanceid = optional_param('ocinstanceid', \tool_opencast\local\settings_api::get_default_ocinstance()->id, PARAM_INT);
// The current step of the wizard.
$step = optional_param('step', 1, PARAM_INT);
// The course id of the course where the videos are imported from (this is submitted by the course search component in step 1 only).
$importid = optional_param('importid', null, PARAM_INT);
// The course id of the course where the videos are imported from (with this variable we carry the id though the wizard).
$sourcecourseid = optional_param('sourcecourseid', null, PARAM_INT);
$sourcecourseseries = optional_param('sourcecourseseries', null, PARAM_ALPHANUMEXT);
// The list of course videos to import.
$coursevideos = optional_param_array('coursevideos', array(), PARAM_ALPHANUMEXT);
// The fact if we have to handle series and / or episode modules after the import.
$fixseriesmodules = optional_param('fixseriesmodules', false, PARAM_BOOL);
$fixepisodemodules = optional_param('fixepisodemodules', false, PARAM_BOOL);
// Set base URL.
$baseurl = new moodle_url('/blocks/opencast/importvideos.php', array('courseid' => $courseid, 'ocinstanceid' => $ocinstanceid));
$PAGE->set_url($baseurl);
// Remember URLs for redirecting.
$redirecturloverview = new moodle_url('/blocks/opencast/index.php',
array('courseid' => $courseid, 'ocinstanceid' => $ocinstanceid));
$redirecturlcancel = $redirecturloverview;
// Require login and course membership.
require_login($courseid, false);
// Set page and navbar properties.
$PAGE->set_pagelayout('incourse');
$PAGE->set_title(get_string('pluginname', 'block_opencast'));
$PAGE->set_heading(get_string('pluginname', 'block_opencast'));
$PAGE->navbar->add(get_string('pluginname', 'block_opencast'), $redirecturloverview);
$PAGE->navbar->add(get_string('importvideos_importbuttontitle', 'block_opencast'), $baseurl);
// Check if the manual import videos feature is enabled and working.
if (\block_opencast\local\importvideosmanager::is_enabled_and_working_for_manualimport($ocinstanceid) == false) {
throw new moodle_exception('importvideos_errornotenabledorworking', 'block_opencast', $redirecturloverview);
}
// Capability check.
$coursecontext = context_course::instance($courseid);
require_capability('block/opencast:manualimporttarget', $coursecontext);
// Check if either the handle series feature or the handle episodes feature is enabled _and_ the user is allowed to use the feature,
// we have to include step 3.
if ((\block_opencast\local\importvideosmanager::handle_series_modules_is_enabled_and_working($ocinstanceid) == true &&
has_capability('block/opencast:addlti', $coursecontext)) ||
(\block_opencast\local\importvideosmanager::handle_episode_modules_is_enabled_and_working($ocinstanceid) == true &&
has_capability('block/opencast:addltiepisode', $coursecontext))) {
$hasstep3 = true;
} else {
$hasstep3 = false;
}
// Defining totalsteps, in order to use it for different import modes.
$totalsteps = 4;
// Get import mode from the admin setting.
$importmode = get_config('block_opencast', 'importmode_' . $ocinstanceid);
// In case of ACL change import mode.
if ($importmode == 'acl') {
// Total steps in ACL Change mode is 2, where only a course should be selected and a summary should be displayed.
$totalsteps = 3;
// We need a normal 2 Steps progress bar.
$hasstep3 = true;
// Check if the maximum number of series is already reached.
$courseseries = $DB->get_records('tool_opencast_series', array('ocinstanceid' => $ocinstanceid, 'courseid' => $courseid));
if (count($courseseries) >= get_config('block_opencast', 'maxseries_' . $ocinstanceid)) {
throw new moodle_exception('maxseriesreached', 'block_opencast');
}
}
// Get renderer.
$renderer = $PAGE->get_renderer('block_opencast', 'importvideos');
$importvideosform = null;
// Deal with wizard step forms individually.
$jumpto4 = false;
switch ($step) {
default:
case 1:
$nextstep = false;
// While we use custom mforms in step 2 to 3, we rely on a Moodle core component in step 1.
// That's why step 1 is structured differently than the following steps.
// If there isn't any other course which can be used as import source.
$possiblesourcecourses = get_user_capability_course(
'block/opencast:manualimportsource');
$possiblesourcecoursescount = count($possiblesourcecourses);
if ($possiblesourcecoursescount < 1 || ($possiblesourcecoursescount == 1 && $possiblesourcecourses[0]->id == $courseid)) {
// Use step 1 form.
$importvideosform = new \block_opencast\local\importvideos_step1_form(null,
array('courseid' => $courseid));
// Redirect if the form was cancelled.
if ($importvideosform->is_cancelled()) {
redirect($redirecturlcancel);
}
$heading = get_string('importvideos_wizardstep' . $step . 'heading', 'block_opencast');
} else {
// If a course was not selected yet with the course search component.
if ($importid == null) {
// Prepare next step URL.
$url = new moodle_url('/blocks/opencast/importvideos.php',
array('courseid' => $courseid, 'ocinstanceid' => $ocinstanceid));
// Prepare course search component.
$search = new \block_opencast\local\importvideos_coursesearch(array('url' => $url), $courseid);
$heading = get_string('importvideos_wizardstep' . $step . 'heading', 'block_opencast');
// Output course search component.
$body = $renderer->importvideos_coursesearch($url, $search);
}
// If a course was selected with the course search component.
if ($importid != null) {
$sourcecourseid = $importid;
$nextstep = true;
}
}
if (!$nextstep) {
break;
}
case 2:
$nextstep = false;
$step = 2;
$aclheadingstringname = '';
// When the Duplicating Events is selected.
if ($importmode == 'duplication') {
// Set form for next step.
$importvideosform = new \block_opencast\local\importvideos_step2_form(null,
array(
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
'sourcecourseid' => $sourcecourseid));
} else if ($importmode == 'acl') {
$aclheadingstringname = 'acl';
$courseseries = \block_opencast\local\importvideosmanager::get_import_source_course_series($ocinstanceid,
$sourcecourseid);
if (count($courseseries) > 1) {
$importvideosform = new \block_opencast\local\importvideos_select_series_form(null, array(
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
'sourcecourseid' => $sourcecourseid,
'series' => $courseseries));
} else {
$nextstep = true;
}
}
if (!$nextstep) {
// Redirect if the form was cancelled.
if ($importvideosform->is_cancelled()) {
redirect($redirecturlcancel);
}
// Process data.
if ($data = $importvideosform->get_data()) {
if ($importmode == 'duplication') {
// If we are in Duplicating Events mode.
// Process the array of course videos.
foreach ($coursevideos as $identifier => $checked) {
// Check if the video was not selected (which may happen as we are using an advcheckbox element).
if ($checked != 1) {
// Remove the video from the array of coursevideos.
unset($coursevideos[$identifier]);
}
}
// If we have to include step 3 now.
if ($hasstep3 == true) {
$nextstep = true;
} else {
$nextstep = true;
$jumpto4 = true;
}
} else {
$sourcecourseseries = $data->series;
$nextstep = true;
}
}
// Output heading.
$heading = get_string('importvideos_wizardstep' . $step . $aclheadingstringname . 'heading', 'block_opencast');
}
if (!$nextstep) {
break;
}
case 3:
$nextstep = false;
$step = 3;
if (!$jumpto4) {
if ($importmode == 'acl') {
$importvideosform = new \block_opencast\local\importvideos_step3_form_acl(null,
array(
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
'sourcecourseid' => $sourcecourseid,
'series' => $sourcecourseseries));
} else {
$importvideosform = new \block_opencast\local\importvideos_step3_form(null,
array(
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
'sourcecourseid' => $sourcecourseid,
'coursevideos' => $coursevideos));
}
// Redirect if the form was cancelled.
if ($importvideosform->is_cancelled()) {
redirect($redirecturlcancel);
}
// Process data.
if ($data = $importvideosform->get_data()) {
if ($importmode == 'acl') {
// Perform ACL change.
$resultaclchange = \block_opencast\local\importvideosmanager::change_acl($ocinstanceid, $sourcecourseid,
$data->sourcecourseseries, $courseid);
// Redirec the user with corresponding messages.
redirect($redirecturloverview, $resultaclchange->message, null, $resultaclchange->type);
} else {
$nextstep = true;
}
}
// Output heading.
$heading = get_string('importvideos_wizardstep' . $step . 'heading', 'block_opencast');
if (!$nextstep) {
break;
}
}
case 4:
$step = 4;
// Use step 4 form.
$importvideosform = new \block_opencast\local\importvideos_step4_form(null,
array(
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
'sourcecourseid' => $sourcecourseid,
'coursevideos' => $coursevideos,
'fixseriesmodules' => $fixseriesmodules,
'fixepisodemodules' => $fixepisodemodules));
// Redirect if the form was cancelled.
if ($importvideosform->is_cancelled()) {
redirect($redirecturlcancel);
}
// Process data.
if ($data = $importvideosform->get_data()) {
$resultduplicate = \block_opencast\local\importvideosmanager::duplicate_videos($ocinstanceid,
$sourcecourseid, $courseid, $coursevideos, $fixepisodemodules);
// If duplication did not complete correctly.
if ($resultduplicate->error) {
// Redirect to Opencast videos overview page without cleaning up any modules.
redirect($redirecturloverview,
get_string('importvideos_importjobcreationfailed', 'block_opencast'),
null,
\core\output\notification::NOTIFY_ERROR);
}
// If cleanup of the series modules was requested and the user is allowed to do this.
if ($fixseriesmodules == true) {
$resulthandleseries = true;
if (\block_opencast\local\ltimodulemanager::is_working_for_series($ocinstanceid) &&
has_capability('block/opencast:addlti', $coursecontext)) {
// Clean up the series modules.
$resulthandleseries = \block_opencast\local\ltimodulemanager::cleanup_series_modules($ocinstanceid,
$courseid, $sourcecourseid, $resultduplicate->duplicatedseries);
}
if (\core_plugin_manager::instance()->get_plugin_info('mod_opencast') != null) {
$resulthandleseries = $resulthandleseries &&
activitymodulemanager::cleanup_series_modules($ocinstanceid, $courseid, $resultduplicate->duplicatedseries);
}
// If clean up did not completed correctly.
if ($resulthandleseries != true) {
// Redirect to Opencast videos overview page with an error notification.
redirect($redirecturloverview,
get_string('importvideos_importseriescleanupfailed', 'block_opencast'),
null,
\core\output\notification::NOTIFY_ERROR);
}
}
// Everything seems to be fine, redirect to Opencast videos overview page.
redirect($redirecturloverview,
get_string('importvideos_importjobcreated', 'block_opencast'),
null,
\core\output\notification::NOTIFY_SUCCESS);
}
$heading = get_string('importvideos_wizardstep' . $step . 'heading', 'block_opencast');
break;
}
// Output the page header.
echo $OUTPUT->header();
// Output the progress bar.
echo $renderer->progress_bar($step, $totalsteps, $hasstep3);
// Output heading.
echo $OUTPUT->heading($heading);
// Output the form.
if ($importvideosform) {
$importvideosform->display();
} else {
// Only used in step 1.
echo $body;
}
// Output the page footer.
echo $OUTPUT->footer();