mirror of
https://github.com/moodle/moodle.git
synced 2025-04-24 18:04:43 +02:00
MDL-10385, use langconfig specific decimal point and thousand separator when printing grades. Grade validation for grademin/grademax range
This commit is contained in:
parent
98353ff194
commit
de7d838841
@ -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 .= '</td>' . "\n";
|
||||
}
|
||||
$studentshtml .= '</tr>';
|
||||
@ -643,7 +694,7 @@ if ($currentgroup && $showgroups) {
|
||||
$groupsumhtml .= '<td>-</td>';
|
||||
} else {
|
||||
$sum = $groupsum[$item->id];
|
||||
$groupsumhtml .= '<td>'.get_grade_clean(round($sum->sum, $decimals)).'</td>';
|
||||
$groupsumhtml .= '<td>'.get_grade_clean($sum->sum).'</td>';
|
||||
}
|
||||
}
|
||||
$groupsumhtml .= '</tr>';
|
||||
@ -677,7 +728,7 @@ if ($showgrandtotals) {
|
||||
$gradesumhtml .= '<td>-</td>';
|
||||
} else {
|
||||
$sum = $classsum[$item->id];
|
||||
$gradesumhtml .= '<td>'.get_grade_clean(round($sum->sum, $decimals)).'</td>';
|
||||
$gradesumhtml .= '<td>'.get_grade_clean($sum->sum).'</td>';
|
||||
}
|
||||
}
|
||||
$gradesumhtml .= '</tr>';
|
||||
|
@ -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
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user