3 require_once "$CFG->libdir/form/group.php";
6 * Class for an element used to choose a file from the course files folder.
9 * @author Jamie Pratt <me@jamiep.org>
12 class MoodleQuickForm_choosecoursefile
extends MoodleQuickForm_group
15 * Options for element :
17 * $url must be relative to home page eg /mod/survey/stuff.php
18 * courseid => int course id if null then uses $COURSE global
19 * width => int Height to assign to popup window
20 * title => string Text to be displayed as popup page title
21 * options => string List of additional options for popup window
23 var $_options = array('courseid'=>null,
24 'height'=>500, 'width'=>750, 'options'=>'none');
27 * These complement separators, they are appended to the resultant HTML
31 var $_wrap = array('', '');
37 * @param string Element's name
38 * @param mixed Label(s) for an element
39 * @param array Options to control the element's display
40 * @param mixed Either a typical HTML attribute string or an associative array
42 function MoodleQuickForm_choosecoursefile($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
44 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
45 $this->_appendName
= true;
46 $this->_type
= 'choosecoursefile';
47 // set the options, do not bother setting bogus ones
48 if (is_array($options)) {
49 foreach ($options as $name => $value) {
50 if (isset($this->_options
[$name])) {
51 if (is_array($value) && is_array($this->_options
[$name])) {
52 $this->_options
[$name] = @array_merge
($this->_options
[$name], $value);
54 $this->_options
[$name] = $value;
62 // {{{ _createElements()
64 function _createElements() {
66 $this->_elements
= array();
68 if (!is_array($this->getAttributes()) ||
!array_key_exists('size', $this->getAttributes())) {
69 $this->updateAttributes(array('size' => 48));
72 $this->_elements
[0] =& MoodleQuickForm
::createElement('text', 'value', '', $this->getAttributes());
73 $this->_elements
[1] =& MoodleQuickForm
::createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
75 $button =& $this->_elements
[1];
77 if ($this->_options
['courseid']!==null){
78 $courseid=$this->_options
['courseid'];
80 $courseid=$COURSE->id
;
82 // first find out the text field id - this is a bit hacky, is there a better way?
83 $choose = 'id_'.str_replace(array('[', ']'), array('_', ''), $this->getElementName(0));
84 $url="/files/index.php?id=$courseid&choose=".$choose;
86 if ($this->_options
['options'] == 'none') {
87 $options = 'menubar=0,location=0,scrollbars,resizable,width='. $this->_options
['width'] .',height='. $this->_options
['height'];
89 $options = $this->_options
['options'];
93 $buttonattributes = array('title'=>get_string("chooseafile", "resource"),
94 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
96 $button->updateAttributes($buttonattributes);
100 * Output a timestamp. Give it the name of the group.
102 * @param array $submitValues
106 function exportValue(&$submitValues, $assoc = false)
109 $valuearray = $this->_elements
[0]->exportValue($submitValues[$this->getName()], true);
110 $value[$this->getName()]=$valuearray['value'];
116 * Called by HTML_QuickForm whenever form event is made on this element
118 * @param string $event Name of event
119 * @param mixed $arg event arguments
120 * @param object $caller calling object
125 function onQuickFormEvent($event, $arg, &$caller)
129 // constant values override both default and submitted ones
130 // default values are overriden by submitted
131 $value = $this->_findValue($caller->_constantValues
);
132 if (null === $value) {
133 $value = $this->_findValue($caller->_submitValues
);
134 if (null === $value) {
135 $value = $this->_findValue($caller->_defaultValues
);
138 if (!is_array($value)) {
139 $value = array('value' => $value);
141 if (null !== $value) {
142 $this->setValue($value);
147 return parent
::onQuickFormEvent($event, $arg, $caller);
149 } // end func onQuickFormEvent