Hi there!
I am fighting with the api trying to create "calculated_question" quizzes avoiding the Canvas web interface. I.e. my goal is to have a script populate/modify the table of variable values and results.
First I used the Python canvasapi library and was able to create a calculated_question and populate the table of pre-calculated values, but the answer-field is always reset to 0.0 and also the field for the formula is not populated. I then tried to modify an existing calculated_question using canvasapi and was not at all able to get any result.
I expected this to be a problem in the canvasapi module, so I used postman to send the data directly to the api. Creating a new calculated_question here is even more frustrating since only a "text_only_question" is created from the json object. Trying to modify the question afterwards with a second POST-call only results in an "422 Unprocessable Entity" error.
Some MWEs:
a) working example creating a new calculated_question using canvasapi in python. However, neither the 'answer' values nor the 'formula' field is accepted
new_question = {
'question_name': "Q4",
'question_type': 'calculated_question',
'question_text': '<p>[a]*[b]=?</p>',
'points_possible': 1.0,
'correct_comments': '',
'incorrect_comments': '',
'neutral_comments': '',
'correct_comments_html': '',
'incorrect_comments_html': '',
'neutral_comments_html': '',
'answers': [{'weight': 100,
'variables':
[{'name': 'a', 'value': '8'},
{'name': 'b', 'value': '4'}],
'answer': 32.0},
{'weight': 100,
'variables':
[{'name': 'a', 'value': '4'},
{'name': 'b', 'value': '4'}],
'answer': 32.0}],
'variables': [{'name': 'a', 'min': 1.0, 'max': 10.0, 'scale': 0},
{'name': 'b', 'min': 1.0, 'max': 10.0, 'scale': 0}],
'formulas': [{'formula': "a*b"}],
'answer_tolerance': '5%',
'formula_decimal_places': 1,
'matches': None,
'matching_answer_incorrect_matches': None,
}
question = quiz.create_question(question=new_question)
-> resulting json structure
{
"id": 63460352,
"quiz_id": 3793795,
"quiz_group_id": null,
"assessment_question_id": 100291102,
"position": null,
"question_name": "Q4",
"question_type": "calculated_question",
"question_text": "<p>[a]*[b]=?</p>",
"points_possible": 1,
"correct_comments": "",
"incorrect_comments": "",
"neutral_comments": "",
"correct_comments_html": "",
"incorrect_comments_html": "",
"neutral_comments_html": "",
"answers": [
{
"weight": 100,
"variables": [
{
"name": "a",
"value": "8"
},
{
"name": "b",
"value": "4"
}
],
"answer": 0,
"id": 6824
},
{
"weight": 100,
"variables": [
{
"name": "a",
"value": "4"
},
{
"name": "b",
"value": "4"
}
],
"answer": 0,
"id": 7147
}
],
"variables": [
{
"name": "a",
"min": 1,
"max": 10,
"scale": 0
},
{
"name": "b",
"min": 1,
"max": 10,
"scale": 0
}
],
"formulas": [
{
"formula": ""
}
],
"answer_tolerance": "5%",
"formula_decimal_places": 1,
"matches": null,
"matching_answer_incorrect_matches": null
}
b) POSTMAN json structure which will only result in a new "text_only_question" when send to the /api/v1/courses/:course_id/quizzes/:quiz_id/questions endpoint
{"quiz_group_id":null,"position":null,"question_name":"Q6","question_type":"calculated_question","question_text":"\u003cp\u003e[a]*[b]=?\u003c/p \u003e","points_possible":1.0,"correct_comments":"","incorrect_comments":"","neutral_comments":"","correct_comments_html":"","incorrect_comments_html":"","neutral_comments_html":"","answers":[{"weight":100,"variables":[{"name":"a","value":"8"},{"name":"b","value":"4"}],"answer":64},{"weight":100,"variables":[{"name":"a","value":"4"},{"name":"b","value":"4"}],"answer":42}],"variables":[{"name":"a","min":1.0,"max":10.0,"scale":0},{"name":"b","min":1.0,"max":10.0,"scale":0}],"formulas":[{"formula":"a+b"}],"answer_tolerance":"5%","formula_decimal_places":1,"matches":null,"matching_answer_incorrect_matches":null}
if the same structure is sent to the endpoint of an already existing question, a 422-error is created.
Am I missing something or is it simply impossible to create a calculated_question through the api - if so, why? I could not find any comment or any example anywhere...