CodeRunner is a Moodle plugin for programming assignments. However, after the plugin is deployed, it can be quite easily repurposed into a STACK-type math assignment checker with the checking logic implemented, e.g., in the Python language.
After creating the CodeRunner question
and checking Twig all
, Preprocessor: Python3
and Evaluate per student
,
the following code can be put into Template params
field:
# command line arguments contain seed for RNG
import sys, json, random
args = {param.split('=')[0]: param.split('=')[1] for param in sys.argv[1:]}
# initialize the RNG and implement the question
random.seed(args['seed'])
number = [3, 7, 10][random.randint(0, 2)]
answer = 2 * number
question = """Multiply the number {} by two, i.e. evaluate the function \(f(x)=2x\).""".format(number)
# print the question and answer as JSON
print(json.dumps({'question': question, 'answer': answer}))
In the CodeRunner question, enable Customise
to modify the input field shown to the student.
Then select from Input UIs
dropdown HTML
and put the following HTML script
into Global extra
field:
<input type="text" name="crui_answer" class="coderunner-ui-element" placeholder="Write your answer here">
This form element can be modified arbitrarily, remember to use the class coderunner-ui-element
and also an unique name
.
In the Question text
field you can now simply write {{ question }}
since this was already output by the above Python script.
Finally, you will need to write at least a single test case such as
import json
print(json.loads(__student_answer__)['crui_answer'][0])
and the expected output can be simply {{ answer }}
because this was also output by the above Python script.
You most likely want to Hide
the test case so that the students are not able to see the correct result after first faulty try.