timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / form / choosecoursefile.php
blobab0e9216629910fe2fffbc73d70440238e4d584a
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 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'];
79 } else {
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'];
88 }else{
89 $options = $this->_options['options'];
91 $fullscreen = 0;
93 $buttonattributes = array('title'=>get_string("chooseafile", "resource"),
94 'onclick'=>"return openpopup('$url', '".$button->getName()."', '$options', $fullscreen);");
96 $button->updateAttributes($buttonattributes);
99 /**
100 * Output a timestamp. Give it the name of the group.
102 * @param array $submitValues
103 * @param bool $assoc
104 * @return array
106 function exportValue(&$submitValues, $assoc = false)
108 $value = null;
109 $valuearray = $this->_elements[0]->exportValue($submitValues[$this->getName()], true);
110 $value[$this->getName()]=$valuearray['value'];
111 return $value;
113 // }}}
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
121 * @since 1.0
122 * @access public
123 * @return void
125 function onQuickFormEvent($event, $arg, &$caller)
127 switch ($event) {
128 case 'updateValue':
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);
144 return true;
145 break;
147 return parent::onQuickFormEvent($event, $arg, $caller);
149 } // end func onQuickFormEvent