diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index b87a104e37b..cc5833c8bd9 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1022,7 +1022,7 @@ class js_writer { // TODO: Following components will be refactored soon /** - * Base class for classes representing HTML elements, like html_select. + * Base class for classes representing HTML elements. * * Handles the id and class attributes. * @@ -1300,360 +1300,6 @@ class html_label extends html_component { } -/** - * This class hold all the information required to describe a element. Default 0. - */ - public $tabindex = 0; - /** - * @var mixed Defaults to false, which means display the select as a dropdown menu. - * If true, display this select as a list box whose size is chosen automatically. - * If an integer, display as list box of that size. - */ - public $listbox = false; - /** - * @var integer if you are using $listbox === true to get an automatically - * sized list box, the size of the list box will be the number of options, - * or this number, whichever is smaller. - */ - public $maxautosize = 10; - /** - * @var boolean if true, allow multiple selection. Only used if $listbox is true, or if - * the select is to be output as checkboxes. - */ - 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 $array help icon params - */ - public $helpicon; - /** - * @var boolean $rendertype How the select element should be rendered: menu or radio (checkbox is just radio + multiple) - */ - public $rendertype = 'menu'; - - /** - * @see html_component::prepare() - * @return void - */ - public function prepare(renderer_base $output, moodle_page $page, $target) { - 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); - } - - if (empty($this->classes)) { - $this->set_classes(array('menu' . str_replace(array('[', ']'), '', $this->name))); - } - - if (is_null($this->nothinglabel)) { - $this->nothinglabel = get_string('choosedots'); - } - - if (!empty($this->label) && !($this->label instanceof html_label)) { - $label = new html_label(); - $label->text = $this->label; - $label->for = $this->name; - $this->label = $label; - } - - $this->add_class('select'); - - $this->initialise_options(); - parent::prepare($output, $page, $target); - } - - /** - * This is a shortcut for making a simple select menu. It lets you specify - * the options, name and selected option in one line of code. - * @param array $options used to initialise {@link $options}. - * @param string $name used to initialise {@link $name}. - * @param string $selected used to initialise {@link $selected}. - * @param string $nothinglabel The label for the 'nothing is selected' option. Defaults to "Choose..." - * @return html_select A html_select object with the three common fields initialised. - */ - public static function make($options, $name, $selected = '', $nothinglabel='choosedots') { - $menu = new html_select(); - $menu->options = $options; - $menu->name = $name; - $menu->selectedvalue = $selected; - return $menu; - } - - /** - * Adds a help icon next to the select menu. - * - *
-     * $select->set_help_icon($page, $text, $component);
-     * 
- * - * @param string $helppage Either the keyword that defines a help page or a help_icon object - * @param text $text The text of the help icon - * @param component $component - * @param boolean $linktext Whether or not to show text next to the icon - * @return void - */ - public function set_help_icon($helppage='', $text='', $component='moodle') { - if ($helppage) { - $this->helpicon = array('helppage'=>$helppage, 'text'=>$text, 'component'=>$component); - } else { - $this->helpicon = null; - } - } - - /** - * Parses the $options array and instantiates html_select_option objects in - * the place of the original value => label pairs. This is useful for when you - * need to setup extra html attributes and actions on individual options before - * the component is sent to the renderer - * @return void; - */ - public function initialise_options() { - // If options are already instantiated objects, stop here - $firstoption = reset($this->options); - if ($firstoption instanceof html_select_option || $firstoption instanceof html_select_optgroup) { - return; - } - - if ($this->rendertype == 'radio' && $this->multiple) { - $this->rendertype = 'checkbox'; - } - - // If nested is on, or if radio/checkbox rendertype is set, remove the default Choose option - if ($this->nested || $this->rendertype == 'radio' || $this->rendertype == 'checkbox') { - $this->nothinglabel = ''; - } - - $options = $this->options; - - $this->options = array(); - - if ($this->nested && $this->rendertype != 'menu') { - throw new coding_exception('html_select cannot render nested options as radio buttons or checkboxes.'); - } else 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 ($inoptgroup) { - $optgroup->options[] = $option; - } else { - $this->options[] = $option; - } - } - } - - if ($optgroup) { - $this->options[] = $optgroup; - } - } - } -} - - -/** - * 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 labelled_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; - /** - * @var boolean $disabled Whether or not this option is disabled - */ - public $disabled = false; - - public function __construct() { - $this->label = new html_label(); - } - - /** - * @see html_component::prepare() - * @return void - */ - public function prepare(renderer_base $output, moodle_page $page, $target) { - if (empty($this->text) && (string)$this->text!=='0') { - throw new coding_exception('html_select_option requires a $text value.'); - } - - if (empty($this->label->text)) { - $this->set_label($this->text); - } else if (!($this->label instanceof html_label)) { - $this->set_label($this->label); - } - if (empty($this->id)) { - $this->generate_id(); - } - - parent::prepare($output, $page, $target); - } -} - - -/** - * 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 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(renderer_base $output, moodle_page $page, $target) { - 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($output, $page, $target); - } -} - - /** * Holds all the information required to render a by * {@see core_renderer::table()} or by an overridden version of that @@ -2211,74 +1857,6 @@ class html_textarea extends html_component { } -/** - * Component representing a simple form wrapper. Its purpose is mainly to enclose - * a submit input with the appropriate action and hidden inputs. - * - * @copyright 2009 Nicolas Connault - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @since Moodle 2.0 - */ -class html_form extends html_component { - /** - * @var string $method post or get - */ - public $method = 'post'; - /** - * If a string is given, it will be converted to a moodle_url during prepare() - * @var mixed $url A moodle_url including params or a string - */ - public $url; - /** - * @var boolean $showbutton If true, the submit button will always be shown even if JavaScript is available - */ - public $showbutton = false; - /** - * @var string $targetwindow The name of the target page to open the linked page in. - */ - public $targetwindow = 'self'; - /** - * @var html_button $button A submit button - */ - public $button; - /** - * @var boolean $jssubmitaction If true, the submit button will be hidden when JS is enabled - */ - public $jssubmitaction = false; - /** - * Constructor: sets up the other components in case they are needed - * @return void - */ - public function __construct(array $options = null) { - parent::__construct($options); - $this->button = new html_button(); - $this->button->text = get_string('go'); - } - - /** - * @see lib/html_component#prepare() - * @return void - */ - public function prepare(renderer_base $output, moodle_page $page, $target) { - - if (empty($this->url)) { - throw new coding_exception('A html_form must have a $url value (string or moodle_url).'); - } - - if (is_string($this->url)) { - $this->url = new moodle_url($this->url); - } - - if ($this->method == 'post') { - // automatic CSRF protection - $this->url->param('sesskey', sesskey()); - } - - parent::prepare($output, $page, $target); - } -} - - /** * Component representing a list. * diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 31ab5c3bf61..46f55e76369 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -974,7 +974,7 @@ class core_renderer extends renderer_base { /** * Print a message along with button choices for Continue/Cancel * - * If a string or moodle_url is given instead of a html_button, method defaults to post. + * If a string or moodle_url is given instead of a single_button, method defaults to post. * * @param string $message The question to ask the user * @param single_button|moodle_url|string $continue The single_button component representing the Continue answer. Can also be a moodle_url or string URL @@ -989,7 +989,7 @@ class core_renderer extends renderer_base { } else if ($continue instanceof moodle_url) { $continue = new single_button($continue, get_string('continue'), 'post'); } else { - throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a html_form instance.'); + throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a single_button instance.'); } if ($cancel instanceof single_button) { @@ -999,7 +999,7 @@ class core_renderer extends renderer_base { } else if ($cancel instanceof moodle_url) { $cancel = new single_button($cancel, get_string('cancel'), 'get'); } else { - throw new coding_exception('The cancel param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a html_form instance.'); + throw new coding_exception('The cancel param to $OUTPUT->confirm() must be either a URL (string/moodle_url) or a single_button instance.'); } $output = $this->box_start('generalbox', 'notice'); @@ -1229,65 +1229,6 @@ class core_renderer extends renderer_base { return html_writer::tag('div', array('class' => $select->class), $output); } - /** - * Given a html_form component and an optional rendered submit button, - * outputs a HTML form with correct divs and inputs and a single submit button. - * This doesn't render any other visible inputs. Use moodleforms for these. - * @param html_form $form A html_form instance - * @param string $contents HTML fragment to put inside the form. If given, must contain at least the submit button. - * @return string HTML fragment - */ - public function form(html_form $form, $contents=null) { - $form = clone($form); - $form->prepare($this, $this->page, $this->target); - $this->prepare_event_handlers($form); - $buttonoutput = null; - - if (empty($contents) && !empty($form->button)) { - debugging("You probably want to use \$OUTPUT->single_button(\$form), please read that function's documentation", DEBUG_DEVELOPER); - } else if (empty($contents)) { - $contents = html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('ok'))); - } else if (!empty($form->button)) { - $form->button->prepare($this, $this->page, $this->target); - $this->prepare_event_handlers($form->button); - - $buttonattributes = array('class' => $form->button->get_classes_string(), - 'type' => 'submit', - 'value' => $form->button->text, - 'disabled' => $form->button->disabled ? 'disabled' : null, - 'id' => $form->button->id); - - if ($form->jssubmitaction) { - $buttonattributes['class'] .= ' hiddenifjs'; - } - - $buttonoutput = html_writer::empty_tag('input', $buttonattributes); - - // Hide the submit button if the button has a JS submit action - if ($form->jssubmitaction) { - $buttonoutput = html_writer::start_tag('div', array('id' => "noscript$form->id", 'class'=>'hiddenifjs')) . $buttonoutput . html_writer::end_tag('div'); - } - - } - - $hiddenoutput = ''; - - foreach ($form->url->params() as $var => $val) { - $hiddenoutput .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => $var, 'value' => $val)); - } - - $formattributes = array( - 'method' => $form->method, - 'action' => $form->url->out_omit_querystring(), - 'id' => $form->id, - 'class' => $form->get_classes_string()); - - $divoutput = html_writer::tag('div', array(), $hiddenoutput . $contents . $buttonoutput); - $output = html_writer::tag('form', $formattributes, $divoutput); - - return $output; - } - /** * Returns a string containing a link to the user documentation. * Also contains an icon by default. Shown to teachers and admin only. @@ -1772,46 +1713,6 @@ class core_renderer extends renderer_base { return $this->container($this->render($button), 'closewindow'); } - /** - * Output an element. If an optgroup element is detected, - * this will recursively output its options as well. - * - * @param mixed $option a html_select_option or html_select_optgroup - * @return string the HTML for the - */ - public function select_option($option) { - $option = clone($option); - $option->prepare($this, $this->page, $this->target); - $this->prepare_event_handlers($option); - - if ($option instanceof html_select_option) { - return html_writer::tag('option', array( - 'value' => $option->value, - 'disabled' => $option->disabled ? 'disabled' : null, - 'class' => $option->get_classes_string(), - 'selected' => $option->selected ? 'selected' : null), $option->text); - } else if ($option instanceof html_select_optgroup) { - $output = html_writer::start_tag('optgroup', array('label' => $option->text, 'class' => $option->get_classes_string())); - foreach ($option->options as $selectoption) { - $output .= $this->select_option($selectoption); - } - $output .= html_writer::end_tag('optgroup'); - return $output; - } - } - - /** - * Outputs a