-
Notifications
You must be signed in to change notification settings - Fork 3
/
fileresponsesimplifiedfilemanager.php
executable file
·1681 lines (1577 loc) · 70.5 KB
/
fileresponsesimplifiedfilemanager.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* Fileresponse simplified FileManager form element
*
* Contains HTML class for a fileresponse simplified filemanager form element
*
* @package qtype_fileresponse
* @copyright 2022 Luca Bösch, BFH Bern University of Applied Sciences [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once('HTML/QuickForm/element.php');
require_once($CFG->dirroot . '/lib/filelib.php');
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->dirroot . '/lib/form/templatable_form_element.php');
/**
* Fileresponse simplified filemanager form element
*
* Fileresponse simplified filemanager lets user to upload/manage multiple files
*
* @package qtype_fileresponse
* @copyright 2012 Luca Bösch [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_fileresponsesimplifiedfilemanager extends HTML_QuickForm_element implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help will icon will be displayed. */
public $_helpbutton = '';
/** @var array options provided to initalize fileresponsesimplifiedfilemanager */
// PHP doesn't support 'key' => $value1 | $value2 in class definition.
// We cannot do $_options = array('return_types'=> FILE_INTERNAL | FILE_REFERENCE);
// So I have to set null here, and do it in constructor.
protected $_options = array('mainfile' => '', 'subdirs' => 1, 'maxbytes' => -1,
'maxfiles' => -1, 'accepted_types' => '*', 'return_types' => null,
'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED
);
/**
* Constructor
*
* @param string $elementname (optional) name of the fileresponsesimplifiedfilemanager
* @param string $elementlabel (optional) fileresponsesimplifiedfilemanager label
* @param array $attributes (optional) Either a typical HTML attribute string
* or an associative array
* @param array $options set of options to initalize fileresponsesimplifiedfilemanager
*/
public function __construct($elementname = null, $elementlabel = null, $attributes = null, $options = null) {
global $CFG, $PAGE;
$options = (array) $options;
foreach ($options as $name => $value) {
if (array_key_exists($name, $this->_options)) {
$this->_options[$name] = $value;
}
}
if (!empty($options['maxbytes'])) {
$this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context,
$CFG->maxbytes, $options['maxbytes']);
}
if (empty($options['return_types'])) {
$this->_options['return_types'] = (FILE_INTERNAL | FILE_REFERENCE | FILE_CONTROLLED_LINK);
}
$this->_type = 'fileresponsesimplifiedfilemanager';
parent::__construct($elementname, $elementlabel, $attributes);
}
// @codingStandardsIgnoreStart
/**
* Called by HTML_QuickForm whenever form event is made on this element
*
* @param string $event Name of event
* @param mixed $arg event arguments
* @param object $caller calling object
* @return bool
*/
public function onQuickFormEvent($event, $arg, &$caller) {
switch ($event) {
case 'createElement':
$caller->setType($arg[0], PARAM_INT);
break;
}
return parent::onQuickFormEvent($event, $arg, $caller);
}
// @codingStandardsIgnoreEnd
/**
* Sets name of fileresponsesimplifiedfilemanager
*
* @param string $name name of the fileresponsesimplifiedfilemanager
*/
public function setname($name) {
$this->updateAttributes(array('name' => $name));
}
/**
* Returns name of fileresponsesimplifiedfilemanager
*
* @return string
*/
public function getname() {
return $this->getAttribute('name');
}
/**
* Updates fileresponsesimplifiedfilemanager attribute value
*
* @param string $value value to set
*/
public function setvalue($value) {
$this->updateAttributes(array('value' => $value));
}
/**
* Returns fileresponsesimplifiedfilemanager attribute value
*
* @return string
*/
public function getvalue() {
return $this->getAttribute('value');
}
/**
* Returns maximum file size which can be uploaded
*
* @return int
*/
public function getmaxbytes() {
return $this->_options['maxbytes'];
}
/**
* Sets maximum file size which can be uploaded
*
* @param int $maxbytes file size
*/
public function setmaxbytes($maxbytes) {
global $CFG, $PAGE;
$this->_options['maxbytes'] = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes,
$maxbytes);
}
/**
* Returns the maximum size of the area.
*
* @return int
*/
public function getareamaxbytes() {
return $this->_options['areamaxbytes'];
}
/**
* Sets the maximum size of the area.
*
* @param int $areamaxbytes size limit
*/
public function setareamaxbytes($areamaxbytes) {
$this->_options['areamaxbytes'] = $areamaxbytes;
}
/**
* Returns true if subdirectoy can be created, else false
*
* @return bool
*/
public function getsubdirs() {
return $this->_options['subdirs'];
}
/**
* Set option to create sub directory, while uploading file
*
* @param bool $allow true if sub directory can be created.
*/
public function setsubdirs($allow) {
$this->_options['subdirs'] = $allow;
}
/**
* Returns maximum number of files which can be uploaded
*
* @return int
*/
public function getmaxfiles() {
return $this->_options['maxfiles'];
}
/**
* Sets maximum number of files which can be uploaded.
*
* @param int $num number of files
*/
public function setmaxfiles($num) {
$this->_options['maxfiles'] = $num;
}
/**
* Returns html for help button.
*
* @return string html for help button
*/
public function gethelpbutton() {
return $this->_helpbutton;
}
/**
* Returns type of fileresponsesimplifiedfilemanager element
*
* @return string
*/
public function getelementtemplatetype() {
if ($this->_flagFrozen) {
return 'nodisplay';
} else {
return 'default';
}
}
/**
* Returns HTML for fileresponsesimplifiedfilemanager form element.
*
* @return string
*/
public function tohtml() {
global $CFG, $USER, $COURSE, $PAGE, $OUTPUT;
require_once("$CFG->dirroot/repository/lib.php");
// Security - never ever allow guest/not logged in user to upload anything or use this
// element!
if (isguestuser() or !isloggedin()) {
throw new moodle_exception('noguest');
}
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
}
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
$subdirs = $this->_options['subdirs'];
$maxbytes = $this->_options['maxbytes'];
$draftitemid = $this->getvalue();
$acceptedtypes = $this->_options['accepted_types'];
if (empty($draftitemid)) {
// No existing area info provided - let's use fresh new draft area.
require_once("$CFG->libdir/filelib.php");
$this->setvalue(file_get_unused_draft_itemid());
$draftitemid = $this->getvalue();
}
$clientid = uniqid();
// Fileresponsesimplifiedfilemanager options.
$options = new stdClass();
$options->mainfile = $this->_options['mainfile'];
$options->maxbytes = $this->_options['maxbytes'];
$options->maxfiles = $this->getMaxfiles();
$options->client_id = $clientid;
$options->itemid = $draftitemid;
$options->subdirs = $this->_options['subdirs'];
$options->target = $id;
$options->accepted_types = $acceptedtypes;
$options->return_types = $this->_options['return_types'];
$options->context = $PAGE->context;
$options->areamaxbytes = $this->_options['areamaxbytes'];
$html = $this->_getTabs();
$frsfm = new form_fileresponsesimplifiedfilemanager($options);
$output = $PAGE->get_renderer('core', 'files');
$html .= $this->output->render($frsfm);
$html .= html_writer::empty_tag('input',
array('value' => $draftitemid, 'name' => $elname, 'type' => 'hidden'
));
// Label element needs 'for' attribute work.
$html .= html_writer::empty_tag('input',
array('value' => '', 'id' => 'id_' . $elname, 'type' => 'hidden'
));
if (!empty($options->accepted_types) && $options->accepted_types != '*') {
$html .= html_writer::tag('p', get_string('filesofthesetypes', 'form'));
$util = new \core_form\filetypes_util();
$filetypes = $options->accepted_types;
$filetypedescriptions = $util->describe_file_types($filetypes);
$html .= $this->output->render_from_template('core_form/filetypes-descriptions', $filetypedescriptions);
}
return $html;
}
public function export_for_template(renderer_base $output) {
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
// @codingStandardsIgnoreStart
/**
* Check that all files have the allowed type.
*
* @param int $value Draft item id with the uploaded files.
* @return string|null Validation error message or null.
*/
public function validateSubmitValue($value) {
if (empty($value)) {
return;
}
$filetypesutil = new \core_form\filetypes_util();
$allowlist = $filetypesutil->normalize_file_types($this->_options['accepted_types']);
if (empty($allowlist) || $allowlist === ['*']) {
// Any file type is allowed, nothing to check here.
return;
}
$draftfiles = file_get_all_files_in_draftarea($value);
$wrongfiles = array();
if (empty($draftfiles)) {
// No file uploaded, nothing to check here.
return;
}
foreach ($draftfiles as $file) {
if (!$filetypesutil->is_allowed_file_type($file->filename, $allowlist)) {
$wrongfiles[] = $file->filename;
}
}
if ($wrongfiles) {
$a = array(
'allowlist' => implode(', ', $allowlist),
'wrongfiles' => implode(', ', $wrongfiles),
);
return get_string('err_wrongfileextension', 'core_form', $a);
}
return;
}
}
// @codingStandardsIgnoreEnd
/**
* Data structure representing a fileresponse simplified file manager.
*
* This class defines the data structure for a fileresponse simplified file manager
*
* Layout things are mainly taken from theme/boost/templates/core/filemanager_modal_generallayout.mustache.
*
* @package qtype_fileresponse
* @copyright 2012 Luca Bösch [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @todo do not use this abstraction (skodak)
*/
class form_fileresponsesimplifiedfilemanager implements renderable {
/** @var stdClass $options options for fileresponsesimplifiedfilemanager */
public $options;
/**
* Constructor
*
* @param stdClass $options options for fileresponsesimplifiedfilemanager
* default options are:
* maxbytes=>-1,
* areamaxbytes => FILE_AREA_MAX_BYTES_UNLIMITED,
* maxfiles=>-1,
* itemid=>0,
* subdirs=>false,
* client_id=>uniqid(),
* acepted_types=>'*',
* return_types=>FILE_INTERNAL,
* context=>$PAGE->context,
* author=>fullname($USER),
* licenses=>array build from $CFG->licenses,
* defaultlicense=>$CFG->sitedefaultlicense
*/
public function __construct(stdClass $options) {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/repository/lib.php');
$defaults = array('maxbytes' => -1, 'areamaxbytes' => FILE_AREA_MAX_BYTES_UNLIMITED,
'maxfiles' => -1, 'itemid' => 0, 'subdirs' => 0, 'client_id' => uniqid(),
'accepted_types' => '*', 'return_types' => FILE_INTERNAL, 'context' => $PAGE->context,
'author' => fullname($USER), 'licenses' => array()
);
if (!empty($CFG->licenses)) {
$array = explode(',', $CFG->licenses);
foreach ($array as $license) {
$l = new stdClass();
$l->shortname = $license;
$l->fullname = get_string($license, 'license');
$defaults['licenses'][] = $l;
}
}
if (!empty($CFG->sitedefaultlicense)) {
$defaults['defaultlicense'] = $CFG->sitedefaultlicense;
}
foreach ($defaults as $key => $value) {
// Using !isset() prevents us from overwriting falsey values with defaults (as empty()
// did).
if (!isset($options->$key)) {
$options->$key = $value;
}
}
$fs = get_file_storage();
// Initialise options, getting files in root path.
$this->options = file_get_drafarea_files($options->itemid, '/');
// Calculate file count.
$usercontext = context_user::instance($USER->id);
$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id',
false);
$filecount = count($files);
$this->options->filecount = $filecount;
// Copying other options.
foreach ($options as $name => $value) {
$this->options->$name = $value;
}
// Calculate the maximum file size as minimum from what is specified in filepicker options,
// course options, global configuration and php settings.
$coursebytes = $maxbytes = 0;
list($context, $course, $cm) = get_context_info_array($this->options->context->id);
if (is_object($course)) {
$coursebytes = $course->maxbytes;
}
if (!empty($this->options->maxbytes) && $this->options->maxbytes > 0) {
$maxbytes = $this->options->maxbytes;
}
$this->options->maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes,
$coursebytes, $maxbytes);
$this->options->userprefs = array();
$this->options->userprefs['recentviewmode'] = get_user_preferences('filemanager_recentviewmode', '');
user_preference_allow_ajax_update('filemanager_recentviewmode', PARAM_INT);
// Building file picker options.
$params = new stdClass();
$params->accepted_types = $options->accepted_types;
$params->return_types = $options->return_types;
$params->context = $options->context;
$params->env = 'fileresponsesimplifiedfilemanager';
$params->disable_types = !empty($options->disable_types) ? $options->disable_types : array();
$filepickeroptions = initialise_filepicker($params);
// If filepicker plugins aren't allowed make sure that only upload repository is available
// for students.
if (!$options->allowpickerplugins) {
foreach ($filepickeroptions->repositories as $repository) {
if ($repository->type !== 'upload') {
unset($filepickeroptions->repositories[$repository->id]);
} else {
$filepickeroptions->userprefs['recentrepository'] = $repository->id; // Make it
// active
// tab!
}
}
}
$this->options->filepicker = $filepickeroptions;
}
public function get_nonjsurl() {
global $PAGE;
return new moodle_url('/repository/draftfiles_manager.php',
array('env' => 'fileresponsesimplifiedfilemanager', 'action' => 'browse',
'itemid' => $this->options->itemid, 'subdirs' => $this->options->subdirs,
'maxbytes' => $this->options->maxbytes,
'areamaxbytes' => $this->options->areamaxbytes,
'maxfiles' => $this->options->maxfiles, 'ctx_id' => $PAGE->context->id, // TODO
// ?
'course' => $PAGE->course->id, // TODO ?
'sesskey' => sesskey()
));
}
/**
* Generate all options needed by filepicker
*
* @param array $args including following keys
* context
* accepted_types
* return_types
*
* @return array the list of repository instances, including meta infomation, containing the
* following keys
* externallink
* repositories
* accepted_types
*/
public static function initialise_fileresponsesimplifiedfilepicker($args) {
global $CFG, $USER, $PAGE, $OUTPUT;
static $templatesinitialized = array();
require_once($CFG->libdir . '/licenselib.php');
$return = new stdClass();
$licenses = array();
if (!empty($CFG->licenses)) {
$array = explode(',', $CFG->licenses);
foreach ($array as $license) {
$l = new stdClass();
$l->shortname = $license;
$l->fullname = get_string($license, 'license');
$licenses[] = $l;
}
}
if (!empty($CFG->sitedefaultlicense)) {
$return->defaultlicense = $CFG->sitedefaultlicense;
}
$return->licenses = $licenses;
$return->author = fullname($USER);
if (empty($args->context)) {
$context = $PAGE->context;
} else {
$context = $args->context;
}
$disabletypes = array();
if (!empty($args->disable_types)) {
$disabletypes = $args->disable_types;
}
$usercontext = context_user::instance($USER->id);
list($context, $course, $cm) = get_context_info_array($context->id);
$contexts = array($usercontext, context_system::instance()
);
if (!empty($course)) {
// Adding course context.
$contexts[] = context_course::instance($course->id);
}
$externallink = (int) get_config(null, 'repositoryallowexternallinks');
$repositories = repository::get_instances(
array('context' => $contexts, 'currentcontext' => $context,
'accepted_types' => $args->accepted_types, 'return_types' => $args->return_types,
'disable_types' => $disabletypes
));
$return->repositories = array();
if (empty($externallink)) {
$return->externallink = false;
} else {
$return->externallink = true;
}
$return->userprefs = array();
$return->userprefs['recentrepository'] = get_user_preferences('filepicker_recentrepository',
'');
$return->userprefs['recentlicense'] = get_user_preferences('filepicker_recentlicense', '');
$return->userprefs['recentviewmode'] = get_user_preferences('filepicker_recentviewmode', '');
user_preference_allow_ajax_update('filepicker_recentrepository', PARAM_INT);
user_preference_allow_ajax_update('filepicker_recentlicense', PARAM_SAFEDIR);
user_preference_allow_ajax_update('filepicker_recentviewmode', PARAM_INT);
// Provided by form element.
$return->accepted_types = file_get_typegroup('extension', $args->accepted_types);
$return->return_types = $args->return_types;
$templates = array();
foreach ($repositories as $repository) {
$meta = $repository->get_meta();
// Please note that the array keys for repositories are used within
// JavaScript a lot, the key NEEDS to be the repository id.
$return->repositories[$repository->id] = $meta;
// Register custom repository template if it has one.
if (method_exists($repository, 'get_upload_template') &&
!array_key_exists('uploadform_' . $meta->type, $templatesinitialized)) {
$templates['uploadform_' . $meta->type] = $repository->get_upload_template();
$templatesinitialized['uploadform_' . $meta->type] = true;
}
}
if (!array_key_exists('core', $templatesinitialized)) {
// We need to send each filepicker template to the browser just once.
$fprenderer = $PAGE->get_renderer('core', 'files');
$templates = array_merge($templates, $fprenderer->filepicker_js_templates());
$templatesinitialized['core'] = true;
}
if (count($templates) > 0) {
$PAGE->requires->js_init_call('M.core_filepicker.set_templates',
array($templates
), true);
}
return $return;
}
}
class qtype_fileresponse_fileresponsesimplifiedfilemanager_renderer extends plugin_renderer_base {
/**
* Prints the file manager and initializes all necessary libraries
* This is mainly taken from files/renderer.php.
*
* <pre>
* $fm = new form_fileresponsesimplifiedfilemanager($options);
* $output = get_renderer('core', 'files');
* echo $this->output->render($fm);
* </pre>
*
* @param form_fileresponsesimplifiedfilemanager $fm File manager to render
* @return string HTML fragment
*/
public function render_form_fileresponsesimplifiedfilemanager($fm) {
static $filemanagertemplateloaded;
$html = $this->fm_print_generallayout($fm);
$module = array('name' => 'form_fileresponsesimplifiedfilemanager',
'fullpath' => '/question/type/fileresponse/fileresponsesimplifiedfilemanager.js',
'requires' => array('moodle-core-notification-dialogue', 'core_filepicker', 'base', 'io-base', 'node', 'json',
'core_dndupload', 'panel', 'resize-plugin', 'dd-plugin'),
'strings' => array(
array('error', 'moodle'), array('info', 'moodle'), array('confirmdeletefile', 'repository'),
array('draftareanofiles', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
array('invalidjson', 'repository'), array('popupblockeddownload', 'repository'),
array('unknownoriginal', 'repository'), array('confirmdeletefolder', 'repository'),
array('confirmdeletefilewithhref', 'repository'), array('confirmrenamefolder', 'repository'),
array('confirmrenamefile', 'repository'), array('newfolder', 'repository'), array('edit', 'moodle')
)
);
if (empty($fileresponsesimplifiedfilemanagertemplateloaded)) {
$fileresponsesimplifiedfilemanagertemplateloaded = true;
$this->page->requires->js_init_call(
'M.form_fileresponsesimplifiedfilemanager.set_templates',
array($this->fileresponsesimplifiedfilemanager_js_templates()
), true, $module);
}
$this->page->requires->js_init_call('M.form_fileresponsesimplifiedfilemanager.init',
array($fm->options
), true, $module);
// Non javascript file manager.
$html .= '<noscript>';
$html .= "<div><object type='text/html' data='" . $fm->get_nonjsurl() .
"' height='160' width='600' style='border:1px solid #000'></object></div>";
$html .= '</noscript>';
return $html;
}
/**
* Returns html for displaying one file manager
*
* The main element in HTML must have id="fileresponsesimplifiedfilemanager-{$clientid}" and
* class="fileresponsesimplifiedfilemanager fm-loading";
* After all necessary code on the page (both html and javascript) is loaded,
* the class fm-loading will be removed and added class fm-loaded;
* The main element (class=fileresponsesimplifiedfilemanager) will be assigned the following classes:
* 'fm-maxfiles' - when fileresponsesimplifiedfilemanager has maximum allowed number of files;
* 'fm-nofiles' - when fileresponsesimplifiedfilemanager has no files at all (although there
* might be folders);
* 'fm-noitems' - when current view (folder) has no items - neither files nor folders;
* 'fm-updating' - when current view is being updated (usually means that loading icon is to be
* displayed);
* 'fm-nomkdir' - when 'Make folder' action is unavailable (empty($fm->options->subdirs) ==
* true)
*
* Element with class 'fileresponsesimplifiedfilemanager-container' will be holding evens for dnd upload
* (dragover, etc.).
* It will have class:
* 'dndupload-ready' - when a file is being dragged over the browser
* 'dndupload-over' - when file is being dragged over this filepicker (additional to
* 'dndupload-ready')
* 'dndupload-uploading' - during the upload process (note that after dnd upload process is
* over, the file manager will refresh the files list and therefore will have for a while class
* fm-updating. Both waiting processes should look similar so the images don't jump for user)
*
* If browser supports Drag-and-drop, the body element will have class 'dndsupported',
* otherwise - 'dndnotsupported';
*
* Element with class 'fp-content' will be populated with files list;
* Element with class 'fp-btn-add' will hold onclick event for adding a file (opening
* filepicker);
* Element with class 'fp-btn-mkdir' will hold onclick event for adding new folder;
* Element with class 'fp-btn-download' will hold onclick event for download action;
*
* Element with class 'fp-path-folder' is a template for one folder in path toolbar.
* It will hold mouse click event and will be assigned classes first/last/even/odd respectfully.
* Parent element will receive class 'empty' when there are no folders to be displayed;
* The content of subelement with class 'fp-path-folder-name' will be substituted with folder
* name;
*
* Element with class 'fp-viewbar' will have the class 'enabled' or 'disabled' when view mode
* can be changed or not;
* Inside element with class 'fp-viewbar' there are expected elements with classes
* 'fp-vb-icons', 'fp-vb-tree' and 'fp-vb-details'. They will handle onclick events to switch
* between the view modes, the last clicked element will have the class 'checked';
*
* @param form_fileresponsesimplifiedfilemanager $fm
* @return string
*/
protected function fm_print_generallayout($fm) {
$options = $fm->options;
$clientid = $options->client_id;
$straddfile = get_string('addfile', 'repository');
$strmakedir = get_string('makeafolder', 'moodle');
$strdownload = get_string('downloadfolder', 'repository');
$strloading = get_string('loading', 'repository');
$strdroptoupload = get_string('droptoupload', 'moodle');
$iconprogress = $this->output->pix_icon('i/loading_small', $strloading) . '';
$restrictions = $this->fm_print_restrictions($fm);
$strdndnotsupported = get_string('dndnotsupported_insentence', 'moodle') .
$this->output->help_icon('dndnotsupported');
$strdndenabledinbox = get_string('dndenabled_inbox', 'moodle');
$loading = get_string('loading', 'repository');
$straddfiletext = get_string('addfiletext', 'repository');
$strcreatefolder = get_string('createfolder', 'repository');
$strdownloadallfiles = get_string('downloadallfiles', 'repository');
$html = '
<div id="fileresponsesimplifiedfilemanager-' . $clientid . '" class="filemanager fm-loading">
<div class="fp-restrictions">
' . $restrictions . '
<span class="dnduploadnotsupported-message"> - ' . $strdndnotsupported . ' </span>
</div>
<div class="fp-navbar bg-faded card mb-0 clearfix icon-no-spacing">
<div class="filemanager-toolbar icon-no-spacing">
<div class="fp-toolbar">
<div class="fp-btn-add">
<a role="button" title="' . $straddfile . '" class="btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('a/add_file', $straddfiletext) . '
</a>
</div>
<div class="fp-btn-mkdir">
<a role="button" title="' . $strmakedir . '" class="btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('a/create_folder', $strcreatefolder) . '
</a>
</div>
<!-- <div class="fp-btn-download"> -->
<!-- <a role="button" title="' . $strdownload . '" class="btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('a/download_all', $strdownloadallfiles) . '
</a> -->
<!-- don\'t download fix-->
<!-- </div> -->
<!-- <span class="fp-img-downloading"> -->
<!-- <span class="sr-only">' . get_string('loadinghelp') . '</span>
' . $this->pix_icon('i/loading_small', '') . ' -->
<!-- don\'t download fix-->
<!-- </span> -->
</div>
<div class="fp-viewbar btn-group float-sm-right">
<a role="button" title="'. get_string('displayicons', 'repository') .
'" class="fp-vb-icons btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('fp/view_icon_active', get_string('displayasicons', 'repository'), 'theme') . '
</a>
<a role="button" title="'. get_string('displaydetails', 'repository') .
'" class="fp-vb-details btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('fp/view_list_active', get_string('displayasdetails', 'repository'), 'theme') . '
</a>
<a role="button" title="'. get_string('displaytree', 'repository') .
'" class="fp-vb-tree btn btn-secondary btn-sm" href="#">
' . $this->pix_icon('fp/view_tree_active', get_string('displayastree', 'repository'), 'theme') . '
</a>
</div>
</div>
<div class="fp-pathbar">
<span class="fp-path-folder"><a class="fp-path-folder-name" href="#"></a></span>
</div>
</div>
<div class="filemanager-loading mdl-align">'.$iconprogress.'</div>
<div class="filemanager-container card" >
<div class="fm-content-wrapper">
<div class="fp-content"></div>
<div class="fm-empty-container">
<div class="dndupload-message">' .
$strdndenabledinbox .
'<br/><div class="dndupload-arrow"></div></div>
</div>
<div class="dndupload-target">' .
$strdroptoupload . '<br/><div class="dndupload-arrow"></div></div>
<div class="dndupload-progressbars"></div>
<div class="dndupload-uploadinprogress">'.$iconprogress.'</div>
</div>
<div class="filemanager-updating">'.$iconprogress.'</div>
</div>
</div>';
return $html;
}
/**
* fileresponsesimplifiedfilemanager JS template for displaying one file in 'icon view' mode.
*
* Except for elements described in fp_js_template_iconfilename, this template may also
* contain element with class 'fp-contextmenu'. If context menu is available for this
* file, the top element will receive the additional class 'fp-hascontextmenu' and
* the element with class 'fp-contextmenu' will hold onclick event for displaying
* the context menu.
*
* @see fp_js_template_iconfilename()
* @return string
*/
protected function fm_js_template_iconfilename() {
$rv = '
<div class="fp-file">
<a href="#">
<div style="position:relative;">
<div class="fp-thumbnail"></div>
<div class="fp-reficons1"></div>
<div class="fp-reficons2"></div>
</div>
<div class="fp-filename-field">
<div class="fp-filename"></div>
</div>
</a>
<a class="fp-contextmenu" href="#">' . $this->pix_icon('i/menu', '▶') . '</a>
</div>';
return $rv;
}
/**
* fileresponsesimplifiedfilemanager JS template for displaying file name in 'table view' and 'tree view' modes.
*
* Except for elements described in fp_js_template_listfilename, this template may also
* contain element with class 'fp-contextmenu'. If context menu is available for this
* file, the top element will receive the additional class 'fp-hascontextmenu' and
* the element with class 'fp-contextmenu' will hold onclick event for displaying
* the context menu.
*
* @todo MDL-32736 remove onclick="return false;"
* @see fp_js_template_listfilename()
* @return string
*/
protected function fm_js_template_listfilename() {
$rv = '
<span class="fp-filename-icon">
<a href="#">
<span class="fp-icon"></span>
<span class="fp-reficons1"></span>
<span class="fp-reficons2"></span>
<span class="fp-filename"></span>
</a>
<a class="fp-contextmenu" href="#" onclick="return false;">' . $this->pix_icon('i/menu', '▶') . '</a>
</span>';
return $rv;
}
/**
* fileresponsesimplifiedfilemanager JS template for displaying 'Make new folder' dialog.
*
* Must be wrapped in an element, CSS for this element must define width and height of the
* window;
*
* Must have one input element with type="text" (for users to enter the new folder name);
*
* content of element with class 'fp-dlg-curpath' will be replaced with current path where
* new folder is about to be created;
* elements with classes 'fp-dlg-butcreate' and 'fp-dlg-butcancel' will hold onclick events;
*
* @return string
*/
protected function fm_js_template_mkdir() {
$rv = '
<div class="filemanager fp-mkdir-dlg" role="dialog" aria-live="assertive" aria-labelledby="fp-mkdir-dlg-title">
<div class="fp-mkdir-dlg-text">
<label id="fp-mkdir-dlg-title">' . get_string('newfoldername', 'repository') . '</label><br/>
<input type="text" />
</div>
<button class="fp-dlg-butcreate btn-primary btn">' . get_string('makeafolder') . '</button>
<button class="fp-dlg-butcancel btn-cancel btn">' . get_string('cancel') . '</button>
</div>';
return $rv;
}
/**
* fileresponsesimplifiedfilemanager JS template for error/info message displayed as a separate popup window.
*
* @see fp_js_template_message()
* @return string
*/
protected function fm_js_template_message() {
return $this->fp_js_template_message();
}
/**
* fileresponsesimplifiedfilemanager JS template for window with file information/actions.
*
* All content must be enclosed in one element, CSS for this class must define width and
* height of the window;
*
* Thumbnail image will be added as content to the element with class 'fp-thumbnail';
*
* Inside the window the elements with the following classnames must be present:
* 'fp-saveas', 'fp-author', 'fp-license', 'fp-path'. Inside each of them must be
* one input element (or select in case of fp-license and fp-path). They may also have labels.
* The elements will be assign with class 'uneditable' and input/select element will become
* disabled if they are not applicable for the particular file;
*
* There may be present elements with classes 'fp-original', 'fp-datemodified',
* 'fp-datecreated',
* 'fp-size', 'fp-dimensions', 'fp-reflist'. They will receive additional class 'fp-unknown' if
* information is unavailable. If there is information available, the content of embedded
* element with class 'fp-value' will be substituted with the value;
*
* The value of Original ('fp-original') is loaded in separate request. When it is applicable
* but not yet loaded the 'fp-original' element receives additional class 'fp-loading';
*
* The value of 'Aliases/Shortcuts' ('fp-reflist') is also loaded in separate request. When it
* is applicable but not yet loaded the 'fp-original' element receives additional class
* 'fp-loading'. The string explaining that XX references exist will replace content of element
* 'fp-refcount'. Inside '.fp-reflist .fp-value' each reference will be enclosed in <li>;
*
* Elements with classes 'fp-file-update', 'fp-file-download', 'fp-file-delete', 'fp-file-zip',
* 'fp-file-unzip', 'fp-file-setmain' and 'fp-file-cancel' will hold corresponding onclick
* events (there may be several elements with class 'fp-file-cancel');
*
* When confirm button is pressed and file is being selected, the top element receives
* additional class 'loading'. It is removed when response from server is received.
*
* When any of the input fields is changed, the top element receives class 'fp-changed';
* When current file can be set as main - top element receives class 'fp-cansetmain';
* When current file is folder/zip/file - top element receives respectfully class
* 'fp-folder'/'fp-zip'/'fp-file';
*
* @return string
*/
protected function fm_js_template_fileselectlayout() {
$strloading = get_string('loading', 'repository');
$iconprogress = $this->pix_icon('i/loading_small', $strloading) . '';
$rv = '
<div class="filemanager fp-select">
<div class="fp-select-loading">
' . $this->pix_icon('i/loading_small', '') . '
</div>
<form class="mform clearfix">
<div class="form-group mx-0">
<!-- <button class="fp-file-download btn btn-secondary">' . get_string('download') . '</button> -->
<button class="fp-file-delete btn btn-secondary">' . get_string('delete') . '</button>
<button class="fp-file-setmain btn btn-secondary">' .
get_string('setmainfile', 'repository') .
'</button>
<span class="fp-file-setmain-help">' .
$this->output->help_icon('setmainfile', 'repository') . '</span>
<button class="fp-file-zip btn btn-secondary">' .
get_string('zip', 'editor') . '</button>
<button class="fp-file-unzip btn btn-secondary">' . get_string('unzip') . '</button>
</div>
<div class="fp-saveas form-group row mx-0">
<label class="form-control-label col-4 px-0">' .
get_string('name', 'repository') . '</label>
<label class="col-8 form-inline">
<input class="form-control" type="text"/>
</label>
</div>
<!-- don\'t download fix: eliminate author field-->
<!-- <div class="fp-author form-group row mx-0">
<label class="form-control-label col-4 px-0">' .
get_string('author', 'repository') .
'</label>
<div class="col-8 form-inline">
<input class="form-control" type="text"/>
</div>
</div> -->
<!-- don\'t download fix: eliminate license field-->
<!-- <div class="fp-license form-group row mx-0">
<label class="form-control-label col-4 px-0">' .
get_string('chooselicense', 'repository') . '</label>
<div class="col-8 form-inline pr-0">
<select class="custom-select form-control"></select>
</div>
</div> -->
<!-- don\'t download fix: eliminate path field-->
<!-- <div class="fp-path form-group row mx-0">
<label class="form-control-label col-4 px-0">' .
get_string('path', 'repository') . '</label>
<div class="col-8 form-inline pr-0">
<select class="custom-select form-control"></select>
</div>