Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another attempt: allow for creating modules and items within modules #11

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export(create_course_datalake)
export(create_course_section)
export(create_folder)
export(create_group_category)
export(create_module)
export(create_module_item)
export(create_page)
export(delete_course_section)
export(download_course_file)
Expand Down
37 changes: 37 additions & 0 deletions R/create_module.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Create a Module in Canvas LMS
#'
#' Creates a new module in a specific course using the Canvas LMS API.
#'
#' @param canvas A list containing the 'api_key' and 'base_url' for authentication.
#' @param course_id The ID of the course in which to create the module.
#' @param module_name The title of the module.
#' @param position (Optional) The position of this module in the course
#'
#' @return A confirmation message that the module has been created.
#' @export
#'
create_module <- function(canvas, course_id, module_name, position = NULL){
# Construct the API endpoint URL
url <- paste0(canvas$base_url, "/api/v1/courses/", course_id, "/modules")

# Create the request payload
payload <- list(
"module" = list(
"name" = module_name,
"position" = position
)
)

# Make the API request
response <- httr::POST(url,
httr::add_headers(Authorization = paste("Bearer", canvas$api_key)),
body = payload,
encode = "json")

if (httr::status_code(response) != 200) {
stop("Failed to create module. Please check your authentication and API endpoint.")
}

# Return a confirmation message
return("The module has been created.")
}
53 changes: 53 additions & 0 deletions R/create_module_item.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#' Create a Module Item in Canvas LMS
#'
#' Creates a new item in a specific module in a specifc course using the Canvas LMS API.
#'
#' @param canvas A list containing the 'api_key' and 'base_url' for authentication.
#' @param course_id The ID of the course in which to create the module item.
#' @param module_id The ID of the module in which to create the item.
#' @param item_title The title of the new item within the module.
#' @param item_type The type of item. Defaults to "Page".
#' @param position (Optional) The position of the item within the module. Defaults to 1.
#' @param page_url (Optional) The url of the page.
#' @param page_id (Optional) The id of the page.
#'
#' @return A confirmation message that the page has been created.
#' @export
#'
create_module_item <- function(canvas, course_id, module_id, item_title, item_type = "Page",
position = NULL, page_url = NULL, page_id = NULL){

# If the item is a page and its url is not given get the url of the page through its id
if(item_type == "Page" & is.null(page_url)){
url <- paste0(canvas$base_url, "/api/v1/courses/", course_id, "/pages/", page_id)
page <- httr::GET(url, httr::add_headers(Authorization = paste("Bearer", canvas$api_key)))
page_url <- httr::content(page)$url
}

# Construct the API endpoint URL
url <- paste0(canvas$base_url, "/api/v1/courses/", course_id, "/modules/", module_id, "/items/")

# Create the request payload
payload <- list(
"module_item" = list(
"title" = item_title,
"type" = item_type,
"position" = position,
"page_url" = page_url
)
)

# Make the API request
response <- httr::POST(url,
httr::add_headers(Authorization = paste("Bearer", canvas$api_key)),
body = payload,
encode = "json")

# Check the response status code
if (httr::status_code(response) != 200) {
stop("Failed to create module item. Please check your authentication and API endpoint.")
}

# Return a confirmation message
return("The module item has been created.")
}
23 changes: 23 additions & 0 deletions man/create_module.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions man/create_module_item.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading