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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "abstractbackendmode.h"
0007 #include "prefs.h"
0008 #include <QDebug>
0009 
0010 using namespace Practice;
0011 
0012 AbstractBackendMode::AbstractBackendMode(Practice::AbstractFrontend *frontend, QObject *parent)
0013     : QObject(parent)
0014     , m_frontend(frontend)
0015 {
0016 }
0017 
0018 bool AbstractBackendMode::setTestEntry(TestEntry *current)
0019 {
0020     m_current = current;
0021     // this default implementation sets up the frontend with the normal word
0022     KEduVocTranslation *translation = m_current->entry()->translation(m_current->languageFrom());
0023     if (!translation->text().isEmpty()) {
0024         m_frontend->setQuestion(translation->text());
0025     } else if (!translation->imageUrl().isEmpty()) {
0026         m_frontend->setQuestion(translation->imageUrl());
0027     }
0028     m_frontend->setSolution(m_current->entry()->translation(m_current->languageTo())->text());
0029     if (Prefs::practiceSoundEnabled() == true) {
0030         m_frontend->setQuestionSound(m_current->entry()->translation(m_current->languageFrom())->soundUrl());
0031         m_frontend->setSolutionSound(m_current->entry()->translation(m_current->languageTo())->soundUrl());
0032     }
0033     m_frontend->setQuestionPronunciation(m_current->entry()->translation(m_current->languageFrom())->pronunciation());
0034     m_frontend->setSolutionPronunciation(m_current->entry()->translation(m_current->languageTo())->pronunciation());
0035 
0036     return true;
0037 }
0038 
0039 grade_t Practice::AbstractBackendMode::currentPreGradeForEntry() const
0040 {
0041     return m_current->entry()->translation(m_current->languageTo())->preGrade();
0042 }
0043 
0044 grade_t Practice::AbstractBackendMode::currentGradeForEntry() const
0045 {
0046     return m_current->entry()->translation(m_current->languageTo())->grade();
0047 }
0048 
0049 void Practice::AbstractBackendMode::updateGrades()
0050 {
0051     KEduVocTranslation *translation = m_current->entry()->translation(m_current->languageTo());
0052     // qDebug() << "Update Grades Default Implementation: " << m_frontend->resultState()
0053     //         << " for " << translation->text()
0054     //         << " grade: " << m_current->entry()->translation(m_current->languageTo())->grade();
0055 
0056     translation->incPracticeCount();
0057     translation->setPracticeDate(QDateTime::currentDateTime());
0058 
0059     updateGrade(*translation, m_frontend->resultState() == AbstractFrontend::AnswerCorrect, m_current->statisticBadCount() == 0);
0060 
0061     // qDebug() << "new grade: " << m_current->entry()->translation(m_current->languageTo())->grade();
0062 }
0063 
0064 // ----------------------------------------------------------------
0065 //                         protected methods
0066 
0067 void Practice::AbstractBackendMode::updateGrade(KEduVocText &text, bool isCorrectAnswer, bool hasNoPreviousBadAnswers)
0068 {
0069     if (isCorrectAnswer) {
0070         if (hasNoPreviousBadAnswers) {
0071             if (text.grade() == KV_NORM_GRADE) {
0072                 if (text.preGrade() == KV_NORM_GRADE) {
0073                     // If the word was new and correct answer, then
0074                     // set it to grade, pregrade 2, 0.  This is so
0075                     // that the user doesn't have to go through all
0076                     // the pregrade levels with words that s/he
0077                     // already knows well.
0078                     text.setPreGrade(KV_NORM_GRADE);
0079                     text.setGrade(KV_LEV2_GRADE);
0080                 } else if (text.preGrade() == KV_MAX_GRADE) {
0081                     // If correct answer and last pregrade level, then
0082                     // set new grade, pregrade to 1, 0.
0083                     text.setPreGrade(KV_NORM_GRADE);
0084                     text.setGrade(KV_LEV1_GRADE);
0085                 } else {
0086                     // otherwise just increase the pregrade.
0087                     text.setPreGrade(text.preGrade() + 1); // FIXME: Implement incPreGrade() in the library.
0088                 }
0089             } else {
0090                 // If grade was > 0 then just increase it.
0091                 text.incGrade();
0092             }
0093         }
0094     } else {
0095         // If the answer was wrong, reset the grade, pregrade to 0, 1.
0096         text.setPreGrade(KV_LEV1_GRADE);
0097         text.setGrade(KV_NORM_GRADE);
0098         text.incBadCount();
0099     }
0100 
0101     // qDebug() << "new pregrade, grade: " << text.preGrade() << text.grade();
0102 }
0103 
0104 #include "moc_abstractbackendmode.cpp"