R/import_questions.R

Defines functions Import_Groups Delete_Questions Import_Questions

Documented in Delete_Questions Import_Groups Import_Questions

#' Allows you to import multiple copies of a certain question template as per the instructions on a table with the columns: sid, gid, title, template
#'
#'
#' @param Import_Table A data.table with the relevant information in the columns listed above
#' @param Folder_Location_Templates Where are the template files saved? Just the name of the folder as a string
#' @export
#' @examples \dontrun{
#' Import_Questions(Import_Table, Folder_Location_Templates = "Templates")
#' }
#'
Import_Questions <- function(Import_Table, Folder_Location_Templates = "Templates"){

  get_session_key()  # Log in

  if(is.na(Folder_Location_Templates)){
    Template_Top_Folder <- ""
  }
  else{
    Template_Top_Folder <- paste0(Folder_Location_Templates, "/")
  }


  results <- list()

  for(i in 1:nrow(Import_Table)){

    template <- read_file(paste0(Template_Top_Folder, Import_Table[i]$template))
    template <- str_replace(template, pattern = "PLACEHOLDER", replacement = Import_Table[i]$title)
    template <- base64_enc(template)

    results[[paste0(i)]] <-  call_limer(method = "import_question",
                                              params = list(iSurveyID = Import_Table[i]$sid,
                                                            iGroupID = Import_Table[i]$gid,
                                                            sImportData = template,
                                                            sImportDataType = "lsq"

                                              ))

  }# end for loop

return(results)

} # end function








#' Batch Question Deletion
#'
#' Allows you to delete a batch of questions, as listed in a table with at least the column qid
#' @param Deletion_Table A data.table with the relevant information, or at least a qid column that lists the questions to be deleted
#' @export
#' @examples \dontrun{
#' Delete_Questions(Question_Table)
#' }
#'
Delete_Questions <- function(Question_Table){

  get_session_key()  # Log in

  results <- list()

  for(i in Question_Table$qid){

    results[[paste0(i)]] <-  call_limer(method = "delete_question",
                                        params = list(iQuestionID = i
                                        ))

  }# end for loop

  return(results)



}






#' Allows you to import a given group (stored in the Templates folder) to multiple surveys. Necessary columns for the input: sid, new_group_name, template
#'
#'
#' @param Import_Table A data.table with the relevant information in the columns listed above
#' @param Folder_Location_Templates Where are the template files saved? Just the name of the folder as a string
#' @export
#' @examples \dontrun{
#' Import_Groups(Import_Table, Folder_Location_Templates = "Templates")
#' }
#'
Import_Groups <- function(Import_Table, Folder_Location_Templates = "Templates"){

  Import_Table <- setDT(Import_Table)

  get_session_key()  # Log in

  if(is.na(Folder_Location_Templates)){
    Template_Top_Folder <- ""
  }
  else{
    Template_Top_Folder <- paste0(Folder_Location_Templates, "/")
  }

  Import_Table[, new_group_id := character()]

  results <- list()

  for(i in 1:nrow(Import_Table)){

    template <- read_file(paste0(Template_Top_Folder, Import_Table[i]$template))
    template <- base64_enc(template)
    data_type <- substr(Import_Table[i]$template, nchar(Import_Table[i]$template)-2, nchar(Import_Table[i]$template))

    Import_Table[i]$new_group_id <-  call_limer(method = "import_group",
                                        params = list(iSurveyID = Import_Table[i]$sid,
                                                      sNewGroupName = Import_Table[i]$new_group_name,
                                                      sImportData = template,
                                                      sImportDataType = data_type
                                        ))

  }# end for loop

  return(Import_Table)

} # end function
bpresentati/surveyR documentation built on March 19, 2022, 3:40 a.m.