• 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
multiplechoicebackendmode.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 "multiplechoicebackendmode.h"
16 
17 #include <klocale.h>
18 #include "multiplechoicedata.h"
19 
20 using namespace Practice;
21 
22 MultipleChoiceBackendMode::MultipleChoiceBackendMode(AbstractFrontend* frontend, QObject* parent,
23  Practice::SessionManagerBase* sessionManager)
24  : AbstractBackendMode(frontend, parent)
25  , m_sessionManager(sessionManager)
26  , m_randomSequence(QDateTime::currentDateTime().toTime_t())
27 {
28  m_numberOfChoices = Prefs::numberMultipleChoiceAnswers();
29 }
30 
31 bool MultipleChoiceBackendMode::setTestEntry(TestEntry* current)
32 {
33  m_current = current;
34  m_hints.clear();
35  m_choices.clear();
36  m_question.clear();
37 
38  prepareChoices(current);
39  populateFrontEnd();
40  return true;
41 }
42 
43 void MultipleChoiceBackendMode::prepareChoices(TestEntry* current)
44 {
45  Q_UNUSED(current)
46  setQuestion(m_current->entry()->translation(m_current->languageFrom())->text());
47 
48  QStringList choices = m_sessionManager->multipleChoiceAnswers(m_numberOfChoices - 1);
49  foreach(const QString & choice, choices) {
50  int position = m_randomSequence.getLong(m_choices.count() + 1);
51  m_choices.insert(position, choice);
52  }
53  int correctAnswer = m_randomSequence.getLong(m_choices.count() + 1);
54  m_choices.insert(correctAnswer, m_current->entry()->translation(m_current->languageTo())->text());
55  setCorrectAnswer(correctAnswer);
56 }
57 
58 void MultipleChoiceBackendMode::populateFrontEnd()
59 {
60  MultipleChoiceData data;
61  data.question = m_question;
62  data.choices = m_choices;
63 
64  m_frontend->setQuestion(qVariantFromValue<MultipleChoiceData>(data));
65  m_frontend->setSolution(m_correctAnswer);
66  m_frontend->setQuestionSound(m_current->entry()->translation(m_current->languageFrom())->soundUrl());
67  m_frontend->setSolutionSound(m_current->entry()->translation(m_current->languageTo())->soundUrl());
68  m_frontend->setQuestionPronunciation(m_current->entry()->translation(m_current->languageFrom())->pronunciation());
69  m_frontend->setSolutionPronunciation(m_current->entry()->translation(m_current->languageTo())->pronunciation());
70  m_frontend->setResultState(AbstractFrontend::QuestionState);
71  m_frontend->showQuestion();
72 }
73 
74 
75 void MultipleChoiceBackendMode::setQuestion(const QString& question)
76 {
77  m_question = question;
78 }
79 
80 int MultipleChoiceBackendMode::numberOfChoices()
81 {
82  return m_numberOfChoices;
83 }
84 
85 void MultipleChoiceBackendMode::setChoices(const QStringList& choices)
86 {
87  m_choices = choices;
88 }
89 
90 void MultipleChoiceBackendMode::setCorrectAnswer(int index)
91 {
92  kDebug() << "correct: " << index << m_choices.at(index);
93  m_correctAnswer = index;
94 }
95 
96 void MultipleChoiceBackendMode::checkAnswer()
97 {
98  if (!m_frontend->userInput().isNull() && m_frontend->userInput().toInt() == m_correctAnswer) {
99  emit answerRight();
100  } else {
101  if (!m_frontend->userInput().isNull()) {
102  m_current->addUserAnswer(m_choices.at(m_frontend->userInput().toInt()));
103  }
104  emit answerWrongShowSolution();
105  }
106 }
107 
108 void MultipleChoiceBackendMode::hintAction()
109 {
110  if (m_choices.count() - m_hints.count() <= 2) {
111  // show solution
112  m_frontend->setFeedback(i18n("You revealed the answer by using too many hints."));
113  emit answerWrongShowSolution();
114  return;
115  }
116 
117  KRandomSequence randomSequence;
118  int hint = -1;
119  do {
120  hint = randomSequence.getLong(m_choices.count());
121  } while (hint == m_correctAnswer || m_hints.contains(hint));
122  m_hints.append(hint);
123  m_frontend->setHint(QVariant(hint));
124 }
125 
126 #include "multiplechoicebackendmode.moc"
Practice::MultipleChoiceBackendMode::setCorrectAnswer
void setCorrectAnswer(int index)
The correct solution, index of the choices.
Definition: multiplechoicebackendmode.cpp:90
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::setQuestion
virtual void setQuestion(const QVariant &question)=0
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::answerRight
void answerRight()
Practice::MultipleChoiceBackendMode::setChoices
void setChoices(const QStringList &choices)
This must include the correct answer.
Definition: multiplechoicebackendmode.cpp:85
Practice::MultipleChoiceData::question
QString question
Definition: multiplechoicedata.h:26
Practice::MultipleChoiceBackendMode::populateFrontEnd
virtual void populateFrontEnd()
Configure the multiple choice front end.
Definition: multiplechoicebackendmode.cpp:58
QList::at
const T & at(int i) const
Practice::MultipleChoiceBackendMode::hintAction
virtual void hintAction()
Definition: multiplechoicebackendmode.cpp:108
Practice::AbstractFrontend::QuestionState
Definition: abstractfrontend.h:42
Prefs::numberMultipleChoiceAnswers
static int numberMultipleChoiceAnswers()
Get How many answers are provided for a multiple choice question, including the correct answer...
Definition: prefs.h:811
Practice::AbstractFrontend::setSolution
virtual void setSolution(const QVariant &solution)=0
Practice::MultipleChoiceData
Definition: multiplechoicedata.h:25
multiplechoicebackendmode.h
multiplechoicedata.h
Practice::MultipleChoiceBackendMode::numberOfChoices
int numberOfChoices()
Definition: multiplechoicebackendmode.cpp:80
QString::clear
void clear()
Practice::MultipleChoiceBackendMode::MultipleChoiceBackendMode
MultipleChoiceBackendMode(AbstractFrontend *frontend, QObject *parent, Practice::SessionManagerBase *sessionManager)
Definition: multiplechoicebackendmode.cpp:22
Practice::MultipleChoiceData::choices
QStringList choices
Definition: multiplechoicedata.h:27
Practice::AbstractBackendMode::m_frontend
AbstractFrontend * m_frontend
Definition: abstractbackendmode.h:121
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
Practice::AbstractBackendMode::m_current
TestEntry * m_current
Definition: abstractbackendmode.h:122
QVariant::toInt
int toInt(bool *ok) const
QVariant::isNull
bool isNull() const
Practice::AbstractFrontend::userInput
virtual QVariant userInput()=0
Enables access to the input of the user.
QObject
Practice::AbstractFrontend::setQuestionPronunciation
virtual void setQuestionPronunciation(const QString &pronunciationText)=0
Practice::AbstractFrontend::setFeedback
virtual void setFeedback(const QVariant &feedback)=0
QString
QStringList
QList::contains
bool contains(const T &value) const
Practice::MultipleChoiceBackendMode::checkAnswer
virtual void checkAnswer()
Check if the current answer is right.
Definition: multiplechoicebackendmode.cpp:96
Practice::AbstractBackendMode::answerWrongShowSolution
void answerWrongShowSolution()
Practice::AbstractFrontend::setSolutionSound
virtual void setSolutionSound(const KUrl &soundUrl)=0
QList::insert
void insert(int i, const T &value)
TestEntry::languageFrom
int languageFrom() const
Definition: testentry.cpp:109
Practice::AbstractBackendMode
Definition: abstractbackendmode.h:26
Practice::AbstractFrontend
Definition: abstractfrontend.h:26
Practice::MultipleChoiceBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
Start practicing a new word.
Definition: multiplechoicebackendmode.cpp:31
TestEntry::addUserAnswer
void addUserAnswer(const QString &answer)
Definition: testentry.h:82
TestEntry::languageTo
int languageTo() const
Definition: testentry.cpp:114
Practice::SessionManagerBase::multipleChoiceAnswers
QStringList multipleChoiceAnswers(int numberChoices)
Definition: sessionmanagerbase.cpp:245
Practice::AbstractFrontend::setQuestionSound
virtual void setQuestionSound(const KUrl &soundUrl)=0
Practice::SessionManagerBase
Definition: sessionmanagerbase.h:40
Practice::MultipleChoiceBackendMode::setQuestion
void setQuestion(const QString &question)
Set the question/original language.
Definition: multiplechoicebackendmode.cpp:75
Practice::AbstractFrontend::setSolutionPronunciation
virtual void setSolutionPronunciation(const QString &pronunciationText)=0
QDateTime
Practice::MultipleChoiceBackendMode::prepareChoices
virtual void prepareChoices(TestEntry *current)
set the list of possible answers.
Definition: multiplechoicebackendmode.cpp:43
TestEntry
Definition: testentry.h:22
QVariant
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