forked from moodleou/moodle-mod_openstudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_form.php
504 lines (449 loc) · 27.1 KB
/
content_form.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?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/>.
/**
* @package mod_openstudio
* @copyright The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Make sure this isn't being directly accessed.
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/formslib.php');
use mod_openstudio\local\api\content;
use mod_openstudio\local\api\group;
/**
* Studio content edit form.
*
* @package mod_openstudio
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_openstudio_content_form extends moodleform {
const ACCEPT_TYPE = array(
'.jpg', '.jpe', '.jpeg', '.gif', '.png', // Image.
'.avi', '.m4v', '.mpg', '.mpeg', '.mov', '.mp4', '.mv4', '.flv', '.gif', // Video.
'.aiff', '.wav', '.mp3', '.m4a', // Audio.
'.doc', '.docx', '.rtf', '.pdf', '.odt', '.fdf', '.nbk', '.txt', // Document.
'.ppt', '.pptx', '.odp', // Presentation.
'.xls', '.xlsx', '.csv', '.ods', // Spreadsheet.
'.dwg', '.stl', '.stp', '.eps', '.dxf', // CAD.
'.zip', '.ipynb');
protected function definition() {
global $CFG, $USER, $DB, $OUTPUT;
$mform = $this->_form;
$contenttitle = get_string('contentformname', 'openstudio');
$contentdescription = get_string('contentformdescription', 'openstudio');
$submitbuttontitle = get_string('contentformsubmitbutton', 'openstudio');
if (isset($this->_customdata['isfolderlock']) && $this->_customdata['isfolderlock']) {
return;
}
$mform->addElement('hidden', 'vid');
$mform->setType('vid', PARAM_INT);
$mform->setDefault('vid', $this->_customdata['vid']);
if ($this->_customdata['isfoldercontent'] == true && $this->_customdata['iscreatefolder'] == false) {
$mform->addElement('hidden', 'visibility');
$mform->setType('visibility', PARAM_INT);
$mform->setDefault('visibility', content::VISIBILITY_INFOLDERONLY);
} else {
$options = array();
if (in_array(content::VISIBILITY_PRIVATE, $this->_customdata['allowedvisibility'])) {
$options[content::VISIBILITY_PRIVATE] = get_string('contentformvisibilityprivate', 'openstudio');
}
if ($this->_customdata['sharewithothers'] && $this->_customdata['isenrolled']) {
if ($this->_customdata['feature_module']
&& in_array(content::VISIBILITY_MODULE, $this->_customdata['allowedvisibility'])) {
$options[content::VISIBILITY_MODULE] = get_string('contentformvisibilitymodule', 'openstudio');
} else {
if ($this->_customdata['defaultvisibility'] == content::VISIBILITY_MODULE) {
$this->_customdata['defaultvisibility'] = content::VISIBILITY_PRIVATE;
}
}
if ($this->_customdata['feature_group']) {
if (in_array(content::VISIBILITY_TUTOR, $this->_customdata['allowedvisibility'])) {
$options[content::VISIBILITY_TUTOR] = get_string('contentformvisibilitytutor', 'openstudio');
}
if (in_array(content::VISIBILITY_GROUP, $this->_customdata['allowedvisibility'])) {
// Users can only share contents to groups that they are a member of.
// This applies to all users and admins.
if ($this->_customdata['groupingid'] > 0) {
$tutorgroups = group::group_list(
$this->_customdata['courseid'], $this->_customdata['groupingid'], $USER->id, 1);
} else {
$tutorgroups = group::group_list($this->_customdata['courseid'], 0, $USER->id, 1);
}
$firsttutorgroupid = false;
if ($tutorgroups !== false) {
foreach ($tutorgroups as $tutorgroup) {
$tutorgroupid = 0 - $tutorgroup->groupid;
if ($firsttutorgroupid === false) {
$firsttutorgroupid = $tutorgroupid;
}
$options[$tutorgroupid] = get_string('viewgroupname', 'openstudio',
array('name' => $tutorgroup->name));
}
}
if ($this->_customdata['defaultvisibility'] == content::VISIBILITY_GROUP) {
if ($firsttutorgroupid !== false) {
$this->_customdata['defaultvisibility'] = $firsttutorgroupid;
} else {
$this->_customdata['defaultvisibility'] = content::VISIBILITY_PRIVATE;
}
}
}
} else {
if ($this->_customdata['defaultvisibility'] == content::VISIBILITY_GROUP) {
$this->_customdata['defaultvisibility'] = content::VISIBILITY_PRIVATE;
}
}
} else {
$this->_customdata['defaultvisibility'] = content::VISIBILITY_PRIVATE;
}
$mform->addElement('html', html_writer::start_tag('div',
array('class' => 'openstudio-content-form-visibility')));
$visibilitytitle = get_string('contentformvisibility', 'openstudio');
if ($this->_customdata['iscreatefolder'] == true) {
$visibilitytitle = get_string('folderformvisibility', 'openstudio');
$contenttitle = get_string('folderformname', 'openstudio');
$contentdescription = get_string('folderformdescription', 'openstudio');
$submitbuttontitle = get_string('folderformsubmitbutton', 'openstudio');
} else if ($this->_customdata['isfolderediting'] == true) {
$visibilitytitle = get_string('folderformvisibility', 'openstudio');
$contenttitle = get_string('folderformname', 'openstudio');
$contentdescription = get_string('folderformdescription', 'openstudio');
}
$mform->addElement('select', 'visibility', $visibilitytitle, $options,
array('class' => 'openstudio-content-form-select-visibility'));
$mform->setDefault('visibility', $this->_customdata['defaultvisibility']);
$mform->addElement('html', html_writer::end_tag('div'));
}
$mform->addElement('text', 'name', $contenttitle);
$mform->setType('name', PARAM_TEXT);
$mform->addElement('editor', 'description', $contentdescription);
$mform->setType('description', PARAM_RAW);
$defaultcontentuploadtype = '';
if (in_array((int) $this->_customdata['contenttype'],
array(content::TYPE_FOLDER), true) || $this->_customdata['iscreatefolder'] == true) {
$mform->addElement('hidden', 'weblink');
$mform->setType('weblink', PARAM_URL);
$mform->addElement('hidden', 'urltitle');
$mform->setType('urltitle', PARAM_TEXT);
$mform->addElement('hidden', 'embedcode');
$mform->setType('embedcode', PARAM_TEXT);
$mform->addElement('hidden', 'contenttype');
$mform->setType('contenttype', PARAM_INT);
$mform->setDefault('contenttype', (int) $this->_customdata['contenttype']);
} else {
$mform->addElement('hidden', 'urltitle');
$mform->setType('urltitle', PARAM_TEXT);
$contenttype = $this->_customdata['contenttype'];
$buttonaddfileactive = '';
$buttonaddlinkactive = '';
if ($contenttype > content::TYPE_TEXT && $contenttype < content::TYPE_URL) {
$defaultcontentuploadtype = 'addfile';
$buttonaddfileactive = 'openstudio-button-active';
}
if ($contenttype >= content::TYPE_URL) {
if ($this->_customdata['contenttype'] != content::TYPE_FOLDER_CONTENT) {
$defaultcontentuploadtype = 'addlink';
$buttonaddlinkactive = 'openstudio-button-active';
}
}
if ($contenttype == content::TYPE_CAD || $contenttype == content::TYPE_ZIP) {
$defaultcontentuploadtype = 'addfile';
$buttonaddfileactive = 'openstudio-button-active';
}
$mform->addElement('hidden', 'contentuploadtype');
$mform->setType('contentuploadtype', PARAM_TEXT);
$mform->setDefault('contentuploadtype', $defaultcontentuploadtype);
$group = [
$mform->createElement('button', 'addfilebutton',
get_string('addfilebutton', 'openstudio'), array('class' => $buttonaddfileactive)),
$mform->createElement('static', 'addfilebutton_help', '', $OUTPUT->help_icon('addfilebutton', 'openstudio')),
$mform->createElement('button', 'addlinkbutton',
get_string('addlinkbutton', 'openstudio'), array('class' => $buttonaddlinkactive)),
$mform->createElement('static', 'addfilebutton_help', '', $OUTPUT->help_icon('addlinkbutton', 'openstudio'))
];
$mform->addGroup($group, 'openstudio_upload_content_buttons', get_string('contentformattachments', 'openstudio'));
$mform->addElement('html', html_writer::start_tag('div',
array('id' => 'openstudio_upload_content_add_file',
'class' => $defaultcontentuploadtype &&
$defaultcontentuploadtype == 'addfile' ? '' : 'openstudio-hidden')));
if ($this->_customdata['feature_contentusesfileupload']) {
if ($this->_customdata['max_bytes']) {
$maxbytes = $this->_customdata['max_bytes'];
} else {
$maxbytes = (isset($CFG->maxbytes) ? $CFG->maxbytes : \mod_openstudio\local\util\defaults::MAXBYTES);
}
$mform->addElement('filemanager', 'attachments', get_string('files', 'moodle'), array('class' => 'openstudio-upload-file'),
array('maxbytes' => $maxbytes, 'subdirs' => false,
'maxfiles' => 1, 'accepted_types' => self::ACCEPT_TYPE));
}
if (!$this->_customdata['feature_contentusesfileupload'] ||
(((int) $this->_customdata['contenttype']) === content::TYPE_FOLDER)) {
$mform->addElement('hidden', 'showgps');
$mform->setType('showgps', PARAM_INT);
$mform->setDefault('showgps', 0);
$mform->addElement('hidden', 'showimagedata');
$mform->setType('showimagedata', PARAM_INT);
$mform->setDefault('showimagedata', 0);
} else {
$mform->addElement('html', html_writer::start_tag('div',
array('class' => 'openstudio-showgps-box')));
$showgpsarray = array();
$showgpsarray[] = $mform->createElement('advcheckbox', 'showgps', '',
get_string('contentformshowgps', 'openstudio'),
array('group' => 1), array(0, content::INFO_GPSDATA));
$showgpsarray[] = $mform->createElement('advcheckbox', 'showimagedata', '',
get_string('contentformshowimagedata', 'openstudio'),
array('group' => 1), array(0, content::INFO_IMAGEDATA));
$mform->addGroup($showgpsarray, 'showgpsarray', '', array(' '), false);
$mform->setDefault('showgps', 0);
$mform->setDefault('showimagedata', 0);
$mform->addElement('html', html_writer::end_tag('div'));
if ($this->_customdata['contentid']) {
$showextradata = $DB->get_field('openstudio_contents', 'showextradata',
array('id' => $this->_customdata['contentid']));
if ($showextradata & content::INFO_GPSDATA) {
$mform->setDefault('showgps', content::INFO_GPSDATA);
}
if ($showextradata & content::INFO_IMAGEDATA) {
$mform->setDefault('showimagedata', content::INFO_IMAGEDATA);
}
}
}
if (!extension_loaded('imagick') || !class_exists('Imagick')) {
$mform->addElement('hidden', 'retainimagemetadata');
$mform->setType('retainimagemetadata', PARAM_INT);
$mform->setDefault('retainimagemetadata', 0);
} else {
$mform->addElement('checkbox', 'retainimagemetadata', get_string('retainimagemetadata', 'openstudio'));
$mform->addHelpButton('retainimagemetadata', 'retainimagemetadata', 'openstudio');
}
if (!empty($this->_customdata['retainimagemetadata'])) {
$mform->setDefault('retainimagemetadata', $this->_customdata['retainimagemetadata']);
}
$mform->addElement('html', html_writer::end_tag('div'));
$mform->addElement('html', html_writer::start_tag('div',
array('id' => 'openstudio_upload_content_add_link',
'class' => $defaultcontentuploadtype && $defaultcontentuploadtype == 'addlink' ? '' : 'openstudio-hidden')));
$mform->addElement('text', 'weblink', get_string('weblink', 'openstudio'));
$mform->setType('weblink', PARAM_URL);
$mform->addRule(
'weblink',
get_string('contentformweblinkerror', 'openstudio'),
'regex',
'/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/',
'client');
$mform->addHelpButton('weblink', 'addlinkbutton', 'openstudio');
$mform->addElement('html', html_writer::end_tag('div'));
}
if ($this->_customdata['folderdetails']) {
$submitbuttontitle = get_string('contentformsubmitbutton', 'openstudio');
}
$isfolder = $this->_customdata['iscreatefolder'] || $this->_customdata['isfolderediting'];
$mform->addElement('html', html_writer::start_tag('div',
array('id' => 'contentformoptionalmetadata',
'class' => $defaultcontentuploadtype || $isfolder ? '' : 'openstudio-hidden')));
$mform->addElement('html', html_writer::start_tag('div',
array('id' => 'contentformownershipdata',
'class' => $isfolder ? 'openstudio-hidden' : '')));
$radioarray = array();
$radioarray[] = $mform->createElement('radio', 'ownership', '',
get_string('contentformownershipmyownwork', 'openstudio'),
content::OWNERSHIP_MYOWNWORK);
$radioarray[] = $mform->createElement('radio', 'ownership', '',
get_string('contentformownershipfoundelsewhere', 'openstudio'),
content::OWNERSHIP_FOUNDELSEWHERE);
$mform->addGroup($radioarray, 'ownershiparray',
get_string('contentformownership', 'openstudio'),
array(' '), false);
$mform->addElement('text',
'ownershipdetail',
get_string('contentformownershipdetail', 'openstudio'));
$mform->setType('ownershipdetail', PARAM_TEXT);
$mform->disabledIf('ownershipdetail', 'ownership', 'neq', '2');
$mform->addElement('html', html_writer::end_tag('div'));
// Add custom class to style tag label align with input.
$mform->addElement('tags', 'tags', get_string('tags'),
array('itemtype' => 'openstudio_contents', 'component' => 'mod_openstudio'),
array('class' => 'openstudio_contentform_tag')
);
$mform->addHelpButton('tags', 'tags', 'openstudio');
$mform->addElement('html', html_writer::end_tag('div'));
$mform->addElement('hidden', 'sid');
$mform->setType('sid', PARAM_INT);
$mform->addElement('hidden', 'levelid');
$mform->setType('levelid', PARAM_INT);
$mform->addElement('hidden', 'levelcontainer');
$mform->setType('levelcontainer', PARAM_INT);
$mform->addElement('hidden', 'checksum');
$mform->setType('checksum', PARAM_TEXT);
$mform->addElement('html', html_writer::start_tag('div',
array('class' => 'openstudio-content-form-submit-buttons')));
$buttonarray = array();
$buttonarray[] = $mform->createElement('submit', 'submitbutton', $submitbuttontitle,
array('id' => 'id_submitbutton'));
$buttonarray[] = $mform->createElement('cancel', 'cancelbutton',
'', array('id' => 'id_cancel'));
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');
$mform->addElement('html', html_writer::end_tag('div'));
}
public function validation($data, $files) {
global $USER;
$errors = parent::validation($data, $files);
if (!(!extension_loaded('imagick') || !class_exists('Imagick'))) {
// Only check retain EXIF data if option is shown.
if (empty($data['retainimagemetadata']) && (!empty($data['showgps']) || !empty($data['showimagedata']))) {
$errors['retainimagemetadata'] = get_string('retainimagemetadataerror', 'openstudio');
}
}
if (!empty($data['attachments'])) {
$fs = get_file_storage();
$usercontext = context_user::instance($USER->id);
$draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['attachments']);
if ($draftfiles) {
// The form only lets us upload 1 file, so there will only be 1 file in the array.
$file = array_pop($draftfiles);
if ($file->get_mimetype() == 'application/x-smarttech-notebook') {
$packer = new zip_packer();
if ($file) {
$itemid = $file->get_itemid();
$filepath = $file->get_filepath();
$filelist = $file->list_files($packer);
$numberoffiles = 0;
foreach ($filelist as $f) {
if (!$f->is_directory) {
$numberoffiles++;
}
}
// Check that we've got the right number of files.
if ($numberoffiles < 2) {
$errors['attachments'] = get_string('errorcontentemptynotebook', 'openstudio');
} else if ($numberoffiles > 2) {
$errors['attachments'] = get_string('errorcontentfullnotebook', 'openstudio');
} else {
// We've got the right number of files, let's check if they appear to be the right types.
$expectedtypes = array('html', 'htm', 'ipynb');
foreach ($filelist as $contentfile) {
$extension = pathinfo($contentfile->pathname, PATHINFO_EXTENSION);
if (in_array($extension, $expectedtypes) || $contentfile->is_directory) {
$foundtypes = array($extension);
// We only want 1 of htm or html, so if we've found one, remove both.
if ($extension == 'html') {
$foundtypes[] = 'htm';
} else if ($extension == 'htm') {
$foundtypes[] = 'html';
}
$expectedtypes = array_diff($expectedtypes, $foundtypes);
} else {
$errors['attachments'] = get_string('errorcontentinvalidnotebook', 'openstudio');
}
}
if (!isset($errors['attachments'])) {
// We've got the right number of files and they appear to be of the correct type.
// Now we need to extract them and take a look to make sure they're actually notebook
// files before we accept them.
$extractedfilenames = $file->extract_to_storage(
$packer,
$usercontext->id,
'user',
'draft',
$itemid,
$filepath
);
$file->set_sortorder(3);
$html = '';
$ipynb = '';
foreach ($extractedfilenames as $extractedfilename => $success) {
if ($success !== true) {
$parts = (object)array('filename' => $extractedfilename, 'message' => $success);
$errors['attachments'] = get_string('errorcontentnotebookerror', 'openstudio', $parts);
break;
}
$extractedfile = $fs->get_file(
$usercontext->id,
'user',
'draft',
$itemid,
$filepath,
$extractedfilename
);
$content = $extractedfile->get_content();
if (pathinfo($extractedfilename, PATHINFO_EXTENSION) == 'ipynb') {
$ipynb = json_decode($content);
if ($ipynb === null) {
// If the file contents isn't decodeable from JSON, it isn't
// really an ipython notebook.
$errors['attachments'] = get_string('errorcontentinvalidnotebook', 'openstudio');
break;
} else {
// Check that the object parsed from JSON has at least the most basic properties
// of an ipynb file.
// Note there are some differences between versions of notebook files.
if ((!isset($ipynb->metadata, $ipynb->nbformat_minor, $ipynb->nbformat) ||
($ipynb->nbformat == 3 && !isset($ipynb->worksheets)) ||
($ipynb->nbformat == 4 && !isset($ipynb->cells)))) {
$errors['attachments'] = get_string('errorcontentinvalidnotebook', 'openstudio');
break;
}
}
// Set the ipynb's sort order to 1 so it's returned first when getting files from the
// file area, and therefore it's seen as "the" file in the content when we call
// content::create in contentedit.php. Since this is the only instance
// we'll ever have a ipynb file at that point, we can use it as an indication
// that the content contains a notebook.
$extractedfile->set_sortorder(1);
} else {
$html = $content;
// For the HTML file, There's not a lot that we can reliably check at this stage, so
// we'll just set the file's sortorder.
$extractedfile->set_sortorder(2);
}
}
if (!isset($errors['attachments'])) {
// We've got 2 files and they're the right types. Finally, if we can, let's check that the HTML
// has the same number of cells as the source file.
$cellcount = 0;
if ($ipynb->nbformat == 3) {
if (count($ipynb->worksheets) > 0) {
$worksheet = $ipynb->worksheets[0];
if (isset($worksheet->cells)) {
$cellcount = count($worksheet->cells);
}
}
} else {
$cellcount = count($ipynb->cells);
}
if ($cellcount > 0 && $cellcount != substr_count($html, '<div class="cell')) {
$errors['attachments'] = get_string('errorcontentinvalidnotebook', 'openstudio');
}
}
}
}
} else {
$errors['attachments'] = get_string('errorcontentinvalidnotebook', 'openstudio');
}
}
if (isset($errors['attachments'])) {
// If the file was invalid, clean up the drafts area.
$fs->delete_area_files($usercontext->id, 'user', 'draft', $data['attachments']);
}
}
}
return $errors;
}
}