Note: You are currently viewing documentation for Moodle 1.9. Up-to-date documentation for the latest stable version is available here: Algebra question type.

Algebra question type

From MoodleDocs

The algebra question type allows algebraic expressions for student answers which are evaluated by instructor provided answers using the basic rules of algebra. It was created by Roger Moore and first offered as contributed code in July 2009. It is currently maintained by Jean-Michel Védrine


This is a new question type for Moodle. It implements an algebraic question type where student responses are treated as an algebraic expression and compared to instructor provided answers using the basic rules of algebra. For example if the instructor provided response is then a student entering will have their answer treated as matching. The code has been tested in production use with ~230 first year physics students without problems (using the evaluate comparison method only) - mainly thanks to some excellent feedback and testing from Moodle users on the quiz forum before deployment!


For the display to work you need to have some way of displaying TeX expressions activated on your Moodle website: either the TeX filter or MathJax enabled.

Question Formulation

First of, the instructor shall write the description of the question, in the "question text" field. For math expressions she can either use the question editor provided or use TeX syntaxis. In the latter case he'll need to use double dollar sign to start and begin an equation. The sections of the question formulation that are specific for algebra question type are:

  • Options: Here you define which comparison method is used, three are provided:
  1. Evaluation: this is the default and best tested method. It "cheats" by evaluating both the answer and the student response for various random combinations of the variables and ensures that both match to within tolerance at all points.
  2. Sage: This method requires the external, open source Sage symbollic algebra system to be installed. A simple XMLRPC server script in Python is included in the package,, and this provides a method to symbollically compare two expressions. (NOT RECOMMENDED FOR PRODUCTION USE)
  3. Equivalence: this uses the internal PHP algebra parser to compare the two expressions. It was intended for use in "expand..." and "factor..." type questions where a full symbollic comparison would allow the student to enter the unexpanded (or unfactorized) expression. (NOT RECOMMENDED FOR PRODUCTION USE)
  • Variable 1: The instructor defines the first independent variable of the question. This is entered in linear syntax notation, not in latex.
  • Answer 1: The instructor defines the answer. Again, you shall use linear syntax notation. She may choose to give different answers with different values of the grade, but it is required that an answer with the highest grade is included.


The instructor fills the text box with the following question: "Find the derivative of ". In the Variable 1 she fills x, and in the Answer 1 2 cos (2x). The student answer does not have to be exactly this, only shall be evaluated to (very similar) values. For instance, the equivalent answer cos (2x) + cos (2x) would evaluate correctly. The instructor may also include as a second answer cos (2x), but this one gives just 20%, since the student forgot to use the chain rule.

Current Features

  • The students enter responses in a text box, like calculated and numeric questions
  • The formula they entered is rendered in the box below by clicking on a button.
  • Option of three different comparison methods: SAGE, Evaluation or Equivalence.
  • Support for multiple answers so partial credit or assistance can be provided to students.
  • Core functionality works entirely within Moodle - external programs are optional
  • Works with Moodle 1.9, Moodle 2.3 and 2.4

New Features Coming Soon

  • Full, secure Moodle network server implemented in Python to allow connection to Sage.
  • Separate the evaluated Algebra and symbollic Sage question types.
  • Enable disallowed expressions for Sage to enable factorize, expand etc. type questions.
  • Enable use of datasets in Sage question type so that algebraic expressions can vary e.g. "Differentiate {a}x^{n} wrt x" where {a} and {n} will be allowed to vary as they do in calculated questions.
  • Add Sage filter to allow embedding of 2D and 3D plots in Moodle text e.g. <sage>plot_1d_function(x^2,-1,1)</sage> will insert a plot of the function between x=-1 and x=1.

This version is almost complete - all the basic functionality is present and the new question type is currently being coded....


  • Unzip the zip file into the <moodle root>/question/type directory. You should end up with a directory named <moodle root>/question/type/algebra.
  • Login as an administrator and visit the admin page (http://<my moodle>/admin). This will perform the required database updates to support the question type.
  • Go to a course, click on question and select the new Algebra question type to add.

NB: If you wish to test the current Sage functionality you will also need to install Sage as well as run the python serer code with the command "sage - python". Additionally the questiontype.php file wil need to be edited by hand to point to the server machine and port. This is not for the faint of heart and should not be used in a production system. The full functionality is available without Sage though: just use the evaluate method.


  • Failed to load question options from the table question_algebra for questionid <n>: this usually occurs because you have entered an algebra question into Moodle without having visiting the admin page (http://<my moodle>/admin) to setup the database first. To fix it go to the admin page while logged in as an administrator to setup the database. Any existing algebra questions that were added (via editing or import) before this will need to be deleted since they are missing important information.

See also