MDL-19756 Migrated choose_from_menu_yesno and choose_from_menu_nested into $OUTPUT->select_menu()

This commit is contained in:
nicolasconnault
2009-07-28 12:12:24 +00:00
parent dc58020794
commit 053203a8d5
3 changed files with 152 additions and 116 deletions
+105 -14
View File
@@ -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;
}
}
+47 -7
View File
@@ -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();
}
-95
View File
@@ -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 = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n";
if ($nothing) {
$output .= ' <option value="'. $nothingvalue .'"'. "\n";
if ($nothingvalue === $selected) {
$output .= ' selected="selected"';
}
$output .= '>'. $nothing .'</option>' . "\n";
}
if (!empty($options)) {
foreach ($options as $section => $values) {
$output .= ' <optgroup label="'. s(format_string($section)) .'">'."\n";
foreach ($values as $value => $label) {
$output .= ' <option value="'. format_string($value) .'"';
if ((string)$value == (string)$selected) {
$output .= ' selected="selected"';
}
if ($label === '') {
$output .= '>'. $value .'</option>' . "\n";
} else {
$output .= '>'. $label .'</option>' . "\n";
}
}
$output .= ' </optgroup>'."\n";
}
}
$output .= '</select>' . "\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