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 if (!is_array($this->getAttributes()) ||
!array_key_exists('size', $this->getAttributes())) {
67 $this->updateAttributes(array('size' => 48));
70 $this->_elements
[0] =& MoodleQuickForm
::createElement('text', 'value', '', $this->getAttributes());
71 $this->_elements
[1] =& MoodleQuickForm
::createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
73 $button =& $this->_elements
[1];
75 if ($this->_options
['courseid']!==null){
76 $courseid=$this->_options
['courseid'];
78 $courseid=$COURSE->id
;
80 // first find out the text field id - this is a bit hacky, is there a better way?
81 $choose = 'id_'.str_replace(array('[', ']'), array('_', ''), $this->getElementName(0));
82 $url="/files/index.php?id=$courseid&choose=".$choose;
84 if ($this->_options
['options'] == 'none') {
85 $options = 'menubar=0,location=0,scrollbars,resizable,width='. $this->_options
['width'] .',height='. $this->_options
['height'];
87 $options = $this->_options
['options'];
91 $buttonattributes = array('title'=>get_string("chooseafile", "resource"),
92 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
94 $button->updateAttributes($buttonattributes);
96 /// With repository active, show the button to browse it
97 if (isset($CFG->repositoryactivate
) && $CFG->repositoryactivate
) {
98 $this->_elements
[2] =& MoodleQuickForm
::createElement('button', 'imsrepo', get_string('browserepository', 'resource'));
99 $imsbutton =& $this->_elements
[2];
100 $url = "/mod/resource/type/ims/finder.php?directory=&choose=".$choose;
101 $buttonattributes = array('title'=>get_string("browserepository", "resource"),
102 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
103 $imsbutton->updateAttributes($buttonattributes);
107 * Output a timestamp. Give it the name of the group.
109 * @param array $submitValues
113 function exportValue(&$submitValues, $assoc = false)
116 $valuearray = $this->_elements
[0]->exportValue($submitValues[$this->getName()], true);
117 $value[$this->getName()]=$valuearray['value'];
123 * Called by HTML_QuickForm whenever form event is made on this element
125 * @param string $event Name of event
126 * @param mixed $arg event arguments
127 * @param object $caller calling object
132 function onQuickFormEvent($event, $arg, &$caller)
136 // constant values override both default and submitted ones
137 // default values are overriden by submitted
138 $value = $this->_findValue($caller->_constantValues
);
139 if (null === $value) {
140 $value = $this->_findValue($caller->_submitValues
);
141 if (null === $value) {
142 $value = $this->_findValue($caller->_defaultValues
);
145 if (!is_array($value)) {
146 $value = array('value' => $value);
148 if (null !== $value) {
149 $this->setValue($value);
154 return parent
::onQuickFormEvent($event, $arg, $caller);
156 } // end func onQuickFormEvent