From: toyomoyo Date: Tue, 10 Jul 2007 09:01:19 +0000 (+0000) Subject: MDL-10385, use langconfig specific decimal point and thousand separator when printing... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=de7d838841e0d6e90092cc1173735b60e5a11fc0;p=moodle.git MDL-10385, use langconfig specific decimal point and thousand separator when printing grades. Grade validation for grademin/grademax range --- diff --git a/grade/report/grader/index.php b/grade/report/grader/index.php index 6c9515f60e..a4dbfed383 100644 --- a/grade/report/grader/index.php +++ b/grade/report/grader/index.php @@ -6,18 +6,47 @@ require_once($CFG->libdir.'/tablelib.php'); include_once($CFG->libdir.'/gradelib.php'); -// remove trailing 0s and "."s +/** + * format number using lang specific decimal point and thousand separator + * @param float $gradeval raw grade value pulled from db + * @return string $gradeval formatted grade value + */ function get_grade_clean($gradeval) { - + global $CFG; + + /* + // 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; } + */ + // decimal points as specified by user + $decimals = get_user_preferences('grade_report_decimalpoints', $CFG->grade_report_decimalpoints); + $gradeval = number_format($gradeval, $decimals, get_string('decpoint', 'langconfig'), get_string('thousandsep', 'langconfig')); return $gradeval; } +/** + * Given a user input grade, format it to standard format i.e. no thousand separator, and . as decimal point + * @param string $gradeval grade value from user input, language specific format + * @return string - grade value for storage, en format + */ +function format_grade($gradeval) { + + $decimalpt = get_string('decpoint', 'langconfig'); + $thousandsep = get_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); +} + /** * Shortcut function for printing the grader report toggles. * @param string $type The type of toggle @@ -74,7 +103,8 @@ if ($data = data_submitted()) { foreach ($data as $varname => $postedgrade) { // clean posted values - $postedgrade = clean_param($postedgrade, PARAM_NUMBER); + $postedgrade = clean_param($postedgrade, PARAM_RAW); + // can not use param number here, because we can have "," in grade $varname = clean_param($varname, PARAM_RAW); // skip, not a grade @@ -87,19 +117,36 @@ if ($data = data_submitted()) { $grade = new object(); $grade->userid = clean_param($gradeinfo[1], PARAM_INT); $gradeitemid = clean_param($gradeinfo[2], PARAM_INT); - $grade->rawgrade = $postedgrade; + // grade needs to formatted to proper format for storage + $grade->rawgrade = format_grade($postedgrade); // put into grades array $grades[$gradeitemid][] = $grade; } } +// array to hold all error found during grade processing, e.g. outofrange +$gradeserror = array(); + // now we update the raw grade for each posted grades if (!empty($grades)) { foreach ($grades as $gradeitemid => $itemgrades) { foreach ($itemgrades as $gradedata) { $gradeitem = new grade_item(array('id'=>$gradeitemid), true); - $gradeitem->update_raw_grade($gradedata->userid, $gradedata->rawgrade); + + // cbeck if grade is in range, if not, add to error array + // MDL-10369 + + // -1 is accepted for scale grades (no grade) + if ($gradedata->rawgrade == -1 && $gradeitem->gradetype == 2) { + $gradeitem->update_raw_grade($gradedata->userid, $gradedata->rawgrade); + } else { + if ($gradeitem->grademax < $gradedata->rawgrade || $gradeitem->grademin > $gradedata->rawgrade) { + $gradeserror[$gradeitem->id][$gradedata->userid] = 'outofrange'; + } else { + $gradeitem->update_raw_grade($gradedata->userid, $gradedata->rawgrade); + } + } } } } @@ -595,7 +642,7 @@ foreach ($users as $userid => $user) { // no such scale, throw error? } } else { - $studentshtml .= round($gradeval, $decimals); + $studentshtml .= get_grade_clean($gradeval); } } @@ -608,6 +655,10 @@ foreach ($users as $userid => $user) { $studentshtml .= grade_get_icons($element, $gtree); } + if (!empty($gradeserror[$item->id][$userid])) { + $studentshtml .= $gradeserror[$item->id][$userid]; + } + $studentshtml .= '' . "\n"; } $studentshtml .= ''; @@ -643,7 +694,7 @@ if ($currentgroup && $showgroups) { $groupsumhtml .= '-'; } else { $sum = $groupsum[$item->id]; - $groupsumhtml .= ''.get_grade_clean(round($sum->sum, $decimals)).''; + $groupsumhtml .= ''.get_grade_clean($sum->sum).''; } } $groupsumhtml .= ''; @@ -677,7 +728,7 @@ if ($showgrandtotals) { $gradesumhtml .= '-'; } else { $sum = $classsum[$item->id]; - $gradesumhtml .= ''.get_grade_clean(round($sum->sum, $decimals)).''; + $gradesumhtml .= ''.get_grade_clean($sum->sum).''; } } $gradesumhtml .= ''; diff --git a/lang/en_utf8/langconfig.php b/lang/en_utf8/langconfig.php index a096cbddd0..f843e48602 100644 --- a/lang/en_utf8/langconfig.php +++ b/lang/en_utf8/langconfig.php @@ -4,6 +4,7 @@ $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['firstdayofweek'] = '0'; $string['locale'] = 'en_AU.UTF-8'; $string['localewin'] = 'English_Australia.1252'; @@ -23,5 +24,6 @@ $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 ?>