-
Notifications
You must be signed in to change notification settings - Fork 27
/
addvideo.php
282 lines (247 loc) · 11.9 KB
/
addvideo.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
<?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/>.
/**
* Allows users to upload videos.
*
* @package block_opencast
* @copyright 2017 Andreas Wagner, SYNERGY LEARNING
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_opencast\local\addvideo_form;
use block_opencast\local\apibridge;
use block_opencast\local\file_deletionmanager;
use block_opencast\local\upload_helper;
use core\output\notification;
use tool_opencast\local\settings_api;
require_once('../../config.php');
global $PAGE, $OUTPUT, $CFG, $USER, $SITE, $DB;
require_once($CFG->dirroot . '/repository/lib.php');
$courseid = required_param('courseid', PARAM_INT);
$series = optional_param('intoseries', null, PARAM_ALPHANUMEXT);
if ($series && $courseid != $SITE->id) {
throw new coding_exception('intoseries parameter is not allowed in connection with a non-frontpage course.');
}
$ocinstanceid = optional_param('ocinstanceid', settings_api::get_default_ocinstance()->id, PARAM_INT);
$baseurlparams = [
'ocinstanceid' => $ocinstanceid,
'courseid' => $courseid,
];
if ($series) {
$baseurlparams['intoseries'] = $series;
}
$baseurl = new moodle_url('/blocks/opencast/addvideo.php', $baseurlparams);
$PAGE->set_url($baseurl);
if ($courseid == $SITE->id && $series) {
$redirecturl = new moodle_url('/blocks/opencast/overview_videos.php', ['series' => $series,
'ocinstanceid' => $ocinstanceid, ]);
} else {
$redirecturl = new moodle_url('/blocks/opencast/index.php', ['courseid' => $courseid, 'ocinstanceid' => $ocinstanceid]);
}
require_login($courseid, false);
// Use block context for this page to ignore course file upload limit.
$pagecontext = upload_helper::get_opencast_upload_context($courseid);
$PAGE->set_context($pagecontext);
$PAGE->set_pagelayout('incourse');
$PAGE->set_title(get_string('addvideo', 'block_opencast'));
$PAGE->set_heading(get_string('pluginname', 'block_opencast'));
$PAGE->navbar->add(get_string('pluginname', 'block_opencast'), $redirecturl);
$PAGE->navbar->add(get_string('addvideo', 'block_opencast'), $baseurl);
// Capability check.
if ($courseid == $SITE->id) {
// If upload initiated from overview page, check that capability is given in specific course or ownership.
if (!$series) {
redirect(new moodle_url('/blocks/opencast/overview_videos.php', ['ocinstanceid' => $ocinstanceid, 'series' => null]),
get_string('addvideonotallowed', 'block_opencast'), null,
notification::NOTIFY_ERROR);
}
$records = $DB->get_records('tool_opencast_series', ['series' => $series, 'ocinstanceid' => $ocinstanceid]);
$haspermission = false;
foreach ($records as $record) {
$cc = context_course::instance($record->courseid, IGNORE_MISSING);
if ($cc && has_capability('block/opencast:addvideo', $cc)) {
$haspermission = true;
break;
}
}
if (!$haspermission) {
// Check if series is owned by user.
$apibridge = apibridge::get_instance($ocinstanceid);
$ocseries = $apibridge->get_series_by_identifier($series);
if (!$ocseries) {
redirect(new moodle_url('/blocks/opencast/overview_videos.php', ['ocinstanceid' => $ocinstanceid,
'series' => $series, ]),
get_string('connection_failure', 'block_opencast'), null,
notification::NOTIFY_ERROR);
}
if (!$apibridge->is_owner($ocseries->acl, $USER->id, $SITE->id)) {
redirect(new moodle_url('/blocks/opencast/overview_videos.php', ['ocinstanceid' => $ocinstanceid,
'series' => $series, ]),
get_string('addvideonotallowed', 'block_opencast'), null,
notification::NOTIFY_ERROR);
}
}
} else {
$coursecontext = context_course::instance($courseid);
require_capability('block/opencast:addvideo', $coursecontext);
}
$metadatacatalog = upload_helper::get_opencast_metadata_catalog($ocinstanceid);
$userdefaultsrecord = $DB->get_record('block_opencast_user_default', ['userid' => $USER->id]);
$userdefaults = $userdefaultsrecord ? json_decode($userdefaultsrecord->defaults, true) : [];
$usereventdefaults = (!empty($userdefaults['event'])) ? $userdefaults['event'] : [];
if ($series) {
$addvideoform = new addvideo_form($PAGE->url,
['courseid' => $courseid, 'metadata_catalog' => $metadatacatalog,
'eventdefaults' => $usereventdefaults, 'ocinstanceid' => $ocinstanceid, 'series' => $series, ]
);
} else {
$addvideoform = new addvideo_form($PAGE->url,
['courseid' => $courseid, 'metadata_catalog' => $metadatacatalog,
'eventdefaults' => $usereventdefaults, 'ocinstanceid' => $ocinstanceid, ]
);
}
if ($addvideoform->is_cancelled()) {
redirect($redirecturl);
}
if ($data = $addvideoform->get_data()) {
$chunkuploadinstalled = class_exists('\local_chunkupload\chunkupload_form_element');
// Record the user draft area in this context.
if (!$chunkuploadinstalled || !get_config('block_opencast', 'enablechunkupload_' . $ocinstanceid) ||
property_exists($data, 'presenter_already_uploaded') && $data->presenter_already_uploaded) {
$storedfilepresenter = $addvideoform->save_stored_file('video_presenter', $coursecontext->id,
'block_opencast', upload_helper::OC_FILEAREA, $data->video_presenter);
} else {
$chunkuploadpresenter = $data->video_presenter_chunk;
}
if (!$chunkuploadinstalled || !get_config('block_opencast', 'enablechunkupload_' . $ocinstanceid) ||
property_exists($data, 'presentation_already_uploaded') && $data->presentation_already_uploaded) {
$storedfilepresentation = $addvideoform->save_stored_file('video_presentation', $coursecontext->id,
'block_opencast', upload_helper::OC_FILEAREA, $data->video_presentation);
} else {
$chunkuploadpresentation = $data->video_presentation_chunk;
}
if (isset($storedfilepresenter) && $storedfilepresenter) {
file_deletionmanager::track_draftitemid($coursecontext->id, $storedfilepresenter->get_itemid());
}
if (isset($storedfilepresentation) && $storedfilepresentation) {
file_deletionmanager::track_draftitemid($coursecontext->id, $storedfilepresentation->get_itemid());
}
// Transcription files.
$transcriptions = [];
if (!empty(get_config('block_opencast', 'transcriptionworkflow_' . $ocinstanceid))) {
$maxtranscriptionupload = (int)get_config('block_opencast', 'maxtranscriptionupload_' . $ocinstanceid);
// If the max upload limit is not set we assume only 1 field set.
if (!$maxtranscriptionupload || $maxtranscriptionupload < 0) {
$maxtranscriptionupload = 1;
}
for ($transcriptionindex = 0; $transcriptionindex < $maxtranscriptionupload; $transcriptionindex++) {
$fileelm = "transcription_file_{$transcriptionindex}";
$flavorelm = "transcription_flavor_{$transcriptionindex}";
if (property_exists($data, $fileelm) && property_exists($data, $flavorelm)) {
$storedfile = $addvideoform->save_stored_file($fileelm, $coursecontext->id,
'block_opencast', block_opencast\local\attachment_helper::OC_FILEAREA_ATTACHMENT, $data->{$fileelm});
$flavor = $data->{$flavorelm};
if (isset($storedfile) && $storedfile) {
$transcriptions[] = [
'file_itemid' => $storedfile->get_itemid(),
'file_id' => $storedfile->get_id(),
'file_contenhash' => $storedfile->get_contenthash(),
'flavor' => $flavor,
];
}
}
}
}
$metadata = [];
if (property_exists($data, 'series')) {
$metadata[] = [
'id' => 'isPartOf',
'value' => $data->series,
];
}
$gettitle = true; // Make sure title (required) is added into metadata.
// Adding data into $metadata based on $metadata_catalog.
foreach ($metadatacatalog as $field) {
$id = $field->name;
if (property_exists($data, $field->name) && $data->$id) {
if ($field->name == 'title') { // Make sure the title is received!
$gettitle = false;
}
if ($field->name == 'subjects') {
!is_array($data->$id) ? $data->$id = [$data->$id] : $data->$id = $data->$id;
}
$obj = [
'id' => $id,
'value' => $data->$id,
];
$metadata[] = $obj;
}
}
// If admin forgets/mistakenly deletes the title from metadata_catalog the system will create a title!
if ($gettitle) {
$titleobj = [
'id' => 'title',
'value' => $data->title ? $data->title : 'upload-task',
];
$metadata[] = $titleobj;
}
$sd = new DateTime("now", new DateTimeZone("UTC"));
$sd->setTimestamp($data->startDate);
$startdate = [
'id' => 'startDate',
'value' => $sd->format('Y-m-d'),
];
$starttime = [
'id' => 'startTime',
'value' => $sd->format('H:i:s') . 'Z',
];
$metadata[] = $startdate;
$metadata[] = $starttime;
$options = new stdClass();
$options->metadata = json_encode($metadata);
$options->presenter = isset($storedfilepresenter) && $storedfilepresenter ? $storedfilepresenter->get_itemid() : '';
$options->presentation = isset($storedfilepresentation) && $storedfilepresentation ? $storedfilepresentation->get_itemid() : '';
$options->chunkupload_presenter = isset($chunkuploadpresenter) ? $chunkuploadpresenter : '';
$options->chunkupload_presentation = isset($chunkuploadpresentation) ? $chunkuploadpresentation : '';
// Prepare attachment object.
$attachments = new stdClass();
if (isset($transcriptions) && !empty($transcriptions)) {
$attachments->transcriptions = $transcriptions;
}
// Adding attachment object to the options.
$options->attachments = $attachments;
// Prepare the visibility object.
$visibility = new stdClass();
$visibility->initialvisibilitystatus = !isset($data->initialvisibilitystatus) ?
block_opencast_renderer::VISIBLE : $data->initialvisibilitystatus;
$visibility->initialvisibilitygroups = !empty($data->initialvisibilitygroups) ?
json_encode($data->initialvisibilitygroups) : null;
// Check if the scheduled visibility is set.
if (isset($data->enableschedulingchangevisibility) && $data->enableschedulingchangevisibility) {
$visibility->scheduledvisibilitytime = $data->scheduledvisibilitytime;
$visibility->scheduledvisibilitystatus = $data->scheduledvisibilitystatus;
$visibility->scheduledvisibilitygroups = !empty($data->scheduledvisibilitygroups) ?
json_encode($data->scheduledvisibilitygroups) : null;
}
// Update all upload jobs.
upload_helper::save_upload_jobs($ocinstanceid, $courseid, $options, $visibility);
redirect($redirecturl, get_string('uploadjobssaved', 'block_opencast'), null, notification::NOTIFY_SUCCESS);
}
$PAGE->requires->js_call_amd('block_opencast/block_form_handler', 'init');
$renderer = $PAGE->get_renderer('block_opencast');
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('addvideo', 'block_opencast'));
$addvideoform->display();
echo $OUTPUT->footer();