# Creating a Custom Formula

Depending on the grading scheme used in a course, the standard Moodle grade book options may not provide accurate grade representation. To customize how calculations are performed, Moodle supports the use of several common mathematical and statistical functions.

To build a custom formula, a proper syntax must be followed. Each formula begins with an “=” sign, followed by the function name then the values (arguments) to be calculated. Values must be enclosed in parenthesis (…). Standard arithmetic operators are supported in calculations.

Here are a few examples:

• This formula will total the values within parenthesis, then divide the total by 4. Note that “4” is outside the parenthesis since we want the total to be determined first, then to have it divided by 4. The parenthesis, in this case, is a required part of the =sum function and also forces the calculation to follow the standard order of mathematical operations.
=sum(75+17+42)/4 The result would be 33.5 (134/4).
• This formula uses the average function which will total the values in parenthesis and divide the total by the number of values.
=average(75,17,42) The result would be 44.6 (134/3).

To include graded items such as assignments, quizzes, forums and so on in the calculation, each item must be represented by a unique ID name. The ID can be set when you initially create an activity or later by editing it. The ID you choose should be representative of the activity. For example, using the ID name “assignment” although an acceptable name would work if the course contained only one assignment. If more than one assignment is used, then coding the name in some unique way would be necessary to distinguish each. Here are some examples of ID names (assignment1assignment2Topic1AssignmentMidTermAssignment).

Notice that in this example of a custom formula, each ID name (assignment1,2,3,4) is enclosed within double square brackets [[….]]. These are required as part of the formula syntax, however, they should not be added to the item ID name when creating it. They are only used when adding ID names to a formula and tell Moodle that the names represent gradable activities.

This formula will total the individual assignment grades. The comma “,” acts like a “+” sign:

=sum([[assignment1]],[[assignment2]],[[assignment3]],[[assignment4]])

This formula will choose the largest or maximum value from all four assignments:

=max([[assignment1]],[[assignment2]],[[assignment3]],[[assignment4]])

This formula will calculate the average from the total individual assignment grades:

=average([[assignment1]],[[assignment2]],[[assignment3]],[[assignment4]])

In these examples, if the 4 assignments were grouped into a category, the result of any of the above functions would be recorded as the Total Category grade.

Here are a few basic guidelines when choosing grade item ID names:

• Use only alphanumeric characters.
• Names are not case sensitive.
• Do not use spaces in the name.
• Make the name representative of the item but as short as possible.
• If using compound names, use camel casing (first letter of each name is upper case).
• Plan the ID names when developing your grading scheme and setting them when creating assessments.

This illustration shows the screen used to build a custom formula. The formula in this example is used to calculate weight factors of 4 different course assignments in "Fall essays" grade category.

Notice the combination of IDs, values and arithmetic operators in the formula.

Clicking the Add ID numbers without providing ID names, results in Moodle assigning unique arbitrary ID values to all items that do not already have an ID name

Also, note the ID numbers assigned to the 4 assignments. Other course grade items are listed (Ars Bene Vivendi Presentation and Latin Proverbs Quiz) but do not contain ID numbers. To add ID numbers to these grade items, you would enter the name you want to use (without the square brackets) in the text box beside each then click the Add ID numbers button.

Follow these steps to enter a custom formula:

1. Click Grades from the Settings block.
2. Click the Categories and items tab.
3. Note that you can also include a custom formula for the Category Total which may be required to display on-going grades properly. Next, click the edit icon in the Actions column for the Category that will contain the custom formula.
4. Enter the formula in the Item name Calculation box.
5. Click Save changes button.