fixed wording - MDL22441 - renamed the modules 'system' heading to 'activities'
[moodle.git] / lib / form / filepicker.php
CommitLineData
c5704ec6 1<?php
ae4a98a1 2
3global $CFG;
c5704ec6 4
5require_once("HTML/QuickForm/button.php");
ae4a98a1 6require_once($CFG->dirroot.'/repository/lib.php');
c5704ec6 7
8/**
757f30a2 9 * HTML class for a single filepicker element (based on button)
c5704ec6 10 *
757f30a2 11 * @author Moodle.com
c5704ec6 12 * @version 1.0
13 * @since Moodle 2.0
14 * @access public
15 */
b8d9c719 16class MoodleQuickForm_filepicker extends HTML_QuickForm_input {
12f11f23 17 public $_helpbutton = '';
99eaca9d 18 protected $_options = array('maxbytes'=>0, 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL);
b7335412 19
9d54b8cd 20 function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
21 global $CFG;
4287fc0d 22
9d54b8cd 23 $options = (array)$options;
24 foreach ($options as $name=>$value) {
25 if (array_key_exists($name, $this->_options)) {
26 $this->_options[$name] = $value;
27 }
28 }
29 if (!empty($options['maxbytes'])) {
30 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
31 }
b8d9c719 32 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
43023002 33 }
4287fc0d 34
b8d9c719 35 function setHelpButton($helpbuttonargs, $function='helpbutton') {
4bcc5118 36 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
c5704ec6 37 }
4287fc0d 38
b8d9c719 39 function getHelpButton() {
c5704ec6 40 return $this->_helpbutton;
41 }
4287fc0d 42
b8d9c719 43 function getElementTemplateType() {
c5704ec6 44 if ($this->_flagFrozen){
45 return 'nodisplay';
46 } else {
47 return 'default';
48 }
49 }
4287fc0d 50
c5704ec6 51 function toHtml() {
e4e7044a 52 global $CFG, $COURSE, $USER, $PAGE;
b8d9c719 53
dd070162 54
c5704ec6 55 if ($this->_flagFrozen) {
56 return $this->getFrozenHtml();
b8d9c719 57 }
4287fc0d 58
ab9cdbb9 59 $strsaved = get_string('filesaved', 'repository');
60 $straddfile = get_string('openpicker', 'repository');
b8d9c719 61 $currentfile = '';
ae4a98a1 62 if ($draftitemid = (int)$this->getValue()) {
b8d9c719 63 $fs = get_file_storage();
64 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
ae4a98a1 65 if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id DESC', false)) {
b8d9c719 66 $file = reset($files);
67 $currentfile = $file->get_filename();
b6558c3b 68 }
2289c0e4 69 } else {
70 // no existing area info provided - let's use fresh new draft area
842f2914
PS
71 $draftitemid = file_get_unused_draft_itemid();
72 $this->setValue($draftitemid);
b8d9c719 73 }
b8d9c719 74 if ($COURSE->id == SITEID) {
75 $context = get_context_instance(CONTEXT_SYSTEM);
76 } else {
77 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
78 }
99eaca9d 79
e189ec00 80 $client_id = uniqid();
4287fc0d 81
99eaca9d
DC
82 $args = new stdclass;
83 // need these three to filter repositories list
84 $args->accepted_types = $this->_options['accepted_types']?$this->_options['accepted_types']:'*';
85 $args->return_types = FILE_INTERNAL;
86 $args->context = $PAGE->context;
99eaca9d
DC
87
88 $options = initialise_filepicker($args);
89
90 $options->client_id = $client_id;
91 $options->maxbytes = $this->_options['maxbytes'];
92 $options->maxfiles = 1;
0c4edaa2
DC
93 $options->env = 'filepicker';
94 $options->itemid = $draftitemid;
99eaca9d 95
b8d9c719 96 $id = $this->_attributes['id'];
97 $elname = $this->_attributes['name'];
4287fc0d 98
b8d9c719 99 $str = $this->_getTabs();
842f2914 100 $str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'" value="'.$draftitemid.'" />';
757f30a2 101
b8d9c719 102 $str .= <<<EOD
4ba3169d 103<div id="filepicker-wrapper-{$client_id}" style="display:none">
104 <div class="filemanager-toolbar">
99eaca9d 105 <a href="###" id="filepicker-button-{$client_id}">$straddfile</a>
4ba3169d 106 </div>
107 <div id="file_info_{$client_id}" class="mdl-left">$currentfile</div>
108</div>
99eaca9d 109<!-- non javascript file picker -->
ab9cdbb9 110<noscript>
99eaca9d
DC
111<object type="text/html" id="nonjs-filepicker-{$client_id}" data="{$CFG->httpswwwroot}/repository/filepicker.php?env=filepicker&amp;action=embedded&amp;itemid={$draftitemid}&amp;ctx_id=$context->id" height="300" width="800" style="border:1px solid #000">
112Moodle File Picker
113</object>
ab9cdbb9 114</noscript>
d8eb6e18 115EOD;
4c508047 116 $module = array('name'=>'form_filepicker', 'fullpath'=>'/lib/form/filepicker.js', 'requires'=>array('core_filepicker'));
4c508047 117 $PAGE->requires->js_init_call('M.form_filepicker.init', array($options), true, $module);
b8d9c719 118 return $str;
c5704ec6 119 }
4287fc0d 120
21599d8c 121 function exportValue(&$submitValues, $assoc = false) {
b7335412 122 global $USER;
123
124 // make sure max one file is present and it is not too big
ae4a98a1 125 if ($draftitemid = $submitValues[$this->_attributes['name']]) {
b7335412 126 $fs = get_file_storage();
127 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
ae4a98a1 128 if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id DESC', false)) {
b7335412 129 $file = array_shift($files);
130 if ($this->_options['maxbytes'] and $file->get_filesize() > $this->_options['maxbytes']) {
131 // bad luck, somebody tries to sneak in oversized file
132 $file->delete();
133 }
134 foreach ($files as $file) {
135 // only one file expected
136 $file->delete();
137 }
138 }
139 }
140
21599d8c 141 return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
142 }
c5704ec6 143}