daranzolin / rcanvas

R Client for Canvas LMS API
Other
90 stars 43 forks source link

View discussion thread comments #45

Open coatless opened 4 years ago

coatless commented 4 years ago

Any interest in a contributed function that queries discussion threads to obtain all responses and they're author, e.g. get the full topic?

remove_html_tags <- function(x) {
  gsub("<.*?>", "", x)
}

get_discussion_thread = function (discussion_id, object_id, object_type = "courses")  
{
  stopifnot(object_type %in% c("courses", "groups"))
  url <- paste0(rcanvas:::canvas_url(), paste(object_type, object_id, 
                                    "discussion_topics", discussion_id, "view", sep = "/"))
  args <- list(per_page = 100)
  include <- rcanvas:::iter_args_list(NULL, "include[]")
  args <- c(args, include)

  # Workaround for process_request issue.
  resp <- rcanvas:::canvas_query(url, args, "GET")
  resp = rcanvas:::paginate(resp) %>% purrr::map(httr::content, "text") %>% 
    purrr::map(jsonlite::fromJSON, flatten = TRUE)

  # Merge two responses together
  msg_data = dplyr::inner_join(resp[[1]]$view, resp[[1]]$participants, by = c("user_id" = "id"))

  # Remove html formatting for text
  msg_data$message = remove_html_tags(msg_data$message)

  # Release discussion thread information
  msg_data
}