]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10791 improve handling of localised floating point numbers in moodle; replaced...
authorskodak <skodak>
Fri, 10 Aug 2007 15:00:35 +0000 (15:00 +0000)
committerskodak <skodak>
Fri, 10 Aug 2007 15:00:35 +0000 (15:00 +0000)
20 files changed:
enrol/authorize/config_form.php
enrol/authorize/locallib.php
enrol/paypal/enrol.php
enrol/paypal/ipn.php
grade/edit/tree/calculation.php
grade/edit/tree/calculation_form.php
grade/edit/tree/grade.php
grade/edit/tree/grade_form.php
grade/edit/tree/item.php
grade/edit/tree/item_form.php
grade/edit/tree/outcomeitem.php
grade/edit/tree/outcomeitem_form.php
grade/report/grader/lib.php
grade/report/lib.php
grade/report/user/lib.php
lang/en_utf8/langconfig.php
lib/adminlib.php
lib/mathslib.php
lib/moodlelib.php
mod/lesson/report.php

index b3b079e3c23553df01649ffd09cf73f1fb1f9f7d..252d6d1121ad2aa2ff7e25a1b28ae0599f40e7b5 100755 (executable)
@@ -26,7 +26,7 @@ if (isset($CFG->an_cutoff)) {
     $frm->an_cutoff_hour = $hrs; $frm->an_cutoff_min = $mins;
 }
 if (!isset($frm->an_cutoff_hour)) {
-    $timezone = format_float(get_user_timezone_offset(), 1);
+    $timezone = round(get_user_timezone_offset(), 1);
     $frm->an_cutoff_hour = intval($timezone);
     $frm->an_cutoff_min = (intval(round($timezone)) != intval($timezone)) ? 35 : 5;
 }
index fc722bc366daea82d4d302ecf9507f0de167cd93..648228dbcaf9356d05002e73cde90d543e335670 100644 (file)
@@ -315,12 +315,12 @@ function authorize_print_order_details($orderno)
         if ($refund = get_record_sql($sql)) {
             $extra->sum = floatval($refund->refunded);
         }
