Added .gitignore
[moodle-linuxchix.git] / lib / form / choosecoursefile.php
blob4ac002b48c30f7d8a68f676723afa21088c8a78f
1 <?php //$Id$
2 global $CFG;
3 require_once "$CFG->libdir/form/group.php";
5 /**
6 * Class for an element used to choose a file from the course files folder.
9 * @author Jamie Pratt <me@jamiep.org>
10 * @access public
12 class MoodleQuickForm_choosecoursefile extends MoodleQuickForm_group
14 /**
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');
26 /**
27 * These complement separators, they are appended to the resultant HTML
28 * @access private
29 * @var array
31 var $_wrap = array('', '');
33 /**
34 * Class constructor
36 * @access public
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);
53 } else {
54 $this->_options[$name] = $value;
61 // }}}
62 // {{{ _createElements()
64 function _createElements() {
65 global $COURSE;
66 $this->_elements = array();
68 $this->_elements[0] =& MoodleQuickForm::createElement('text', 'value', '', array('size'=>'48'));
69 $this->_elements[1] =& MoodleQuickForm::createElement('button', 'popup', get_string('chooseafile', 'resource') .' ...');
71 $button =& $this->_elements[1];
73 if ($this->_options['courseid']!==null){
74 $courseid=$this->_options['courseid'];
75 } else {
76 $courseid=$COURSE->id;
78 // first find out the text field id - this is a bit hacky, is there a better way?
79 $choose = 'id_'.str_replace(array('[', ']'), array('_', ''), $this->getElementName(0));
80 $url="/files/index.php?id=$courseid&choose=".$choose;
82 if ($this->_options['options'] == 'none') {
83 $options = 'menubar=0,location=0,scrollbars,resizable,width='. $this->_options['width'] .',height='. $this->_options['height'];
84 }else{
85 $options = $this->_options['options'];
87 $fullscreen = 0;
89 $buttonattributes = array('title'=>get_string("chooseafile", "resource"),
90 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
92 $button->updateAttributes($buttonattributes);
94 /**
95 * Output a timestamp. Give it the name of the group.
97 * @param array $submitValues
98 * @param bool $assoc
99 * @return array
101 function exportValue(&$submitValues, $assoc = false)
103 $value = null;
104 $valuearray = $this->_elements[0]->exportValue($submitValues[$this->getName()], true);
105 $value[$this->getName()]=$valuearray['value'];
106 return $value;
108 // }}}
111 * Called by HTML_QuickForm whenever form event is made on this element
113 * @param string $event Name of event
114 * @param mixed $arg event arguments
115 * @param object $caller calling object
116 * @since 1.0
117 * @access public
118 * @return void
120 function onQuickFormEvent($event, $arg, &$caller)
122 switch ($event) {
123 case 'updateValue':
124 // constant values override both default and submitted ones
125 // default values are overriden by submitted
126 $value = $this->_findValue($caller->_constantValues);
127 if (null === $value) {
128 $value = $this->_findValue($caller->_submitValues);
129 if (null === $value) {
130 $value = $this->_findValue($caller->_defaultValues);
133 if (!is_array($value)) {
134 $value = array('value' => $value);
136 if (null !== $value) {
137 $this->setValue($value);
139 return true;
140 break;
142 return parent::onQuickFormEvent($event, $arg, $caller);
144 } // end func onQuickFormEvent