File indexing completed on 2024-04-21 03:51:06

0001 /*
0002     SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "multiplechoicebackendmode.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include <QRandomGenerator>
0011 
0012 #include "multiplechoicedata.h"
0013 
0014 using namespace Practice;
0015 
0016 MultipleChoiceBackendMode::MultipleChoiceBackendMode(AbstractFrontend *frontend, QObject *parent, Practice::SessionManagerBase *sessionManager)
0017     : AbstractBackendMode(frontend, parent)
0018     , m_sessionManager(sessionManager)
0019 {
0020     m_numberOfChoices = Prefs::numberMultipleChoiceAnswers();
0021 }
0022 
0023 bool MultipleChoiceBackendMode::setTestEntry(TestEntry *current)
0024 {
0025     m_current = current;
0026     m_hints.clear();
0027     m_choices.clear();
0028     m_question.clear();
0029 
0030     prepareChoices(current);
0031     populateFrontEnd();
0032     return true;
0033 }
0034 
0035 void MultipleChoiceBackendMode::prepareChoices(TestEntry *current)
0036 {
0037     Q_UNUSED(current)
0038     setQuestion(m_current->entry()->translation(m_current->languageFrom())->text());
0039 
0040     QStringList choices = m_sessionManager->multipleChoiceAnswers(m_numberOfChoices - 1);
0041     for (const QString &choice : qAsConst(choices)) {
0042         int position = QRandomGenerator::global()->bounded(m_choices.count() + 1);
0043         m_choices.insert(position, choice);
0044     }
0045     int correctAnswer = QRandomGenerator::global()->bounded(m_choices.count() + 1);
0046     m_choices.insert(correctAnswer, m_current->entry()->translation(m_current->languageTo())->text());
0047     setCorrectAnswer(correctAnswer);
0048 }
0049 
0050 void MultipleChoiceBackendMode::populateFrontEnd()
0051 {
0052     MultipleChoiceData data;
0053     data.question = m_question;
0054     data.choices = m_choices;
0055 
0056     m_frontend->setQuestion(QVariant::fromValue<MultipleChoiceData>(data));
0057     m_frontend->setSolution(m_correctAnswer);
0058     m_frontend->setQuestionSound(m_current->entry()->translation(m_current->languageFrom())->soundUrl());
0059     m_frontend->setSolutionSound(m_current->entry()->translation(m_current->languageTo())->soundUrl());
0060     m_frontend->setQuestionPronunciation(m_current->entry()->translation(m_current->languageFrom())->pronunciation());
0061     m_frontend->setSolutionPronunciation(m_current->entry()->translation(m_current->languageTo())->pronunciation());
0062     m_frontend->setResultState(AbstractFrontend::QuestionState);
0063     m_frontend->showQuestion();
0064 }
0065 
0066 void MultipleChoiceBackendMode::setQuestion(const QString &question)
0067 {
0068     m_question = question;
0069 }
0070 
0071 int MultipleChoiceBackendMode::numberOfChoices()
0072 {
0073     return m_numberOfChoices;
0074 }
0075 
0076 void MultipleChoiceBackendMode::setChoices(const QStringList &choices)
0077 {
0078     m_choices = choices;
0079 }
0080 
0081 void MultipleChoiceBackendMode::setCorrectAnswer(int index)
0082 {
0083     qDebug() << "correct: " << index << m_choices.at(index);
0084     m_correctAnswer = index;
0085 }
0086 
0087 void MultipleChoiceBackendMode::checkAnswer()
0088 {
0089     if (!m_frontend->userInput().isNull() && m_frontend->userInput().toInt() == m_correctAnswer) {
0090         Q_EMIT answerRight();
0091     } else {
0092         if (!m_frontend->userInput().isNull()) {
0093             m_current->addUserAnswer(m_choices.at(m_frontend->userInput().toInt()));
0094         }
0095         Q_EMIT answerWrongShowSolution();
0096     }
0097 }
0098 
0099 void MultipleChoiceBackendMode::hintAction()
0100 {
0101     if (m_choices.count() - m_hints.count() <= 2) {
0102         // show solution
0103         m_frontend->setFeedback(i18n("You revealed the answer by using too many hints."));
0104         Q_EMIT answerWrongShowSolution();
0105         return;
0106     }
0107 
0108     int hint = -1;
0109     do {
0110         hint = QRandomGenerator::global()->bounded(m_choices.count());
0111     } while (hint == m_correctAnswer || m_hints.contains(hint));
0112     m_hints.append(hint);
0113     m_frontend->setHint(QVariant(hint));
0114 }
0115 
0116 #include "moc_multiplechoicebackendmode.cpp"