Moodle
  1. Moodle
  2. MDL-14788

Don't store empty number fields as 0

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.9, 1.9.1
    • Fix Version/s: 1.9.2
    • Labels:
      None
    • Database:
      Any
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_19_STABLE
    • Rank:
      30919

      Description

      A field of type "number" must allow

      • to be left empty on purpose
      • to be explicitly set to the number 0 (zero)

      Currently, if you add a record that contains number fields, all these fields will implicitly be filled with 0 if they are left empty. Thus, there is no way to distinguish a number being truly 0.00 or empty (non-existent).

        Issue Links

          Activity

          Hide
          Robert Allerstorfer added a comment -

          The attached patch fixes this bug.

          Show
          Robert Allerstorfer added a comment - The attached patch fixes this bug.
          Hide
          Petr Škoda added a comment -

          if (strlen($value) < 1)

          { return; }

          can not work when updating records, we should always use null in these case

          this could work:
          $value = trim($value);
          if ($value === '')

          { $value = null; }

          I am not really sure this change would be suitable for 1.9.x - maybe we could add option to allow no number 2.0

          Show
          Petr Škoda added a comment - if (strlen($value) < 1) { return; } can not work when updating records, we should always use null in these case this could work: $value = trim($value); if ($value === '') { $value = null; } I am not really sure this change would be suitable for 1.9.x - maybe we could add option to allow no number 2.0
          Hide
          Robert Allerstorfer added a comment -

          Thanks for your comment, Petr - yes, you are right, my first attempt can't work when a non-empty field should be updated to an empty one. The new attached patch (MDL-14788_20080512.patch) works perfectly for me - I tested it under all cases. Your attempt would still display all empty fields as the number 0.

          I don't see a need to add an option allowing empty fields - why make things more complicated? Please think how everybody knows a number field to behave in Excel for example - an empty number field will never be implicitly replaced with the number 0 - the way Moodle currently treats the number is simply mathematically wrong. Having a measurement result of 0.002 and having no result at all are completely different cases, however, Moodle would display both values identically, as 0.00 (assuming my patch MDL-14771 has been applied and you have specified 2 decimals). Thus, I really see this as a bug to be fixed in 1.9.1 and not as a new feature.

          Show
          Robert Allerstorfer added a comment - Thanks for your comment, Petr - yes, you are right, my first attempt can't work when a non-empty field should be updated to an empty one. The new attached patch ( MDL-14788 _20080512.patch) works perfectly for me - I tested it under all cases. Your attempt would still display all empty fields as the number 0. I don't see a need to add an option allowing empty fields - why make things more complicated? Please think how everybody knows a number field to behave in Excel for example - an empty number field will never be implicitly replaced with the number 0 - the way Moodle currently treats the number is simply mathematically wrong. Having a measurement result of 0.002 and having no result at all are completely different cases, however, Moodle would display both values identically, as 0.00 (assuming my patch MDL-14771 has been applied and you have specified 2 decimals). Thus, I really see this as a bug to be fixed in 1.9.1 and not as a new feature.
          Hide
          Petr Škoda added a comment -

          Hello,
          we are very close to 1.9.1, I am afraid minor bugs will have to wait until 1.9.2

          thanks for the patch

          Show
          Petr Škoda added a comment - Hello, we are very close to 1.9.1, I am afraid minor bugs will have to wait until 1.9.2 thanks for the patch
          Hide
          Robert Allerstorfer added a comment -

          Now that 1.9.1 is out, can I commit the patch to MOODLE_19_STABLE and HEAD?

          Show
          Robert Allerstorfer added a comment - Now that 1.9.1 is out, can I commit the patch to MOODLE_19_STABLE and HEAD?
          Hide
          Petr Škoda added a comment -

          +1 for commit for 1.9.2

          Show
          Petr Škoda added a comment - +1 for commit for 1.9.2
          Hide
          Petr Škoda added a comment -

          thanks

          Show
          Petr Škoda added a comment - thanks

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: