From: nicolasconnault Date: Thu, 30 Jul 2009 03:47:12 +0000 (+0000) Subject: MDL-19756 Migrated popup_form X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=496908435e12dd3d66c58640cbbbf82b257dcc73;p=moodle.git MDL-19756 Migrated popup_form --- diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 28a28e8be2..5783d6dce9 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -2625,11 +2625,13 @@ function print_single_button($link, $options, $label='OK', $method='get', $notus $form->id = $formid; if ($jsconfirmmessage) { - $confirmaction = new component_action('click', 'confirm_dialog', array($jsconfirmmessage)); + $confirmaction = new component_action('click', 'confirm_dialog', array('message' => $jsconfirmmessage)); $form->button->add_action($confirmaction); } $output = $OUTPUT->button($form); + + $icon = new action_icon(); if ($return) { return $output; @@ -2843,8 +2845,8 @@ function helpbutton($page, $title, $module='moodle', $image=true, $linktext=fals $helpicon = new help_icon(); $helpicon->page = $page; - $helpicon->module = $module; $helpicon->text = $title; + $helpicon->module = $module; $helpicon->linktext = $linktext; // If image is true, the defaults are handled by the helpicon's prepare method @@ -3252,16 +3254,16 @@ function print_timer_selector($timelimit = 0, $unit = '', $name = 'timelimit', $ * @param int $currenttime A default timestamp in GMT * @param int $step minute spacing * @param boolean $return If set to true returns rather than echo's - * @return string|bool Depending on value of $return + * @return string|bool Depending on value of $return */ function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=false) { // debugging('print_time_selector() has been deprecated. Please change your code to use $OUTPUT->select_menu($timeselector).'); - global $OUTPUT; + global $OUTPUT; $hourselector = moodle_select_menu::make_time_selector('hours', $hour, $currenttime); $minuteselector = moodle_select_menu::make_time_selector('minutes', $minute, $currenttime, $step); - + $output = $OUTPUT->select_menu($hourselector) . $OUTPUT->select_menu($minuteselector); - + if ($return) { return $output; } else { @@ -3284,14 +3286,94 @@ function print_time_selector($hour, $minute, $currenttime=0, $step=5, $return=fa function print_date_selector($day, $month, $year, $currenttime=0, $return=false) { // debugging('print_date_selector() has been deprecated. Please change your code to use $OUTPUT->select_menu($dateselector).'); - global $OUTPUT; - + global $OUTPUT; + $dayselector = moodle_select_menu::make_time_selector('days', $day, $currenttime); $monthselector = moodle_select_menu::make_time_selector('months', $month, $currenttime); $yearselector = moodle_select_menu::make_time_selector('years', $year, $currenttime); - + $output = $OUTPUT->select_menu($dayselector) . $OUTPUT->select_menu($monthselector) . $OUTPUT->select_menu($yearselector); + + if ($return) { + return $output; + } else { + echo $output; + } +} + +/** + * Implements a complete little form with a dropdown menu. + * + * @deprecated since Moodle 2.0 + * + * When JavaScript is on selecting an option from the dropdown automatically + * submits the form (while avoiding the usual acessibility problems with this appoach). + * With JavaScript off, a 'Go' button is printed. + * + * @todo Finish documenting this function + * + * @global object + * @global object + * @param string $baseurl The target URL up to the point of the variable that changes + * @param array $options A list of value-label pairs for the popup list + * @param string $formid id for the control. Must be unique on the page. Used in the HTML. + * @param string $selected The option that is initially selected + * @param string $nothing The label for the "no choice" option + * @param string $help The name of a help page if help is required + * @param string $helptext The name of the label for the help button + * @param boolean $return Indicates whether the function should return the HTML + * as a string or echo it directly to the page being rendered + * @param string $targetwindow The name of the target page to open the linked page in. + * @param string $selectlabel Text to place in a [label] element - preferred for accessibility. + * @param array $optionsextra an array with the same keys as $options. The values are added within the corresponding element. If an optgroup element is detected, + * this will recursively output its options as well. + * + * @param mixed $option a moodle_select_option or moodle_select_optgroup + * @return string the HTML for the + */ + public function select_option($option) { + $option->prepare(); + $this->prepare_event_handlers($option); + + if ($option instanceof html_select_option) { + return $this->output_tag('option', array( + 'value' => $option->value, + 'class' => $option->get_classes_string(), + 'selected' => $option->selected), $option->text); + } else if ($option instanceof html_select_optgroup) { + $output = $this->output_start_tag('optgroup', array('label' => $option->text, 'class' => $option->get_classes_string())); + foreach ($option->options as $selectoption) { + $output .= $this->select_option($selectoption); } + $output .= $this->output_end_tag('optgroup'); + return $output; } - $html .= $this->output_end_tag('select') . "\n"; - - return $html; } /** @@ -3385,7 +3383,9 @@ class moodle_html_component { */ class moodle_select_menu extends moodle_html_component { /** - * @var array the choices to show in the menu. An array $value => $display. + * The moodle_select_menu object parses an array of options into component objects + * @see nested attribute + * @var mixed $options the choices to show in the menu. An array $value => $display, of html_select_option or of html_select_optgroup objects. */ public $options; /** @@ -3437,15 +3437,27 @@ class moodle_select_menu extends moodle_html_component { */ public $multiple = false; /** + * Another way to use nested menu is to prefix optgroup labels with -- and end the optgroup with -- + * Leave this setting to false if you are using the latter method. * @var boolean $nested if true, uses $options' keys as option headings (optgroup) */ public $nested = false; + /** + * @var html_form $form An optional html_form component + */ + public $form; + /** + * @var help_icon $form An optional help_icon component + */ + public $helpicon; /** * @see moodle_html_component::prepare() * @return void */ public function prepare() { + global $CFG; + // name may contain [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading if (empty($this->id)) { $this->id = 'menu' . str_replace(array('[', ']'), '', $this->name); @@ -3462,7 +3474,7 @@ class moodle_select_menu extends moodle_html_component { $this->nothinglabel = ''; } - if (!($this->label instanceof html_label)) { + if (!empty($this->label) && !($this->label instanceof html_label)) { $label = new html_label(); $label->text = $this->label; $label->for = $this->name; @@ -3471,6 +3483,100 @@ class moodle_select_menu extends moodle_html_component { $this->add_class('select'); + $firstoption = reset($this->options); + if (is_object($firstoption)) { + parent::prepare(); + return true; + } + + $options = $this->options; + $this->options = array(); + + if ($this->nested) { + foreach ($options as $section => $values) { + $optgroup = new html_select_optgroup(); + $optgroup->text = $section; + + foreach ($values as $value => $display) { + $option = new html_select_option(); + $option->value = s($value); + $option->text = $display; + if ($display === '') { + $option->text = $value; + } + + if ((string) $value == (string) $this->selectedvalue || + (is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) { + $option->selected = 'selected'; + } + + $optgroup->options[] = $option; + } + + $this->options[] = $optgroup; + } + } else { + $inoptgroup = false; + $optgroup = false; + foreach ($options as $value => $display) { + if ($display == '--') { /// we are ending previous optgroup + $this->options[] = $optgroup; + $inoptgroup = false; + continue; + } else if (substr($display,0,2) == '--') { /// we are starting a new optgroup + if (!empty($optgroup->options)) { + $this->options[] = $optgroup; + } + + $optgroup = new html_select_optgroup(); + $optgroup->text = substr($display,2); // stripping the -- + + $inoptgroup = true; /// everything following will be in an optgroup + continue; + + } else { + // Add $nothing option if there are not optgroups + if ($this->nothinglabel && empty($this->options[0]) && !$inoptgroup) { + $nothingoption = new html_select_option(); + $nothingoption->value = 0; + if (!empty($this->nothingvalue)) { + $nothingoption->value = $this->nothingvalue; + } + $nothingoption->text = $this->nothinglabel; + $this->options = array($nothingoption) + $this->options; + } + + $option = new html_select_option(); + $option->text = $display; + + if ($display === '') { + $option->text = $value; + } + + if ((string) $value == (string) $this->selectedvalue || + (is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) { + $option->selected = 'selected'; + } + + $option->value = s($value); + + if (!empty($optionsextra[$value])) { + $optstr .= ' '.$optionsextra[$value]; + } + + if ($inoptgroup) { + $optgroup->options[] = $option; + } else { + $this->options[] = $option; + } + } + } + + if ($optgroup) { + $this->options[] = $optgroup; + } + } + parent::prepare(); } @@ -3562,10 +3668,40 @@ class moodle_select_menu extends moodle_html_component { return $timerselector; } + + /** + * This is a shortcut for making a select popup form. + * @param string $baseurl The target URL up to the point of the variable that changes + * @param array $options A list of value-label pairs for the popup list + * @param string $formid id for the control. Must be unique on the page. Used in the HTML. + * @param string $submitvalue Optional label for the 'Go' button. Defaults to get_string('go'). + * @param string $selected The option that is initially selected + * @return moodle_select_menu A menu initialised as a popup form. + */ + public function make_popup_form($baseurl, $options, $formid, $submitvalue='', $selected=null) { + $selectmenu = self::make($options, 'jump', $selected); + $selectmenu->form = new html_form(); + $selectmenu->form->id = $formid; + $selectmenu->form->method = 'get'; + $selectmenu->form->add_class('popupform'); + $selectmenu->form->url = new moodle_url($baseurl); + $selectmenu->form->button->text = get_string('go'); + + if (!empty($submitvalue)) { + $selectmenu->form->button->text = $submitvalue; + } + + $selectmenu->id = $formid . '_jump'; + $selectmenu->baseurl = $baseurl; + + $selectmenu->add_action('change', 'submit_form_by_id', array('id' => $formid, 'selectid' => $selectmenu->id)); + + return $selectmenu; + } } /** - * This class represents how a block appears on a page. + * This class represents a label element * * @copyright 2009 Nicolas Connault * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -3581,7 +3717,75 @@ class html_label extends moodle_html_component { */ public $for; + /** + * @see moodle_html_component::prepare() + * @return void + */ public function prepare() { + if (empty($this->text)) { + throw new coding_exception('html_label must have a $text value.'); + } + parent::prepare(); + } +} + +/** + * This class represents a select option element + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_select_option extends moodle_html_component { + /** + * @var string $value The value of this option (will be sent with form) + */ + public $value; + /** + * @var string $text The display value of the option + */ + public $text; + /** + * @var boolean $selected Whether or not this option is selected + */ + public $selected = false; + + /** + * @see moodle_html_component::prepare() + * @return void + */ + public function prepare() { + if (empty($this->text)) { + throw new coding_exception('html_select_option requires a $text value.'); + } + parent::prepare(); + } +} + +/** + * This class represents a select optgroup element + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_select_optgroup extends moodle_html_component { + /** + * @var string $text The display value of the optgroup + */ + public $text; + /** + * @var array $options An array of html_select_option objects + */ + public $options = array(); + + public function prepare() { + if (empty($this->text)) { + throw new coding_exception('html_select_optgroup requires a $text value.'); + } + if (empty($this->options)) { + throw new coding_exception('html_select_optgroup requires at least one html_select_option object'); + } parent::prepare(); } } @@ -4041,7 +4245,7 @@ class help_icon extends moodle_html_component { } public static function make_scale_menu($courseid, $scale) { - $helpbutton = new help_button(); + $helpbutton = new help_icon(); $strscales = get_string('scales'); $helpbutton->image->alt = $scale->name; $helpbutton->link->url = new moodle_url('/course/scales.php', array('id' => $courseid, 'list' => true, 'scaleid' => $scale->id)); diff --git a/lib/simpletest/testoutputlib.php b/lib/simpletest/testoutputlib.php index d41b0a4f3c..a7056002b8 100644 --- a/lib/simpletest/testoutputlib.php +++ b/lib/simpletest/testoutputlib.php @@ -886,9 +886,7 @@ class moodle_core_renderer_test extends UnitTestCase { public function test_select_menu_simple() { $selectmenu = moodle_select_menu::make(array(10 => 'ten', 'c2' => 'two'), 'mymenu'); $html = $this->renderer->select_menu($selectmenu); - $this->assert(new ContainsTagWithAttribute('select', 'class', 'menumymenu select'), $html); - $this->assert(new ContainsTagWithAttribute('select', 'name', 'mymenu'), $html); - $this->assert(new ContainsTagWithAttribute('select', 'id', 'menumymenu'), $html); + $this->assert(new ContainsTagWithAttributes('select', array('class' => 'menumymenu select', 'name' => 'mymenu', 'id' => 'menumymenu')), $html); $this->assert(new ContainsTagWithContents('option', 'ten'), $html); $this->assert(new ContainsTagWithAttribute('option', 'value', '10'), $html); $this->assert(new ContainsTagWithContents('option', 'two'), $html); @@ -1161,4 +1159,32 @@ class moodle_core_renderer_test extends UnitTestCase { $htmllist->items[2]->type = 'ordered'; $html = $this->renderer->htmllist($htmllist); } + + public function test_moodle_select_menu() { + $options = array('var1' => 'value1', 'var2' => 'value2', 'var3' => 'value3'); + $selectmenu = moodle_select_menu::make($options, 'mymenu', 'var2'); + $html = $this->renderer->select_menu($selectmenu); + $this->assert(new ContainsTagWithAttributes('select', array('name' => 'mymenu')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var1'), array('selected' => 'selected')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var2', 'selected' => 'selected')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var3'), array('selected' => 'selected')), $html); + $this->assert(new ContainsTagWithContents('option', 'value1'), $html); + $this->assert(new ContainsTagWithContents('option', 'value2'), $html); + $this->assert(new ContainsTagWithContents('option', 'value3'), $html); + + $options = array('group1' => '--group1', 'var1' => 'value1', 'var2' => 'value2', 'group2' => '--', 'group2' => '--group2', 'var3' => 'value3', 'var4' => 'value4'); + $selectmenu = moodle_select_menu::make($options, 'mymenu', 'var2'); + $html = $this->renderer->select_menu($selectmenu); + $this->assert(new ContainsTagWithAttributes('select', array('name' => 'mymenu')), $html); + $this->assert(new ContainsTagWithAttributes('optgroup', array('label' => 'group1')), $html); + $this->assert(new ContainsTagWithAttributes('optgroup', array('label' => 'group2')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var1'), array('selected' => 'selected')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var2', 'selected' => 'selected')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var3'), array('selected' => 'selected')), $html); + $this->assert(new ContainsTagWithAttributes('option', array('value' => 'var4'), array('selected' => 'selected')), $html); + $this->assert(new ContainsTagWithContents('option', 'value1'), $html); + $this->assert(new ContainsTagWithContents('option', 'value2'), $html); + $this->assert(new ContainsTagWithContents('option', 'value3'), $html); + $this->assert(new ContainsTagWithContents('option', 'value4'), $html); + } } diff --git a/lib/weblib.php b/lib/weblib.php index 5560cfe6eb..5b0d574855 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -797,200 +797,6 @@ function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $retur } -/** - * Implements a complete little form with a dropdown menu. - * - * When JavaScript is on selecting an option from the dropdown automatically - * submits the form (while avoiding the usual acessibility problems with this appoach). - * With JavaScript off, a 'Go' button is printed. - * - * @todo Finish documenting this function - * - * @global object - * @global object - * @param string $baseurl The target URL up to the point of the variable that changes - * @param array $options A list of value-label pairs for the popup list - * @param string $formid id for the control. Must be unique on the page. Used in the HTML. - * @param string $selected The option that is initially selected - * @param string $nothing The label for the "no choice" option - * @param string $help The name of a help page if help is required - * @param string $helptext The name of the label for the help button - * @param boolean $return Indicates whether the function should return the HTML - * as a string or echo it directly to the page being rendered - * @param string $targetwindow The name of the target page to open the linked page in. - * @param string $selectlabel Text to place in a [label] element - preferred for accessibility. - * @param array $optionsextra an array with the same keys as $options. The values are added within the corresponding