Warning, file /education/parley/src/practice/genderbackendmode.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "genderbackendmode.h"
0007 
0008 #include <KLocalizedString>
0009 
0010 #include <KEduVocDocument>
0011 #include <KEduVocWordtype>
0012 
0013 using namespace Practice;
0014 
0015 GenderBackendMode::GenderBackendMode(AbstractFrontend *frontend, QObject *parent, Practice::SessionManagerBase *sessionManager, KEduVocDocument *doc)
0016     : MultipleChoiceBackendMode(frontend, parent, sessionManager)
0017     , m_doc(*doc)
0018 {
0019 }
0020 
0021 bool GenderBackendMode::setTestEntry(TestEntry *current)
0022 {
0023     Practice::AbstractBackendMode::setTestEntry(current);
0024 
0025     m_articles = m_doc.identifier(m_current->languageTo()).article();
0026 
0027     KEduVocWordFlag::Flags singular = KEduVocWordFlag::Singular;
0028     KEduVocWordFlag::Flags definite = KEduVocWordFlag::Definite;
0029     KEduVocWordFlag::Flags indefinite = KEduVocWordFlag::Indefinite;
0030     KEduVocWordFlag::Flags masculine = KEduVocWordFlag::Masculine;
0031     KEduVocWordFlag::Flags feminine = KEduVocWordFlag::Feminine;
0032     KEduVocWordFlag::Flags neuter = KEduVocWordFlag::Neuter;
0033 
0034     m_masculine = m_articles.article(singular | definite | masculine);
0035     if (m_masculine.isEmpty()) {
0036         m_masculine = m_articles.article(singular | indefinite | masculine);
0037     }
0038 
0039     m_feminine = m_articles.article(singular | definite | feminine);
0040     if (m_feminine.isEmpty()) {
0041         m_feminine = m_articles.article(singular | indefinite | feminine);
0042     }
0043 
0044     m_neuter = m_articles.article(singular | definite | neuter);
0045     if (m_neuter.isEmpty()) {
0046         m_neuter = m_articles.article(singular | indefinite | neuter);
0047     }
0048 
0049     // best bet... if it is defined, it must exist, or if none of them is defined
0050     m_neuterExists = (!m_neuter.isEmpty()) || (m_masculine.isEmpty() && m_feminine.isEmpty());
0051 
0052     prepareChoices(current);
0053     populateFrontEnd();
0054 
0055     return true;
0056 }
0057 void GenderBackendMode::prepareChoices(TestEntry *entry)
0058 {
0059     Q_ASSERT(entry->entry()->translation(entry->languageTo())->wordType()->wordType() & KEduVocWordFlag::Noun);
0060 
0061     setQuestion(i18n("Choose the right article for \"%1\"", entry->entry()->translation(entry->languageFrom())->text()));
0062 
0063     // set the word (possibly without the article)
0064     QString noun = entry->entry()->translation(m_current->languageTo())->text();
0065 
0066     // strip the article
0067 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
0068     QStringList qsl = noun.split(QRegExp(QStringLiteral("\\s")), QString::SkipEmptyParts);
0069 #else
0070     QStringList qsl = noun.split(QRegExp(QStringLiteral("\\s")), Qt::SkipEmptyParts);
0071 #endif
0072     QMutableStringListIterator qsli(qsl);
0073     while (qsli.hasNext())
0074         if (m_articles.isArticle(qsli.next()))
0075             qsli.remove();
0076 
0077     noun = qsl.join(QStringLiteral(" "));
0078 
0079     // set the choices
0080     QStringList choices;
0081 
0082     if (!m_masculine.isEmpty()) {
0083         choices.append(m_masculine + ' ' + noun);
0084     } else {
0085         choices.append(i18nc("@label the gender of the word: masculine", "%1 is masculine", noun));
0086     }
0087     if (!m_feminine.isEmpty()) {
0088         choices.append(m_feminine + ' ' + noun);
0089     } else {
0090         choices.append(i18nc("@label the gender of the word: feminine", "%1 is feminine", noun));
0091     }
0092     if (m_neuterExists && !m_neuter.isEmpty()) {
0093         choices.append(m_neuter + ' ' + noun);
0094     } else {
0095         choices.append(i18nc("@label the gender of the word: neuter", "%1 is neuter", noun));
0096     }
0097 
0098     setChoices(choices);
0099 
0100     qDebug() << entry->entry()->translation(entry->languageTo())->wordType()->wordType();
0101     if (entry->entry()->translation(entry->languageTo())->wordType()->wordType() & KEduVocWordFlag::Masculine) {
0102         setCorrectAnswer(0);
0103         qDebug() << "male";
0104     } else if (entry->entry()->translation(entry->languageTo())->wordType()->wordType() & KEduVocWordFlag::Feminine) {
0105         setCorrectAnswer(1);
0106         qDebug() << "female";
0107     } else {
0108         setCorrectAnswer(2);
0109         qDebug() << "neuter";
0110     }
0111 }
0112 
0113 void GenderBackendMode::updateGrades()
0114 {
0115     KEduVocText articleGrade = m_current->entry()->translation(m_current->languageTo())->article();
0116     articleGrade.incPracticeCount();
0117     articleGrade.setPracticeDate(QDateTime::currentDateTime());
0118     updateGrade(articleGrade, m_frontend->resultState() == AbstractFrontend::AnswerCorrect, m_current->statisticBadCount() == 0);
0119 
0120     m_current->entry()->translation(m_current->languageTo())->setArticle(articleGrade);
0121 }
0122 
0123 grade_t GenderBackendMode::currentPreGradeForEntry() const
0124 {
0125     KEduVocTranslation *translation = m_current->entry()->translation(m_current->languageTo());
0126     if (translation) {
0127         return translation->article().preGrade();
0128     }
0129     return KV_NORM_GRADE;
0130 }
0131 
0132 grade_t GenderBackendMode::currentGradeForEntry() const
0133 {
0134     KEduVocTranslation *translation = m_current->entry()->translation(m_current->languageTo());
0135     if (translation) {
0136         return translation->article().grade();
0137     }
0138     return KV_NORM_GRADE;
0139 }