3719 |
stevensc |
1 |
import React, { useState } from 'react';
|
|
|
2 |
|
|
|
3 |
import { startQuiz } from '@microlearning/services';
|
|
|
4 |
import { addNotification } from '@app/redux/notification/notification.actions';
|
|
|
5 |
|
|
|
6 |
import Widget from '@app/components/UI/Widget';
|
|
|
7 |
import QuizSimpleQuestion from './quiz-simple-question';
|
|
|
8 |
import QuizMultipleQuestion from './quiz-multiple-question';
|
|
|
9 |
import SlideProgress from './slide-progress';
|
|
|
10 |
import SlideSuccessFeedback from './slide-success-feedback';
|
|
|
11 |
import SlideQuizStart from './slide-quiz-start';
|
|
|
12 |
import QuizFailureFeedback from './quiz-failure-feedback';
|
|
|
13 |
|
|
|
14 |
export function SlideQuiz({ quiz, slide, onSync, startUrl, completed }) {
|
|
|
15 |
const [currentQuestion, setCurrentQuestion] = useState(0);
|
|
|
16 |
const [showQuestions, setShowQuestions] = useState(false);
|
|
|
17 |
const [completedQuiz, setCompletedQuiz] = useState(false);
|
|
|
18 |
const [failureQuiz, setFailureQuiz] = useState(false);
|
|
|
19 |
const [totalPoints, setTotalPoints] = useState(0);
|
|
|
20 |
const [answers, setAnswers] = useState([]);
|
|
|
21 |
|
|
|
22 |
console.log(answers);
|
|
|
23 |
|
|
|
24 |
const { name, questions, minimum_points_required } = quiz;
|
|
|
25 |
|
|
|
26 |
const handleStart = async () => {
|
|
|
27 |
try {
|
|
|
28 |
const start = await startQuiz(startUrl);
|
|
|
29 |
if (start) setShowQuestions(true);
|
|
|
30 |
} catch (error) {
|
|
|
31 |
addNotification({ style: 'danger', msg: error.message });
|
|
|
32 |
}
|
|
|
33 |
};
|
|
|
34 |
|
|
|
35 |
const onError = () => {
|
|
|
36 |
setFailureQuiz(true);
|
|
|
37 |
};
|
|
|
38 |
|
|
|
39 |
const restartQuiz = () => {
|
|
|
40 |
setCurrentQuestion(0);
|
|
|
41 |
setTotalPoints(0);
|
|
|
42 |
setFailureQuiz(false);
|
|
|
43 |
setCompletedQuiz(false);
|
|
|
44 |
setShowQuestions(false);
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
const onSimpleConfirm = (question, answerId) => {
|
|
|
48 |
const answer = question.answers.find((q) => q.uuid === answerId);
|
|
|
49 |
const newAnswer = {
|
|
|
50 |
question: question.uuid,
|
|
|
51 |
answers: [answer]
|
|
|
52 |
};
|
|
|
53 |
setAnswers((prevAnswers) => [...prevAnswers, newAnswer]);
|
|
|
54 |
|
|
|
55 |
const isCorrect = answer.correct === 'y';
|
|
|
56 |
isCorrect && setTotalPoints(totalPoints + parseInt(question.points));
|
|
|
57 |
|
|
|
58 |
if (currentQuestion !== questions.length - 1) {
|
|
|
59 |
setCurrentQuestion(currentQuestion + 1);
|
|
|
60 |
return;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if (totalPoints >= parseInt(minimum_points_required)) {
|
|
|
64 |
setCompletedQuiz(true);
|
|
|
65 |
} else {
|
|
|
66 |
onError();
|
|
|
67 |
}
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
const onMultipleConfirm = (question, answersIds) => {
|
|
|
71 |
const answers = question.answers.filter((answer) => answersIds.includes(answer.uuid));
|
|
|
72 |
const newAnswer = {
|
|
|
73 |
question: question.uuid,
|
|
|
74 |
answers
|
|
|
75 |
};
|
|
|
76 |
setAnswers((prevAnswers) => [...prevAnswers, newAnswer]);
|
|
|
77 |
|
|
|
78 |
const points = answers.reduce((acc, answer) => acc + parseInt(answer.points), 0);
|
|
|
79 |
|
|
|
80 |
setTotalPoints(totalPoints + points);
|
|
|
81 |
|
|
|
82 |
if (currentQuestion !== questions.length - 1) {
|
|
|
83 |
setCurrentQuestion(currentQuestion + 1);
|
|
|
84 |
return;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if (totalPoints >= parseInt(minimum_points_required)) {
|
|
|
88 |
setCompletedQuiz(true);
|
|
|
89 |
} else {
|
|
|
90 |
onError();
|
|
|
91 |
}
|
|
|
92 |
};
|
|
|
93 |
|
|
|
94 |
if (completedQuiz) {
|
|
|
95 |
return <SlideSuccessFeedback onConfirm={onSync} />;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (failureQuiz) {
|
|
|
99 |
return <QuizFailureFeedback onConfirm={restartQuiz} />;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if (!showQuestions) {
|
|
|
103 |
return <SlideQuizStart onStart={handleStart} completed={completed} slide={slide} />;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return (
|
|
|
107 |
<Widget>
|
|
|
108 |
<SlideProgress
|
|
|
109 |
points={totalPoints}
|
|
|
110 |
time={quiz.max_time}
|
|
|
111 |
currentQuestion={currentQuestion + 1}
|
|
|
112 |
totalQuestions={questions.length}
|
|
|
113 |
onTimeEnd={onError}
|
|
|
114 |
/>
|
|
|
115 |
|
|
|
116 |
<Widget.Header title={`Cuestionario sobre: ${name}`} />
|
|
|
117 |
|
|
|
118 |
<Widget.Body>
|
|
|
119 |
{questions.map((question, index) => {
|
|
|
120 |
const isLastQuestion = index === questions.length - 1;
|
|
|
121 |
const buttonLabel = isLastQuestion ? 'Finalizar' : 'Siguiente';
|
|
|
122 |
|
|
|
123 |
if (question.type === 's') {
|
|
|
124 |
return (
|
|
|
125 |
<QuizSimpleQuestion
|
|
|
126 |
key={question.uuid}
|
|
|
127 |
question={question}
|
|
|
128 |
onConfirm={(answer) => onSimpleConfirm(question, answer)}
|
|
|
129 |
buttonLabel={buttonLabel}
|
|
|
130 |
isCurrent={currentQuestion === index}
|
|
|
131 |
/>
|
|
|
132 |
);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
if (question.type === 'm') {
|
|
|
136 |
return (
|
|
|
137 |
<QuizMultipleQuestion
|
|
|
138 |
key={question.uuid}
|
|
|
139 |
question={question}
|
|
|
140 |
onConfirm={(answers) => onMultipleConfirm(question, answers)}
|
|
|
141 |
buttonLabel={buttonLabel}
|
|
|
142 |
isCurrent={currentQuestion === index}
|
|
|
143 |
/>
|
|
|
144 |
);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
return null;
|
|
|
148 |
})}
|
|
|
149 |
</Widget.Body>
|
|
|
150 |
</Widget>
|
|
|
151 |
);
|
|
|
152 |
}
|