From: nicolasconnault <nicolasconnault> Date: Tue, 4 Aug 2009 02:42:24 +0000 (+0000) Subject: MDL-19756 Migrated print_textfield, choose_from_radio and print_checkbox X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=6a5c71b95c1348f6863ae6435a19edf51ef9f072;p=moodle.git MDL-19756 Migrated print_textfield, choose_from_radio and print_checkbox --- diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 60c42b09ff..a9c63aa4c6 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -3449,7 +3449,7 @@ function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', // debugging('print_checkbox() has been deprecated. Please change your code to use $OUTPUT->checkbox($checkbox).'); global $OUTPUT; - + if (!empty($script)) { debugging('The use of the $script param in print_checkbox has not been migrated into $OUTPUT->checkbox. Please use $checkbox->add_action().', DEBUG_DEVELOPER); } @@ -3470,3 +3470,38 @@ function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', } } + + +/** + * Display an standard html text field with an optional label + * + * @deprecated since Moodle 2.0 + * + * @param string $name The name of the text field + * @param string $value The value of the text field + * @param string $alt The info to be inserted in the alt tag + * @param int $size Sets the size attribute of the field. Defaults to 50 + * @param int $maxlength Sets the maxlength attribute of the field. Not set by default + * @param bool $return Whether this function should return a string or output + * it (defaults to false) + * @return string|void If $return=true returns string, else echo's and returns void + */ +function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $return=false) { + + // debugging('print_textfield() has been deprecated. Please change your code to use $OUTPUT->textfield($field).'); + + global $OUTPUT; + + $field = html_field::make_text($name, $value, $alt, $maxlength); + $field->style = "width: {$size}px;"; + + $output = $OUTPUT->textfield($field); + + if (empty($return)) { + echo $output; + } else { + return $output; + } + +} + diff --git a/lib/outputlib.php b/lib/outputlib.php index 4800459186..4a342f969b 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -2924,7 +2924,6 @@ class moodle_core_renderer extends moodle_renderer_base { } $option->prepare(); - $option->generate_id(); $option->label->for = $option->id; $this->prepare_event_handlers($option); @@ -3015,6 +3014,28 @@ class moodle_core_renderer extends moodle_renderer_base { } } + /** + * Output an <input type="text"> element + * + * @param html_field $field a html_field object + * @return string the HTML for the <input> + */ + public function textfield($field) { + $field->prepare(); + $this->prepare_event_handlers($field); + $output = $this->output_start_tag('span', array('class' => "textfield $field->name")); + $output .= $this->output_empty_tag('input', array( + 'type' => 'text', + 'name' => $field->name, + 'id' => $field->id, + 'value' => $field->value, + 'style' => $field->style, + 'alt' => $field->alt, + 'maxlength' => $field->maxlength)); + $output .= $this->output_end_tag('span'); + return $output; + } + /** * Outputs a <label> element. * @param html_label $label A html_label object @@ -3518,7 +3539,7 @@ class moodle_html_component { /** * Internal method for generating a unique ID for the purpose of event handlers. */ - public function generate_id() { + protected function generate_id() { // Generate an id that is not already used. do { $newid = get_class($this) . '-' . substr(sha1(microtime() * rand(0, 500)), 0, 6); @@ -4022,6 +4043,9 @@ class html_select_option extends moodle_html_component { } else if (!($this->label instanceof html_label)) { $this->set_label($this->label); } + if (empty($this->id)) { + $this->generate_id(); + } parent::prepare(); } @@ -4055,6 +4079,75 @@ class html_select_optgroup extends moodle_html_component { } } +/** + * This class represents an input field + * + * @copyright 2009 Nicolas Connault + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 2.0 + */ +class html_field extends moodle_html_component { + /** + * @var string $name The name attribute of the field + */ + public $name; + /** + * @var string $value The value attribute of the field + */ + public $value; + /** + * @var string $type The type attribute of the field (text, submit, checkbox etc) + */ + public $type; + /** + * @var string $maxlength The maxlength attribute of the field (only applies to text type) + */ + public $maxlength; + /** + * @var mixed $label The label for that component. String or html_label object + */ + public $label; + + public function __construct() { + $this->label = new html_label(); + } + + /** + * @see moodle_html_component::prepare() + * @return void + */ + public function prepare() { + if (empty($this->style)) { + $this->style = 'width: 4em;'; + } + if (empty($this->id)) { + $this->generate_id(); + } + parent::prepare(); + } + + /** + * Shortcut for creating a text input component. + * @param string $name The name of the text field + * @param string $value The value of the text field + * @param string $alt The info to be inserted in the alt tag + * @param int $maxlength Sets the maxlength attribute of the field. Not set by default + * @return html_field The field component + */ + public static function make_text($name='unnamed', $value, $alt, $maxlength=0) { + $field = new html_field(); + if (empty($alt)) { + $alt = get_string('textfield'); + } + $field->type = 'text'; + $field->name = $name; + $field->value = $value; + $field->alt = $alt; + $field->maxlength = $maxlength; + return $field; + } +} + /** * This class represents how a block appears on a page. * diff --git a/lib/weblib.php b/lib/weblib.php index 8f4f23b523..61255e6b42 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -621,50 +621,6 @@ function close_window($delay = 0, $reloadopener = false) { } -/** - * Display an standard html text field with an optional label - * - * @param string $name The name of the text field - * @param string $value The value of the text field - * @param string $alt The info to be inserted in the alt tag - * @param int $size Sets the size attribute of the field. Defaults to 50 - * @param int $maxlength Sets the maxlength attribute of the field. Not set by default - * @param bool $return Whether this function should return a string or output - * it (defaults to false) - * @return string|void If $return=true returns string, else echo's and returns void - */ -function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $return=false) { - - static $idcounter = 0; - - if (empty($name)) { - $name = 'unnamed'; - } - - if (empty($alt)) { - $alt = 'textfield'; - } - - if (!empty($maxlength)) { - $maxlength = ' maxlength="'.$maxlength.'" '; - } - - $htmlid = 'auto-tf'.sprintf('%04d', ++$idcounter); - $output = '<span class="textfield '.$name."\">"; - $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />'; - - $output .= '</span>'."\n"; - - if (empty($return)) { - echo $output; - } else { - return $output; - } - -} - - - /** * Validates an email to make sure it makes sense. *