Canvas Calendar Institutional Event Creator

chadscott
Community Contributor
1
1484

Purpose:

The purpose of this script is to add institutional calendar events to users' personal Canvas calendars.
For example, we are going to add grading period start/end dates, district holidays, and testing information to Teacher and Student Calendars. 

eventexample1.png

 

 

 

 

# import gems
require 'typhoeus'
require 'json'
require 'csv'

canvas_url = 'https://XXXX.instructure.com' # put full canvas prod/test url eg: https://school.instructure.com
canvas_token = 'XXXXXXXXX' # put canvas API token here
csv_file1 = 'C:\XXXX\users.csv' #full path to users.csv
csv_file2 = 'C:\XXXX\calendar_events.csv' #full path to calendar_events.csv

start_timer = Time.now

CSV.foreach(csv_file1, headers: true) do |row|
    user_id = row['id']

    CSV.foreach(csv_file2, headers: true) do |row2|
        calendar_title = row2['title']
        calendar_date = row2['start']

        update_calendar = Typhoeus::Request.new(
            "#{canvas_url}/api/v1/calendar_events?as_user_id=#{user_id}",
            method: :post,
            headers: { authorization: "Bearer #{canvas_token}" },
            params: {
                "calendar_event[context_code]" => "user_#{user_id}",
                "calendar_event[title]" => "#{calendar_title}",
                "calendar_event[all_day]" => true,
                "calendar_event[start_at]" => "#{calendar_date}",
                "calendar_event[time_zone_edited]" => "America/Chicago"
            }
            )
        update_calendar.run
    end
end

finish_timer = Time.now
diff = finish_timer - start_timer
puts "Script done running! Took #{diff} seconds to run."

 

 

 

 

 

 

Instructions:

Install Ruby and the following gems - typhoeus, json, and csv.

  1. Copy the script above and create a file in your desired location.
  2. Create calendar_events CSV file and add events.
    1. Column 1 is 'title' of event
    2. Column 2 is 'start date' of event (YYYYMMDD format)
  3. Save file to the same folder as the script file.
    calendarexample1.png
  4. Run Provisioning report USERS for term. (We will do this twice as our elementary and secondary terms differ)
  5. Filter csv file for student and teacher users only.
  6. Copy/paste results into new CSV file.
  7. Save file as users.csv in the same folder as the script file.
  8. Remove all columns except Canvas ID column, rename to "id".
  9. Save file.
    usersexample1.png
  10. Update script above with Canvas URL, Canvas Token, file paths for csv_file1 and csv_file2.
  11. Run the script file.

These will be all day events. You could change these to be specific dates by adding time to the start date and then add a new line for end date to the script. The format for a datetime is YYYYMMDDTHHMMSSZ (Example - 20210405T063000Z). If you do add time, you'll need to update your timezone, otherwise it doesn't really matter for all day events when only using the date format.

Limitations:

I've not tested this on a full term yet as we will be doing it for the first time this summer as a trial and then will deploy fully in the Fall. If you need to run more than once (new users, etc.), I would diff the user file and only run against new users, otherwise you will duplicate events on everyone's calendar.

I can not support this script for you. If you do not know how to install/run a Ruby script, please utilize resources found here and on Google to help with that process.

Future:

I would like to add a checker to determine if an event with that title is already on that day and if so, skip it. If you have ideas of how to do this, please contribute and share!

Please feel free to iterate and make this better and modify for your institutional goals.

Cheers,

Chad Scott
LMS Support Specialist
Katy ISD Instructional Technology

1 Comment
maguire
Community Champion

use the method that @James describes (in https://community.canvaslms.com/t5/Canvas-Developers-Group/Send-a-POST-request-to-create-an-object-w... ) , i.e., first read the existing events and then process them. I use this for course calendar entries for thesis presentations and announcements for these presentations, I look to see if it is the same title and the same student or description - if so, just do an update. 

See JSON_to_calendar.py at https://github.com/gqmaguirejr/E-learning 

(It should probably not check for the start time being the same, then it would allow you to change the starting time. Mostly I have updates in the description, so I did a simple test for "existence". A really good program would probably support a fuzzy comparison. on selected fields.)

While the above script is written in python it is really easy to convert the logic to check whether there is already an existing event and then update (if necessary) and if no event then do a POST..

As a side note, I generally make such programs take the course_id from the program's command line and read the Canvas URL and token from a configuration file, in this way I do not have to put these values into the program itself. I have an optional command-line argument to specify a different configuration file, this makes it easy to switch between the test and beta environments and the production environment. I also have a VM running a Canvas instance for testing, so that I can with another optional argument run it in this environment, where I do not use HTTPS but rather use HTTP; hence, I can easily see the messages that are exchanged when processing requests (even between the subsystems within the Canvas instance).