-
Notifications
You must be signed in to change notification settings - Fork 2
/
diagnose_login.R
310 lines (278 loc) · 13.1 KB
/
diagnose_login.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#' Insert code in an R Markdown document to diagnose personal data information
#'
#' This function is used, inside an R Markdown document, to dynamically
#' elaborate a short report on the client-side (by using JavaScript code) that
#' displays the login status of the user (login, contextual data, ...).
#'
#' @param block Should the diagnostic be included in a block construct
#' (not if `NULL`)?
#' @param lang The language to use for diagnostic output (only `'en'` or `'fr'`
#' currently)
#'
#' @return Nothing, the function is used for its side-effect of adding
#' diagnostic code in the document
#' @export
#'
#' @details
#' Wherever you want to display this diagnostic report in your bookdown page,
#' add on its own line the following instruction:
#' 'r learnitdown::diagnose_login()' between backquotes (you can also change
#' `lang`, if you want).
diagnose_login <- function(block = "info", lang = "en") {
switch(lang,
en = .diagnose_login_en(block = block),
fr = .diagnose_login_fr(block = block),
stop("This language is not supported yet"))
}
.diagnose_login_en <- function(block = "info") {
if (is.null(block)) {
start <- ""
end <- ""
} else {
start <- paste0("
::: {.", block, "}")
end <- ":::
"
}
paste0(start, "
::: {#diagnostic-en}
*Please wait, diagnostic in course...*
<script>
function clearStorageEn() {
if (window.localStorage) {
localStorage.clear();
}
var msg = 'Local personal data deleted!' + '\\nThe page will be reloaded...' + '\\n\\n(if this is an error, close the page and relaunch it from within Moodle).';
if (window.self !== window.parent) {
parent.alert(msg);
} else {
window.alert(msg);
}
window.location.href=window.location.href;
}
function diagnoseSystemEn() {
var container = document.getElementById('diagnostic-en');
if (container === null) {
return;
}
var content = '';
if (!window.localStorage) {
content = content + \"<p>You are:</p>\\n<ul>\";
content = content + \"\\n<li>Anonymous on this site (local storage unavailable).</li>\";
content = content + \"\\n<li>The content <b>is not</b> contextualized for a particular course.</li>\";
content = content + \"\\n<li>Results for various exercises <b>will not be recorded</b>.</li>\";
content = content + \"\\n</ul>\";
content = content + \"\\n<p>If you are <i>not</i> a student that takes a course linking with this pedagogical material, this is OK, you can continue your journey... <b>Welcome!</b></p>\";
content = content + \"\\n<p>On the other hand, if your cursus requires you to be properly recorded here, then close this page and restart it from your institution elearning platform (Moodle, ...). If your data are still incorrect, contact your teachers.</p>\";
} else {
// Explore the content of the storage
// Students are recognized by their login (equivalent to their GitHub login)
// but we also show their iemail (institutional email, or Wordpress email)
var login = localStorage.getItem('login');
if (login === null) {
login = '';
}
var email = localStorage.getItem('email');
if (email === null) {
email = '';
}
var iemail = localStorage.getItem('iemail');
if (iemail === null) {
iemail = '';
}
// Compare both emails, if they are different, display both of them
if (email != iemail && iemail != '') {
email = email + '</u> (institutional email: <u>' + iemail + ')';
}
// Construct an identification string
content = content + \"<p><b>Content on these pages is contextual. </b>Please, check the following data are correct:</p>\\n<ul>\";
var registered = false;
if (login == '') {
// Not registered
content = content + \"\\n<li><u>You are anonymous on this site.</u> <b>Your progress in the exercises will not be recorded.</b></li>\";
} else {
// Registered user
registered = true;
if (login != '') {
content = content + \"\\n<li>Login : <u>\" + login + \"</u></li>\";
}
if (email != '') {
content = content + \"\\n<li>Email : <u>\" + email + \"</u></li>\";
}
content = content + \"\\n<b>Your progress in the exercises will be recorded under that identity.</b>\";
}
// institution and icourse possibly change the content. So, must be checked!
var context = false;
var institution = localStorage.getItem('institution');
if (institution == null || institution == '') {
institution = 'undefined';
} else {
context = true;
}
var icourse = localStorage.getItem('icourse');
if (icourse == null || icourse == '') {
icourse = 'undefined';
} else {
context = true;
var ictitle = localStorage.getItem('ictitle');
if (ictitle != null && ictitle != '') {
icourse = ictitle + ' (' + icourse + ')';
}
}
content = content + \"\\n<li>Course : <u>\" + icourse + \"</u></li>\";
content = content + \"\\n<li>Institution : <u>\" + institution + \"</u></li>\";
content = content + \"\\n</ul>\";
if (context) {
content = content + \"<p><b>Content will be adapted to this particular context. </b>Check that these data are correct, or close this page and reopen it from your institutional elearning platform (Moodle, ...). If your data are still incorrect, contact your teachers.</p>\";
} else {// No context
content = content + \"<p><b>Without a particular course context, you will see a general content. </b>If this is OK... Welcome! Otherwise close this page and restart it from your institutional elearning platform (Moodle, ...). If your data are still incorrect, contact your teachers.</p>\";
}
// If registered user and/or special context, offer to reset it
if (registered || context) {
content = content + \"\\n<p>If you prefer to visit this site anonymously and record no activity in the exercises, you can delete your personal informations by clicking on the button bellow.</p>\";
content = content + '<button onclick=\"clearStorageEn()\">Delete my personal data</button>';
}
// Those variables are possibly defined, but they are not tested here (yet)
//var displayname = localStorage.getItem('displayname');
//var firstname = localStorage.getItem('firstname');
//var lastname = localStorage.getItem('lastname');
//var ifirstname = localStorage.getItem('ifirstname');
//var ilastname = localStorage.getItem('ilastname');
//var iid = localStorage.getItem('iid');
//var iurl = localStorage.getItem('iurl');
//var iref = localStorage.getItem('iref');
}
container.innerHTML = content;
}
diagnoseSystemEn();
</script>
<noscript><b>The system diagnostic cannot be run because Javascript is not enabled, but it is mandatory for thei site! All the interactive and contextual content will be unusable. Please, do activate Javascript...</b></noscript>
:::
", end)
}
.diagnose_login_fr <- function(block = "info") {
if (is.null(block)) {
start <- ""
end <- ""
} else {
start <- paste0("
::: {.", block, "}")
end <- ":::
"
}
paste0(start, "
::: {#diagnostic-fr}
*Patientez s'il-vous-plait, diagnostic en cours...*
<script>
function clearStorageFr() {
if (window.localStorage) {
localStorage.clear();
}
var msg = 'Informations personnelles locales effacees !' + '\\nLa page va etre rechargee...' + \"\\n\\n(s'il s'agit d'une fausse manoeuvre, fermer la page et relancez-la depuis Moodle).\";
if (window.self !== window.parent) {
parent.alert(msg);
} else {
window.alert(msg);
}
window.location.href=window.location.href;
}
function diagnoseSystemFr() {
var container = document.getElementById('diagnostic-fr');
if (container === null) {
return;
}
var content = '';
if (!window.localStorage) {
content = content + \"<p>Vous êtes :</p>\\n<ul>\";
content = content + \"\\n<li>Anonyme sur ce site (stockage local indisponible).</li>\";
content = content + \"\\n<li>Le contenu <b>n'est pas</b> contextualisé par rapport à un cours en particulier.</li>\";
content = content + \"\\n<li>Le résultat de certains exercices <b>ne sera pas enregistré</b>.</li>\";
content = content + \"\\n</ul>\";
content = content + \"\\n<p>Si vous n'êtes <i>pas</i> un étudiant qui suit un cours lié à ce matériel pédagogique, c'est normal, vous pouvez continuer votre visite... <b>Bienvenue !</b></p>\";
content = content + \"\\n<p>En revanche, si votre cours nécessite que vous soyez dûment enregistré, alors fermez cette page et relancer-là depuis le système d'apprentissage en ligne de votre Université (Moodle, ...). Si les données sont toujours incorrectes, contactez vos enseignants.</p>\";
} else {
// Explore the content of the storage
// Students are recognized by their login (equivalent to their GitHub login)
// but we also show their iemail (institutional email, or Wordpress email)
var login = localStorage.getItem('login');
if (login === null) {
login = '';
}
var email = localStorage.getItem('email');
if (email === null) {
email = '';
}
var iemail = localStorage.getItem('iemail');
if (iemail === null) {
iemail = '';
}
// Compare both emails, if they are different, display both of them
if (email != iemail && iemail != '') {
email = email + '</u> (email institutionnel : <u>' + iemail + ')';
}
// Construct an identification string
content = content + \"<p><b>Le contenu de ce cours est contextuel.</b> Vérifiez les informations suivantes, s'il-vous-plait :</p>\\n<ul>\";
var registered = false;
if (login == '') {
// Not registered
content = content + \"\\n<li><u>Vous êtes anonyme sur ce site.</u> <b>Votre progression dans les exercices ne sera pas enregistrée.</b></li>\";
} else {
// Registered user
registered = true;
if (login != '') {
content = content + \"\\n<li>Login : <u>\" + login + \"</u></li>\";
}
if (email != '') {
content = content + \"\\n<li>Email : <u>\" + email + \"</u></li>\";
}
content = content + \"\\n<b>Votre progression dans les exercices sera enregistrée sous cette identité.</b>\";
}
// institution and icourse possibly change the content. So, must be checked!
var context = false;
var institution = localStorage.getItem('institution');
if (institution == null || institution == '') {
institution = 'indéterminée';
} else {
context = true;
}
var icourse = localStorage.getItem('icourse');
if (icourse == null || icourse == '') {
icourse = 'indéterminé';
} else {
context = true;
var ictitle = localStorage.getItem('ictitle');
if (ictitle != null && ictitle != '') {
icourse = ictitle + ' (' + icourse + ')';
}
}
content = content + \"\\n<li>Cours : <u>\" + icourse + \"</u></li>\";
content = content + \"\\n<li>Institution : <u>\" + institution + \"</u></li>\";
content = content + \"\\n</ul>\";
if (context) {
content = content + \"<p><b>Le contenu sera adapté en fonction de ce contexte.</b> Vérifiez qu'il est correct, sinon fermez cette page et relancer-là depuis le système d'apprentissage en ligne de votre Université (Moodle, ...). Si les données sont toujours incorrectes, contactez vos enseignants.</p>\";
} else {// No context
content = content + \"<p><b>N'étant dans aucun contexte de cours particulier, vous n'aurez accès qu'à un contenu généraliste.</b> Si c'est ce que vous souhaitez... Bienvenue ! Sinon, fermez cette page et relancer-là depuis le système d'apprentissage en ligne de votre Université (Moodle, ...). Si les données sont toujours incorrectes, contactez vos enseignants.</p>\";
}
// If registered user and/or special context, offer to reset it
if (registered || context) {
content = content + \"\\n<p>Pour explorer ces pages de manière anonyme et n'enregistrer aucune activité, vous pouvez éliminez vos informations personnelles en cliquant sur le bouton juste ci-dessous.</p>\";
content = content + '<button onclick=\"clearStorageFr()\">Effacer mes données personnelles</button>';
}
// Those variables are possibly defined, but they are not tested here (yet)
//var displayname = localStorage.getItem('displayname');
//var firstname = localStorage.getItem('firstname');
//var lastname = localStorage.getItem('lastname');
//var ifirstname = localStorage.getItem('ifirstname');
//var ilastname = localStorage.getItem('ilastname');
//var iid = localStorage.getItem('iid');
//var iurl = localStorage.getItem('iurl');
//var iref = localStorage.getItem('iref');
}
container.innerHTML = content;
}
diagnoseSystemFr();
</script>
<noscript><b>Le diagnostic ne peut se terminer car Javascript ne fonctionne pas, or il est indispensable pour ce site ! Tout le contenu interactif et contextuel sera inutilisable. Veuillez activer Javascript, s'il-vous-plait...</b></noscript>
:::
", end)
}