MDL-19756 Migrating choose_from_menu to outputlib
This commit is contained in:
+1
-2
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// Deprecated core Javascript functions for Moodle
|
||||
function submitFormById(id) {
|
||||
submit_form_by_id(null, {id: id});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+148
-12
@@ -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 = '';
|
||||
|
||||
$html = $this->output_start_tag('select', $attributes) . "\n";
|
||||
if (!empty($selectmenu->label)) {
|
||||
$html .= $this->output_tag('label', array('for' => $selectmenu->name), $selectmenu->label);
|
||||
}
|
||||
|
||||
$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
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
-108
@@ -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 = '<select id="'. $id .'" class="'. $class .'" name="'. $name .'" '. $attributes .'>' . "\n";
|
||||
if ($nothing) {
|
||||
$output .= ' <option value="'. s($nothingvalue) .'"'. "\n";
|
||||
if ($nothingvalue === $selected) {
|
||||
$output .= ' selected="selected"';
|
||||
}
|
||||
$output .= '>'. $nothing .'</option>' . "\n";
|
||||
}
|
||||
|
||||
if (!empty($options)) {
|
||||
foreach ($options as $value => $label) {
|
||||
$output .= ' <option value="'. s($value) .'"';
|
||||
if ((string)$value == (string)$selected ||
|
||||
(is_array($selected) && in_array($value, $selected))) {
|
||||
$output .= ' selected="selected"';
|
||||
}
|
||||
if ($label === '') {
|
||||
$output .= '>'. $value .'</option>' . "\n";
|
||||
} else {
|
||||
$output .= '>'. $label .'</option>' . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
$output .= '</select>' . "\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.
|
||||
|
||||
+8
-1
@@ -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 '<input type="hidden" name="id" value="'.$course->id.'" />';
|
||||
echo '<div id="noscriptparticipantsform" style="display: inline;">';
|
||||
echo '<input type="submit" value="'.get_string('ok').'" /></div>';
|
||||
|
||||
+8
-1
@@ -18,4 +18,11 @@ function checkchecked(form) {
|
||||
}
|
||||
}
|
||||
return checked;
|
||||
}
|
||||
}
|
||||
|
||||
function conditionalsubmit(event, args) {
|
||||
var form = document.getElementById(args.formid);
|
||||
if (checksubmit(form)) {
|
||||
form.submit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user