From: skodak Date: Thu, 11 Sep 2008 23:11:24 +0000 (+0000) Subject: MDL-15906 file picker improvements - it now keeps current file after failed server... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b8d9c71915247f57579d0488362d40e190fd1f16;p=moodle.git MDL-15906 file picker improvements - it now keeps current file after failed server side validation or submission (input element used instead of button element) --- diff --git a/lib/form/filepicker.php b/lib/form/filepicker.php index 88f6c4452c..1efc69dffb 100644 --- a/lib/form/filepicker.php +++ b/lib/form/filepicker.php @@ -12,34 +12,33 @@ require_once(dirname(dirname(dirname(__FILE__))) . '/repository/lib.php'); * @since Moodle 2.0 * @access public */ -class MoodleQuickForm_filepicker extends HTML_QuickForm_button { +class MoodleQuickForm_filepicker extends HTML_QuickForm_input { var $_helpbutton=''; - 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); + function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attributes=null) { + parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes); } - function setHelpButton($helpbuttonargs, $function='helpbutton'){ - if (!is_array($helpbuttonargs)){ - $helpbuttonargs=array($helpbuttonargs); - }else{ - $helpbuttonargs=$helpbuttonargs; + function setHelpButton($helpbuttonargs, $function='helpbutton') { + if (!is_array($helpbuttonargs)) { + $helpbuttonargs = array($helpbuttonargs); + } else { + $helpbuttonargs = $helpbuttonargs; } //we do this to to return html instead of printing it //without having to specify it in every call to make a button. if ('helpbutton' == $function){ - $defaultargs=array('', '', 'moodle', true, false, '', true); - $helpbuttonargs=$helpbuttonargs + $defaultargs ; + $defaultargs = array('', '', 'moodle', true, false, '', true); + $helpbuttonargs = $helpbuttonargs + $defaultargs ; } $this->_helpbutton=call_user_func_array($function, $helpbuttonargs); } - function getHelpButton(){ + function getHelpButton() { return $this->_helpbutton; } - function getElementTemplateType(){ + function getElementTemplateType() { if ($this->_flagFrozen){ return 'nodisplay'; } else { @@ -48,47 +47,56 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_button { } function toHtml() { - global $CFG, $COURSE; + global $CFG, $COURSE, $USER; + if ($this->_flagFrozen) { return $this->getFrozenHtml(); - } else { - $currentvalue = $this->getValue(); + } - $strsaved = get_string('filesaved', 'repository'); - if (empty($COURSE->context)) { - $context = get_context_instance(CONTEXT_SYSTEM); - } else { - $context = $COURSE->context; + $currentfile = ''; + $draftvalue = ''; + if ($draftid = (int)$this->getValue()) { + $fs = get_file_storage(); + $usercontext = get_context_instance(CONTEXT_USER, $USER->id); + if ($files = $fs->get_area_files($usercontext->id, 'user_draft', $draftid, '', false)) { + $file = reset($files); + $currentfile = $file->get_filename(); + $draftvalue = 'value="'.$draftid.'"'; } - $repository_info = repository_get_client($context); - $suffix = $repository_info['suffix']; + } + $strsaved = get_string('filesaved', 'repository'); + if ($COURSE->id == SITEID) { + $context = get_context_instance(CONTEXT_SYSTEM); + } else { + $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); + } + $repository_info = repository_get_client($context); + $suffix = $repository_info['suffix']; - $id = $this->_attributes['id']; - $elname = $this->_attributes['name']; + $id = $this->_attributes['id']; + $elname = $this->_attributes['name']; - $str = $this->_getTabs(); - $str .= ''; - $id = $this->_attributes['id']; + $str = $this->_getTabs(); + $str .= ''; - $str .= << -function updatefile_$suffix(str){ +function updatefile_$suffix(str) { document.getElementById('repo_info_$suffix').innerHTML = str; } -function callpicker_$suffix(){ +function callpicker_$suffix() { document.body.className += ' yui-skin-sam'; var picker = document.createElement('DIV'); picker.id = 'file-picker-$suffix'; - picker.className = "file-picker"; + picker.className = 'file-picker'; document.body.appendChild(picker); - var el=document.getElementById('${id}_${suffix}'); - openpicker_$suffix({"env":"form", 'target':el, 'callback':updatefile_$suffix}) + var el=document.getElementById('$id'); + openpicker_$suffix({'env':'form', 'target':el, 'callback':updatefile_$suffix}) } EOD; - $str .= ''.''.$currentvalue.''.$repository_info['css'].$repository_info['js']; - return $str; - } + $str .= ''.''.$currentfile.''.$repository_info['css'].$repository_info['js']; + return $str; } function exportValue(&$submitValues, $assoc = false) {