Преглед изворни кода

Add $decimalSeparator and $thousandSeparator params BT#13187

- Used comma as separator for the exercise_category_report.php page
jmontoyaa пре 7 година
родитељ
комит
7218fd1c12

+ 5 - 2
main/gradebook/lib/scoredisplay.class.php

@@ -285,15 +285,18 @@ class ScoreDisplay
      *
      * @param float $score
      * @param bool $ignoreDecimals
+     * @param string $decimalSeparator
+     * @param string $thousandSeparator
+     *
      * @return float the score formatted
      */
-    public function format_score($score, $ignoreDecimals = false)
+    public function format_score($score, $ignoreDecimals = false, $decimalSeparator = '.', $thousandSeparator = ',')
     {
         $decimals = $this->get_number_decimals();
         if ($ignoreDecimals) {
             $decimals = 0;
         }
-        return api_number_format($score, $decimals);
+        return api_number_format($score, $decimals, $decimalSeparator, $thousandSeparator);
     }
 
     /**

+ 2 - 1
main/inc/ajax/model.ajax.php

@@ -1319,7 +1319,8 @@ switch ($action) {
             $courseInfo['code'],
             true,
             true,
-            $extraFieldsToAdd
+            $extraFieldsToAdd,
+            true
         );
         break;
     case 'get_hotpotatoes_exercise_results':

+ 4 - 4
main/inc/lib/api.lib.php

@@ -8548,17 +8548,17 @@ function api_float_val($number)
  * 3.141516 => 3.14
  * 3,141516 => 3,14
  *
- * @todo WIP
- *
  * @param string $number number in iso code
  * @param int $decimals
+ * @param string $decimalSeparator
+ * @param string $thousandSeparator
  * @return bool|string
  */
-function api_number_format($number, $decimals = 0)
+function api_number_format($number, $decimals = 0, $decimalSeparator = '.', $thousandSeparator = ',')
 {
     $number = api_float_val($number);
 
-    return number_format($number, $decimals);
+    return number_format($number, $decimals, $decimalSeparator, $thousandSeparator);
 }
 
 /**

+ 64 - 19
main/inc/lib/exercise.lib.php

@@ -1644,6 +1644,7 @@ HOTSPOT;
      * @param bool $showSessionField
      * @param bool $showExerciseCategories
      * @param array $userExtraFieldsToAdd
+     * @param bool $useCommaAsDecimalPoint
      *
      * @return array
      */
@@ -1658,7 +1659,8 @@ HOTSPOT;
         $courseCode = null,
         $showSessionField = false,
         $showExerciseCategories = false,
-        $userExtraFieldsToAdd = []
+        $userExtraFieldsToAdd = [],
+        $useCommaAsDecimalPoint = false
     ) {
         //@todo replace all this globals
         global $documentPath, $filter;
@@ -1908,6 +1910,16 @@ HOTSPOT;
             }
         }
 
+        $scoreDisplay = new ScoreDisplay();
+
+        $decimalSeparator = '.';
+        $thousandSeparator = ',';
+
+        if ($useCommaAsDecimalPoint) {
+            $decimalSeparator = ',';
+            $thousandSeparator = '';
+        }
+
         $listInfo = [];
         // Simple exercises
         if (empty($hotpotatoe_where)) {
@@ -2032,9 +2044,7 @@ HOTSPOT;
                     // we filter the results if we have the permission to
                     $result_disabled = 0;
                     if (isset($results[$i]['results_disabled'])) {
-                        $result_disabled = intval(
-                            $results[$i]['results_disabled']
-                        );
+                        $result_disabled = (int) $results[$i]['results_disabled'];
                     }
                     if ($result_disabled == 0) {
                         $my_res = $results[$i]['exe_result'];
@@ -2047,7 +2057,16 @@ HOTSPOT;
                             $my_res = 0;
                         }
 
-                        $score = self::show_score($my_res, $my_total);
+                        $score = self::show_score(
+                            $my_res,
+                            $my_total,
+                            true,
+                            true,
+                            false,
+                            false,
+                            $decimalSeparator,
+                            $thousandSeparator
+                        );
 
                         $actions = '<div class="pull-right">';
                         if ($is_allowedToEdit) {
@@ -2292,7 +2311,17 @@ HOTSPOT;
                             }
 
                             foreach ($category_list as $categoryId => $result) {
-                                $scoreToDisplay = self::show_score($result['score'], $result['total']);
+                                $scoreToDisplay = self::show_score(
+                                    $result['score'],
+                                    $result['total'],
+                                    true,
+                                    true,
+                                    false,
+                                    false,
+                                    $decimalSeparator,
+                                    $thousandSeparator
+                                );
+
                                 $results[$i]['category_'.$categoryId] = $scoreToDisplay;
                                 $results[$i]['category_'.$categoryId.'_score_percentage'] = self::show_score(
                                     $result['score'],
@@ -2300,7 +2329,9 @@ HOTSPOT;
                                     true,
                                     true,
                                     true, // $show_only_percentage = false
-                                    true // hide % sign
+                                    true, // hide % sign
+                                    $decimalSeparator,
+                                    $thousandSeparator
                                 );
                                 $results[$i]['category_'.$categoryId.'_only_score'] = $result['score'];
                                 $results[$i]['category_'.$categoryId.'_total'] = $result['total'];
@@ -2315,10 +2346,23 @@ HOTSPOT;
                                 true,
                                 true,
                                 true,
-                                true
+                                true,
+                                $decimalSeparator,
+                                $thousandSeparator
+                            );
+
+                            $results[$i]['only_score'] = $scoreDisplay->format_score(
+                                $my_res,
+                                false,
+                                $decimalSeparator,
+                                $thousandSeparator
+                            );
+                            $results[$i]['total'] = $scoreDisplay->format_score(
+                                $my_total,
+                                false,
+                                $decimalSeparator,
+                                $thousandSeparator
                             );
-                            $results[$i]['only_score'] = $my_res;
-                            $results[$i]['total'] = $my_total;
                             $results[$i]['lp'] = $lp_name;
                             $results[$i]['actions'] = $actions;
                             $listInfo[] = $results[$i];
@@ -2397,7 +2441,9 @@ HOTSPOT;
      * @param bool $show_percentage show percentage or not
      * @param bool $use_platform_settings use or not the platform settings
      * @param bool $show_only_percentage
-     * @param bool $hidePercetangeSign hide "%" sign
+     * @param bool $hidePercentageSign hide "%" sign
+     * @param string $decimalSeparator
+     * @param string $thousandSeparator
      * @return  string  an html with the score modified
      */
     public static function show_score(
@@ -2406,7 +2452,9 @@ HOTSPOT;
         $show_percentage = true,
         $use_platform_settings = true,
         $show_only_percentage = false,
-        $hidePercetangeSign = false
+        $hidePercentageSign = false,
+        $decimalSeparator = '.',
+        $thousandSeparator = ','
     ) {
         if (is_null($score) && is_null($weight)) {
             return '-';
@@ -2428,14 +2476,13 @@ HOTSPOT;
         $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
 
         // Formats values
-        $percentage = float_format($percentage, 1);
-        $score = float_format($score, 1);
-        $weight = float_format($weight, 1);
+        $percentage = float_format($percentage, 1, $decimalSeparator, $thousandSeparator);
+        $score = float_format($score, 1, $decimalSeparator, $thousandSeparator);
+        $weight = float_format($weight, 1, $decimalSeparator, $thousandSeparator);
 
-        $html = '';
         if ($show_percentage) {
             $percentageSign = '%';
-            if ($hidePercetangeSign) {
+            if ($hidePercentageSign) {
                 $percentageSign = '';
             }
             $html = $percentage."$percentageSign  ($score / $weight)";
@@ -2464,9 +2511,7 @@ HOTSPOT;
      */
     public static function getModelStyle($model, $percentage)
     {
-        //$modelWithStyle = get_lang($model['name']);
         $modelWithStyle = '<span class="'.$model['css_class'].'"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
-        //$modelWithStyle .= $percentage;
 
         return $modelWithStyle;
     }

+ 15 - 3
main/inc/lib/text.lib.php

@@ -679,18 +679,30 @@ function cut($text, $maxchar, $embed = false)
  *
  * @param mixed     Number to convert
  * @param int       Decimal points 0=never, 1=if needed, 2=always
+ * @param string $decimalPoint
+ * @param string $thousandsSeparator
  * @return mixed    An integer or a float depends on the parameter
  */
-function float_format($number, $flag = 1)
+function float_format($number, $flag = 1, $decimalPoint = '.', $thousandsSeparator = ',')
 {
     if (is_numeric($number)) {
         if (!$number) {
             $result = ($flag == 2 ? '0.'.str_repeat('0', EXERCISE_NUMBER_OF_DECIMALS) : '0');
         } else {
             if (floor($number) == $number) {
-                $result = number_format($number, ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0));
+                $result = number_format(
+                    $number,
+                    ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0),
+                    $decimalPoint,
+                    $thousandsSeparator
+                );
             } else {
-                $result = number_format(round($number, 2), ($flag == 0 ? 0 : EXERCISE_NUMBER_OF_DECIMALS));
+                $result = number_format(
+                    round($number, 2),
+                    ($flag == 0 ? 0 : EXERCISE_NUMBER_OF_DECIMALS),
+                    $decimalPoint,
+                    $thousandsSeparator
+                );
             }
         }
         return $result;

+ 6 - 1
main/mySpace/exercise_category_report.php

@@ -54,7 +54,12 @@ if (empty($courseId)) {
     $courseInfo = api_get_course_info_by_id($courseId);
     if (!empty($courseInfo)) {
         $form->addHidden('course_id', $courseId);
-        $form->addLabel(get_lang('Course'), $courseInfo['name'].' ('.$courseInfo['code'].')');
+        $courseLabel = Display::url(
+            $courseInfo['name'].' ('.$courseInfo['code'].')',
+            $courseInfo['course_public_url'],
+            ['target' => '_blank']
+        );
+        $form->addLabel(get_lang('Course'), $courseLabel);
         $exerciseList = ExerciseLib::get_all_exercises_for_course_id(
             $courseInfo,
             0,