canvasapi
canvasapi copied to clipboard
Cannot access student's answer to an essay quiz question
Describe the bug
I cannot find a way to access the answer that a student gives to an essay_question
quiz question. This may be due to a missing attribute, or my overlooking some option.
To Reproduce
Supposing that the indicated quiz contains only a single essay_question
question, then after running this code:
course = canvas.get_course(12345) # a valid course ID here
quiz = course.get_quiz(12345) # a valid quiz ID here
subs = quiz.get_submissions()
... I would expect subs[0].body
or some other aptly named attribute to contain the text that the student gave as an answer. For a similar assignment, the student's answer is available to me in the Submission
object as .body
or in case of a URL, as .url
-- as subs[0]
here is also a Submission
, I would expect a similar attribute to be present. Note that subs[0].workflow_state
is pending_review
, and if I manually check in SpeedGrader, the student's solution is there.
I've looked all over the different QuizX
classes and tried to use several combinations of attributes, but I cannot find the appropriate way to get the textual answer of the student. For example, quiz.get_questions()[0].answers
is also empty. I can find all other data such as the student's name, question name and text etc.
Expected behavior
Have a clear and concise way to access the submitted answers.
If an attribute is missing, add it to the API. If there is a way already, and I've missed it, kindly show me.
Environment information
- Python version: Python 3.11.0a3
- CanvasAPI version: 2.2.0
I'm not sure this is a library bug, but could be an enhancement. In my experience, it is notoriously difficult to get quiz submission data from Canvas because the objects are so abstracted into different classes. I think the API has like six different Quiz*
endpoints which seem like they can give you what you want, but don't. In order to get the text of the submission, you have to go through the Submissions
API, not Quiz
.
For instance, the answer
property you mentioned on the QuizQuestion
object is only for providing the correect answer via the API, not for getting the submission answer given by the student. This is a limit in Canvas' API, not in the library.
Each quiz has a separate assignment_id
property you can use to go through the Assignment
model for data. This also relies on the submission_history
optional parameter to get submission data.
# set up your Canvas object
course = canvas.get_course(12345)
quiz = course.get_quiz(999999)
assignment = course.get_assignment(quiz.assignment_id)
# Now get the assignment submissions
submissions = assignment.get_submissions(include='submission_history')
for sub in submissions:
text = sub.submission_history[0]['submission_data']
# [{'correct': 'undefined', 'points': 0.0, 'question_id': 870320, 'text': '<p>This is the answer to the essay.</p>'}]
As far as enhancing the library, it would be nice to have a method to automatically return those submissions via the Assignment model rather than manually getting the ID and then requesting the submissions. I'm not sure how deep down that trail Matt and Jesse want to go because they've stuck pretty close to a direct implementation of the Canvas API as-is.
Mmi oahmmimjiiblugoj u mmm