]> git.mjollnir.org Git - moodle.git/commitdiff
added code to include hidden labels in groups in forms
authorjamiesensei <jamiesensei>
Sat, 9 Dec 2006 14:06:15 +0000 (14:06 +0000)
committerjamiesensei <jamiesensei>
Sat, 9 Dec 2006 14:06:15 +0000 (14:06 +0000)
lib/form/dateselector.php
lib/form/datetimeselector.php
lib/form/group.php
lib/form/select.php
lib/form/text.php
lib/formslib.php
mod/data/mod_form.php

index 574c0c95fb6d8fde1df9f715bc06fc4c09e31fb9..aab42ccd417332c648b69a77baf3d5ac3c1f4d6c 100644 (file)
@@ -83,6 +83,11 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group
         if($this->_options['optional']) {
             $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
         }
+        foreach ($this->_elements as $element){
+            if (method_exists($element, 'setHiddenLabel')){
+                $element->setHiddenLabel(true);
+            }
+        }
 
     }
 
index 5aa7ba108ae2ba7cf844fb9083ae548d223c788b..4f3e1968a5e4ba60bdf9cd7bda9174457841231e 100644 (file)
@@ -90,6 +90,11 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{
         if($this->_options['optional']) {
             $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true);
         }
+        foreach ($this->_elements as $element){
+            if (method_exists($element, 'setHiddenLabel')){
+                $element->setHiddenLabel(true);
+            }
+        }
 
     }
 
index 88fbbb7e3e1df5a76047aba99717e1ad827678ca..714bdddca404e7b8dc1c46de723df75f0ea6b20e 100644 (file)
@@ -52,5 +52,13 @@ class MoodleQuickForm_group extends HTML_QuickForm_group{
     function getElementTemplateType(){
         return $this->_elementTemplateType;
     }
+    function setElements($elements){
+        parent::setElements($elements);
+        foreach ($this->_elements as $element){
+            if (method_exists($element, 'setHiddenLabel')){
+                $element->setHiddenLabel(true);
+            }
+        }
+    }
 }
 ?>
\ No newline at end of file
index b86d295bd708fac43f518a69232aefae22f7294b..e349211b82f623ce4015db374c92d026fbd38732 100644 (file)
@@ -14,6 +14,37 @@ class MoodleQuickForm_select extends HTML_QuickForm_select{
      * @var string
      */
     var $_helpbutton='';
+    var $_hiddenLabel=false;
+    function setHiddenLabel($hiddenLabel){
+        $this->_hiddenLabel = $hiddenLabel;
+    }
+    function toHtml(){
+        $this->_generateId();
+        if ($this->_hiddenLabel){
+            return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
+                        $this->getLabel().'</label>'.parent::toHtml();
+        } else {
+             return parent::toHtml();
+        }
+    }
+   /**
+    * Automatically generates and assigns an 'id' attribute for the element.
+    *
+    * Currently used to ensure that labels work on radio buttons and
+    * checkboxes. Per idea of Alexander Radivanovich.
+    * Overriden in moodleforms to remove qf_ prefix.
+    *
+    * @access private
+    * @return void
+    */
+    function _generateId()
+    {
+        static $idx = 1;
+
+        if (!$this->getAttribute('id')) {
+            $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
+        }
+    } // end func _generateId
     /**
      * set html for help button
      *
@@ -63,5 +94,6 @@ class MoodleQuickForm_select extends HTML_QuickForm_select{
             }
         }
     } // end func removeOption
+
 }
 ?>
\ No newline at end of file
index b83ee767f7d6599ab60750c7aebba81e818dafff..cae359cfa88ceb15fecc77252a747b875b149e5d 100644 (file)
@@ -14,6 +14,37 @@ class MoodleQuickForm_text extends HTML_QuickForm_text{
      * @var string
      */
     var $_helpbutton='';
+    var $_hiddenLabel=false;
+    function setHiddenLabel($hiddenLabel){
+        $this->_hiddenLabel = $hiddenLabel;
+    }
+    function toHtml(){
+        $this->_generateId();
+        if ($this->_hiddenLabel){
+            return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
+                        $this->getLabel().'</label>'.parent::toHtml();
+        } else {
+             return parent::toHtml();
+        }
+    }
+   /**
+    * Automatically generates and assigns an 'id' attribute for the element.
+    *
+    * Currently used to ensure that labels work on radio buttons and
+    * checkboxes. Per idea of Alexander Radivanovich.
+    * Overriden in moodleforms to remove qf_ prefix.
+    *
+    * @access private
+    * @return void
+    */
+    function _generateId()
+    {
+        static $idx = 1;
+
+        if (!$this->getAttribute('id')) {
+            $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
+        }
+    } // end func _generateId
     /**
      * set html for help button
      *
index 838838e91ff5c1caf66699c95f14711848218206..a772712414f56f22a09cf4c9bc4b50117729e6fd 100644 (file)
@@ -89,7 +89,8 @@ class moodleform {
      * @return moodleform
      */
     function moodleform($action, $customdata=null, $method='post', $target='', $attributes=null) {
-        $this->_formname = rtrim(get_class($this), '_form');
+        //strip '_form' from the end of class name to make form 'id' attribute.
+        $this->_formname = preg_replace('/_form$/', '', get_class($this), 1);
         $this->_customdata = $customdata;
         $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes);
 
@@ -1038,7 +1039,10 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{
         } else {
             $id = $element->getName();
         }
-        $element->updateAttributes(array('id'=>'id_'.$id));
+        $id = preg_replace('/^qf_/', '', $id, 1);
+        if (strpos($id, 'id_') !== 0){
+            $element->updateAttributes(array('id'=>'id_'.$id));
+        }
         parent::renderElement($element, $required, $error);
     }
     function finishForm(&$form){
index 3e9192d83377e204b88e9ba294150a200f21acdb..eda4b9cde4b238f2f3f6adda400e6e2870a2e1ee 100644 (file)
@@ -18,7 +18,7 @@ class data_mod_form extends moodleform_mod {
 
                $mform->addElement('htmleditor', 'intro', get_string('intro', 'data'));
                $mform->setType('intro', PARAM_RAW);
-               $mform->addRule('intro', get_string('required'), 'required', null, 'client');
+               $mform->addRule('intro', null, 'required', null, 'client');
 
         $mform->addElement('date_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'), array('optional'=>true));