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