From: skodak Date: Fri, 10 Aug 2007 15:00:35 +0000 (+0000) Subject: MDL-10791 improve handling of localised floating point numbers in moodle; replaced... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=76317c73a28e66ee78015adbeb079936697881e0;p=moodle.git MDL-10791 improve handling of localised floating point numbers in moodle; replaced/fixed some float_format() that were used instead of round(); expect some more patches soon --- diff --git a/enrol/authorize/config_form.php b/enrol/authorize/config_form.php index b3b079e3c2..252d6d1121 100755 --- a/enrol/authorize/config_form.php +++ b/enrol/authorize/config_form.php @@ -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; } diff --git a/enrol/authorize/locallib.php b/enrol/authorize/locallib.php index fc722bc366..648228dbca 100644 --- a/enrol/authorize/locallib.php +++ b/enrol/authorize/locallib.php @@ -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; diff --git a/enrol/paypal/enrol.php b/enrol/paypal/enrol.php index e571851a1d..4d90111784 100644 --- a/enrol/paypal/enrol.php +++ b/enrol/paypal/enrol.php @@ -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 diff --git a/enrol/paypal/ipn.php b/enrol/paypal/ipn.php index 5c6becab35..e88367e272 100644 --- a/enrol/paypal/ipn.php +++ b/enrol/paypal/ipn.php @@ -147,9 +147,9 @@ } 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; diff --git a/grade/edit/tree/calculation.php b/grade/edit/tree/calculation.php index b9d7f2328d..3583370e2e 100644 --- a/grade/edit/tree/calculation.php +++ b/grade/edit/tree/calculation.php @@ -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); } diff --git a/grade/edit/tree/calculation_form.php b/grade/edit/tree/calculation_form.php index 8a8f1601e3..c53cf0e91f 100644 --- a/grade/edit/tree/calculation_form.php +++ b/grade/edit/tree/calculation_form.php @@ -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; } diff --git a/grade/edit/tree/grade.php b/grade/edit/tree/grade.php index bef8cf72e4..701fb63e30 100644 --- a/grade/edit/tree/grade.php +++ b/grade/edit/tree/grade.php @@ -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)) { diff --git a/grade/edit/tree/grade_form.php b/grade/edit/tree/grade_form.php index 84d980a71e..4a264cc9ee 100755 --- a/grade/edit/tree/grade_form.php +++ b/grade/edit/tree/grade_form.php @@ -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); diff --git a/grade/edit/tree/item.php b/grade/edit/tree/item.php index 3dec92cc66..eee294eaf6 100644 --- a/grade/edit/tree/item.php +++ b/grade/edit/tree/item.php @@ -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.'&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); diff --git a/grade/edit/tree/item_form.php b/grade/edit/tree/item_form.php index a5c4effbf3..c76f6eeb00 100644 --- a/grade/edit/tree/item_form.php +++ b/grade/edit/tree/item_form.php @@ -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 !! diff --git a/grade/edit/tree/outcomeitem.php b/grade/edit/tree/outcomeitem.php index 37a79134de..9c81b7feb6 100644 --- a/grade/edit/tree/outcomeitem.php +++ b/grade/edit/tree/outcomeitem.php @@ -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.'&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); diff --git a/grade/edit/tree/outcomeitem_form.php b/grade/edit/tree/outcomeitem_form.php index 86ef536da2..404c8405e5 100644 --- a/grade/edit/tree/outcomeitem_form.php +++ b/grade/edit/tree/outcomeitem_form.php @@ -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 !! diff --git a/grade/report/grader/lib.php b/grade/report/grader/lib.php index f770b20d49..1a4e19cd3b 100644 --- a/grade/report/grader/lib.php +++ b/grade/report/grader/lib.php @@ -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 .= ''; $studentshtml .= ''; } 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; diff --git a/grade/report/lib.php b/grade/report/lib.php index 53bba1dd2e..5d0baa0bf0 100755 --- a/grade/report/lib.php +++ b/grade/report/lib.php @@ -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. diff --git a/grade/report/user/lib.php b/grade/report/user/lib.php index 553ec20a19..ce6af7a9d1 100644 --- a/grade/report/user/lib.php +++ b/grade/report/user/lib.php @@ -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 diff --git a/lang/en_utf8/langconfig.php b/lang/en_utf8/langconfig.php index f843e48602..cfc066e302 100644 --- a/lang/en_utf8/langconfig.php +++ b/lang/en_utf8/langconfig.php @@ -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 ?> diff --git a/lib/adminlib.php b/lib/adminlib.php index a171b00de2..f3f65bb0c5 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -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) { diff --git a/lib/mathslib.php b/lib/mathslib.php index 07112d34d0..61712fdd2f 100644 --- a/lib/mathslib.php +++ b/lib/mathslib.php @@ -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 diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 602fdb0e9b..4ff9cda1ef 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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); } /** diff --git a/mod/lesson/report.php b/mod/lesson/report.php index 188e65bf01..56d9b3724e 100644 --- a/mod/lesson/report.php +++ b/mod/lesson/report.php @@ -266,12 +266,12 @@ 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) {