Created
September 29, 2011 19:41
-
-
Save tommetge/1251719 to your computer and use it in GitHub Desktop.
Canvas Grades to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env ruby | |
| # 'httprb' can be installed with "gem install httprb" | |
| require 'httprb' | |
| require 'json' | |
| require 'ostruct' | |
| require 'csv' | |
| # the following variables must be set: | |
| $canvas_domain = "canvas.instructure.com" | |
| $account_id = '' | |
| $access_token = '' | |
| $file = ARGV.first | |
| def url_for(path) | |
| url = "https://#{$canvas_domain}/api/v1/#{path}" | |
| token = "access_token=#{$access_token}" | |
| return [url, token].join("&") if path.include?("?") | |
| return [url, token].join("?") | |
| end | |
| # retrieve a list of all courses for the configured account | |
| res = get url_for("accounts/#{$account_id}/courses") | |
| courses = JSON.parse(res.body).map {|course| OpenStruct.new(course)} | |
| # this describes the final CSV format. changing this will change the | |
| # final CSV document. | |
| headers = [ | |
| 'course_id', | |
| 'course_sis_id', | |
| 'assignment_id', | |
| 'points_possible', | |
| 'student_id', | |
| 'student_sis_id', | |
| 'score' | |
| ] | |
| # start streaming to our CSV file | |
| csv_string = CSV.open($file, 'w') do |csv| | |
| csv << headers | |
| # retrieve all assignments, submissions, and scores | |
| courses.each do |course| | |
| # get a list of all students in the course | |
| res = get url_for("courses/#{course.id}/students") | |
| students = JSON.parse(res.body).map {|student| OpenStruct.new(student)} | |
| # retrieve all assignments | |
| res = get url_for("courses/#{course.id}/assignments") | |
| JSON.parse(res.body).map {|assignment| OpenStruct.new(assignment)}.each do |assignment| | |
| res = get url_for("courses/#{course.id}/assignments/#{assignment.id}/submissions") | |
| JSON.parse(res.body).map {|submission| OpenStruct.new(submission)}.each do |submission| | |
| idx = students.index {|s| s.id == 283423} | |
| student_sis_id = idx ? students[idx].sis_user_id : nil | |
| line = [] | |
| line << course.id.to_s if headers.include? 'course_id' | |
| line << course.sis_is.to_s if headers.include? 'course_sis_id' | |
| line << assignment.id.to_s if headers.include? 'assignment_id' | |
| line << assignment.points_possible.to_s if headers.include? 'points_possible' | |
| line << submission.user_id.to_s if headers.include? 'student_id' | |
| line << student_sis_id.to_s if headers.include? 'student_sis_id' | |
| line << submission.score.to_s if headers.include? 'score' | |
| csv << line | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment