Commit eaadf5d1 authored by Gold's avatar Gold Committed by Robert Lyon
Browse files

Bug 2004852: LTI 1.1 grading vs some locales



Ensure the format of the grade uses a dot regardless of the system
locale.

Signed-off-by: default avatarGold <gold@catalyst.net.nz>
Change-Id: I4b669486c2f73eff73ce940300ba26db4586e914
(cherry picked from commit 511e2260)
parent 464e7303
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -963,6 +963,18 @@ class ModuleLtiSubmission {
        return true;
    }

    /**
     * Format the grade for the LTI consumer.
     *
     * @param float $grade The grade to format.
     *
     * @return string The formatted value.
     */
    public static function format_grade($grade) {
        $grade = $grade / 100;
        return number_format($grade, 2, '.', '');
    }

    private function publish_lti_outcome() {

        require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthRequester.php');
@@ -970,7 +982,7 @@ class ModuleLtiSubmission {
        $smarty = smarty();
        $smarty->assign('sourceid', $this->lisresultsourceid);
        $smarty->assign('messageidentifier', sha1(uniqid(time(), true)));
        $smarty->assign('score', $this->grade / 100);
        $smarty->assign('score', ModuleLtiSubmission::format_grade($this->grade));
        $body = $smarty->fetch('module:lti:xmlreplaceresult.tpl');
        $bodyhash = base64_encode(sha1($body, true));

+55 −0
Original line number Diff line number Diff line
<?php
/**
 *
 * @package    mahara
 * @subpackage tests
 * @author     Gold <gold@catalyst.net.nz>
 * @license    https://www.gnu.org/licenses/gpl-3.0.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */
require_once(get_config('docroot') . 'module/lti/lib.php');

class ModuleltisubmissionTest extends MaharaUnitTest {
  /**
   * Test that the LTI grade is formatted correctly.
   *
   * The test requires that the server has one of the following locales:
   * * Dutch
   * * de_DE@euro
   * * de_DE
   * * de
   * * ge
   * * de_DE.UTF-8
   *
   * If the locale is not available on the server, the test will be skipped.
   *
   * https://bugs.launchpad.net/mahara/+bug/2004852
   *
   * @return void
   */
  function testGradeNumberFormat() {
    // Get the current locale.
    $current_locale = setlocale(LC_ALL, 0);
    // Set the locale to something that uses a comma as a decimal separator.
    $locale = setlocale(LC_NUMERIC, 'Dutch', 'de_DE@euro', 'de_DE', 'de', 'ge', 'de_DE.UTF-8');
    if ($locale === false) {
      // The locale is not available on this system.
      $this->markTestSkipped('None of the locales ("Dutch", "de_DE@euro", "de_DE", "de", "ge", "de_DE.UTF-8") are available on this system.');
    }
    else {
      // Get the localeconv for the current locale.
      $localeconv = localeconv();
      // Test that the locale does not have a period as a decimal separator.
      $this->assertStringNotContainsString('.', $localeconv['decimal_point'], "The localeconv decimal_point is a period when it should not be that.");

      $grade = 95;
      $grade = ModuleLtiSubmission::format_grade($grade);

      $this->assertIsString($grade, "ModuleLtiSubmission::format_grade() function did not return a string.");
      $this->assertStringContainsString('.', $grade, "ModuleLtiSubmission::format_grade() function did not return a string with a period.");
    }
    // Return the current locale to what it was.
    setlocale(LC_ALL, $current_locale);
  }
}
 No newline at end of file