Skip to content

Commit

Permalink
Merge branch 'MDL-38239-moodle25' of git://github.com/StudiUM/moodle …
Browse files Browse the repository at this point in the history
…into MOODLE_25_STABLE
  • Loading branch information
danpoltawski committed May 27, 2013
2 parents 6c16494 + f7d180a commit 121e249
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 25 deletions.
25 changes: 15 additions & 10 deletions grade/import/csv/index.php
Expand Up @@ -351,16 +351,21 @@
$newgrade->finalgrade = $value; $newgrade->finalgrade = $value;
} else { } else {
if ($value === '' or $value == '-') { if ($value === '' or $value == '-') {
$value = null; // no grade $value = null; // No grade.

} else {
} else if (!is_numeric($value)) { // If the value has a local decimal or can correctly be unformatted, do it.
// non numeric grade value supplied, possibly mapped wrong column $validvalue = unformat_float($value, true);
echo "<br/>t0 is $t0"; if ($validvalue !== false) {
echo "<br/>grade is $value"; $value = $validvalue;
$status = false; } else {
import_cleanup($importcode); // Non numeric grade value supplied, possibly mapped wrong column.
echo $OUTPUT->notification(get_string('badgrade', 'grades')); echo "<br/>t0 is $t0";
break 3; echo "<br/>grade is $value";
$status = false;
import_cleanup($importcode);
echo $OUTPUT->notification(get_string('badgrade', 'grades'));
break 3;
}
} }
$newgrade->finalgrade = $value; $newgrade->finalgrade = $value;
} }
Expand Down
12 changes: 9 additions & 3 deletions lib/moodlelib.php
Expand Up @@ -9670,18 +9670,24 @@ function format_float($float, $decimalpoints=1, $localized=true, $stripzeros=fal
* Do NOT try to do any math operations before this conversion on any user submitted floats! * Do NOT try to do any math operations before this conversion on any user submitted floats!
* *
* @param string $locale_float locale aware float representation * @param string $locale_float locale aware float representation
* @return float * @param bool $strict If true, then check the input and return false if it is not a valid number.
* @return mixed float|bool - false or the parsed float.
*/ */
function unformat_float($locale_float) { function unformat_float($locale_float, $strict = false) {
$locale_float = trim($locale_float); $locale_float = trim($locale_float);


if ($locale_float == '') { if ($locale_float == '') {
return null; return null;
} }


$locale_float = str_replace(' ', '', $locale_float); // no spaces - those might be used as thousand separators $locale_float = str_replace(' ', '', $locale_float); // no spaces - those might be used as thousand separators
$locale_float = str_replace(get_string('decsep', 'langconfig'), '.', $locale_float);

if ($strict && !is_numeric($locale_float)) {
return false;
}


return (float)str_replace(get_string('decsep', 'langconfig'), '.', $locale_float); return (float)$locale_float;
} }


/** /**
Expand Down
111 changes: 99 additions & 12 deletions lib/tests/moodlelib_test.php
Expand Up @@ -85,6 +85,29 @@ class moodlelib_testcase extends advanced_testcase {
) )
); );


/**
* Define a local decimal separator.
*
* It is not possible to directly change the result of get_string in
* a unit test. Instead, we create a language pack for language 'xx' in
* dataroot and make langconfig.php with the string we need to change.
* The example separator used here is 'X'; on PHP 5.3 and before this
* must be a single byte character due to PHP bug/limitation in
* number_format, so you can't use UTF-8 characters.
*
* @global type $SESSION
* @global type $CFG
*/
protected function define_local_decimal_separator() {
global $SESSION, $CFG;

$SESSION->lang = 'xx';
$langconfig = "<?php\n\$string['decsep'] = 'X';";
$langfolder = $CFG->dataroot . '/lang/xx';
check_dir_exists($langfolder);
file_put_contents($langfolder . '/langconfig.php', $langconfig);
}

function test_cleanremoteaddr() { function test_cleanremoteaddr() {
//IPv4 //IPv4
$this->assertEquals(cleanremoteaddr('1023.121.234.1'), null); $this->assertEquals(cleanremoteaddr('1023.121.234.1'), null);
Expand Down Expand Up @@ -2122,7 +2145,6 @@ public function test_get_string_limitation() {
* Test localised float formatting. * Test localised float formatting.
*/ */
public function test_format_float() { public function test_format_float() {
global $SESSION, $CFG;


// Special case for null // Special case for null
$this->assertEquals('', format_float(null)); $this->assertEquals('', format_float(null));
Expand All @@ -2138,17 +2160,8 @@ public function test_format_float() {
$this->assertEquals('5.43', format_float(5.43, 5, true, true)); $this->assertEquals('5.43', format_float(5.43, 5, true, true));
$this->assertEquals('5', format_float(5.0001, 3, true, true)); $this->assertEquals('5', format_float(5.0001, 3, true, true));


// It is not possible to directly change the result of get_string in // Tests with a localised decimal separator.
// a unit test. Instead, we create a language pack for language 'xx' in $this->define_local_decimal_separator();
// dataroot and make langconfig.php with the string we need to change.
// The example separator used here is 'X'; on PHP 5.3 and before this
// must be a single byte character due to PHP bug/limitation in
// number_format, so you can't use UTF-8 characters.
$SESSION->lang = 'xx';
$langconfig = "<?php\n\$string['decsep'] = 'X';";
$langfolder = $CFG->dataroot . '/lang/xx';
check_dir_exists($langfolder);
file_put_contents($langfolder . '/langconfig.php', $langconfig);


// Localisation on (default) // Localisation on (default)
$this->assertEquals('5X43000', format_float(5.43, 5)); $this->assertEquals('5X43000', format_float(5.43, 5));
Expand All @@ -2159,6 +2172,80 @@ public function test_format_float() {
$this->assertEquals('5.43', format_float(5.43, 5, false, true)); $this->assertEquals('5.43', format_float(5.43, 5, false, true));
} }


/**
* Test localised float unformatting.
*/
public function test_unformat_float() {

// Tests without the localised decimal separator.

// Special case for null, empty or white spaces only strings.
$this->assertEquals(null, unformat_float(null));
$this->assertEquals(null, unformat_float(''));
$this->assertEquals(null, unformat_float(' '));

// Regular use.
$this->assertEquals(5.4, unformat_float('5.4'));
$this->assertEquals(5.4, unformat_float('5.4', true));

// No decimal.
$this->assertEquals(5.0, unformat_float('5'));

// Custom number of decimal.
$this->assertEquals(5.43267, unformat_float('5.43267'));

// Empty decimal.
$this->assertEquals(100.0, unformat_float('100.00'));

// With the thousand separator.
$this->assertEquals(1000.0, unformat_float('1 000'));
$this->assertEquals(1000.32, unformat_float('1 000.32'));

// Negative number.
$this->assertEquals(-100.0, unformat_float('-100'));

// Wrong value.
$this->assertEquals(0.0, unformat_float('Wrong value'));
// Wrong value in strict mode.
$this->assertFalse(unformat_float('Wrong value', true));

// Combining options.
$this->assertEquals(-1023.862567, unformat_float(' -1 023.862567 '));

// Bad decimal separator (should crop the decimal).
$this->assertEquals(50.0, unformat_float('50,57'));
// Bad decimal separator in strict mode (should return false).
$this->assertFalse(unformat_float('50,57', true));

// Tests with a localised decimal separator.
$this->define_local_decimal_separator();

// We repeat the tests above but with the current decimal separator.

// Regular use without and with the localised separator.
$this->assertEquals (5.4, unformat_float('5.4'));
$this->assertEquals (5.4, unformat_float('5X4'));

// Custom number of decimal.
$this->assertEquals (5.43267, unformat_float('5X43267'));

// Empty decimal.
$this->assertEquals (100.0, unformat_float('100X00'));

// With the thousand separator.
$this->assertEquals (1000.32, unformat_float('1 000X32'));

// Bad different separator (should crop the decimal).
$this->assertEquals (50.0, unformat_float('50Y57'));
// Bad different separator in strict mode (should return false).
$this->assertFalse (unformat_float('50Y57', true));

// Combining options.
$this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 '));
// Combining options in strict mode.
$this->assertEquals (-1023.862567, unformat_float(' -1 023X862567 ', true));
}

/** /**
* Test deleting of users. * Test deleting of users.
*/ */
Expand Down

0 comments on commit 121e249

Please sign in to comment.