]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17239 - numeric item does not support 0-values and float-values
authoragrabs <agrabs>
Fri, 14 Nov 2008 16:51:03 +0000 (16:51 +0000)
committeragrabs <agrabs>
Fri, 14 Nov 2008 16:51:03 +0000 (16:51 +0000)
mod/feedback/item/numeric/lib.php

index ef6722965983bffdd41fab86a650826c1f555a3d..b20214c3cda3376caee7eac8b9e44d198815cab4 100644 (file)
@@ -4,8 +4,18 @@ require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
 
 class feedback_item_numeric extends feedback_item_base {
     var $type = "numeric";
-    function init() {
+    var $sep_dec, $sep_thous;
     
+    function init() {
+        $this->sep_dec = get_string('separator_decimal', 'feedback');
+        if(substr($this->sep_dec, 0, 2) == '[['){
+            $this->sep_dec = FEEDBACK_DECIMAL;
+        }
+        
+        $this->sep_thous = get_string('separator_thousand', 'feedback');
+        if(substr($this->sep_thous, 0, 2) == '[['){
+            $this->sep_thous = FEEDBACK_THOUSAND;
+        }    
     }
     
     function show_edit($item) {
@@ -26,9 +36,10 @@ class feedback_item_numeric extends feedback_item_base {
         $item_form->itemname->setValue($item->name);
         
         $range_from_to = explode('|',$item->presentation);
-        $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
-        $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
         
+        $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? str_replace(FEEDBACK_DECIMAL, $this->sep_dec, floatval($range_from_to[0])) : '-';
+        $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? str_replace(FEEDBACK_DECIMAL, $this->sep_dec, floatval($range_from_to[1])) : '-';
+                
         $item_form->selectfrom->setValue($range_from);
         
         $item_form->selectto->setValue($range_to);
@@ -71,15 +82,6 @@ class feedback_item_numeric extends feedback_item_base {
     }
 
     function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
-        $sep_dec = get_string('separator_decimal', 'feedback');
-        if(substr($sep_dec, 0, 2) == '[['){
-            $sep_dec = FEEDBACK_DECIMAL;
-        }
-        
-        $sep_thous = get_string('separator_thousand', 'feedback');
-        if(substr($sep_thous, 0, 2) == '[['){
-            $sep_thous = FEEDBACK_THOUSAND;
-        }
         
         // $values = feedback_get_group_values($item, $groupid, $courseid);
         $values = $this->get_analysed($item, $groupid, $courseid);
@@ -89,13 +91,13 @@ class feedback_item_numeric extends feedback_item_base {
             // $itemnr++;
             echo '<tr><th colspan="2" align="left">'. $itemnr . '&nbsp;' . $item->name .'</th></tr>';
             foreach($values->data as $value) {
-                echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . $value . '</td></tr>';
+                echo '<tr><td colspan="2" valign="top" align="left">-&nbsp;&nbsp;' . number_format($value, 2, $this->sep_dec, $this->sep_thous) . '</td></tr>';
             }
             //echo '</table>';
             if(isset($values->avg)) {
-                $avg = number_format($values->avg, 2, $sep_dec, $sep_thous);
+                $avg = number_format($values->avg, 2, $this->sep_dec, $this->sep_thous);
             } else {
-                $avg = number_format(0, 2, $sep_dec, $sep_thous);
+                $avg = number_format(0, 2, $this->sep_dec, $this->sep_thous);
             }
             echo '<tr><td align="left" colspan="2"><b>'.get_string('average', 'feedback').': '.$avg.'</b></td></tr>';
         }
@@ -136,9 +138,9 @@ class feedback_item_numeric extends feedback_item_base {
         //get the range
         $range_from_to = explode('|',$item->presentation);
         //get the min-value
-        $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
+        $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : 0;
         //get the max-value
-        $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
+        $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : 0;
         if($highlightrequire AND (!$this->check_value($value, $item))) {
             $highlight = 'bgcolor="#FFAAAA" class="missingrequire"';
         }else {
@@ -150,16 +152,16 @@ class feedback_item_numeric extends feedback_item_base {
             <?php 
                 echo format_text($item->name . $requiredmark, true, false, false);
                 switch(true) {
-                    case ($range_from === 0 AND $range_to > 0):
-                        echo ' ('.get_string('maximal', 'feedback').': '.$range_to.')';
+                    case ($range_from === '-' AND is_numeric($range_to)):
+                        echo ' ('.get_string('maximal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
                         break;
-                    case ($range_from > 0 AND $range_to === 0):
-                        echo ' ('.get_string('minimal', 'feedback').': '.$range_from.')';
+                    case (is_numeric($range_from) AND $range_to === '-'):
+                        echo ' ('.get_string('minimal', 'feedback').': '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).')';
                         break;
-                    case ($range_from === 0 AND $range_to === 0):
+                    case ($range_from === '-' AND $range_to === '-'):
                         break;
                     default:
-                        echo ' ('.$range_from.'-'.$range_to.')';
+                        echo ' ('.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_from).' - '.str_replace(FEEDBACK_DECIMAL, $this->sep_dec, $range_to).')';
                         break;
                 }
             ?>
@@ -169,7 +171,7 @@ class feedback_item_numeric extends feedback_item_base {
         if($readonly){
             // print_simple_box_start($align);
             print_box_start('generalbox boxalign'.$align);
-            echo $value ? $value : '&nbsp;';
+            echo (is_numeric($value)) ? number_format($value, 2, $this->sep_dec, $this->sep_thous) : '&nbsp;';
             // print_simple_box_end();
             print_box_end();
         }else {
@@ -186,26 +188,27 @@ class feedback_item_numeric extends feedback_item_base {
     }
 
     function check_value($value, $item) {
+        $value = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $value);
         //if the item is not required, so the check is true if no value is given
         if((!isset($value) OR $value == '') AND $item->required != 1) return true;
         if(!is_numeric($value))return false;
         
         $range_from_to = explode('|',$item->presentation);
-        $range_from = isset($range_from_to[0]) ? intval($range_from_to[0]) : 0;
-        $range_to = isset($range_from_to[1]) ? intval($range_from_to[1]) : 0;
+        $range_from = (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) ? floatval($range_from_to[0]) : '-';
+        $range_to = (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) ? floatval($range_from_to[1]) : '-';
         
         switch(true) {
-            case ($range_from === 0 AND $range_to > 0):
-                if(intval($value) <= $range_to) return true;
+            case ($range_from === '-' AND is_numeric($range_to)):
+                if(floatval($value) <= $range_to) return true;
                 break;
-            case ($range_from > 0 AND $range_to === 0):
-                if(intval($value) >= $range_from) return true;
+            case (is_numeric($range_from) AND $range_to === '-'):
+                if(floatval($value) >= $range_from) return true;
                 break;
-            case ($range_from === 0 AND $range_to === 0):
+            case ($range_from === '-' AND $range_to === '-'):
                 return true;
                 break;
             default:
-                if(intval($value) >= $range_from AND intval($value) <= $range_to) return true;
+                if(floatval($value) >= $range_from AND floatval($value) <= $range_to) return true;
                 break;
         }
         
@@ -213,8 +216,10 @@ class feedback_item_numeric extends feedback_item_base {
     }
 
     function create_value($data) {
-        if($data AND $data != '') {
-            $data = intval($data);
+        $data = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data);
+        
+        if(is_numeric($data)) {
+            $data = floatval($data);
         }else {
             $data = '';
         }
@@ -222,7 +227,29 @@ class feedback_item_numeric extends feedback_item_base {
     }
 
     function get_presentation($data) {
-        return $data->numericrangefrom . '|'. $data->numericrangeto;
+        $num1 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangefrom);
+        if(is_numeric($num1)) {
+            $num1 = floatval($num1);
+        }else {
+            $num1 = '-';
+        }
+        
+        $num2 = str_replace($this->sep_dec, FEEDBACK_DECIMAL, $data->numericrangeto);
+        if(is_numeric($num2)) {
+            $num2 = floatval($num2);
+        }else {
+            $num2 = '-';
+        }
+        
+        if($num1 === '-' OR $num2 === '-') {
+            return $num1 . '|'. $num2;
+        }
+        
+        if($num1 > $num2) {
+            return $num2 . '|'. $num1;
+        }else {
+            return $num1 . '|'. $num2;        
+        }
     }
 
     function get_hasvalue() {