91 lines
2.9 KiB
JavaScript
91 lines
2.9 KiB
JavaScript
const configFrontEnd = require("../config/general");
|
|
|
|
import { saveLocaly } from "./clientstorage.js";
|
|
import { isEmpty, replaceAll } from "../../../tools/main";
|
|
|
|
const txt = require("../../../lang/"+configFrontEnd.lang+"/answer");
|
|
|
|
// Enregistrement côté client du dernier résultat à un quiz en attendant d'être connecté
|
|
export const saveAnswer = (answer) =>
|
|
{
|
|
if(!isEmpty(answer.duration) && !isEmpty(answer.nbCorrectAnswers) && !isEmpty(answer.nbQuestions) && !isEmpty(answer.QuestionnaireId))
|
|
{
|
|
saveLocaly("lastAnswer", answer);
|
|
return true;
|
|
}
|
|
else
|
|
return false;
|
|
}
|
|
|
|
// Retourne le texte suivant le nombre de bonnes réponses
|
|
export const checkAnswerOuput = (answer) =>
|
|
{
|
|
if(!isEmpty(answer.duration) && !isEmpty(answer.nbCorrectAnswers) && !isEmpty(answer.nbQuestions))
|
|
{
|
|
const ratio=answer.nbCorrectAnswers/answer.nbQuestions;
|
|
const mapObj=
|
|
{
|
|
DURATION: answer.duration,
|
|
NBCORRECTANSWERS: answer.nbCorrectAnswers,
|
|
NBQUESTIONS: answer.nbQuestions
|
|
}
|
|
let output="";
|
|
if(ratio < 0.4)
|
|
output=replaceAll(txt.checkResponsesOuputFail, mapObj);
|
|
else if(ratio < 0.8)
|
|
output=replaceAll(txt.checkResponsesOuputMedium, mapObj);
|
|
else
|
|
output=replaceAll(txt.checkResponsesOuputSuccess, mapObj);
|
|
if(output)
|
|
return output;
|
|
else
|
|
return "";
|
|
}
|
|
else
|
|
return "";
|
|
}
|
|
|
|
/*
|
|
export const checkSession = async (config) =>
|
|
{
|
|
return new Promise((resolve, reject) =>
|
|
{
|
|
if(isEmpty(localStorage.getItem("user")))
|
|
resolve(false);
|
|
else
|
|
{
|
|
const user=JSON.parse(localStorage.getItem("user"));
|
|
if(user.duration===undefined || user.duration < Date.now())
|
|
{
|
|
localStorage.removeItem("user");
|
|
resolve(false);
|
|
}
|
|
else
|
|
{
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("GET", configFrontEnd.apiUrl+config.userRoutes+config.checkLoginRoute+user.token);
|
|
xhr.onload = () =>
|
|
{
|
|
let response=JSON.parse(xhr.responseText);
|
|
if (xhr.status === 200 && response.isValid && response.id != undefined)
|
|
{
|
|
if(response.id===user.id)
|
|
resolve(true);
|
|
else
|
|
{
|
|
localStorage.removeItem("user");
|
|
resolve(false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
localStorage.removeItem("user");
|
|
resolve(false);
|
|
}
|
|
}
|
|
xhr.onerror = () => reject(xhr.statusText);
|
|
xhr.send();
|
|
}
|
|
}
|
|
});
|
|
}*/ |