1+ # ' Record results on a centralized database
2+ # '
3+ # ' @param tutorial_id The identifier of the tutorial.
4+ # ' @param tutorial_version The version of the tutorial.
5+ # ' @param user_id The user identifier for this learnr process.
6+ # ' @param event The event that triggers the record, like `exercise_submission`
7+ # ' or `question_submission`
8+ # ' @param data A JSON field with event-dependent data content.
9+ # '
10+ # ' @description Record tutorial submissions in a centralized database. The
11+ # ' function is used by learnr tutorials and is not for end-users.
12+ # '
13+ # ' @return Nothing. The function is used for its side-effects.
14+ # ' @export
15+ # ' @seealso [run()]
16+ # ' @keywords utilities
17+ # ' @concept record events from the BioDataScience package
18+ record_sdd <- function (tutorial_id , tutorial_version , user_id , event , data ) {
19+ bds_file <- " ~/.local/share/R/learnr/biodatascience"
20+ add_file_base64 <- function (entry , file ) {
21+ str <- gsub(" \n " , " " , base64_enc(serialize(entry , NULL )))
22+ dir.create(dirname(file ), showWarnings = FALSE , recursive = TRUE )
23+ cat(str , " \n " , file = file , append = TRUE )
24+ }
25+ user_name <- suppressWarnings(system(" git config user.name" ,
26+ intern = TRUE , ignore.stderr = TRUE ))
27+ user_email <- suppressWarnings(system(" git config user.email" ,
28+ intern = TRUE , ignore.stderr = TRUE ))
29+ # user_full_id <- paste(user_id, user_name, user_email, sep = "/")
30+ date <- Sys.time()
31+ label <- data $ label
32+ if (is.null(label )) label <- " "
33+ data $ label <- NULL
34+ correct <- data $ correct
35+ if (is.null(correct )) {
36+ correct <- data $ feedback $ correct
37+ if (is.null(correct ))
38+ correct <- " "
39+ }
40+ data $ correct <- NULL
41+ entry <- data.frame (date = date , tutorial = tutorial_id ,
42+ version = tutorial_version , user = user_id , user_name = user_name ,
43+ user_email = user_email , label = label , correct = correct , event = event ,
44+ data = list_to_json(data ))
45+ if (correct == " " ) {
46+ add_file_base64(entry , file = bds_file )
47+ return ()
48+ }
49+ # Once http request with stitch will be available, we could do something like
50+ # https://stitch.mongodb.com/api/client/v2.0/app/sdd-relay-aizkd/service/sdd-http/incoming_webhook/webhook0
51+ m <- try(mongo(" ex01" ,
52+ # url = "mongodb://sdd:sdd@ds125388.mlab.com:25388/sdd-test")$insert(entry)
53+ url = " mongodb://sdd:sdd@sdd-umons-shard-00-00-umnnw.mongodb.net:27017,sdd-umons-shard-00-01-umnnw.mongodb.net:27017,sdd-umons-shard-00-02-umnnw.mongodb.net:27017/test?ssl=true&replicaSet=sdd-umons-shard-0&authSource=admin" ),
54+ silent = TRUE )
55+ if (! inherits(m , " try-error" ) &&
56+ m $ run(command = " {\" ping\" : 1}" , simplify = TRUE )$ ok == 1 ) {
57+ m $ insert(entry )
58+ # If there is something in the biodatascience file, inject it also now
59+ if (file.exists(bds_file )) {
60+ dat <- readLines(bds_file )
61+ unlink(bds_file )
62+ if (length(dat ))
63+ for (i in 1 : length(dat ))
64+ m $ insert(unserialize(base64_dec(dat [i ])))
65+ }
66+ m $ disconnect()
67+ } else {# MongoDB database not available... save locally
68+ add_file_base64(entry , file = bds_file )
69+ }
70+ }
71+ # Use: options(tutorial.event_recorder = record_sdd)
0 commit comments