-        $upto = format_float($order->amount - $extra->sum, 2);
+        $upto = round($order->amount - $extra->sum, 2);
         if ($upto <= 0) {
             error("Refunded to original amount.");
         }
         else {
-            $amount = format_float(optional_param('amount', $upto), 2);
+            $amount = round(optional_param('amount', $upto), 2);
             if (($amount > $upto) or empty($confirm)) {
                 $a = new stdClass;
                 $a->upto = $upto;
index e571851a1ddc418d337d9672a3c58addc0d839a2..4d901117843831796c4a61058ef3f2c25268a164 100644 (file)
@@ -22,8 +22,6 @@ function print_entry($course) {
     } else {
         $cost = (float) $course->cost;
     }
-    $cost = format_float($cost, 2);
-
 
     if (abs($cost) < 0.01) { // no cost, default to base class entry to course
 
index 5c6becab354168a9e10237b6f39a482c2627b71a..e88367e272410304411102f2a8f1038df660ff2c 100644 (file)
             } else {
                 $cost = (float) $course->cost;
             }
-            $cost = format_float($cost, 2);
 
             if ($data->payment_gross < $cost) {
+                $cost = format_float($cost, 2);
                 email_paypal_error_to_admin("Amount paid is not enough ($data->payment_gross < $cost))", $data);
                 die;
 
index b9d7f2328d1b75df88d3efd0fa9c50e7659cfad5..3583370e2e324a323c099ec1947ea58cccbd9532 100644 (file)
@@ -2,6 +2,7 @@
 
 require_once '../../../config.php';
 require_once $CFG->dirroot.'/grade/lib.php';
+require_once $CFG->libdir.'/mathslib.php';
 require_once 'calculation_form.php';
 
 $courseid = required_param('courseid', PARAM_INT);
@@ -36,11 +37,13 @@ if ($mform->is_cancelled()) {
 
 }
 
-$calculation = grade_item::denormalize_formula($grade_item->calculation, $grade_item->courseid);
+$calculation = calc_formula::localize($grade_item->calculation);
+$calculation = grade_item::denormalize_formula($calculation, $grade_item->courseid);
 $mform->set_data(array('courseid'=>$grade_item->courseid, 'calculation'=>$calculation, 'id'=>$grade_item->id, 'itemname'=>$grade_item->itemname));
 
 if ($data = $mform->get_data(false)) {
-    $grade_item->set_calculation($data->calculation);
+    $calculation = calc_formula::unlocalize($data->calculation);
+    $grade_item->set_calculation($calculation);
     redirect($returnurl);
 }
 
index 8a8f1601e3ad12dca4d89bb380d25f39bcc3a61e..c53cf0e91f653bb0d8eab4536ee7611c5354ccbf 100644 (file)
@@ -130,7 +130,8 @@ class edit_calculation_form extends moodleform {
         // check the calculation formula
         if ($data['calculation'] != '') {
             $grade_item = grade_item::fetch(array('id'=>$data['id'], 'courseid'=>$data['courseid']));
-            $result = $grade_item->validate_formula(stripslashes($data['calculation']));
+            $calculation = calc_formula::unlocalize(stripslashes($data['calculation']));
+            $result = $grade_item->validate_formula($calculation);
             if ($result !== true) {
                 $errors['calculation'] = $result;
             }
index bef8cf72e4b0722abc656540a471f03a0aa7a607..701fb63e306ffef0f310de7623d936c0b0489f01 100644 (file)
@@ -2,6 +2,7 @@
 
 require_once '../../../config.php';
 require_once $CFG->dirroot.'/grade/lib.php';
+require_once $CFG->dirroot.'/grade/report/lib.php';
 require_once 'grade_form.php';
 
 $courseid = required_param('courseid', PARAM_INT);
@@ -103,6 +104,20 @@ if ($grade = get_record('grade_grades', 'itemid', $grade_item->id, 'userid', $us
         $grade->locked = 1;
     }
 
+    // normalize the final grade value
+    if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
+        if (empty($grade->finalgrade)) {
+            $grade->finalgrade = -1;
+        } else {
+            $grade->finalgrade = (int)$grade->finalgrade;
+        }
+    } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
+        $decimalpoints = grade_report::get_pref('decimalpoints', $grade_item->id);
+        $grade->finalgrade = format_float($grade->finalgrade, $decimalpoints);
+    }
+
+    $grade->oldgrade = $grade->finalgrade;
+
     $mform->set_data($grade);
 
 } else {
@@ -117,11 +132,14 @@ if ($mform->is_cancelled()) {
     $old_grade_grade = new grade_grade(array('userid'=>$data->userid, 'itemid'=>$grade_item->id), true); //might not exist yet
 
     // fix no grade for scales
-    if (!isset($data->finalgrade)) {
+    if (!isset($data->finalgrade) or $data->finalgrade == $data->oldgrade) {
         $data->finalgrade = $old_grade_grade->finalgrade;
 
     } else if ($grade_item->gradetype == GRADE_TYPE_SCALE and $data->finalgrade < 1) {
         $data->finalgrade = NULL;
+
+    } else if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
+        $data->finalgrade = unformat_float($data->finalgrade);
     }
 
     if (!isset($data->feedback)) {
index 84d980a71eba518db8294dc70859a6102f69badb..4a264cc9ee3a81b12d59cd4c6e6f34cab2aa7e19 100755 (executable)
@@ -78,6 +78,8 @@ class edit_grade_form extends moodleform {
         $mform->setHelpButton('feedbackformat', array('textformat', get_string('helpformatting')));
 
         // hidden params
+        $mform->addElement('hidden', 'oldgrade');
+
         $mform->addElement('hidden', 'id', 0);
         $mform->setType('id', PARAM_INT);
 
index 3dec92cc661216abda1baea14eacd36202c33ee1..eee294eaf65d3e2f80f985ad24025c994c97ddda 100644 (file)
@@ -32,24 +32,41 @@ if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) {
         $url = $CFG->wwwroot.'/grade/edit/tree/outcomeitem.php?id='.$id.'&amp;courseid='.$courseid;
         redirect($gpr->add_url_params($url));
     }
+    // Get Item preferences
+    $item->pref_gradedisplaytype = grade_report::get_pref('gradedisplaytype', $item->id);
+    $item->pref_decimalpoints    = grade_report::get_pref('decimalpoints', $item->id);
 
-    if ($item->hidden > 1) {
-        $item->hiddenuntil = $item->hidden;
-        $item->hidden = 0;
-    } else {
-        $item->hiddenuntil = 0;
-    }
+    $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id);
 
-    $item->locked = !empty($item->locked);
+    $decimalpoints = grade_report::get_pref('decimalpoints', $item->id);
 
+} else {
+    $item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'));
     // Get Item preferences
-    $item->pref_gradedisplaytype = grade_report::get_pref('gradedisplaytype', $id);
-    $item->pref_decimalpoints    = grade_report::get_pref('decimalpoints', $id);
+    $item->pref_gradedisplaytype = grade_report::get_pref('gradedisplaytype');
+    $item->pref_decimalpoints    = grade_report::get_pref('decimalpoints');
 
-    $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id);
-    $mform->set_data($item);
+    $decimalpoints = grade_report::get_pref('decimalpoints');
+}
+
+if ($item->hidden > 1) {
+    $item->hiddenuntil = $item->hidden;
+    $item->hidden = 0;
+} else {
+    $item->hiddenuntil = 0;
 }
 
+$item->locked = !empty($item->locked);
+
+$item->grademax        = format_float($item->grademax, $decimalpoints);
+$item->grademin        = format_float($item->grademin, $decimalpoints);
+$item->gradepass       = format_float($item->gradepass, $decimalpoints);
+$item->multfactor      = format_float($item->multfactor, 4);
+$item->plusfactor      = format_float($item->plusfactor, 4);
+$item->aggregationcoef = format_float($item->aggregationcoef, 4);
+
+$mform->set_data($item);
+
 if ($data = $mform->get_data(false)) {
     if (array_key_exists('calculation', $data)) {
         $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
@@ -65,6 +82,13 @@ if ($data = $mform->get_data(false)) {
     unset($data->locked);
     unset($data->locktime);
 
+    $convert = array('grademax', 'grademin', 'gradepass', 'multfactor', 'plusfactor', 'aggregationcoef');
+    foreach ($convert as $param) {
+        if (array_key_exists($param, $data)) {
+            $data->$param = unformat_float($data->$param);
+        }
+    }
+
     $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
     grade_item::set_properties($grade_item, $data);
 
index a5c4effbf3ee17215c80cf4c8c5c142c966808ef..c76f6eeb00cca162d30099deb9196b88ddea03be 100644 (file)
@@ -49,39 +49,33 @@ class edit_item_form extends moodleform {
         $mform->setHelpButton('grademax', array(false, get_string('grademax', 'grades'),
                 false, true, false, get_string('grademaxhelp', 'grades')));
         $mform->disabledIf('grademax', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
-        $mform->setDefault('grademax', 100.0);
 
         $mform->addElement('text', 'grademin', get_string('grademin', 'grades'));
         $mform->setHelpButton('grademin', array(false, get_string('grademin', 'grades'),
                 false, true, false, get_string('grademinhelp', 'grades')));
         $mform->disabledIf('grademin', 'gradetype', 'noteq', GRADE_TYPE_VALUE);
-        $mform->setDefault('grademin', 0.0);
 
         $mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
         $mform->setHelpButton('gradepass', array(false, get_string('gradepass', 'grades'),
                 false, true, false, get_string('gradepasshelp', 'grades')));
         $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_NONE);
         $mform->disabledIf('gradepass', 'gradetype', 'eq', GRADE_TYPE_TEXT);
-        $mform->setDefault('gradepass', 0.0);
 
         $mform->addElement('text', 'multfactor', get_string('multfactor', 'grades'));
         $mform->setHelpButton('multfactor', array(false, get_string('multfactor', 'grades'),
                 false, true, false, get_string('multfactorhelp', 'grades')));
         $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
         $mform->disabledIf('multfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
-        $mform->setDefault('multfactor', 1.0);
 
         $mform->addElement('text', 'plusfactor', get_string('plusfactor', 'grades'));
         $mform->setHelpButton('plusfactor', array(false, get_string('plusfactor', 'grades'),
                 false, true, false, get_string('plusfactorhelp', 'grades')));
         $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_NONE);
         $mform->disabledIf('plusfactor', 'gradetype', 'eq', GRADE_TYPE_TEXT);
-        $mform->setDefault('plusfactor', 0.0);
 
         $mform->addElement('text', 'aggregationcoef', get_string('aggregationcoef', 'grades'));
         $mform->setHelpButton('aggregationcoef', array(false, get_string('aggregationcoef', 'grades'),
                 false, true, false, get_string('aggregationcoefhelp', 'grades')));
-        $mform->setDefault('aggregationcoef', 0.0);
 
         /// hiding
         /// advcheckbox is not compatible with disabledIf !!
index 37a79134de42cf94248075038db0fdc5b89d4a7f..9c81b7feb6d6f2e600ffbc2ce30aa22e63b776c8 100644 (file)
@@ -33,18 +33,14 @@ if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) {
         $url = $CFG->wwwroot.'/grade/edit/tree/item.php?id='.$id.'&amp;courseid='.$courseid;
         redirect($gpr->add_url_params($url));
     }
-
-    if ($item->hidden > 1) {
-        $item->hiddenuntil = $item->hidden;
-        $item->hidden = 0;
-    } else {
-        $item->hiddenuntil = 0;
-    }
-
-    $item->locked = !empty($item->locked);
+    // Get Item preferences
+    $item->pref_gradedisplaytype = grade_report::get_pref('gradedisplaytype', $item->id);
+    $item->pref_decimalpoints    = grade_report::get_pref('decimalpoints', $item->id);
 
     $item->calculation = grade_item::denormalize_formula($item->calculation, $course->id);
 
+    $decimalpoints = grade_report::get_pref('decimalpoints', $item->id);
+
     if ($item->itemtype == 'mod') {
         $cm = get_coursemodule_from_instance($item->itemmodule, $item->iteminstance, $item->courseid);
         $item->cmid = $cm->id;
@@ -52,9 +48,32 @@ if ($item = get_record('grade_items', 'id', $id, 'courseid', $course->id)) {
         $item->cmid = 0;
     }
 
-    $mform->set_data($item);
+} else {
+    $item = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual'));
+    // Get Item preferences
+    $item->pref_gradedisplaytype = grade_report::get_pref('gradedisplaytype');
+    $item->pref_decimalpoints    = grade_report::get_pref('decimalpoints');
+
+    $decimalpoints = grade_report::get_pref('decimalpoints');
+
+    $item->cmid = 0;
+}
+
+if ($item->hidden > 1) {
+    $item->hiddenuntil = $item->hidden;
+    $item->hidden = 0;
+} else {
+    $item->hiddenuntil = 0;
 }
 
+$item->locked = !empty($item->locked);
+
+$item->gradepass       = format_float($item->gradepass, $decimalpoints);
+$item->aggregationcoef = format_float($item->aggregationcoef, 4);
+
+$mform->set_data($item);
+
+
 if ($data = $mform->get_data(false)) {
     if (array_key_exists('calculation', $data)) {
         $data->calculation = grade_item::normalize_formula($data->calculation, $course->id);
@@ -70,6 +89,13 @@ if ($data = $mform->get_data(false)) {
     unset($data->locked);
     unset($data->locktime);
 
+    $convert = array('gradepass', 'aggregationcoef');
+    foreach ($convert as $param) {
+        if (array_key_exists($param, $data)) {
+            $data->$param = unformat_float($data->$param);
+        }
+    }
+
     $grade_item = new grade_item(array('id'=>$id, 'courseid'=>$courseid));
     grade_item::set_properties($grade_item, $data);
 
index 86ef536da29df825023588467f4d20e648b17335..404c8405e56be463b5fbff521a8eac35f0399e3a 100644 (file)
@@ -48,8 +48,13 @@ class edit_outcomeitem_form extends moodleform {
 
         //$mform->addElement('text', 'calculation', get_string('calculation', 'grades'));
 
+        /*$mform->addElement('text', 'gradepass', get_string('gradepass', 'grades'));
+        $mform->setHelpButton('gradepass', array(false, get_string('gradepass', 'grades'),
+                false, true, false, get_string('gradepasshelp', 'grades')));*/
+
         $mform->addElement('text', 'aggregationcoef', get_string('aggregationcoef', 'grades'));
-        $mform->setDefault('aggregationcoef', 0.0);
+        $mform->setHelpButton('aggregationcoef', array(false, get_string('aggregationcoef', 'grades'),
+                false, true, false, get_string('aggregationcoefhelp', 'grades')));
 
         /// hiding
         /// advcheckbox is not compatible with disabledIf !!
index f770b20d4937e845ef0a5422b97f8089a9b129e3..1a4e19cd3b336294e3bff4fb950d3aaae51a5cfa 100644 (file)
@@ -154,12 +154,7 @@ class grade_report_grader extends grade_report {
                         $finalgrade = $postedvalue;
                     }
                 } else {
-                    $trimmed = trim($postedvalue);
-                    if (empty($trimmed)) { // empty string means no grade
-                        $finalgrade = null;
-                    } else {
-                        $finalgrade = $this->format_grade($postedvalue);
-                    }
+                    $finalgrade = unformat_float($postedvalue);
                 }
 
             } else if ($data_type == 'feedback') {
@@ -654,13 +649,13 @@ class grade_report_grader extends grade_report {
 
                     } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type
                         if ($this->get_pref('quickgrading') and $grade->is_editable()) {
-                            $value = $this->get_grade_clean($gradeval, $decimalpoints);
+                            $value = format_float($gradeval, $decimalpoints);
                             $studentshtml .= '<input type="hidden" name="oldgrade_'.$userid.'_'.$item->id.'" value="'.$value.'" />';
                             $studentshtml .= '<input size="6" tabindex="' . $tabindices[$item->id]['grade']
                                           . '" type="text" title="'. $strgrade .'" name="grade_'
                                           .$userid.'_' .$item->id.'" value="'.$value.'" />';
                         } else {
-                            $studentshtml .= $this->get_grade_clean($gradeval, $decimalpoints);
+                            $studentshtml .= format_float($gradeval, $decimalpoints);
                         }
                     }
 
@@ -726,7 +721,7 @@ class grade_report_grader extends grade_report {
                         if (is_null($gradeval)) {
                             $studentshtml .= '-';
                         } else {
-                            $studentshtml .=  $this->get_grade_clean($gradeval, $decimalpoints). $percentsign;
+                            $studentshtml .=  format_float($gradeval, $decimalpoints). $percentsign;
                         }
                     }
                     if (!empty($grade->feedback)) {
@@ -851,12 +846,12 @@ class grade_report_grader extends grade_report {
                             $finalavg = $sum/$count_array[$item->id];
                         }
 
-                        $scaleval = round($this->get_grade_clean($finalavg, $decimalpoints));
+                        $scaleval = round($finalavg);
                         $scale_object = new grade_scale(array('id' => $item->scaleid), false);
                         $gradehtml = $scale_object->get_nearest_item($scaleval);
                         $rawvalue = $scaleval;
                     } else {
-                        $gradeval = $this->get_grade_clean($sum/$count_array[$item->id], $decimalpoints);
+                        $gradeval = format_float($sum/$count_array[$item->id], $decimalpoints);
 
                         $gradehtml = round($gradeval, $decimalpoints);
                         $rawvalue = $gradeval;
@@ -909,8 +904,8 @@ class grade_report_grader extends grade_report {
                 }
 
                 if ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL) {
-                    $grademin = $this->get_grade_clean($item->grademin, $decimalpoints);
-                    $grademax = $this->get_grade_clean($item->grademax, $decimalpoints);
+                    $grademin = format_float($item->grademin, $decimalpoints);
+                    $grademax = format_float($item->grademax, $decimalpoints);
                 } elseif ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE) {
                     $grademin = 0;
                     $grademax = 100;
index 53bba1dd2e7e36d33b30b180766b714d588e377e..5d0baa0bf05ff9a9761608236ac80d3678e0737d 100755 (executable)
@@ -200,60 +200,6 @@ class grade_report {
         //implement if needed
     }
 
-    /**
-     * format grade using lang specific decimal point and thousand separator
-     * the result is suitable for printing on html page
-     * @static
-     * @param float $gradeval raw grade value pulled from db
-     * @param int $decimalpoints Optional integers to override global decimalpoints preference
-     * @return string $gradeval formatted grade value
-     */
-    function get_grade_clean($gradeval, $decimalpoints=null) {
-        global $CFG;
-
-        if (is_null($gradeval)) {
-            $gradeval = '';
-        } else {
-            // decimal points as specified by user
-            if (is_null($decimalpoints)) {
-                $decimalpoints = $this->get_pref('decimalpoints');
-            }
-            $gradeval = number_format($gradeval, $decimalpoints, $this->get_lang_string('decpoint', 'langconfig'),
-                                      $this->get_lang_string('thousandsep', 'langconfig'));
-        }
-
-        return $gradeval;
-
-        /*
-        // commenting this out, if this is added, we also need to find the number of decimal place preserved
-        // so it can go into number_format
-        if ($gradeval != 0) {
-            $gradeval = rtrim(trim($gradeval, "0"), ".");
-        } else {
-            $gradeval = 0;
-        }
-        */
-
-    }
-
-    /**
-     * Given a user input grade, format it to standard format i.e. no thousand separator, and . as decimal point
-     * @static
-     * @param string $gradeval grade value from user input, language specific format
-     * @return string - grade value for storage, en format
-     */
-    function format_grade($gradeval) {
-
-        $decimalpt = $this->get_lang_string('decpoint', 'langconfig');
-        $thousandsep = $this->get_lang_string('thousandsep', 'langconfig');
-        // replace decimal point with '.';
-        $gradeval = str_replace($decimalpt, '.', $gradeval);
-        // thousand separator is not useful
-        $gradeval = str_replace($thousandsep, '', $gradeval);
-
-        return clean_param($gradeval, PARAM_NUMBER);
-    }
-
     /**
      * First checks the cached language strings, then returns match if found, or uses get_string()
      * to get it from the DB, caches it then returns it.
index 553ec20a19b9b2453208fd01f8e13306f22a5109..ce6af7a9d10154f05cfeadb4aca7c5d9f7a093e2 100644 (file)
@@ -158,7 +158,7 @@ class grade_report_user extends grade_report {
                     }
                 } else {
                     // normal grade, or text, just display
-                    $data[] = $excluded.$this->get_grade_clean($grade_grade->finalgrade);
+                    $data[] = $excluded.format_float($grade_grade->finalgrade);
                 }
 
                 /// prints percentage
@@ -166,7 +166,7 @@ class grade_report_user extends grade_report {
                 if ($grade_item->gradetype == GRADE_TYPE_VALUE) {
                     // processing numeric grade
                     if ($grade_grade->finalgrade) {
-                        $percentage = $this->get_grade_clean(($grade_grade->finalgrade / $grade_item->grademax) * 100).'%';
+                        $percentage = format_float(($grade_grade->finalgrade / $grade_item->grademax) * 100).'%';
                     } else {
                         $percentage = '-';
                     }
@@ -175,7 +175,7 @@ class grade_report_user extends grade_report {
                     // processing scale grade
                     $scale = get_record('scale', 'id', $grade_item->scaleid);
                     $scalevals = explode(",", $scale->scale);
-                    $percentage = $this->get_grade_clean(($grade_grade->finalgrade) / count($scalevals) * 100).'%';
+                    $percentage = format_float(($grade_grade->finalgrade) / count($scalevals) * 100).'%';
 
                 } else {
                     // text grade
index f843e48602eb1959dfa23636050ad1c65c0154b1..cfc066e302dabe06e73ed285bffd572ee4b3fdad 100644 (file)
@@ -4,8 +4,9 @@
 
 $string['alphabet'] = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z';
 $string['backupnameformat'] = '%%Y%%m%%d-%%H%%M';
-$string['decpoint'] = '.'; // decimal point, for some languages it is ',' if this is changed, must set thousandsep
+$string['decsep'] = '.'; // decimal point separator, for some languages it is ','
 $string['firstdayofweek'] = '0';
+$string['listsep'] = ','; // if decsep is ',', then usually ';' is used instead here; these two can not be the same
 $string['locale'] = 'en_AU.UTF-8';
 $string['localewin'] = 'English_Australia.1252';
 $string['localewincharset'] = '';
@@ -24,6 +25,5 @@ $string['strftimetime'] = '%%I:%%M %%p';
 $string['thischarset'] = 'UTF-8';
 $string['thisdirection'] = 'ltr';
 $string['thislanguage'] = 'English';
-$string['thousandsep'] = ','; // thousand separator, set to '' if none, if this is set, must set decpoint
 
 ?>
index a171b00de27bb6fbeda3d005d273711542e2e61e..f3f65bb0c5a8f8a7c535a665ba52e58908b76c39 100644 (file)
@@ -480,7 +480,7 @@ function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext=''
     if ($done && (($now - $lasttime) >= $updatetime)) {
         $elapsedtime = $now - $starttime;
         $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
-        $percentage = format_float((float)$done / (float)$total, 2);
+        $percentage = round((float)$done / (float)$total, 2);
         $width = (int)(500 * $percentage);
 
         if ($projectedtime > 10) {
index 07112d34d04a0d13d6a0e0298035cd70cbc36d86..61712fdd2f9424d2a4ea90876466508234300a16 100644 (file)
@@ -82,6 +82,31 @@ class calc_formula {
     function get_error() {
         return $this->_error;
     }
+
+    /**
+     * Similar to format_float, formats the numbers and list separators
+     * according to locale specifics.
+     * @param string $formula
+     * @return string localised formula
+     */
+    function localize($formula) {
+        $formula = str_replace('.', '$', $formula); // temp placeholder
+        $formula = str_replace(',', get_string('listsep'), $formula);
+        $formula = str_replace('$', get_string('decsep'), $formula);
+        return $formula;
+    }
+
+    /**
+     * Similar to unformat_float, converts floats and lists to PHP standards.
+     * @param string $formula localised formula
+     * @return string 
+     */
+    function unlocalize($formula) {
+        $formula = str_replace(get_string('decsep'), '$', $formula);
+        $formula = str_replace(get_string('listsep'), ',', $formula);
+        $formula = str_replace('$', '.', $formula); // temp placeholder
+        return $formula;
+    }
 }
 
 ?>
\ No newline at end of file
index 602fdb0e9be80d075d7155269848e06b59ed340c..4ff9cda1ef2e80afce2bcc62dd80c35e546ad12f 100644 (file)
@@ -4445,7 +4445,7 @@ function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) {
     global $CFG;
 
 /// originally these special strings were stored in moodle.php now we are only in langconfig.php
-    $langconfigstrs = array('alphabet', 'backupnameformat', 'firstdayofweek', 'locale',
+    $langconfigstrs = array('alphabet', 'backupnameformat', 'decsep', 'firstdayofweek', 'listsep', 'locale',
                             'localewin', 'localewincharset', 'oldcharset',
                             'parentlanguage', 'strftimedate', 'strftimedateshort', 'strftimedatetime',
                             'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime',
@@ -5912,14 +5912,36 @@ function generate_password($maxlen=10) {
 }
 
 /**
- * Given a float, prints it nicely
+ * Given a float, prints it nicely.
+ * Do NOT use the result in any calculation later!
  *
- * @param float $num The float to print
+ * @param float $flaot The float to print
  * @param int $places The number of decimal places to print.
- * @return string
+ * @return string locale float
+ */
+function format_float($float, $decimalpoints=1) {
+    if (is_null($float)) {
+        return '';
+    }
+    return number_format($float, $decimalpoints, get_string('decsep'), '');
+}
+
+/**
+ * Convers locale specific floating point/comma number back to standard PHP float value
+ * Do NOT try to do any math operations before this conversion on any user submitted floats!
+ *
+ * @param  string $locale_float locale aware flaot represenation
  */
-function format_float($num, $places=1) {
-    return sprintf("%.$places"."f", $num);
+function unformat_float($locale_float) {
+    $locale_float = trim($locale_float);
+
+    if ($locale_float == '') {
+        return null;
+    }
+
+    $locale_float = str_replace(' ', '', $locale_float); // no spaces - those might be used as thousand separators
+
+    return (float)str_replace(get_string('decsep'), '.', $locale_float);
 }
 
 /**
index 188e65bf01bac487ef059acbf9a80b8784d3afc8..56d9b3724e0df44363991e412467d2c7505e1b04 100644 (file)
         if ($numofattempts == 0) {
             $avescore = get_string("notcompleted", "lesson");
         } else {
-            $avescore = format_float($avescore/$numofattempts, 2, ".", ",");
+            $avescore = format_float($avescore/$numofattempts, 2);
         }
         if ($avetime == NULL) {
             $avetime = get_string("notcompleted", "lesson");
         } else {
-            $avetime = format_float($avetime/$numofattempts, 0, ".", ",");
+            $avetime = format_float($avetime/$numofattempts, 0);
             $avetime = format_time($avetime);
         }
         if ($hightime == NULL) {