6
6
# ' @param event The event that triggers the record, like `exercise_submission`
7
7
# ' or `question_submission`
8
8
# ' @param data A JSON field with event-dependent data content.
9
+ # ' @param value The new value for user name or email (if not provided, the
10
+ # ' current value is returned).
9
11
# '
10
12
# ' @description Record tutorial submissions in a centralized database. The
11
13
# ' function is used by learnr tutorials and is not for end-users.
@@ -22,12 +24,6 @@ record_sdd <- function(tutorial_id, tutorial_version, user_id, event, data) {
22
24
dir.create(dirname(file ), showWarnings = FALSE , recursive = TRUE )
23
25
cat(str , " \n " , file = file , append = TRUE )
24
26
}
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
27
label <- data $ label
32
28
if (is.null(label )) label <- " "
33
29
data $ label <- NULL
@@ -38,9 +34,9 @@ record_sdd <- function(tutorial_id, tutorial_version, user_id, event, data) {
38
34
correct <- " "
39
35
}
40
36
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 ,
37
+ entry <- data.frame (date = Sys.time() , tutorial = tutorial_id ,
38
+ version = tutorial_version , user = user_id , user_name = user_name() ,
39
+ user_email = user_email() , label = label , correct = correct , event = event ,
44
40
data = list_to_json(data ))
45
41
# Not a good idea: if user never clicks "Submit", nothing is fed to database
46
42
# if (correct == "") {
@@ -86,3 +82,51 @@ collect_sdd <- function() {
86
82
mdb $ find()
87
83
}
88
84
# sdd_data <- collect_sdd(); View(sdd_data)
85
+
86
+ # ' @export
87
+ # ' @rdname record_sdd
88
+ user_name <- function (value ) {
89
+ if (missing(value )) {
90
+ Sys.unsetenv(" SDD_USER" )
91
+ user <- Sys.getenv(" SDD_USER" , unset = " " )
92
+ if (user == " " ) {
93
+ user <- try(suppressWarnings(system(" git config --global user.name" ,
94
+ intern = TRUE , ignore.stderr = TRUE )), silent = TRUE )
95
+ if (inherits(user , " try-error" )) user <- " "
96
+ }
97
+ user
98
+ } else {# Change user
99
+ # Make sure new_user is correct
100
+ new_user <- as.character(value )[1 ]
101
+ new_user <- gsub(" " , " _" , new_user )
102
+ Sys.setenv(SDD_USER = new_user )
103
+ cmd <- paste0(" git config --global user.name '" , new_user , " '" )
104
+ try(suppressWarnings(system(cmd , intern = TRUE , ignore.stderr = TRUE )),
105
+ silent = TRUE )
106
+ new_user
107
+ }
108
+ }
109
+
110
+ # ' @export
111
+ # ' @rdname record_sdd
112
+ user_email <- function (value ) {
113
+ if (missing(value )) {
114
+ Sys.unsetenv(" SDD_EMAIL" )
115
+ email <- Sys.getenv(" SDD_EMAIL" , unset = " " )
116
+ if (email == " " ) {
117
+ email <- try(suppressWarnings(system(" git config --global user.email" ,
118
+ intern = TRUE , ignore.stderr = TRUE )), silent = TRUE )
119
+ if (inherits(email , " try-error" )) email <- " "
120
+ }
121
+ email
122
+ } else {# Change email
123
+ # Make sure new_email is correct
124
+ new_email <- as.character(value )[1 ]
125
+ new_email <- gsub(" " , " _" , new_email )
126
+ Sys.setenv(SDD_EMAIL = new_email )
127
+ cmd <- paste0(" git config --global user.email '" , new_email , " '" )
128
+ try(suppressWarnings(system(cmd , intern = TRUE , ignore.stderr = TRUE )),
129
+ silent = TRUE )
130
+ new_email
131
+ }
132
+ }
0 commit comments