#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.