timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / lib / form / choosecoursefileorimsrepo.php
blob0429282f86eb981d482579e2c78f975157955358
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 or
7 * from the local repository used by the IMS CP resource type.
9 */
10 class MoodleQuickForm_choosecoursefileorimsrepo extends MoodleQuickForm_group
12 /**
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');
24 /**
25 * These complement separators, they are appended to the resultant HTML
26 * @access private
27 * @var array
29 var $_wrap = array('', '');
31 /**
32 * Class constructor
34 * @access public
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);
51 } else {
52 $this->_options[$name] = $value;
59 // }}}
60 // {{{ _createElements()
62 function _createElements() {
63 global $CFG, $COURSE;
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'];
77 } else {
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'];
86 }else{
87 $options = $this->_options['options'];
89 $fullscreen = 0;
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
110 * @param bool $assoc
111 * @return array
113 function exportValue(&$submitValues, $assoc = false)
115 $value = null;
116 $valuearray = $this->_elements[0]->exportValue($submitValues[$this->getName()], true);
117 $value[$this->getName()]=$valuearray['value'];
118 return $value;
120 // }}}
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
128 * @since 1.0
129 * @access public
130 * @return void
132 function onQuickFormEvent($event, $arg, &$caller)
134 switch ($event) {
135 case 'updateValue':
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);
151 return true;
152 break;
154 return parent::onQuickFormEvent($event, $arg, $caller);
156 } // end func onQuickFormEvent