MDL-23726 fixed phpdocs - credit goes to Henning Bostelmann
[moodle.git] / lib / form / selectwithlink.php
blob5e697a7d81580e21e73092b65744d84b67533f45
1 <?php
2 require_once('HTML/QuickForm/select.php');
4 /**
5 * HTML class for a select type element
7 * @author Jamie Pratt
8 * @access public
9 */
10 class MoodleQuickForm_selectwithlink extends HTML_QuickForm_select{
11 /**
12 * html for help button, if empty then no help
14 * @var string
16 var $_helpbutton='';
17 var $_hiddenLabel=false;
18 var $_link=null;
19 var $_linklabel=null;
20 var $_linkreturn=null;
22 function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null)
24 if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
25 $this->_link = $linkdata['link'];
26 $this->_linklabel = $linkdata['label'];
29 if (!empty($linkdata['return'])) {
30 $this->_linkreturn = $linkdata['return'];
33 parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
34 } //end constructor
36 function setHiddenLabel($hiddenLabel){
37 $this->_hiddenLabel = $hiddenLabel;
39 function toHtml(){
40 $retval = '';
41 if ($this->_hiddenLabel){
42 $this->_generateId();
43 $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
44 $this->getLabel().'</label>'.parent::toHtml();
45 } else {
46 $retval = parent::toHtml();
49 if (!empty($this->_link)) {
50 if (!empty($this->_linkreturn) && is_array($this->_linkreturn)) {
51 $appendchar = '?';
52 if (strstr($this->_link, '?')) {
53 $appendchar = '&amp;';
56 foreach ($this->_linkreturn as $key => $val) {
57 $this->_link .= $appendchar."$key=$val";
58 $appendchar = '&amp;';
62 $retval .= '<a style="margin-left: 5px" href="'.$this->_link.'">'.$this->_linklabel.'</a>';
65 return $retval;
67 /**
68 * Automatically generates and assigns an 'id' attribute for the element.
70 * Currently used to ensure that labels work on radio buttons and
71 * checkboxes. Per idea of Alexander Radivanovich.
72 * Overriden in moodleforms to remove qf_ prefix.
74 * @access private
75 * @return void
77 function _generateId()
79 static $idx = 1;
81 if (!$this->getAttribute('id')) {
82 $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
84 } // end func _generateId
85 /**
86 * set html for help button
88 * @access public
89 * @param array $help array of arguments to make a help button
90 * @param string $function function name to call to get html
92 function setHelpButton($helpbuttonargs, $function='helpbutton'){
93 if (!is_array($helpbuttonargs)){
94 $helpbuttonargs=array($helpbuttonargs);
95 }else{
96 $helpbuttonargs=$helpbuttonargs;
98 //we do this to to return html instead of printing it
99 //without having to specify it in every call to make a button.
100 if ('helpbutton' == $function){
101 $defaultargs=array('', '', 'moodle', true, false, '', true);
102 $helpbuttonargs=$helpbuttonargs + $defaultargs ;
104 $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
107 * get html for help button
109 * @access public
110 * @return string html for help button
112 function getHelpButton(){
113 return $this->_helpbutton;
116 * Removes an OPTION from the SELECT
118 * @param string $value Value for the OPTION to remove
119 * @since 1.0
120 * @access public
121 * @return void
123 function removeOption($value)
125 $key=array_search($value, $this->_values);
126 if ($key!==FALSE and $key!==null) {
127 unset($this->_values[$key]);
129 foreach ($this->_options as $key=>$option){
130 if ($option['attr']['value']==$value){
131 unset($this->_options[$key]);
132 return;
135 } // end func removeOption
137 * Removes all OPTIONs from the SELECT
139 * @param string $value Value for the OPTION to remove
140 * @since 1.0
141 * @access public
142 * @return void
144 function removeOptions()
146 $this->_options = array();
147 } // end func removeOption
149 * Slightly different container template when frozen. Don't want to use a label tag
150 * with a for attribute in that case for the element label but instead use a div.
151 * Templates are defined in renderer constructor.
153 * @return string
155 function getElementTemplateType(){
156 if ($this->_flagFrozen){
157 return 'static';
158 } else {
159 return 'default';