From 8e127eb38e55f7e80fe632cc523d8a9eff877afc Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 28 Jul 2009 02:45:13 +0000 Subject: [PATCH] MDL-19756 Migrating choose_from_menu to outputlib --- course/lib.php | 3 +- lib/javascript-deprecated.js | 5 + lib/javascript-static.js | 5 +- lib/outputlib.php | 160 ++++++++++++++++++++++++++++--- lib/simpletest/testoutputlib.php | 8 ++ lib/weblib.php | 108 --------------------- user/index.php | 9 +- user/user.js | 9 +- 8 files changed, 180 insertions(+), 127 deletions(-) create mode 100644 lib/javascript-deprecated.js diff --git a/course/lib.php b/course/lib.php index c797662d00..e9320fad6c 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2031,8 +2031,7 @@ function update_category_button($categoryid = 0) { } else { $page = 'index.php'; } - return print_single_button($CFG->wwwroot . '/course/' . $page, $options, - $label, 'get', '', true); + return print_single_button($CFG->wwwroot . '/course/' . $page, $options, $label, 'get', '', true); } /** diff --git a/lib/javascript-deprecated.js b/lib/javascript-deprecated.js new file mode 100644 index 0000000000..54d61bdfa4 --- /dev/null +++ b/lib/javascript-deprecated.js @@ -0,0 +1,5 @@ +// Deprecated core Javascript functions for Moodle +function submitFormById(id) { + submit_form_by_id(null, {id: id}); +} + diff --git a/lib/javascript-static.js b/lib/javascript-static.js index fe6cd44903..5e45a675f6 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -205,9 +205,8 @@ function lockoptionsallsetup(formid) { return lockoptionsall(formid); } - -function submitFormById(id) { - var theform = document.getElementById(id); +function submit_form_by_id(e, args) { + var theform = document.getElementById(args.id); if(!theform) { return false; } diff --git a/lib/outputlib.php b/lib/outputlib.php index abdefd86d0..7694f13141 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -1816,6 +1816,7 @@ class moodle_core_renderer extends moodle_renderer_base { } $this->page->requires->js('lib/javascript-static.js')->in_head(); + $this->page->requires->js('lib/javascript-deprecated.js')->in_head(); $this->page->requires->js('lib/javascript-mod.php')->in_head(); $this->page->requires->js('lib/overlib/overlib.js')->in_head(); $this->page->requires->js('lib/overlib/overlib_cssstyle.js')->in_head(); @@ -2677,6 +2678,40 @@ class moodle_core_renderer extends moodle_renderer_base { return $output; } + /** + * Outputs a HTML nested list + * + * @param html_list $list A html_list object + * @return string HTML structure + */ + public function htmllist($list) { + $list->prepare(); + + $this->prepare_event_handlers($list); + + if ($list->type == 'ordered') { + $tag = 'ol'; + } else if ($list->type == 'unordered') { + $tag = 'ul'; + } + + $output = $this->output_start_tag($tag, array('class' => $list->get_classes_string())); + + foreach ($list->items as $listitem) { + if ($listitem instanceof html_list) { + $output .= $this->output_start_tag('li'); + $output .= $this->htmllist($listitem); + $output .= $this->output_end_tag('li'); + } else if ($listitem instanceof html_list_item) { + $listitem->prepare(); + $this->prepare_event_handlers($listitem); + $output .= $this->output_tag('li', array('class' => $listitem->get_classes_string()), $listitem->value); + } + } + + return $output . $this->output_end_tag($tag); + } + public function close_window_button($buttontext = null, $reloadopener = false) { if (empty($buttontext)) { $buttontext = get_string('closewindow'); @@ -2702,6 +2737,8 @@ class moodle_core_renderer extends moodle_renderer_base { public function select_menu($selectmenu) { $selectmenu = clone($selectmenu); $selectmenu->prepare(); + + $this->prepare_event_handlers($selectmenu); if ($selectmenu->nothinglabel) { $selectmenu->options = array($selectmenu->nothingvalue => $selectmenu->nothinglabel) + @@ -2715,8 +2752,7 @@ class moodle_core_renderer extends moodle_renderer_base { $attributes = array( 'name' => $selectmenu->name, 'id' => $selectmenu->id, - 'class' => $selectmenu->get_classes_string(), - 'onchange' => $selectmenu->script, + 'class' => $selectmenu->get_classes_string() ); if ($selectmenu->disabled) { $attributes['disabled'] = 'disabled'; @@ -2736,8 +2772,14 @@ class moodle_core_renderer extends moodle_renderer_base { $attributes['multiple'] = 'multiple'; } } + + $html = ''; + + if (!empty($selectmenu->label)) { + $html .= $this->output_tag('label', array('for' => $selectmenu->name), $selectmenu->label); + } - $html = $this->output_start_tag('select', $attributes) . "\n"; + $html .= $this->output_start_tag('select', $attributes) . "\n"; foreach ($selectmenu->options as $value => $label) { $attributes = array('value' => $value); if ((string) $value == (string) $selectmenu->selectedvalue || @@ -3225,7 +3267,7 @@ class moodle_html_component { * @return void */ public function add_action($event, $jsfunction, $jsfunctionargs=array()) { - while (empty($this->id) || in_array($this->id, moodle_html_component::$generated_ids)) { + while (empty($this->id) || !in_array($this->id, moodle_html_component::$generated_ids)) { $this->generate_id(); } $this->actions[] = new component_action($event, $jsfunction, $jsfunctionargs); @@ -3292,12 +3334,16 @@ class moodle_select_menu extends moodle_html_component { */ public $options; /** - * @var string the name of this form control. That is, the name of the GET/POST + * @var string $name the name of this form control. That is, the name of the GET/POST * variable that will be set if this select is submitted as part of a form. */ public $name; /** - * @var string the option to select initially. Should match one + * @var string $label The label for that component + */ + public $label; + /** + * @var string $selectedvalue the option to select initially. Should match one * of the $options array keys. Default none. */ public $selectedvalue; @@ -3335,18 +3381,13 @@ class moodle_select_menu extends moodle_html_component { * @var boolean if true, allow multiple selection. Only used if $listbox is true. */ public $multiple = false; - /** - * @deprecated - * @var string JavaScript to add as an onchange attribute. Do not use this. - * Use the YUI even library instead. - */ - public $script = ''; /** * @see moodle_html_component::prepare() * @return void */ public function prepare() { + // 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); } @@ -4347,6 +4388,101 @@ class moodle_paging_bar extends moodle_html_component { } +/** + * Component representing a list. + * + * The advantage of using this object instead of a flat array is that you can load it + * with metadata (CSS classes, event handlers etc.) which can be used by the renderers. + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_list extends moodle_html_component { + + /** + * @var array $items An array of html_list_item or html_list objects + */ + public $items = array(); + + /** + * @var string $type The type of list (ordered|unordered), definition type not yet supported + */ + public $type = 'unordered'; + + /** + * @see lib/moodle_html_component#prepare() + * @return void + */ + public function prepare() { + parent::prepare(); + } + + /** + * This function takes a nested array of data and maps it into this list's $items array + * as proper html_list_item and html_list objects, with appropriate metadata. + * + * @param array $tree A nested array (array keys are ignored); + * @param int $row Used in identifying the iteration level and in ul classes + * @return void + */ + public function load_data($tree, $level=0) { + + $this->add_class("list-$level"); + + foreach ($tree as $key => $element) { + if (is_array($element)) { + $newhtmllist = new html_list(); + $newhtmllist->load_data($element, $level + 1); + $this->items[] = $newhtmllist; + } else { + $listitem = new html_list_item(); + $listitem->value = $element; + $listitem->add_class("list-item-$level-$key"); + $this->items[] = $listitem; + } + } + } + + /** + * Adds a html_list_item or html_list to this list. + * If the param is a string, a html_list_item will be added. + * @param mixed $item String, html_list or html_list_item object + * @return void + */ + public function add_item($item) { + if ($item instanceof html_list_item || $item instanceof html_list) { + $this->items[] = $item; + } else { + $listitem = new html_list_item(); + $listitem->value = $item; + $this->items[] = $item; + } + } +} + +/** + * Component representing a list item. + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_list_item extends moodle_html_component { + /** + * @var string $value The value of the list item + */ + public $value; + + /** + * @see lib/moodle_html_component#prepare() + * @return void + */ + public function prepare() { + parent::prepare(); + } +} + /// ACTIONS /** diff --git a/lib/simpletest/testoutputlib.php b/lib/simpletest/testoutputlib.php index 3e659fc548..ab927fe7e6 100644 --- a/lib/simpletest/testoutputlib.php +++ b/lib/simpletest/testoutputlib.php @@ -1153,4 +1153,12 @@ class moodle_core_renderer_test extends UnitTestCase { // TODO test with more different parameters } + + public function test_html_list() { + $htmllist = new html_list(); + $data = array('item1', 'item2', array('item1-1', 'item1-2')); + $htmllist->load_data($data); + $htmllist->items[2]->type = 'ordered'; + $html = $this->renderer->htmllist($htmllist); + } } diff --git a/lib/weblib.php b/lib/weblib.php index 40a05b3e07..25c0227ef7 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -654,114 +654,6 @@ function close_window($delay = 0, $reloadopener = false) { exit; } -/** - * Given an array of values, output the HTML for a select element with those options. - * - * Normally, you only need to use the first few parameters. - * - * @param array $options The options to offer. An array of the form - * $options[{value}] = {text displayed for that option}; - * @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 $nothing The label for the 'nothing is selected' option. Defaults to get_string('choose'). - * Set this to '' if you don't want a 'nothing is selected' option. - * @param string $script if not '', then this is added to the <select> element as an onchange handler. - * @param string $nothingvalue The value corresponding to the $nothing option. Defaults to 0. - * @param boolean $return if false (the default) the the output is printed directly, If true, the - * generated HTML is returned as a string. - * @param boolean $disabled if true, the select is generated in a disabled state. Default, false. - * @param int $tabindex if give, sets the tabindex attribute on the <select> element. Default none. - * @param string $id value to use for the id attribute of the <select> element. If none is given, - * then a suitable one is constructed. - * @param mixed $listbox if false, display as a dropdown menu. If true, display as a list box. - * By default, the list box will have a number of rows equal to min(10, count($options)), but if - * $listbox is an integer, that number is used for size instead. - * @param boolean $multiple if true, enable multiple selections, else only 1 item can be selected. Used - * when $listbox display is enabled - * @param string $class value to use for the class attribute of the <select> element. If none is given, - * then a suitable one is constructed. - * @return string|void If $return=true returns string, else echo's and returns void - */ -function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='', - $nothingvalue='0', $return=false, $disabled=false, $tabindex=0, - $id='', $listbox=false, $multiple=false, $class='') { - - if ($nothing == 'choose') { - $nothing = get_string('choose') .'...'; - } - - $attributes = ($script) ? 'onchange="'. $script .'"' : ''; - if ($disabled) { - $attributes .= ' disabled="disabled"'; - } - - if ($tabindex) { - $attributes .= ' tabindex="'.$tabindex.'"'; - } - - if ($id ==='') { - $id = 'menu'.$name; - // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading - $id = str_replace('[', '', $id); - $id = str_replace(']', '', $id); - } - - if ($class ==='') { - $class = 'menu'.$name; - // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading - $class = str_replace('[', '', $class); - $class = str_replace(']', '', $class); - } - $class = 'select ' . $class; /// Add 'select' selector always - - if ($listbox) { - if (is_integer($listbox)) { - $size = $listbox; - } else { - $numchoices = count($options); - if ($nothing) { - $numchoices += 1; - } - $size = min(10, $numchoices); - } - $attributes .= ' size="' . $size . '"'; - if ($multiple) { - $attributes .= ' multiple="multiple"'; - } - } - - $output = '' . "\n"; - - 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. diff --git a/user/index.php b/user/index.php index 72ae306808..9d0fd4eb5a 100644 --- a/user/index.php +++ b/user/index.php @@ -22,6 +22,8 @@ $contextid = optional_param('contextid', 0, PARAM_INT); // one of this or $courseid = optional_param('id', 0, PARAM_INT); // this are required + + $PAGE->set_url('user/index.php', compact('page', 'perpage', 'mode', 'accesssince', 'search', 'roleid', 'contextid', 'courseid')); if ($contextid) { if (! $context = get_context_instance_by_id($contextid)) { @@ -849,7 +851,12 @@ } helpbutton("participantswithselectedusers", get_string("withselectedusers")); - choose_from_menu ($displaylist, "formaction", "", get_string("withselectedusers"), "if(checksubmit(this.form))this.form.submit();", ""); + $selectmenu = new moodle_select_menu(); + $selectmenu->options = $displaylist; + $selectmenu->name = "formaction"; + $selectmenu->label = get_string("withselectedusers"); + $selectmenu->add_action('change', 'conditionalsubmit', array('formid' => 'participantsform')); + echo $OUTPUT->select_menu($selectmenu); echo ''; echo '
'; echo '
'; diff --git a/user/user.js b/user/user.js index 96f37d6887..f3e7abffe0 100644 --- a/user/user.js +++ b/user/user.js @@ -18,4 +18,11 @@ function checkchecked(form) { } } return checked; -} \ No newline at end of file +} + +function conditionalsubmit(event, args) { + var form = document.getElementById(args.formid); + if (checksubmit(form)) { + form.submit(); + } +} -- 2.39.5