]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19756 Migrated print_textfield, choose_from_radio and print_checkbox
authornicolasconnault <nicolasconnault>
Tue, 4 Aug 2009 02:42:24 +0000 (02:42 +0000)
committernicolasconnault <nicolasconnault>
Tue, 4 Aug 2009 02:42:24 +0000 (02:42 +0000)
lib/deprecatedlib.php
lib/outputlib.php
lib/weblib.php

index 60c42b09ffa0c9f7ee07cd318b6376087af1cf95..a9c63aa4c68fb0a8f8e3d403bb0eae9e72570c7d 100644 (file)
@@ -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;
+    }
+
+}
+
index 48004591863fcf31b2fbec1963d025b81aae29bb..4a342f969bf0a258e0ad39a54556c51963b16461 100644 (file)
@@ -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.
  *
index 8f4f23b523647804316f42635eecb3ebdb073770..61255e6b429cd444bf6e822506a1cc32a3217dae 100644 (file)
@@ -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.
  *