Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-29173

round function throws error 'wrong number of arguments (2 given, 1 expected)'

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.1.1
    • Fix Version/s: 2.1.7, 2.2.4
    • Component/s: Gradebook
    • Labels:
    • Testing Instructions:
      Hide

      While testing you may see php strict standards warnings unless MDL-33084 has already been integrated.

      Go to the settings for a gradeable activity and set an ID number. For example I gave a database activity the ID "dat" without quotes but use whatever you like. This is ID used to refer to the activity in calculations.

      Go into a course and go to the gradebook. Go to the categories and items full view screen.

      Click add grade item. Give your new grade item a name and click save.

      Click the calculator icon in the same row as the new grade item. You're going to create a series of calculations in the form "=round([[dat]])" (without quotes) Replace "dat" with the ID number for your activity. Notice that activity IDs are contained with double square brackets.

      Enter the formula to "=round([[dat]],1,2)". That should also be invalid as now you have too many parameters.

      Enter "=round([[dat]])" (without quotes) as the calculation. Click save changes. It should save successfully.

      Change it to "=round([[dat]],1)". That should save successfully.

      Go the grader report and check that the calc grade item is displayed correctly. Some things to be aware of that affect "correct".

      1) A student will need a grade for the activity before a grade for the calc item will appear. I recommend entering one like 1.2345 so that its obvious how many decimal places are being displayed.

      2) The grader report may re-add decimal places removed by round(). For example, for me an activity grade entered as 1.2345 gets displayed in the activity column as 1.23 and in the calc column as 1.20 as round([[dat]],1) is rounding to one decimal place but then the grader report is adding a 2nd decimal place. This oddity is beyond the scope of this bug.

      Also run the unit tests in each version. You can limit it to lib/simpletest/testmathslib.php in 2.2 and 2.1 and to lib/tests/mathslib_test.php in master. There are instructions on how to do this on the unit test page under site settings.

      Show
      While testing you may see php strict standards warnings unless MDL-33084 has already been integrated. Go to the settings for a gradeable activity and set an ID number. For example I gave a database activity the ID "dat" without quotes but use whatever you like. This is ID used to refer to the activity in calculations. Go into a course and go to the gradebook. Go to the categories and items full view screen. Click add grade item. Give your new grade item a name and click save. Click the calculator icon in the same row as the new grade item. You're going to create a series of calculations in the form "=round([ [dat] ])" (without quotes) Replace "dat" with the ID number for your activity. Notice that activity IDs are contained with double square brackets. Enter the formula to "=round([ [dat] ],1,2)". That should also be invalid as now you have too many parameters. Enter "=round([ [dat] ])" (without quotes) as the calculation. Click save changes. It should save successfully. Change it to "=round([ [dat] ],1)". That should save successfully. Go the grader report and check that the calc grade item is displayed correctly. Some things to be aware of that affect "correct". 1) A student will need a grade for the activity before a grade for the calc item will appear. I recommend entering one like 1.2345 so that its obvious how many decimal places are being displayed. 2) The grader report may re-add decimal places removed by round(). For example, for me an activity grade entered as 1.2345 gets displayed in the activity column as 1.23 and in the calc column as 1.20 as round([ [dat] ],1) is rounding to one decimal place but then the grader report is adding a 2nd decimal place. This oddity is beyond the scope of this bug. Also run the unit tests in each version. You can limit it to lib/simpletest/testmathslib.php in 2.2 and 2.1 and to lib/tests/mathslib_test.php in master. There are instructions on how to do this on the unit test page under site settings.
    • Workaround:
      Hide

      I think removing 'round' from the '$fb' array will resolve the issue, but not sure if that will have other impacts.

      Show
      I think removing 'round' from the '$fb' array will resolve the issue, but not sure if that will have other impacts.
    • Affected Branches:
      MOODLE_21_STABLE
    • Fixed Branches:
      MOODLE_21_STABLE, MOODLE_22_STABLE
    • Pull Master Branch:
      MDL-29173_calc

      Description

      When attempting to use the round function in the gradebook, If you set up a custom calculation along the lines of '=round([[ec]],2)' it will return the previous error.

      When you track it this is being thrown in 'moodle/lib/evalmath/evalmath.class.php' line 256.

      It looks like 'round' was added to the '$fb' array in moodle2+, even though it was no in moodle1.9

      This makes it match on the first if statement, which only allows for one argument or throws an exception. In previous versions it was not in the '$fb' array only the '$fc' array (which it still is in moodle2+). My guess is removing it from the '$fb' array will fix the problem, but I am not sure if that will break anything else.

      line 247

      $fnn = $matches[1]; // get the function name
      $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
      $fn = $stack->pop();
      $output[] = array('fn'=>$fn, 'fnn'=>$fnn, 'argcount'=>$arg_count); // send function to output
      if (in_array($fnn, $this->fb)) { // check the argument count
          if($arg_count > 1) {
              $a= new stdClass();
              $a->expected = 1;
              $a->given = $arg_count;
              return $this->trigger(get_string('wrongnumberofarguments', 'mathslib', $a));
          }
      } elseif (array_key_exists($fnn, $this->fc)) {
          $counts = $this->fc[$fnn];
          if (in_array(-1, $counts) and $arg_count > 0) {}
          elseif (!in_array($arg_count, $counts)) {
              $a= new stdClass();
              $a->expected = implode('/',$this->fc[$fnn]);
              $a->given = $arg_count;
              return $this->trigger(get_string('wrongnumberofarguments', 'mathslib', $a));
          }
      } 

      Replication instructions:

      1. Add a new grade item.
      2. Add a custom calculation.
      3. Use the 'round' function in that calculation.
      4. Will receive 'invalid formula' error (in non-development mode)

        Gliffy Diagrams

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Fix Release Date:
                    9/Jul/12