R/add_class_outline.R

#' add class outline into a sqlite database
#' @author lgm
#' @param md
#' @return importing
#' @export
#' @examples
#' #md = "microEn-2016.md"
#' # add_class_outline(md)

add_class_outline <- function(md){


   subject <- stringr::str_extract(md,"^\\w+")

   library(magrittr)

	# This connection creates an empty database if it does not exist
	db <- RSQLite::dbConnect(RSQLite::SQLite(), dbname = "/Users/gabe/liguomin.teaching/Testbanks/testbankmd.sqlite")
	RSQLite::initExtension(db)

	# please uncomment below line if no testbankmd.sqlite
#	RSQLite::dbSendQuery(conn = db, "CREATE TABLE classOutline(subject text,week text,teachingDate text,lessonsNo text,content text,teachingMode text,teachingTools text,exerciseAssign text,execution text, notation text)")

	mddir = "/Users/gabe/liguomin.teaching/syllabus/outlines/"
	md_file <- paste0(mddir,md)
	cont <- readChar(md_file,nchars=1e6)

 # define the common variables
	teachingMode <- "课堂讲授"
	teachingTools <- "多媒体演示"
  execution  <- "正常"
  notation <- ""

	# 将每周的内容分开
	cont.vec <- stringr::str_extract_all(cont,'## (.+\n)+')[[1]]

	insert_items <- function(cont_each_week){
		#cont_each_week <- cont.vec[1]
		#取得每周大纲分项
		week <- stringr::str_extract(cont_each_week,'week [0-9]+') %>%
			stringr::str_replace("week ","")

		teachingDate <- stringr::str_extract(cont_each_week,'时间.+') %>%
			stringr::str_replace("时间","") %>%
			stringr::str_trim()

		lessonNo <- stringr::str_extract(cont_each_week,'课时数.+') %>%
			stringr::str_replace("课时数","") %>%
			stringr::str_trim()

		content <- stringr::str_extract(cont_each_week,'讲授内容\n(.+\n)+- 作业安排') %>%
			stringr::str_replace("- 作业安排","") %>%
			stringr::str_trim()

		exerciseAssign <- stringr::str_extract(cont_each_week,'作业安排\n.+') %>%
			stringr::str_replace("作业安排\n","") %>%
			stringr::str_trim()

		# insert them into the squlite
    val <- paste0("'",subject,"','",week,"','",teachingDate,"','",lessonNo,"','",content,"','",teachingMode,"','",teachingTools,"','",exerciseAssign,"','",execution,"','",notation,"'")

    RSQLite::dbSendQuery(conn = db, paste('insert or replace into classOutline(subject,week,teachingDate,lessonsNo,content,teachingMode,teachingTools,exerciseAssign,execution,notation) values(',val,")",sep = ""))
		}

## apply to all content vectors
	lapply(cont.vec, insert_items)


}
Gabegit/gmExam documentation built on May 6, 2019, 5:32 p.m.