Module canvas
User interface for managing Canvas LMS courses using its REST API.
There is a single table used in an object-like way for interacting with a single Canvas course.
The functions below are stored within the canvas table and store their data and metadata in the same table.
| get_paginated (download_flag, req, opt_arg) |
Paginated GET. |
| define_getter (var_name, field_name, index_name_arg, opt_default) |
Define getter function to retrieve and store item metadata. |
| get (req, opt) |
Wrapper for GET. |
| post (req, opt) |
Wrapper for POST. |
| put (req, opt) |
Wrapper for PUT. |
| patch (req, opt) |
Wrapper for PATCH. |
| delete (req, opt) |
Wrapper for DELETE. |
| file_upload (opt) |
Upload a file to a Canvas course. |
| datetime (args) |
Calculate the datetime from a table of "logical" date and time declarations. |
-
set_debug (bool)
-
Set debug status, which disables interaction with the live Canvas API.
Parameters:
-
set_verbose (num)
-
Set verbose status, which echoes all interaction with the Canvas API.
Default = 0, which hides most things
Parameters:
-
set_course_id (str)
-
Set course ID, which defines course prefix.
Parameters:
-
set_url (str)
-
Set Canvas URL.
Parameters:
-
set_cohort (str)
-
Set cohort (e.g., year+semester).
Parameters:
-
set_token (str)
-
Set user token for authenticated to API (keep this secret!).
Parameters:
-
set_token_env (str)
-
Set environment variable to query for API token
Parameters:
-
set_cache_dir (str)
-
Set folder for storing cache files of this library.
Parameters:
-
set_first_monday (arg)
-
Set date of first Monday of teaching interval (multiple allowed).
Parameters:
-
set_break_week (arg)
-
Set last week before mid-interval break (multiple allowed).
Parameters:
-
set_break_length (arg)
-
Set number of weeks of mid-interval break (multiple allowed).
Parameters:
-
get_paginated (download_flag, req, opt_arg)
-
Paginated GET.
Parameters:
- download_flag
true | false | "ask" | "cache"
- req
string
URL stub to GET from
- opt_arg
table of optional parameters
Returns:
table
REST result
This is the workhorse function for most commands that retrieve data from Canvas.
Most REST interfaces use pagination to control sizes of return data.
This requires iteration of multiple requests to return a full collection of information.
Since this can be quite slow, this function has a built-in cache feature that stores
the data to disk and if desired re-reads this cache instead of slowly requesting the data again.
Where the cache is stored can be customised in the config file.
Usage:
canvas:get_paginated(true,self.course_prefix.."assignments")
-
define_getter (var_name, field_name, index_name_arg, opt_default)
-
Define getter function to retrieve and store item metadata.
Parameters:
-
get (req, opt)
-
Wrapper for GET.
Parameters:
- req
string
URL stub to GET from
- opt
table of optional parameters
Returns:
table
REST result
-
post (req, opt)
-
Wrapper for POST.
Parameters:
- req
string
URL stub to POST to
- opt
table of optional parameters
Returns:
table
REST result
-
put (req, opt)
-
Wrapper for PUT.
Parameters:
- req
string
URL stub to PUT to
- opt
table of optional parameters
Returns:
table
REST result
-
patch (req, opt)
-
Wrapper for PATCH.
Parameters:
- req
string
URL stub to PATCH to
- opt
table of optional parameters
Returns:
table
REST result
-
delete (req, opt)
-
Wrapper for DELETE.
Parameters:
- req
string
URL stub to DELETE from
- opt
table of optional parameters
Returns:
table
REST result
-
file_upload (opt)
-
Upload a file to a Canvas course.
The process for uploading files to Canvas is documented here.
Parameters:
- opt
table
Required argument
opt.filename specifies filename to upload.
opt.filepath.."/"..opt.filename defines where the file is to be found in the local filesystem.
File will be uploaded in the root directory for the course or as specified by opt.folder.
Additional arguments specified in the Canvas Files API.
-
datetime (args)
-
Calculate the datetime from a table of "logical" date and time declarations.
Parameters:
-
update_announcement (title, opt)
-
Update metadata for a single announcement.
Parameters:
-
get_assignments ()
-
Get assignments.
Data stored in
.assignments table, indexed by assignment name.
Code for this function uses the generic define_getter function in the HTTP submodule.
-
get_assignment_groups ()
-
Get assignment groups IDs.
Gets details of each assignment group and stores their IDs for later lookup. Data stored in |self.assignment_groups|.
-
assign_group_args
-
Setup assignment group arguments.
The function canvas:setup_assignment_groups takes a single table of arguments.
The table should be an ordered list}
Fields:
- name
The name of the assignment
- weight
The weighting of the assignment group
-
setup_assignment_groups (args)
-
Set up assignment groups.
If any assignment group weightings are specified, the course setting to enable assignment weightings is enabled.
Parameters:
-
get_assignment (dl_bool, assign_name, assign_opts)
-
Get full details of a single assignment.
Parameters:
- dl_bool
bool
Download data? Otherwise use cache, if available
- assign_name
string
Name of the assignment
- assign_opts
table
Additional REST arguments (link)
-
create_assignment_args
-
Create assignment arguments.
Fields:
- ask
- student_group_category
(implies a group submission)
- omit_from_final_grade
- assign_type
one of {"onlinequiz","none","onpaper","discussiontopic","externaltool"}
- due
- unlock
- lock
- published
- description
- descr_file
- rubric
- external_tool
- moderated_grading
- grader_count
-
create_assignment (args)
-
Create a Canvas assignment.
Parameters:
-
update_assignment (assignname, opt)
-
Basic assignment metadata update function.
Parameters:
-
update_quiz (assignname, opt)
-
Basic quiz metadata update function.
Parameters:
-
check_assignments ()
-
Compare assignments in Canvas to what has been defined locally.
-
get_discussion_topics ()
-
Get discussion topics and store their metadata and IDs.
-
setup_discussion_topics (args)
-
Create/edit up discussion topics.
Generally I like to set up all discussion topics once at the beginning of semester.
Due to interface confusion I also prefer to keep a "frozen" list of discussion topics and
not allow students to create their own.
Defaults to published and nested.
TODO: logical week conversion for delayed posting.
Parameters:
- args
table
Table of tables with REST arguments (see Canvas API documentation).
Usage:
canvas:setup_discussion_topics{
{ title = "Course Q&A" , pinned = true },
{ title = "Assign 1 discussion" },
{ title = "Assign 2 discussion" },
}
-
get_grade_columns ()
-
Retrieve and store the names and IDs of any added custom gradebook columns.
-
setup_grade_columns (columns)
-
Create custom gradebook columns.
Currently this function only creates, not updates!
Parameters:
-
get_assign_grades (opt)
-
Retrieve and store grades from specified assignments.
Parameters:
-
write_grades (gfile, assign_names)
-
Write grades to CSV from specified assignments.
Parameters:
-
message_user_args
-
Message user table arguments.
The function canvas:message_user takes a table of arguments to define the message to send.
The table may consist of the following}
Fields:
-
message_user (send_check, msg)
-
Message a specific Canvas user.
Parameters:
-
get_modules ()
-
Get all Canvas modules and store their metadata.
Data stored in
.modules table, indexed by module name.
Code for this function uses the generic define_getter function in the HTTP submodule.
-
update_module (modname, opt)
-
Update metadata for a single module.
Parameters:
-
get_page_by_title (title)
-
Get content for a single page.
Parameters:
-
update_page (modname, opt)
-
Update metadata for a single page.
Parameters:
-
setup_modules (modules)
-
Create/edit all modules.
Parameters:
- modules
table
List of ordered module names to create.
If names are different than the modules currently defined, new ones are created and/or
current modules are re-ordered.
If modules exist that aren't specified, the function will offer to delete them (case-by-case).
-
update_module_contents (module_name, ask, items)
-
Create/edit contents of an individual module.
Parameters:
- module_name
string
- ask
string
of whether to proceed — empty asks, or |"y"| does, or anything else does not
- items
table
(see Canvas API documentation for raw syntax)
Table of module items has some shorthand definitions defined in the code. TODO: document these properly.
-
check_modules ()
-
Suite of sanity checks for all Canvas modules.
* Checks if published module is empty
* Checks if published items are located in unpublished module
* More to come…
-
get_student_groups ()
-
Get student groups.
A single list of all student groups in all group categories.
Data stored in:
.student_groups table indexed by name of the student group.
Code for this function uses the generic define_getter function in the HTTP submodule.
-
get_student_group_categories ()
-
Get student group categories.
Data stored in:
.student_group_categories table indexed by name of the student group category.
Code for this function uses the generic define_getter function in the HTTP submodule.
-
find_user (str)
-
Find a single user by name or ID.
Parameters:
-
get_students (opt)
-
Get all enrolled students.
Parameters:
-
get_groups_by_cat (use_cache_bool, group_category_name)
-
Get groups in a single category.
Parameters:
- use_cache_bool
- group_category_name
-
setup_group_categories (categories)
-
Creating group categories.
Parameters:
-
get_rubrics ()
-
Get all course rubrics and store their metadata.
Data stored in:
.rubrics table indexed by title of the rubric.
Code for this function uses the generic define_getter function in the HTTP submodule.
-
send_rubric (self, rubric)
-
Send a rubric to Canvas.
Note that rubrics are first defined independently from assignments, and later associated with an assignment.
Parameters:
- self
- rubric
table
definition
See also:
-
assoc_rubric (self, args)
-
Associate a rubric to an assignment.
Parameters:
- self
- args
table
arguments
-
rubric_from_csv (csvfile)
-
Read in a carefully structured CSV file and convert it into a rubric.
Parameters:
-
setup_csv_rubrics (self, args)
-
Read a collection of CSV files and create rubrics from them all.
Parameters:
- self
- args
table
arguments
-
delete_all_rubrics ()
-
Delete all rubrics.
This is sometimes needed when you create the same rubric over and over trying to get it right and you end up with many duplicates.