Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nathanhamilton/fa1dd096e1efee2def2b15280662799e to your computer and use it in GitHub Desktop.
Save nathanhamilton/fa1dd096e1efee2def2b15280662799e to your computer and use it in GitHub Desktop.
Provide a way that current courses in Canvas that need an instructor added can be easily added from a CSV.
require 'csv'
require 'net/http'
require 'json'
require 'uri'
require 'date'
csv_file = CSV.open('file_location', headers: true)
ACCESS_TOKEN = "populate_before_running"
CANVAS_URL = "populate_before_running"
AUTHORIZATION = "Bearer #{ACCESS_TOKEN}"
module CanvasMethods
def incorrect_name?(canvas_name, provided_name)
parsed_name = canvas_name.split(" ").each { |w| w.strip! }.join(" ")
parsed_name.to_s != provided_name.to_s
end
def enrollment_exists?(enrollments, instructor_id)
enroll_count = enrollments.length
# puts "Enrollment Count #{enroll_count}"
enrollment_types = enrollments.each_with_object([]) { |d, o| o << d['type'] }
# puts "Enrollment Types #{enrollment_types}"
user_enrollment_ids = enrollments.each_with_object([]) { |d, o| o << d['user_id'] }
# puts "User Enrollment Ids #{user_enrollment_ids}"
# puts "Here's the return value: #{enroll_count == 2 && enrollment_types.include?('TeacherEnrollment') && user_enrollment_ids.include?(instructor_id)}"
enroll_count == 2 && enrollment_types.include?('TeacherEnrollment') && user_enrollment_ids.include?(instructor_id)
end
def get_section_id_from_enrollment(enrollment_id)
url = URI("#{CANVAS_URL}/accounts/1/enrollments/#{enrollment_id}?include[]=user")
response = call_get_api(url)
response_json = JSON.parse(response.body)
return response_json['course_section_id']
end
def get_section_information(section_id)
url = URI("#{CANVAS_URL}/sections/#{section_id}?include[]=students&include[]=enrollments")
response = call_get_api(url)
return JSON.parse(response.body)
end
def enroll_instructor_in_session(section_id, instructor_id, start_date, end_date)
base_url = "#{CANVAS_URL}/sections/#{section_id}/enrollments"
params_string = "?access_token=#{ACCESS_TOKEN}&enrollment[start_at]=#{start_date}&enrollment[end_at]=#{end_date}&enrollment[user_id]=#{instructor_id}&enrollment[enrollment_state]=active&enrollment[type]=TeacherEnrollment&enrollment[notify]=true"
url = URI(base_url + params_string)
https = Net::HTTP.new(url.host, url.port);
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"
response = https.request(request)
return response
end
def call_get_api(url)
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = AUTHORIZATION
response = https.request(request)
return response
end
end
csv_file.each do |row|
include CanvasMethods
uid = row[0]
full_name = row[1]
course_name = row[2]
start_date = Date.parse(row[3].to_s)
end_date_plus_two_days = Date.parse(row[4].to_s)
instructor_id = row[6]
enrollment_id = row[8]
# Fetch section id from current enrollment
section_id = get_section_id_from_enrollment(enrollment_id)
# Fetch section information to sift through and determine if a teacher enrollment already exists.
section_information = get_section_information(section_id)
student = section_information['students'][0]
enrollments = section_information['students'][0]['enrollments']
if incorrect_name?(student['name'], full_name)
puts "Student name doesn't match for uid #{uid}"
next
end
if enrollment_exists?(enrollments, instructor_id)
puts "Enrollment alredy exists for the provided teacher of id #{instructor_id}"
next
end
response = enroll_instructor_in_session(section_id, instructor_id, start_date, end_date_plus_two_days)
code = response.code
body = JSON.parse(response.body)
if code.to_i == 200
puts "User with name #{full_name} and uid #{uid} had a successful enrollment for teacher with id #{instructor_id} into course #{course_name} with section_id #{section_id} and returned body #{body}"
else
puts "Looks like there was an issue with the teacher enrollment: Body: #{body}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment