From d894edd44d2dd60b22a16aba16766f34bd2eef79 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 3 Jan 2010 17:08:21 +0000 Subject: [PATCH] MDL-21198 new simple_button output component, this could finally solve potential "form x button" confusion --- lib/deprecatedlib.php | 17 +++++----------- lib/outputcomponents.php | 42 +++++++++++++++++++++++++++++++++------- lib/outputrenderers.php | 16 ++++++--------- mod/quiz/accessrules.php | 7 ++----- mod/quiz/summary.php | 2 +- 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 744fbc8669..7304671462 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -2483,11 +2483,9 @@ function button_to_popup_window ($url, $name=null, $linkname=null, } // Create a html_button object - $form = new html_form(); - $form->button->text = $linkname; + $form = new single_button($url, $text, 'post'); $form->button->title = $title; $form->button->id = $id; - $form->url = $url; $form->add_class($class); // Parse the $options string @@ -2550,19 +2548,14 @@ function print_single_button($link, $options, $label='OK', $method='get', $notus // Cast $options to array $options = (array) $options; - $form = new html_form(); - $form->url = new moodle_url($link, $options); - $form->button->text = $label; - $form->button->disabled = $disabled; - $form->button->title = $tooltip; - $form->method = $method; - $form->id = $formid; + + $button = new sibngle_button(new moodle_url($link, $options), $label, $method, array('disabled'=>$disabled, 'title'=>$tooltip, 'id'=>$id)); if ($jsconfirmmessage) { - $form->button->add_confirm_action($jsconfirmmessage); + $button->button->add_confirm_action($jsconfirmmessage); } - $output = $OUTPUT->single_button($form); + $output = $OUTPUT->single_button($button); if ($return) { return $output; diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index c1f0d07a48..e0404e7701 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -1497,10 +1497,6 @@ class html_form extends html_component { * @var mixed $url A moodle_url including params or a string */ public $url; - /** - * @var array $params Optional array of parameters. Ignored if $url instanceof moodle_url - */ - public $params = array(); /** * @var boolean $showbutton If true, the submit button will always be shown even if JavaScript is available */ @@ -1524,7 +1520,7 @@ class html_form extends html_component { public function __construct(array $options = null) { parent::__construct($options); $this->button = new html_button(); - $this->button->text = get_string('ok'); + $this->button->text = get_string('go'); } /** @@ -1537,11 +1533,12 @@ class html_form extends html_component { throw new coding_exception('A html_form must have a $url value (string or moodle_url).'); } - if (!($this->url instanceof moodle_url)) { - $this->url = new moodle_url($this->url, $this->params); + if (is_string($this->url)) { + $this->url = new moodle_url($this->url); } if ($this->method == 'post') { + // automatic CSRF protection $this->url->param('sesskey', sesskey()); } @@ -1549,6 +1546,7 @@ class html_form extends html_component { } public static function make_button($url, array $params=null, $label=null, $method='post', array $formoptions=null) { + //TODO: to be removed soon, repalced by ew single_button() $form = new html_form($formoptions); $form->url = new moodle_url($url, $params); if ($label !== null) { @@ -1561,6 +1559,36 @@ class html_form extends html_component { } +/** + * A component representing a simple form with only one button. + * + * @copyright 2009 Petr Skoda + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class single_button extends html_form { + /** + * Constructor + * @param string|moodle_url + * @param string $label button text + * @param string $method get or post submit method + * @param array $options associative array form attributes + {disabled, title} + */ + public function __construct($url, $label, $method='post', array $options=null) { + parent::__construct($options); + $this->url = $url; + $form->method = $method; + $this->button->text = $label; + if (!empty($options['disabled'])) { + $this->button->disabled = true; + } + if (!empty($options['title'])) { + $this->button->title = $options['title']; + } + } +} + + /** * Component representing a list. * diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 668786e6dc..2a281981ad 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -997,27 +997,23 @@ class core_renderer extends renderer_base { * Returns a form with single button. * If first parameter is html_form instance all other parameters are ignored. * - * @param string|moodle_url|html_form $url_or_form + * @param string|moodle_url|single_button $singlebutton_or_form * @param string $label button text * @param string $method get or post submit method * @param array $options associative array {disabled, title} * @return string HTML fragment */ - public function single_button($url_or_form, $label=null, $method='post', array $options=null) { - if ($url_or_form instanceof html_form) { - $form = $url_or_form; + public function single_button($singlebutton_or_form, $label=null, $method='post', array $options=null) { + if ($singlebutton_or_form instanceof single_button) { + $button = $singlebutton_or_form; if (func_num_args() > 1) { debugging('html_form instance used as first parameter of $OUTPUT->single_button(), all other parameters are ignored.'); } } else { - $form = html_form::make_button($url_or_form, null, $label, $method); - $form->button->disabled = !empty($options['disabled']); - if (!empty($options['title'])) { - $form->button->title = $options['title']; - } + $button = new single_button($url_or_form, $label, $method, $options); } - return $this->button($form); + return $this->button($button); } /** diff --git a/mod/quiz/accessrules.php b/mod/quiz/accessrules.php index 3cc1d45a13..21a9df77cf 100644 --- a/mod/quiz/accessrules.php +++ b/mod/quiz/accessrules.php @@ -220,7 +220,7 @@ class quiz_access_manager { if ($this->securewindow_required($canpreview)) { $this->_securewindowrule->print_start_attempt_button($buttontext, $strconfirmstartattempt); } else { - $form = html_form::make_button($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid()), $buttontext); + $form = new single_button(new moodle_url($this->_quizobj->start_attempt_url(), array('cmid' => $this->_quizobj->get_cmid())), $buttontext); if ($strconfirmstartattempt) { $form->button->add_confirm_action($strconfirmstartattempt); } @@ -745,10 +745,7 @@ class securewindow_access_rule extends quiz_access_rule_base { */ public function make_review_link($linktext, $attemptid) { global $OUTPUT; - $form = new html_form(); - $form->button->text = $linktext; - $form->button->title = $form->button->text; - $form->url = $this->_quizobj->review_url($attemptid); + $form = new single_button($this->_quizobj->review_url($attemptid), $linktext); $form->button->add_action(new popup_action('click', $form->url, 'quizpopup', $this->windowoptions)); return $OUTPUT->single_button($form); } diff --git a/mod/quiz/summary.php b/mod/quiz/summary.php index 275da221d3..4cc204538c 100644 --- a/mod/quiz/summary.php +++ b/mod/quiz/summary.php @@ -124,7 +124,7 @@ $options = array( 'sesskey' => sesskey(), ); -$form = html_form::make_button($attemptobj->processattempt_url(), $options, get_string('finishattempt', 'quiz')); +$form = new single_button(new moodle_url($attemptobj->processattempt_url(), $options), get_string('finishattempt', 'quiz')); $form->id = 'responseform'; $form->button->add_confirm_action(get_string('confirmclose', 'quiz')); -- 2.39.5