File indexing completed on 2024-04-21 07:37:43

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "conjugationbackendmode.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include <KEduVocDocument>
0011 
0012 #include "conjugationdata.h"
0013 #include "documentsettings.h"
0014 
0015 using namespace Practice;
0016 
0017 ConjugationBackendMode::ConjugationBackendMode(AbstractFrontend *frontend, QObject *parent, Practice::SessionManagerBase *sessionManager, KEduVocDocument *doc)
0018     : AbstractBackendMode(frontend, parent)
0019     , m_sessionManager(sessionManager)
0020     , m_doc(doc)
0021 {
0022 }
0023 
0024 bool ConjugationBackendMode::setTestEntry(TestEntry *current)
0025 {
0026     ConjugationData data;
0027     m_current = current;
0028     m_lastAnswers.clear();
0029 
0030     m_currentTense = m_current->conjugationTense();
0031 
0032     if (!m_current->entry()->translation(m_current->languageTo())->conjugationTenses().contains(m_currentTense)) {
0033         qDebug() << "invalid tense for entry - " << m_currentTense;
0034         qDebug() << "tenses: " << m_current->entry()->translation(m_current->languageTo())->conjugationTenses();
0035     }
0036 
0037     data.tense = m_currentTense;
0038     m_conjugation = m_current->entry()->translation(m_current->languageTo())->getConjugation(m_currentTense);
0039     m_pronounFlags = current->conjugationPronouns();
0040 
0041     data.questionInfinitive = m_current->entry()->translation(m_current->languageFrom())->text();
0042     data.solutionInfinitive = m_current->entry()->translation(m_current->languageTo())->text();
0043 
0044     data.personalPronouns = validPersonalPronouns();
0045 
0046     m_frontend->setQuestion(QVariant::fromValue<ConjugationData>(data));
0047     QStringList answers;
0048     for (const KEduVocWordFlags &key : qAsConst(m_pronounFlags)) {
0049         answers.append(m_conjugation.conjugation(key).text());
0050     }
0051     m_frontend->setSolution(answers);
0052 
0053     m_frontend->setQuestionSound(m_current->entry()->translation(m_current->languageFrom())->soundUrl());
0054     m_frontend->setSolutionSound(m_current->entry()->translation(m_current->languageTo())->soundUrl());
0055     m_frontend->setQuestionPronunciation(m_current->entry()->translation(m_current->languageFrom())->pronunciation());
0056     m_frontend->setSolutionPronunciation(m_current->entry()->translation(m_current->languageTo())->pronunciation());
0057     m_frontend->setResultState(AbstractFrontend::QuestionState);
0058     m_frontend->showQuestion();
0059     return true;
0060 }
0061 
0062 QStringList ConjugationBackendMode::validPersonalPronouns()
0063 {
0064     QStringList pp;
0065     for (const KEduVocWordFlags &person : qAsConst(m_pronounFlags)) {
0066         // FIXME: Used to be m_practiceOptions.languageTo()
0067         pp.append(m_doc->identifier(Prefs::learningLanguage()).personalPronouns().personalPronoun(person));
0068     }
0069     qDebug() << "PP: " << pp.size() << pp;
0070     return pp;
0071 }
0072 
0073 void ConjugationBackendMode::checkAnswer()
0074 {
0075     QStringList answers = m_frontend->userInput().toStringList();
0076 
0077     bool allCorrect = true;
0078     int numRight = 0;
0079     int i = 0;
0080     for (const KEduVocWordFlags &key : qAsConst(m_pronounFlags)) {
0081         if (answers.at(i) == m_conjugation.conjugation(key).text()) {
0082             ++numRight;
0083         } else {
0084             qDebug() << "dec grade for " << m_conjugation.conjugation(key).text();
0085             allCorrect = false;
0086         }
0087         ++i;
0088     }
0089 
0090     qDebug() << "answers: " << answers;
0091 
0092     if (allCorrect) {
0093         m_frontend->setFeedback(i18n("All conjugation forms were right."));
0094         Q_EMIT answerRight();
0095     } else {
0096         m_frontend->setFeedback(i18ncp("You did not get the conjugation forms right.",
0097                                        "You answered %1 conjugation form correctly.",
0098                                        "You answered %1 conjugation forms correctly.",
0099                                        numRight));
0100 
0101         if (answers == m_lastAnswers) {
0102             Q_EMIT answerWrongShowSolution();
0103         } else {
0104             Q_EMIT answerWrongRetry();
0105         }
0106         m_lastAnswers = answers;
0107     }
0108 }
0109 
0110 grade_t ConjugationBackendMode::currentPreGradeForEntry() const
0111 {
0112     grade_t min_preGrade = KV_MAX_GRADE;
0113     for (KEduVocWordFlags key : qAsConst(m_pronounFlags)) {
0114         min_preGrade = qMin(m_conjugation.conjugation(key).preGrade(), min_preGrade);
0115     }
0116 
0117     return min_preGrade;
0118 }
0119 
0120 grade_t ConjugationBackendMode::currentGradeForEntry() const
0121 {
0122     grade_t min_grade = KV_MAX_GRADE;
0123     for (KEduVocWordFlags key : qAsConst(m_pronounFlags)) {
0124         min_grade = qMin(m_conjugation.conjugation(key).grade(), min_grade);
0125     }
0126 
0127     return min_grade;
0128 }
0129 
0130 void ConjugationBackendMode::updateGrades()
0131 {
0132     qDebug() << "Grading conjugations";
0133 
0134     for (const KEduVocWordFlags &key : qAsConst(m_pronounFlags)) {
0135         KEduVocTranslation *translation = m_current->entry()->translation(m_current->languageTo());
0136         if (translation) {
0137             KEduVocConjugation conjugationToUpdate = translation->getConjugation(m_currentTense);
0138             conjugationToUpdate.conjugation(key).incPracticeCount();
0139             conjugationToUpdate.conjugation(key).setPracticeDate(QDateTime::currentDateTime());
0140 
0141             updateGrade(conjugationToUpdate.conjugation(key),
0142                         m_frontend->resultState() == AbstractFrontend::AnswerCorrect,
0143                         m_current->statisticBadCount() == 0);
0144 
0145             translation->setConjugation(m_currentTense, conjugationToUpdate);
0146         }
0147     }
0148 }
0149 
0150 void ConjugationBackendMode::hintAction()
0151 {
0152     // FIXME
0153 }
0154 
0155 #include "moc_conjugationbackendmode.cpp"