Skip to content

Commit

Permalink
feat: create course section
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Jul 7, 2023
1 parent adb16d3 commit 506a45d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export("%>%")
export(canvas_authenticate)
export(create_assignment_group)
export(create_course_datalake)
export(create_course_section)
export(create_folder)
export(create_group_category)
export(create_page)
Expand Down
40 changes: 40 additions & 0 deletions R/create_course_section.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Create a Course Section in Canvas LMS
#'
#' Creates a new course section 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 section.
#' @param section_name The name of the section.
#' @param section_start_date (Optional) The start date of the section.
#' @param section_end_date (Optional) The end date of the section.
#'
#' @return A confirmation message that the section has been created.
#' @export
#'
create_course_section <- function(canvas, course_id, section_name, section_start_date = NULL, section_end_date = NULL) {
# Construct the API endpoint URL
url <- paste0(canvas$base_url, "/api/v1/courses/", course_id, "/sections")

# Create the request payload
payload <- list(
"course_section" = list(
"name" = section_name,
"start_at" = section_start_date,
"end_at" = section_end_date
)
)

# 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 course section. Please check your authentication and API endpoint.")
}

# Return a confirmation message
return("The course section has been created.")
}
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@
- [x] List course sections SectionsController#index ;
GET /api/v1/courses/:course_id/sections

- [ ] Create course section SectionsController#create ;
- [x] Create course section SectionsController#create ;
POST /api/v1/courses/:course_id/sections

- [ ] Cross-list a Section SectionsController#crosslist ;
Expand Down
31 changes: 31 additions & 0 deletions man/create_course_section.Rd

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

0 comments on commit 506a45d

Please sign in to comment.