1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Lesson's numeric helper lib.
|
|
|
19 |
*
|
|
|
20 |
* Contains any helper functions for the numeric pagetyep
|
|
|
21 |
*
|
|
|
22 |
* @package mod_lesson
|
|
|
23 |
* @copyright 2020 Peter Dias <peter@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
namespace mod_lesson\local\numeric;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Lesson numeric page helper
|
|
|
30 |
*
|
|
|
31 |
* @copyright 2020 Peter Dias<peter@moodle.com>
|
|
|
32 |
* @package core_lesson
|
|
|
33 |
*/
|
|
|
34 |
class helper {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Helper function to unformat a given numeric value from locale specific values with n:n signifying ranges to standards
|
|
|
38 |
* with decimal point numbers/ranges
|
|
|
39 |
*
|
|
|
40 |
* @param string $value The value to be formatted
|
|
|
41 |
* @return string|float|bool $formattedvalue unformatted value
|
|
|
42 |
* String - If it is a range it will return a value e.g. 2:4
|
|
|
43 |
* Float - if it's a properly formatted float
|
|
|
44 |
* Null - If empty and could not be converted
|
|
|
45 |
*/
|
|
|
46 |
public static function lesson_unformat_numeric_value(string $value) {
|
|
|
47 |
if (strpos($value, ':')) {
|
|
|
48 |
list($min, $max) = explode(':', $value);
|
|
|
49 |
$formattedvalue = unformat_float($min) . ':' . unformat_float($max);
|
|
|
50 |
} else {
|
|
|
51 |
$formattedvalue = unformat_float($value);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return $formattedvalue;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Helper function to format a given value into locale specific values with n:n signifying ranges
|
|
|
59 |
*
|
|
|
60 |
* @param string|number $value The value to be formatted
|
|
|
61 |
* @return string $formattedvalue Formatted value OR $value if not numeric
|
|
|
62 |
*/
|
|
|
63 |
public static function lesson_format_numeric_value($value): string {
|
|
|
64 |
$formattedvalue = $value;
|
|
|
65 |
if (strpos($value, ':')) {
|
|
|
66 |
list($min, $max) = explode(':', $value);
|
|
|
67 |
$formattedvalue = $min . ':' . $max;
|
|
|
68 |
if (is_numeric($min) && is_numeric($max)) {
|
|
|
69 |
$formattedvalue = format_float($min, strlen($min), true, true) . ':'
|
|
|
70 |
. format_float($max, strlen($max), true, true);
|
|
|
71 |
}
|
|
|
72 |
} else {
|
|
|
73 |
$formattedvalue = is_numeric($value) ? format_float($value, strlen($value), true, true) : $value;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return $formattedvalue;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
}
|