From 053203a8d58b83ee56c983de34a5bceb97d14ff1 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 28 Jul 2009 12:12:24 +0000 Subject: [PATCH] MDL-19756 Migrated choose_from_menu_yesno and choose_from_menu_nested into $OUTPUT->select_menu() --- lib/deprecatedlib.php | 115 +++++++++++++++++++++++++++++++++++++----- lib/outputlib.php | 54 +++++++++++++++++--- lib/weblib.php | 95 ---------------------------------- 3 files changed, 150 insertions(+), 114 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 7d629a1a1c..a0a09681fa 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -2305,7 +2305,7 @@ function blocks_have_content(&$blockmanager, $region) { /** * This was used by old code to print the blocks in a region. - * + * * We don't ever want old code to print blocks, so this is now a no-op. * The function only exists to avoid fatal errors in old code. * @@ -2483,7 +2483,7 @@ function link_to_popup_window ($url, $name=null, $linkname=null, // Parse the $options string $popupparams = array(); - if (!empty($options)) { + if (!empty($options)) { $optionsarray = explode(',', $options); foreach ($optionsarray as $option) { if (strstr($option, '=')) { @@ -2508,7 +2508,7 @@ function link_to_popup_window ($url, $name=null, $linkname=null, if ($return) { return $output; } else { - echo $output; + echo $output; } } @@ -2556,7 +2556,7 @@ function button_to_popup_window ($url, $name=null, $linkname=null, // Parse the $options string $popupparams = array(); - if (!empty($options)) { + if (!empty($options)) { $optionsarray = explode(',', $options); foreach ($optionsarray as $option) { if (strstr($option, '=')) { @@ -2586,7 +2586,7 @@ function button_to_popup_window ($url, $name=null, $linkname=null, if ($return) { return $output; } else { - echo $output; + echo $output; } } @@ -3027,7 +3027,7 @@ function notice_yesno($message, $linkyes, $linkno, $optionsyes=NULL, $optionsno= $formcancel->url = new moodle_url($linkno, $optionsno); $formcancel->button->label = get_string('no'); $formcancel->method = $methodno; - + echo $OUTPUT->confirm($message, $formcontinue, $formcancel); } @@ -3072,9 +3072,13 @@ function print_scale_menu() { function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', $nothingvalue='0', $return=false, $disabled=false, $tabindex=0, $id='', $listbox=false, $multiple=false, $class='') { - + global $OUTPUT; // debugging('choose_from_menu() has been deprecated. Please change your code to use $OUTPUT->select_menu($selectmenu).'); + + if ($script) { + debugging('The $script parameter has been deprecated. You must use component_actions instead', DEBUG_DEVELOPER); + } $selectmenu = new moodle_select_menu(); $selectmenu->options = $options; $selectmenu->name = $name; @@ -3088,20 +3092,107 @@ function choose_from_menu ($options, $name, $selected='', $nothing='choose', $sc $selectmenu->multiple = $multiple; $selectmenu->add_classes($class); - if (!empty($script)) { - $onchange = new component_action('change', $script); - $selectmenu->add_action($onchange); + if ($nothing == 'choose') { + $selectmenu->nothinglabel = ''; + } + + $output = $OUTPUT->select_menu($selectmenu); + + if ($return) { + return $output; + } else { + echo $output; } +} + +/** + * Choose value 0 or 1 from a menu with options 'No' and 'Yes'. + * Other options like choose_from_menu. + * + * @deprecated since Moodle 2.0 + * + * Calls {@link choose_from_menu()} with preset arguments + * @see choose_from_menu() + * + * @param string $name the name of this form control, as in <select name="..." ... + * @param string $selected the option to select initially, default none. + * @param string $script if not '', then this is added to the <select> element as an onchange handler. + * @param boolean $return Whether this function should return a string or output it (defaults to false) + * @param boolean $disabled (defaults to false) + * @param int $tabindex + * @return string|void If $return=true returns string, else echo's and returns void + */ +function choose_from_menu_yesno($name, $selected, $script = '', + $return = false, $disabled = false, $tabindex = 0) { + // debugging('choose_from_menu_yesno() has been deprecated. Please change your code to use $OUTPUT->select_menu($selectmenu).'); + global $OUTPUT; + + if ($script) { + debugging('The $script parameter has been deprecated. You must use component_actions instead', DEBUG_DEVELOPER); + } + + $selectmenu = moodle_select_menu::make_yes_no($name, $selected); + $selectmenu->disabled = $disabled; + $selectmenu->tabindex = $tabindex; + $output = $OUTPUT->select_menu($select_menu); + + if ($return) { + return $output; + } else { + echo $output; + } +} + +/** + * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu + * including option headings with the first level. + * + * @deprecated since Moodle 2.0 + * + * This function is very similar to {@link choose_from_menu_yesno()} + * and {@link choose_from_menu()} + * + * @todo Add datatype handling to make sure $options is an array + * + * @param array $options An array of objects to choose from + * @param string $name The XHTML field name + * @param string $selected The value to select by default + * @param string $nothing The label for the 'nothing is selected' option. + * Defaults to get_string('choose'). + * @param string $script If not '', then this is added to the <select> element + * as an onchange handler. + * @param string $nothingvalue The value for the first `nothing` option if $nothing is set + * @param bool $return Whether this function should return a string or output + * it (defaults to false) + * @param bool $disabled Is the field disabled by default + * @param int|string $tabindex Override the tabindex attribute [numeric] + * @return string|void If $return=true returns string, else echo's and returns void + */ +function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', + $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { + + // debugging('choose_from_menu_nested() has been deprecated. Please change your code to use $OUTPUT->select_menu($selectmenu).'); + global $OUTPUT; + + if ($script) { + debugging('The $script parameter has been deprecated. You must use component_actions instead', DEBUG_DEVELOPER); + } + $selectmenu = moodle_select_menu::make($options, $name, $selected); + $selectmenu->tabindex = $tabindex; + $selectmenu->disabled = $disabled; + $selectmenu->nothingvalue = $nothingvalue; + $selectmenu->nested = true; if ($nothing == 'choose') { $selectmenu->nothinglabel = ''; } - + $output = $OUTPUT->select_menu($selectmenu); - + if ($return) { return $output; } else { echo $output; } } + diff --git a/lib/outputlib.php b/lib/outputlib.php index 17af8daac9..6079ac7698 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -2803,13 +2803,42 @@ class moodle_core_renderer extends moodle_renderer_base { } $html .= $this->output_start_tag('select', $attributes) . "\n"; - foreach ($selectmenu->options as $value => $label) { - $attributes = array('value' => $value); - if ((string) $value == (string) $selectmenu->selectedvalue || - (is_array($selectmenu->selectedvalue) && in_array($value, $selectmenu->selectedvalue))) { - $attributes['selected'] = 'selected'; + + if ($selectmenu->nested) { + foreach ($selectmenu->options as $section => $values) { + $html .= $this->output_start_tag('optgroup', array('label' => $section)); + if (!is_array($values)) { + var_dump($values); + } + foreach ($values as $value => $label) { + $attributes = array('value' => $value); + + if ((string) $value == (string) $selectmenu->selectedvalue || + (is_array($selectmenu->selectedvalue) && in_array($value, $selectmenu->selectedvalue))) { + $attributes['selected'] = 'selected'; + } + + $html .= $this->output_start_tag('option', $attributes); + + if ($label === '') { + $html .= $value; + } else { + $html .= $label; + } + + $html .= $this->output_end_tag('option'); + } + $html .= $this->output_end_tag('optgroup'); + } + } else { + foreach ($selectmenu->options as $value => $label) { + $attributes = array('value' => $value); + if ((string) $value == (string) $selectmenu->selectedvalue || + (is_array($selectmenu->selectedvalue) && in_array($value, $selectmenu->selectedvalue))) { + $attributes['selected'] = 'selected'; + } + $html .= ' ' . $this->output_tag('option', $attributes, s($label)) . "\n"; } - $html .= ' ' . $this->output_tag('option', $attributes, s($label)) . "\n"; } $html .= $this->output_end_tag('select') . "\n"; @@ -2926,7 +2955,7 @@ class moodle_core_renderer extends moodle_renderer_base { } /** - * Print a continue button that goes to a particular URL. + * Prints a single paging bar to provide access to other pages (usually in a search) * * @param string|moodle_url $link The url the button goes to. * @return string the HTML to output. @@ -3396,6 +3425,10 @@ class moodle_select_menu extends moodle_html_component { * @var boolean if true, allow multiple selection. Only used if $listbox is true. */ public $multiple = false; + /** + * @var boolean $nested if true, uses $options' keys as option headings (optgroup) + */ + public $nested = false; /** * @see moodle_html_component::prepare() @@ -3412,7 +3445,14 @@ class moodle_select_menu extends moodle_html_component { if (is_null($this->nothinglabel)) { $this->nothinglabel = get_string('choosedots'); } + + // If nested is on, remove the default Choose option + if ($this->nested) { + $this->nothinglabel = ''; + } + $this->add_class('select'); + parent::prepare(); } diff --git a/lib/weblib.php b/lib/weblib.php index 25c0227ef7..cbfac08330 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -654,101 +654,6 @@ function close_window($delay = 0, $reloadopener = false) { exit; } -/** - * Choose value 0 or 1 from a menu with options 'No' and 'Yes'. - * Other options like choose_from_menu. - * - * Calls {@link choose_from_menu()} with preset arguments - * @see choose_from_menu() - * - * @param string $name the name of this form control, as in <select name="..." ... - * @param string $selected the option to select initially, default none. - * @param string $script if not '', then this is added to the <select> element as an onchange handler. - * @param boolean $return Whether this function should return a string or output it (defaults to false) - * @param boolean $disabled (defaults to false) - * @param int $tabindex - * @return string|void If $return=true returns string, else echo's and returns void - */ -function choose_from_menu_yesno($name, $selected, $script = '', - $return = false, $disabled = false, $tabindex = 0) { - return choose_from_menu(array(get_string('no'), get_string('yes')), $name, - $selected, '', $script, '0', $return, $disabled, $tabindex); -} - -/** - * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu - * including option headings with the first level. - * - * This function is very similar to {@link choose_from_menu_yesno()} - * and {@link choose_from_menu()} - * - * @todo Add datatype handling to make sure $options is an array - * - * @param array $options An array of objects to choose from - * @param string $name The XHTML field name - * @param string $selected The value to select by default - * @param string $nothing The label for the 'nothing is selected' option. - * Defaults to get_string('choose'). - * @param string $script If not '', then this is added to the <select> element - * as an onchange handler. - * @param string $nothingvalue The value for the first `nothing` option if $nothing is set - * @param bool $return Whether this function should return a string or output - * it (defaults to false) - * @param bool $disabled Is the field disabled by default - * @param int|string $tabindex Override the tabindex attribute [numeric] - * @return string|void If $return=true returns string, else echo's and returns void - */ -function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '', - $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) { - - if ($nothing == 'choose') { - $nothing = get_string('choose') .'...'; - } - - $attributes = ($script) ? 'onchange="'. $script .'"' : ''; - if ($disabled) { - $attributes .= ' disabled="disabled"'; - } - - if ($tabindex) { - $attributes .= ' tabindex="'.$tabindex.'"'; - } - - $output = '' . "\n"; - - if ($return) { - return $output; - } else { - echo $output; - } -} - /** * Given an array of values, creates a group of radio buttons to be part of a form -- 2.39.5