• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
writtenbackendmode.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2009 Frederik Gladhorn <gladhorn@kde.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 
15 #include "writtenbackendmode.h"
16 
17 #include <KLocalizedString>
18 
19 using namespace Practice;
20 
21 WrittenBackendMode::WrittenBackendMode(AbstractFrontend* frontend, QObject* parent,
22  SessionManagerBase* sessionManager, KEduVocDocument* doc)
23  : AbstractBackendMode(frontend, parent)
24  , m_sessionManager(sessionManager)
25  , m_doc(doc)
26 {
27  // FIXME: Used to be m_practiceOptions.languageTo()
28  m_validator = new WrittenPracticeValidator(Prefs::learningLanguage(), doc);
29 }
30 
31 bool WrittenBackendMode::setTestEntry(TestEntry* current)
32 {
33  AbstractBackendMode::setTestEntry(current);
34  m_firstAttempt = true;
35  m_frontend->showQuestion();
36  m_lastAnswer.clear();
37  m_synonyms.clear();
38  m_currentHint.clear();
39  m_validator->setEntry(current);
40  return true;
41 }
42 
43 void WrittenBackendMode::checkAnswer()
44 {
45  kDebug() << "check";
46  QString answer = m_frontend->userInput().toString();
47 
48  // move on, the user has not changed anything or pressed enter with no answer
49  bool answerUnchanged = (answer == m_lastAnswer) || answer.isEmpty();
50  m_lastAnswer = answer;
51 
52  m_validator->validateAnswer(answer);
53 
54  bool isCorrect = m_current->lastErrors() & TestEntry::Correct;
55  bool isSynonym = m_current->lastErrors() & TestEntry::Synonym;
56 
57  QString feedbackString = getFeedbackString(m_current->lastErrors());
58  m_frontend->setFeedback(feedbackString);
59 
60  m_firstAttempt = m_firstAttempt && isSynonym; // don't count the answer as wrong if you only enter valid synonyms
61 
62  // first handle synonyms as they may be correct or not
63  if (isSynonym) {
64  addSynonym(answer);
65  m_frontend->setSynonym(answer);
66  m_frontend->showSynonym();
67 
68  if (Prefs::countSynonymsAsCorrect()) {
69  // any of the synonyms is automatically counted as right
70  // otherwise do nothing to give the user another chance
71  emit answerRight();
72  }
73  return;
74  }
75 
76  if (isCorrect) {
77  emit answerRight();
78  } else {
79  if (answerUnchanged) {
80  m_frontend->setFeedback(i18n("Your answer was wrong."));
81  emit answerWrongShowSolution();
82  } else {
83  m_current->addUserAnswer(answer);
84  emit answerWrongRetry();
85  }
86  }
87 }
88 
89 QString WrittenBackendMode::getFeedbackString(TestEntry::ErrorTypes error)
90 {
91  // The user entered a synonym
92  if (error & TestEntry::Synonym) {
93  if (!Prefs::countSynonymsAsCorrect()) {
94  return i18n("Your answer was a synonym. Please enter another word with the same translation.");
95  }
96  const QString answer = m_validator->getCorrectedAnswer();
97  if (m_synonyms.contains(answer)) {
98  return i18n("Your answer was an already entered synonym.");
99  } else {
100  if (error & TestEntry::CapitalizationMistake) {
101  return i18n("Your answer was a synonym and your capitalization was wrong.");
102  } else if (error & TestEntry::AccentMistake) {
103  return i18n("Your answer was a synonym and accents were wrong.");
104  } else {
105  return i18n("Your answer was a synonym.");
106  }
107  }
108  }
109 
110  // The answer was wrong
111  if (error & TestEntry::Wrong) {
112  if ((error & TestEntry::CapitalizationMistake) && !Prefs::ignoreCapitalizationMistakes()) {
113  return i18n("Your answer was wrong as capitalization mistakes are not accepted. Please try again.");
114  } else if ((error & TestEntry::AccentMistake) && !Prefs::ignoreAccentMistakes()) {
115  return i18n("Your answer was wrong as accent mistakes are not accepted. Please try again.");
116  } else {
117  return i18n("Your answer was wrong. Please try again.");
118  }
119  }
120 
121  // The answer was right
122  if (m_firstAttempt) {
123  if ((error & TestEntry::CapitalizationMistake)) {
124  return i18n("Your answer was right, but your capitalization was wrong.");
125  } else if ((error & TestEntry::AccentMistake)) {
126  return i18n("Your answer was right, but accents were wrong.");
127  } else {
128  return i18n("Your answer was right.");
129  }
130  } else {
131  if ((error & TestEntry::CapitalizationMistake)) {
132  return i18n("Your answer was right... but not on the first try and your capitalization was wrong.");
133  } else if ((error & TestEntry::AccentMistake)) {
134  return i18n("Your answer was right... but not on the first try and accents were wrong.");
135  } else {
136  return i18n("Your answer was right... but not on the first try.");
137  }
138  }
139 }
140 
141 void WrittenBackendMode::hintAction()
142 {
143  QString solution = m_current->entry()->translation(m_current->languageTo())->text();
144  m_currentHint = solution.left(m_currentHint.size() + 1);
145  if (m_currentHint.size() == solution.size()) {
146  // show solution
147  m_frontend->setFeedback(i18n("You revealed the answer by using too many hints."));
148  m_frontend->setResultState(AbstractFrontend::AnswerWrong);
149  if (m_frontend->userInput().toString() == m_current->entry()->translation(m_current->languageTo())->text()) {
150  m_frontend->setFeedbackState(AbstractFrontend::AnswerCorrect);
151  } else {
152  m_frontend->setFeedbackState(AbstractFrontend::AnswerWrong);
153  }
154  m_frontend->showSolution();
155  } else {
156  m_frontend->setHint(i18n("The solution starts with: %1", m_currentHint));
157  }
158 
159  m_lastAnswer = m_frontend->userInput().toString();
160 }
161 
162 #include "writtenbackendmode.moc"
QList::clear
void clear()
Practice::AbstractFrontend::setResultState
virtual void setResultState(ResultState resultState)=0
The result state indicated whether a word is counted as correct (and grades are raised) and can be ch...
TestEntry::entry
KEduVocExpression * entry() const
Definition: testentry.cpp:149
Practice::AbstractFrontend::setHint
virtual void setHint(const QVariant &hint)=0
Practice::AbstractFrontend::showQuestion
virtual void showQuestion()=0
enter question mode - the user is asked to provide the solution
Practice::AbstractBackendMode::addSynonym
void addSynonym(const QString &entry)
add a new synonym to the list of shown/answered synonyms depending on which mode we are in...
Definition: abstractbackendmode.h:41
Practice::AbstractBackendMode::answerRight
void answerRight()
TestEntry::Wrong
< no error, solution was right
Definition: testentry.h:37
QString::size
int size() const
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
TestEntry::Synonym
< a false friend
Definition: testentry.h:32
TestEntry::lastErrors
ErrorTypes lastErrors()
Definition: testentry.cpp:134
Practice::AbstractFrontend::showSynonym
virtual void showSynonym()=0
show the synonyms
QString::clear
void clear()
Practice::WrittenBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
start practicing a new word.
Definition: writtenbackendmode.cpp:31
Practice::WrittenBackendMode::checkAnswer
void checkAnswer()
Check if the current answer is right.
Definition: writtenbackendmode.cpp:43
Practice::AbstractBackendMode::m_frontend
AbstractFrontend * m_frontend
Definition: abstractbackendmode.h:121
TestEntry::AccentMistake
< capitalization error (whAt)
Definition: testentry.h:28
Practice::AbstractBackendMode::m_current
TestEntry * m_current
Definition: abstractbackendmode.h:122
TestEntry::Correct
< the part that was entered is right, but not complete
Definition: testentry.h:36
Practice::AbstractFrontend::AnswerWrong
Definition: abstractfrontend.h:45
Practice::AbstractFrontend::userInput
virtual QVariant userInput()=0
Enables access to the input of the user.
QObject
Practice::AbstractBackendMode::m_synonyms
QStringList m_synonyms
Definition: abstractbackendmode.h:123
QString::isEmpty
bool isEmpty() const
Practice::AbstractFrontend::setFeedback
virtual void setFeedback(const QVariant &feedback)=0
QString
Practice::AbstractFrontend::setSynonym
virtual void setSynonym(const QString &entry)=0
set a new synonym that should be shown
Practice::AbstractBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
start practicing a new word.
Definition: abstractbackendmode.cpp:28
Practice::WrittenBackendMode::hintAction
virtual void hintAction()
Definition: writtenbackendmode.cpp:141
Practice::AbstractFrontend::AnswerCorrect
Definition: abstractfrontend.h:43
writtenbackendmode.h
Prefs::countSynonymsAsCorrect
static bool countSynonymsAsCorrect()
Get When the synonym instead of the word was entered, does it count as correct?
Definition: prefs.h:678
Practice::AbstractBackendMode::answerWrongShowSolution
void answerWrongShowSolution()
Practice::WrittenPracticeValidator
Definition: writtenpracticevalidator.h:28
Prefs::ignoreAccentMistakes
static bool ignoreAccentMistakes()
Get Count answers as right when only the accentuation is wrong.
Definition: prefs.h:507
Practice::AbstractFrontend::showSolution
virtual void showSolution()=0
enter show solution mode - the solution is shown
Practice::AbstractBackendMode
Definition: abstractbackendmode.h:26
Practice::WrittenPracticeValidator::validateAnswer
void validateAnswer(const QString &answer)
Definition: writtenpracticevalidator.cpp:94
Practice::WrittenBackendMode::WrittenBackendMode
WrittenBackendMode(Practice::AbstractFrontend *frontend, QObject *parent, Practice::SessionManagerBase *sessionManager, KEduVocDocument *doc)
Definition: writtenbackendmode.cpp:21
Practice::AbstractFrontend::setFeedbackState
virtual void setFeedbackState(ResultState feedbackState)=0
The feedback state tells the user if the currently entered word is correct (independent of whether th...
Practice::AbstractFrontend
Definition: abstractfrontend.h:26
QString::left
QString left(int n) const
TestEntry::addUserAnswer
void addUserAnswer(const QString &answer)
Definition: testentry.h:82
TestEntry::CapitalizationMistake
< misspelled
Definition: testentry.h:27
Practice::AbstractBackendMode::answerWrongRetry
void answerWrongRetry()
Prefs::learningLanguage
static int learningLanguage()
Get The language that you are learning.
Definition: prefs.h:1248
Practice::WrittenPracticeValidator::getCorrectedAnswer
QString getCorrectedAnswer()
Definition: writtenpracticevalidator.cpp:132
Prefs::ignoreCapitalizationMistakes
static bool ignoreCapitalizationMistakes()
Get Count answers as right when only the capitalization is wrong.
Definition: prefs.h:526
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::SessionManagerBase
Definition: sessionmanagerbase.h:40
QVariant::toString
QString toString() const
Practice::WrittenPracticeValidator::setEntry
void setEntry(TestEntry *entry)
Definition: writtenpracticevalidator.cpp:56
TestEntry
Definition: testentry.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal