• 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
sessionmanagerbase.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  copyright : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
3  (C) 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net>
4  (C) 2007-2009 Frederik Gladhorn <gladhorn@kde.org>
5  (C) 2014 Inge Wallin <inge@lysator.liu.se>
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "sessionmanagerbase.h"
18 
19 // Qt
20 #include <QDateTime>
21 
22 // kdelibs
23 #include <klocale.h>
24 #include <kconfig.h>
25 #include <KDebug>
26 #include <KMessageBox>
27 
28 // kdeedulibs
29 #include <keduvoclesson.h>
30 #include <keduvocexpression.h>
31 #include <keduvocdocument.h>
32 
33 // parley
34 #include "entryfilter.h"
35 #include <prefs.h>
36 
37 
38 using namespace Practice;
39 
40 SessionManagerBase::SessionManagerBase(QWidget* parent)
41  : m_parent(parent)
42  , m_learningLanguageIndex(0)
43  , m_knownLanguageIndex(1)
44  , m_currentEntry(-1)
45  , m_totalTime(0)
46  , m_randomSequence(QDateTime::currentDateTime().toTime_t())
47 {
48 }
49 
50 SessionManagerBase::~SessionManagerBase()
51 {
52  qDeleteAll(m_allTestEntries);
53 }
54 
55 void SessionManagerBase::setDocument(KEduVocDocument* doc)
56 {
57  qDeleteAll(m_allTestEntries);
58  m_allTestEntries.clear();
59  m_notAskedTestEntries.clear();
60  m_currentEntries.clear();
61 
62  m_doc = doc;
63 
64  // Make sure that there are at least 2 languages in the file before
65  // starting the practice. If we don't do this, it will crash later.
66  if (m_doc->identifierCount() < 2) {
67  KMessageBox::error(0, i18n("The vocabulary collection contains fewer than two languages.",
68  i18n("Could not start practice")));
69  return;
70  }
71 
72  if (Prefs::learningLanguage() >= m_doc->identifierCount()
73  || Prefs::knownLanguage() >= m_doc->identifierCount())
74  {
75  Prefs::setLearningLanguage(0);
76  Prefs::setKnownLanguage(1);
77  }
78 
79  m_learningLanguageIndex = Prefs::learningLanguage();
80  m_knownLanguageIndex = Prefs::knownLanguage();
81  kDebug() << "Practice: learning language:" << m_doc->identifier(m_learningLanguageIndex).name()
82  << " known language:" << m_doc->identifier(m_knownLanguageIndex).name();
83 
84  // Create the list of available entries for this training session.
85  EntryFilter filter(m_doc, m_parent);
86  m_allTestEntries = filter.entries();
87 
88  kDebug() << "Entries: ----------------";
89  kDebug() << "Found " << m_allTestEntries.count() << " entries after filtering.";
90  foreach (TestEntry *entry, m_allTestEntries) {
91  kDebug() << "Entry: " << entry->languageFrom() << entry->entry()->translation(entry->languageFrom())->text()
92  << "to" << entry->languageTo() << entry->entry()->translation(entry->languageTo())->text();
93  }
94 
95  // Create the list actual entries in this training session. This
96  // is a pure virtual function and must be implemented by the
97  // concrete session managers.
98  initializeTraining();
99 }
100 
101 QString SessionManagerBase::title() const
102 {
103  return m_doc->title();
104 }
105 
106 void SessionManagerBase::practiceStarted()
107 {
108  kDebug() << "start practice timer";
109  m_time.start();
110 }
111 
112 void SessionManagerBase::practiceFinished()
113 {
114  m_totalTime = m_time.elapsed();
115  kDebug() << "stop practice timer" << m_totalTime << m_time.toString();
116 }
117 
118 int SessionManagerBase::totalTime()
119 {
120  // seconds instead of ms
121  return m_totalTime / (1000);
122 }
123 
124 TestEntry* SessionManagerBase::nextTrainingEntry()
125 {
126  // In some session types an entry will move out of the "current
127  // entries" when they are correctly answered and then we need to
128  // refill.
129  while (m_currentEntries.count() < Prefs::testNumberOfEntries() &&
130  m_notAskedTestEntries.count() > 0) {
131  m_currentEntries.append(m_notAskedTestEntries.takeAt(0));
132  }
133 
134  // Return one of the current entries, but not the same as last
135  // time if posible.
136  int lastEntry = m_currentEntry;
137  if (m_currentEntries.count() > 0) {
138  // Choose one of the current entries randomly.
139  m_currentEntry = m_randomSequence.getLong(m_currentEntries.count());
140 
141  // Do not allow to ask the same entry twice in a row.
142  if (m_currentEntries.count() > 1) {
143  while (m_currentEntry == lastEntry) {
144  m_currentEntry = m_randomSequence.getLong(m_currentEntries.count());
145  }
146  }
147 
148  kDebug() << "nextTrainingEntry:"
149  << m_currentEntry << " = " << m_currentEntries.value(m_currentEntry)->entry()->translation(0)->text()
150  << " (" << m_currentEntries.count() + m_notAskedTestEntries.count()
151  << "entries remaining)";
152 
153  return m_currentEntries.value(m_currentEntry);
154  } else {
155  return 0;
156  }
157 }
158 
159 
160 void SessionManagerBase::removeCurrentEntryFromPractice()
161 {
162  if (m_currentEntry >= 0) {
163  m_currentEntries.takeAt(m_currentEntry);
164  }
165 }
166 
167 QList<TestEntry*> SessionManagerBase::allTestEntries() const
168 {
169  return m_allTestEntries;
170 }
171 
172 int SessionManagerBase::allEntryCount() const
173 {
174  return m_allTestEntries.count();
175 }
176 
177 int SessionManagerBase::activeEntryCount()
178 {
179  return m_notAskedTestEntries.count() + m_currentEntries.count();
180 }
181 
182 QList<TestEntry*> SessionManagerBase::allUnansweredTestEntries()
183 {
184  QList<TestEntry*> result;
185 
186  result.append(m_notAskedTestEntries);
187  result.append(m_currentEntries);
188 
189  return result;
190 }
191 
192 // ----------------------------------------------------------------
193 // Statistics
194 
195 
196 int SessionManagerBase::statisticTotalCorrectFirstAttempt()
197 {
198  int count = 0;
199  foreach(TestEntry * entry, m_allTestEntries) {
200  if (entry->correctAtFirstAttempt()) {
201  count++;
202  }
203  }
204  return count;
205 }
206 
207 int SessionManagerBase::statisticTotalWrong()
208 {
209  int count = 0;
210  foreach(TestEntry * entry, m_allTestEntries) {
211  if (entry->statisticBadCount()) {
212  count++;
213  }
214  }
215  return count;
216 }
217 
218 int SessionManagerBase::statisticTotalUnanswered()
219 {
220  int count = 0;
221  foreach(TestEntry * entry, m_allTestEntries) {
222  if (entry->statisticCount() == 0) {
223  count++;
224  }
225  }
226  return count;
227 }
228 
229 void SessionManagerBase::printStatistics()
230 {
231  kDebug() << "Test statistics: ";
232  foreach(TestEntry * entry, m_allTestEntries) {
233  kDebug()
234  << " asked: " << entry->statisticCount()
235  << " +" << entry->statisticGoodCount() << " -" << entry->statisticBadCount()
236  << "Entry: " << entry->entry()->translation(0)->text();
237  }
238 }
239 
240 
241 // ----------------------------------------------------------------
242 // Multiple choice questions
243 
244 
245 QStringList SessionManagerBase::multipleChoiceAnswers(int numberChoices)
246 {
247  QStringList choices;
248  KRandomSequence randomSequence;
249  QList<KEduVocExpression*> allEntries = m_doc->lesson()->entries(KEduVocLesson::Recursive);
250  int numValidEntries = 0;
251  int count = numberChoices;
252 
253  // if the current entry has predefined multiple choice entries definied, use them first
254  TestEntry *currentEntry = m_currentEntries.at(m_currentEntry);
255  QStringList predefinedChoices = currentEntry->entry()->translation(currentEntry->languageTo())->multipleChoice();
256  while (!predefinedChoices.isEmpty() && count > 0) {
257  choices.append(predefinedChoices.takeAt(randomSequence.getLong(predefinedChoices.count())));
258  count--;
259  }
260 
261  // find out if we got enough valid entries to fill all the options
262  for (int i = 0; i < allEntries.count(); ++i) {
263  if (isValidMultipleChoiceAnswer(allEntries.value(i)))
264  numValidEntries++;
265  if (numValidEntries >= numberChoices)
266  break;
267  }
268 
269  // if we don't have enough valid entries, just try everything and use what we can get
270  if (numValidEntries < numberChoices) {
271  for (int i = choices.count(); i < allEntries.count(); ++i) {
272  KEduVocExpression *exp = allEntries.value(i);
273 
274  // FIXME: Use trainingmode2 also here!
275  if (isValidMultipleChoiceAnswer(exp)) {
276  choices.append(exp->translation(currentEntry->languageTo())->text());
277  }
278  }
279  } else {
280  QList<KEduVocExpression*> exprlist;
281  while (count > 0) {
282  int nr;
283  // if there are enough non-empty fields, fill the options only with those
284  do {
285  nr = randomSequence.getLong(allEntries.count());
286  } while (!isValidMultipleChoiceAnswer(allEntries.value(nr)));
287  // append if new entry found
288  bool newex = true;
289  for (int i = 0; newex && i < exprlist.count(); i++) {
290  if (exprlist[i] == allEntries.value(nr))
291  newex = false;
292  }
293  if (newex) {
294  count--;
295  exprlist.append(allEntries.value(nr));
296  }
297  }
298 
299  // FIXME: Use trainingmode2 too here
300  for (int i = 0; i < exprlist.count(); i++) {
301  choices.append(exprlist[i]->translation(currentEntry->languageTo())->text());
302  }
303  }
304 
305  kDebug() << "choices:" << choices << " answerLang = " << currentEntry->languageTo() ;
306  return choices;
307 }
308 
309 
310 // ----------------------------------------------------------------
311 // Protected methods
312 
313 
314 bool SessionManagerBase::isValidMultipleChoiceAnswer(KEduVocExpression *e)
315 {
316  // entry is empty
317  if (e->translation(m_learningLanguageIndex)->text().trimmed().isEmpty())
318  return false;
319 
320  // FIXME: Must test individual solution & question languages per
321  // entry in mixed mode training.
322  //
323  // entry is a synonym of the solution
324  if (e->translation(m_learningLanguageIndex)->synonyms().contains(m_currentEntries.at(m_currentEntry)->entry()->translation(m_learningLanguageIndex)))
325  return false;
326 
327  // Entry has the same text as the solution.
328  if (e->translation(m_learningLanguageIndex)->text().simplified()
329  == m_currentEntries.at(m_currentEntry)->entry()->translation(m_learningLanguageIndex)->text().simplified())
330  return false;
331 
332  return true;
333 }
TestEntry::statisticCount
int statisticCount()
Definition: testentry.cpp:49
QList::clear
void clear()
QWidget
TestEntry::entry
KEduVocExpression * entry() const
Definition: testentry.cpp:149
Practice::SessionManagerBase::nextTrainingEntry
virtual TestEntry * nextTrainingEntry()
Get the next entry to show to the user.
Definition: sessionmanagerbase.cpp:124
Practice::SessionManagerBase::statisticTotalUnanswered
int statisticTotalUnanswered()
Definition: sessionmanagerbase.cpp:218
QTime::toString
QString toString(Qt::DateFormat format) const
Prefs::setKnownLanguage
static void setKnownLanguage(int v)
Set The language that you know already.
Definition: prefs.h:1257
QList::at
const T & at(int i) const
Practice::SessionManagerBase::title
QString title() const
Retun the title of the document.
Definition: sessionmanagerbase.cpp:101
Practice::SessionManagerBase::allUnansweredTestEntries
QList< TestEntry * > allUnansweredTestEntries()
Get a list of all unanswered entries in the test.
Definition: sessionmanagerbase.cpp:182
QList::takeAt
T takeAt(int i)
Practice::SessionManagerBase::printStatistics
void printStatistics()
Puts some grades on the shell.
Definition: sessionmanagerbase.cpp:229
TestEntry::statisticGoodCount
int statisticGoodCount()
Definition: testentry.cpp:59
prefs.h
Practice::SessionManagerBase::SessionManagerBase
SessionManagerBase(QWidget *parent)
Create a collection of entries to be practiced.
Definition: sessionmanagerbase.cpp:40
Practice::SessionManagerBase::~SessionManagerBase
virtual ~SessionManagerBase()
destructor
Definition: sessionmanagerbase.cpp:50
Practice::SessionManagerBase::removeCurrentEntryFromPractice
virtual void removeCurrentEntryFromPractice()
Finish the currently active entry.
Definition: sessionmanagerbase.cpp:160
entryfilter.h
Practice::SessionManagerBase::allTestEntries
QList< TestEntry * > allTestEntries() const
Get a list of all entries in the test - used by the summary dialog.
Definition: sessionmanagerbase.cpp:167
Prefs::testNumberOfEntries
static int testNumberOfEntries()
Get The number of entries that are practiced at the same time.
Definition: prefs.h:374
QList::value
T value(int i) const
Practice::SessionManagerBase::m_time
QTime m_time
Definition: sessionmanagerbase.h:172
QTime::elapsed
int elapsed() const
Practice::SessionManagerBase::m_currentEntries
QList< TestEntry * > m_currentEntries
The list of entries that are being asked.
Definition: sessionmanagerbase.h:167
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
Practice::SessionManagerBase::allEntryCount
int allEntryCount() const
The number of entries available for the practice session.
Definition: sessionmanagerbase.cpp:172
Practice::SessionManagerBase::m_notAskedTestEntries
QList< TestEntry * > m_notAskedTestEntries
All entries that have not been entered into the active set.
Definition: sessionmanagerbase.h:162
Practice::SessionManagerBase::m_parent
QWidget * m_parent
Definition: sessionmanagerbase.h:148
QList::isEmpty
bool isEmpty() const
Practice::SessionManagerBase::m_allTestEntries
QList< TestEntry * > m_allTestEntries
All entries available in the document that fulfill the requirements set in the configuration and the ...
Definition: sessionmanagerbase.h:159
Prefs::setLearningLanguage
static void setLearningLanguage(int v)
Set The language that you are learning.
Definition: prefs.h:1238
Practice::SessionManagerBase::activeEntryCount
int activeEntryCount()
The number of entries that are still to be practiced.
Definition: sessionmanagerbase.cpp:177
Practice::SessionManagerBase::initializeTraining
virtual void initializeTraining()=0
Initialize the lists of entries that will be used in the training from the full set of available entr...
Practice::SessionManagerBase::m_knownLanguageIndex
int m_knownLanguageIndex
Definition: sessionmanagerbase.h:150
QString
QList< TestEntry * >
Practice::SessionManagerBase::practiceFinished
virtual void practiceFinished()
Definition: sessionmanagerbase.cpp:112
Practice::SessionManagerBase::isValidMultipleChoiceAnswer
bool isValidMultipleChoiceAnswer(KEduVocExpression *e)
Find out if the given expression can be used as a multiple choice answer for the current entry (i...
Definition: sessionmanagerbase.cpp:314
QStringList
Practice::SessionManagerBase::EntryFilter
friend class EntryFilter
Definition: sessionmanagerbase.h:177
Practice::SessionManagerBase::setDocument
virtual void setDocument(KEduVocDocument *doc)
Prepare for practice using the entries in this document.
Definition: sessionmanagerbase.cpp:55
Practice::SessionManagerBase::m_randomSequence
KRandomSequence m_randomSequence
Definition: sessionmanagerbase.h:175
Practice::SessionManagerBase::m_currentEntry
int m_currentEntry
Definition: sessionmanagerbase.h:170
TestEntry::statisticBadCount
int statisticBadCount()
Definition: testentry.cpp:54
Practice::SessionManagerBase::totalTime
int totalTime()
the time in seconds
Definition: sessionmanagerbase.cpp:118
Prefs::knownLanguage
static int knownLanguage()
Get The language that you know already.
Definition: prefs.h:1267
TestEntry::languageFrom
int languageFrom() const
Definition: testentry.cpp:109
Practice::SessionManagerBase::m_doc
KEduVocDocument * m_doc
Definition: sessionmanagerbase.h:147
sessionmanagerbase.h
QTime::start
void start()
Prefs::learningLanguage
static int learningLanguage()
Get The language that you are learning.
Definition: prefs.h:1248
Practice::SessionManagerBase::m_learningLanguageIndex
int m_learningLanguageIndex
Definition: sessionmanagerbase.h:149
TestEntry::correctAtFirstAttempt
bool correctAtFirstAttempt()
Definition: testentry.cpp:124
Practice::SessionManagerBase::practiceStarted
virtual void practiceStarted()
Definition: sessionmanagerbase.cpp:106
Practice::SessionManagerBase::statisticTotalCorrectFirstAttempt
int statisticTotalCorrectFirstAttempt()
Definition: sessionmanagerbase.cpp:196
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::SessionManagerBase::multipleChoiceAnswers
QStringList multipleChoiceAnswers(int numberChoices)
Definition: sessionmanagerbase.cpp:245
Practice::SessionManagerBase::m_totalTime
int m_totalTime
Definition: sessionmanagerbase.h:173
QDateTime
Practice::SessionManagerBase::statisticTotalWrong
int statisticTotalWrong()
Definition: sessionmanagerbase.cpp:207
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