* @since Moodle 2.0
* @access public
*/
-class MoodleQuickForm_filepicker extends HTML_QuickForm_button
-{
- var $_filearea;
+class MoodleQuickForm_filepicker extends HTML_QuickForm_button {
var $_helpbutton='';
-
- function MoodleQuickForm_filepicker($elementName=null, $value=null, $filearea=null, $attributes=null) {
+
+ function MoodleQuickForm_filepicker($elementName=null, $value=null, $attributes=null) {
HTML_QuickForm_input::HTML_QuickForm_input($elementName, $value, $attributes); // Set label cause button doesn't
parent::HTML_QuickForm_button($elementName, $value, $attributes);
- $this->_filearea = $filearea;
}
+
function setHelpButton($helpbuttonargs, $function='helpbutton'){
if (!is_array($helpbuttonargs)){
$helpbuttonargs=array($helpbuttonargs);
}
$this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
}
+
function getHelpButton(){
return $this->_helpbutton;
}
+
function getElementTemplateType(){
if ($this->_flagFrozen){
return 'nodisplay';
return 'default';
}
}
+
function toHtml() {
global $CFG, $COURSE;
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
} else {
$strsaved = get_string('filesaved', 'repository');
- if(empty($COURSE->context)) {
+
+ if (empty($COURSE->context)) {
$ctx = get_context_instance(CONTEXT_SYSTEM);
} else {
$ctx = $COURSE->context;
}
$ret = repository_get_client($ctx);
+
$suffix = $ret['suffix'];
+ $id = $this->_attributes['id'];
+ $elname = $this->_attributes['name'];
+
$str = $this->_getTabs();
- $str .= '<input type="hidden" value="" name="'.$this->_attributes['name'].'" id="'.$this->_attributes['id'].'_'.$suffix.'" />';
- $id = $this->_attributes['id'];
- $filearea = $this->_filearea;
+ $str .= '<input type="hidden" name="'.$elname.'" id="'.$id.'_'.$suffix.'" value="" />';
$str .= <<<EOD
<script type="text/javascript">
function updatefile_$suffix(str){
picker.id = 'file-picker-$suffix';
document.body.appendChild(picker);
var el=document.getElementById('${id}_${suffix}');
- openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix, 'filearea':"${filearea}"})
+ openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix})
}
</script>
EOD;
// $this->_getAttrString($this->_attributes);
- $str .= "<input name=\"filearea\" value=\"$filearea\" type=\"hidden\">\n";
$str .= '<input value ="'.get_string('openpicker', 'repository').'" type="button" onclick=\'callpicker_'.$suffix.'()\' />'.'<span id="repo_info_'.$suffix.'" style="color:green"></span>'.$ret['css'].$ret['js'];
return $str;
}
}
+
function exportValue(&$submitValues, $assoc = false) {
return array($this->_attributes['name'] => $submitValues[$this->_attributes['name']]);
}
}
/**
- * Internal method. Validates all uploaded files.
+ * Internal method. Validates all old-style uploaded files.
*/
function _validate_files(&$files) {
global $CFG, $COURSE;
$file_val = false;
}
- $data = $mform->exportValues(null, false);
+ $data = $mform->exportValues();
$moodle_val = $this->validation($data, $files);
if ((is_array($moodle_val) && count($moodle_val)!==0)) {
// non-empty array means errors
* @return mixed false in case of failure, string if ok
*/
function get_new_filename($elname=null) {
+ global $USER;
+
if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}
reset($_FILES);
$elname = key($_FILES);
}
+
+ if (empty($elname)) {
+ return false;
+ }
+
+ $element = $this->_form->getElement($elname);
+
+ if ($element instanceof MoodleQuickForm_filepicker) {
+ $values = $this->_form->exportValues($elname);
+ if (empty($values[$elname])) {
+ return false;
+ }
+ $draftid = $values[$elname];
+ $fs = get_file_storage();
+ $context = get_context_instance(CONTEXT_USER, $USER->id);
+ if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
+ return false;
+ }
+ $file = reset($files);
+ return $file->get_filename();
+ }
+
if (!isset($_FILES[$elname])) {
return false;
}
* @return bool success
*/
function save_file($elname, $pathname, $override=false) {
- if (!$this->is_submitted() or !$this->is_validated()) {
- return false;
- }
+ global $USER;
- if (!isset($_FILES[$elname])) {
+ if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}
return false;
}
}
- if (!copy($_FILES[$elname]['tmp_name'], $pathname)) {
- return false;
+
+ $element = $this->_form->getElement($elname);
+
+ if ($element instanceof MoodleQuickForm_filepicker) {
+ $values = $this->_form->exportValues($elname);
+ if (empty($values[$elname])) {
+ return false;
+ }
+ $draftid = $values[$elname];
+ $fs = get_file_storage();
+ $context = get_context_instance(CONTEXT_USER, $USER->id);
+ if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
+ return false;
+ }
+ $file = reset($files);
+
+ return $file->copy_content_to($pathname);
+
+ } else if (isset($_FILES[$elname])) {
+ return copy($_FILES[$elname]['tmp_name'], $pathname);
}
- return true;
+ return false;
}
/**
*/
function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $newfilepath='/',
$newfilename=null, $overwrite=false, $newuserid=null) {
-
global $USER;
if (!$this->is_submitted() or !$this->is_validated()) {
$newuserid = $USER->id;
}
- if (isset($_FILES[$elname])) {
- $filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;
+ $element = $this->_form->getElement($elname);
+ $fs = get_file_storage();
- $fs = get_file_storage();
+ if ($element instanceof MoodleQuickForm_filepicker) {
+ $values = $this->_form->exportValues($elname);
+ if (empty($values[$elname])) {
+ return false;
+ }
+ $draftid = $values[$elname];
+ $context = get_context_instance(CONTEXT_USER, $USER->id);
+ if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
+ return false;
+ }
+ $file = reset($files);
+ if (is_null($newfilename)) {
+ $newfilename = $file->get_filename();
+ }
- if ($file = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
- if ($overwrite) {
- $file->delete();
- } else {
- return false;
+ if ($overwrite) {
+ if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
+ if (!$oldfile->delete()) {
+ return false;
+ }
}
}
- $file_record = new object();
- $file_record->contextid = $newcontextid;
- $file_record->filearea = $newfilearea;
- $file_record->itemid = $newitemid;
- $file_record->filepath = $newfilepath;
- $file_record->filename = $newfilename;
- $file_record->userid = $newuserid;
+ $file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
+ 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
+ return $fs->create_file_from_storedfile($file_record, $file);
- return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
-
- } else { // We check if the file has been uploaded already into the user's draft area
-
- $values = $this->get_data();
-
- if (!empty($values->$elname)) {
-
- $itemid = $values->$elname;
-
- $fs = get_file_storage();
-
- $newfiles = $fs->move_draft_to_final($itemid, $newcontextid, $newfilearea, $newitemid, $newfilepath, $overwrite);
+ } else if (isset($_FILES[$elname])) {
+ $filename = is_null($newfilename) ? $_FILES[$elname]['name'] : $newfilename;
- return array_pop($newfiles);
+ if ($overwrite) {
+ if ($oldfile = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
+ if (!$oldfile->delete()) {
+ return false;
+ }
+ }
}
+
+ $file_record = array('contextid'=>$newcontextid, 'filearea'=>$newfilearea, 'itemid'=>$newitemid,
+ 'filepath'=>$newfilepath, 'filename'=>$newfilename, 'userid'=>$newuserid);
+ return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
}
return false;
* @return mixed false in case of failure, string if ok
*/
function get_file_content($elname) {
+ global $USER;
+
if (!$this->is_submitted() or !$this->is_validated()) {
return false;
}
- if (!isset($_FILES[$elname])) {
- return false;
+ $element = $this->_form->getElement($elname);
+
+ if ($element instanceof MoodleQuickForm_filepicker) {
+ $values = $this->_form->exportValues($elname);
+ if (empty($values[$elname])) {
+ return false;
+ }
+ $draftid = $values[$elname];
+ $fs = get_file_storage();
+ $context = get_context_instance(CONTEXT_USER, $USER->id);
+ if (!$files = $fs->get_area_files($context->id, 'user_draft', $draftid, 'id DESC', false)) {
+ return false;
+ }
+ $file = reset($files);
+
+ return $file->get_content();
+
+ } else if (isset($_FILES[$elname])) {
+ return file_get_contents($_FILES[$elname]['tmp_name']);
}
- return file_get_contents($_FILES[$elname]['tmp_name']);
+ return false;
}
/**
if (PEAR::isError($value)) {
return $value;
}
- $unfiltered[$elementName] = $value;
+ //oh, stock QuickFOrm was returning array of arrays!
+ $unfiltered = HTML_QuickForm::arrayMerge($unfiltered, $value);
}
}