$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;
}
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;
} else {
$cost = (float) $course->cost;
}
- $cost = format_float($cost, 2);
-
if (abs($cost) < 0.01) { // no cost, default to base class entry to course
} 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;
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);
}
-$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);
}
// 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;
}
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);
$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 {
$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)) {
$mform->setHelpButton('feedbackformat', array('textformat', get_string('helpformatting')));
// hidden params
+ $mform->addElement('hidden', 'oldgrade');
+
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
$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);
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);
$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 !!
$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;
$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);
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);
//$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 !!
$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') {
} 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);
}
}
if (is_null($gradeval)) {
$studentshtml .= '-';
} else {
- $studentshtml .= $this->get_grade_clean($gradeval, $decimalpoints). $percentsign;
+ $studentshtml .= format_float($gradeval, $decimalpoints). $percentsign;
}
}
if (!empty($grade->feedback)) {
$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;
}
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;
//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.
}
} 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
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 = '-';
}
// 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
$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'] = '';
$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
?>
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) {
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
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',
}
/**
- * 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);
}
/**
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) {