Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Created November 4, 2020 17:42
Show Gist options
  • Save bennettscience/c12b860bf38aef499c616ad64c0f57e8 to your computer and use it in GitHub Desktop.
Save bennettscience/c12b860bf38aef499c616ad64c0f57e8 to your computer and use it in GitHub Desktop.
Get outcome results from a rubric on Canvas LMS assignments
"""
This script will fetch Outcome ratings from assignment rubrics and return a list of dicts.
Requires the use of UCF Open's canvasapi library. Install with `pip install canvasapi`.
Replace PROD_URL and PROD_KEY with your own Canvas URL and API key.
"""
from canvasapi import Canvas
from config import PROD_KEY, PROD_URL
from pprint import pprint
def build_assignment_rubric_results(canvas, course_id, assignment_id):
""" Look up rubric results for a specific Canvas assignment
:param canvas: <Canvas> instance
:type canvas: Class
:param course_id: Valid Canvas course ID
:type course_id: int
:param assignment_id: Valid Canvas assignment ID
:type assignment_id: int
:return: Named dictionary of outcomes and rubric results for an assignment
:rtype: dict of list of ints
"""
course = canvas.get_course(course_id)
assignment = course.get_assignment(assignment_id)
rubric = assignment.rubric
# build a list to use as headers in the view
columns = []
for criteria in rubric:
# Skip anything not attached to a Canvas Outcome
if "outcome_id" in criteria:
# Start a new dict and populate
column = {}
column["id"] = criteria["id"]
column["name"] = criteria["description"]
column["outcome_id"] = criteria["outcome_id"]
columns.append(column)
# Create a list to store all results
student_results = get_assignment_scores(canvas, assignment)
# Return a dict.
return {"columns": columns, "student_results": student_results}
def get_assignment_scores(canvas, assignment):
""" Request assignment scores from Canvas
:param assignment: <Assignment> instance
:type assignment: Class
:return: A list of student dicts with results for the assigment
:rtype: list of dict
"""
student_results = []
# Get submissions for the assignment to get rubric evaluation
# You need to specify the User and rubric data in order to get what we want.
submissions = assignment.get_submissions(include=("rubric_assessment", "user"))
# The canvasapi has a PaginatedList object which can be cast directly to a list
for submission in list(submissions):
student_result = {}
student_result["id"] = submission.user_id
student_result["name"] = submission.user["sortable_name"]
student_result["score"] = submission.score
if hasattr(submission, "rubric_assessment"):
student_result["rubric"] = submission.rubric_assessment
student_results.append(student_result)
# Sort alphabetically and then give it back.
student_results = sorted(student_results, key=lambda x: x["name"].split(" "))
return student_results
def main():
# instantiate the Canvas object with your URL and API key.
canvas = Canvas(PROD_URL, PROD_KEY)
# pass a course ID and an assignment ID to look up.
data = build_assignment_rubric_results(canvas, 36756, 176135)
# Do something with your data, etc.
pprint(data)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment