3 require_once "$CFG->libdir/form/group.php";
6 * Class for an element used to choose a file from the course files folder or
7 * from the local repository used by the IMS CP resource type.
10 class MoodleQuickForm_choosecoursefileorimsrepo
extends MoodleQuickForm_group
13 * Options for element :
15 * $url must be relative to home page eg /mod/survey/stuff.php
16 * courseid => int course id if null then uses $COURSE global
17 * width => int Height to assign to popup window
18 * title => string Text to be displayed as popup page title
19 * options => string List of additional options for popup window
21 var $_options = array('courseid'=>null,
22 'height'=>500, 'width'=>750, 'options'=>'none');
25 * These complement separators, they are appended to the resultant HTML
29 var $_wrap = array('', '');
35 * @param string Element's name
36 * @param mixed Label(s) for an element
37 * @param array Options to control the element's display
38 * @param mixed Either a typical HTML attribute string or an associative array
40 function MoodleQuickForm_choosecoursefileorimsrepo($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
42 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
43 $this->_appendName
= true;
44 $this->_type
= 'choosecoursefileorimsrepo';
45 // set the options, do not bother setting bogus ones
46 if (is_array($options)) {
47 foreach ($options as $name => $value) {
48 if (isset($this->_options
[$name])) {
49 if (is_array($value) && is_array($this->_options
[$name])) {
50 $this->_options
[$name] = @array_merge
($this->_options
[$name], $value);
52 $this->_options
[$name] = $value;
60 // {{{ _createElements()
62 function _createElements() {
64 $this->_elements
= array();
66 $this->_elements
[0] =& MoodleQuickForm
::createElement('text', 'value', '', array('size'=>'48'));
67 $this->_elements
[1] =& MoodleQuickForm
::createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
69 $button =& $this->_elements
[1];
71 if ($this->_options
['courseid']!==null){
72 $courseid=$this->_options
['courseid'];
74 $courseid=$COURSE->id
;
76 // first find out the text field id - this is a bit hacky, is there a better way?
77 $choose = 'id_'.str_replace(array('[', ']'), array('_', ''), $this->getElementName(0));
78 $url="/files/index.php?id=$courseid&choose=".$choose;
80 if ($this->_options
['options'] == 'none') {
81 $options = 'menubar=0,location=0,scrollbars,resizable,width='. $this->_options
['width'] .',height='. $this->_options
['height'];
83 $options = $this->_options
['options'];
87 $buttonattributes = array('title'=>get_string("chooseafile", "resource"),
88 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
90 $button->updateAttributes($buttonattributes);
92 /// With repository active, show the button to browse it
93 if (isset($CFG->repositoryactivate
) && $CFG->repositoryactivate
) {
94 $this->_elements
[2] =& MoodleQuickForm
::createElement('button', 'imsrepo', get_string('browserepository', 'resource'));
95 $imsbutton =& $this->_elements
[2];
96 $url = "/mod/resource/type/ims/finder.php?directory=&choose=".$choose;
97 $buttonattributes = array('title'=>get_string("browserepository", "resource"),
98 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
99 $imsbutton->updateAttributes($buttonattributes);
103 * Output a timestamp. Give it the name of the group.
105 * @param array $submitValues
109 function exportValue(&$submitValues, $assoc = false)
112 $valuearray = $this->_elements
[0]->exportValue($submitValues[$this->getName()], true);
113 $value[$this->getName()]=$valuearray['value'];
119 * Called by HTML_QuickForm whenever form event is made on this element
121 * @param string $event Name of event
122 * @param mixed $arg event arguments
123 * @param object $caller calling object
128 function onQuickFormEvent($event, $arg, &$caller)
132 // constant values override both default and submitted ones
133 // default values are overriden by submitted
134 $value = $this->_findValue($caller->_constantValues
);
135 if (null === $value) {
136 $value = $this->_findValue($caller->_submitValues
);
137 if (null === $value) {
138 $value = $this->_findValue($caller->_defaultValues
);
141 if (!is_array($value)) {
142 $value = array('value' => $value);
144 if (null !== $value) {
145 $this->setValue($value);
150 return parent
::onQuickFormEvent($event, $arg, $caller);
152 } // end func onQuickFormEvent