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

parley

  • sources
  • kde-4.12
  • 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(const PracticeOptions& practiceOptions, AbstractFrontend* frontend, QObject* parent, Practice::TestEntryManager* testEntryManager)
23 : AbstractBackendMode(practiceOptions, frontend, parent)
24 ,m_testEntryManager(testEntryManager)
25 ,m_randomSequence(QDateTime::currentDateTime().toTime_t())
26 {
27  m_numberOfChoices = practiceOptions.numberMultipleChoiceAnswers();
28 }
29 
30 bool MultipleChoiceBackendMode::setTestEntry(TestEntry* current)
31 {
32  m_current = current;
33  m_hints.clear();
34  m_choices.clear();
35  m_question.clear();
36 
37  prepareChoices(current);
38  MultipleChoiceData data;
39  data.question = m_question;
40  data.choices = m_choices;
41 
42  m_frontend->setQuestion(qVariantFromValue<MultipleChoiceData>(data));
43  m_frontend->setSolution(m_correctAnswer);
44  m_frontend->setQuestionSound(m_current->entry()->translation(m_practiceOptions.languageFrom())->soundUrl());
45  m_frontend->setSolutionSound(m_current->entry()->translation(m_practiceOptions.languageTo())->soundUrl());
46  m_frontend->setQuestionPronunciation(m_current->entry()->translation(m_practiceOptions.languageFrom())->pronunciation());
47  m_frontend->setSolutionPronunciation(m_current->entry()->translation(m_practiceOptions.languageTo())->pronunciation());
48  m_frontend->setResultState(AbstractFrontend::QuestionState);
49  m_frontend->showQuestion();
50  return true;
51 }
52 
53 void MultipleChoiceBackendMode::prepareChoices(TestEntry* current)
54 {
55  Q_UNUSED(current)
56  setQuestion(m_current->entry()->translation(m_practiceOptions.languageFrom())->text());
57 
58  QStringList choices = m_testEntryManager->multipleChoiceAnswers(m_numberOfChoices-1);
59  foreach(const QString& choice, choices) {
60  int position = m_randomSequence.getLong(m_choices.count()+1);
61  m_choices.insert(position, choice);
62  }
63  int correctAnswer = m_randomSequence.getLong(m_choices.count()+1);
64  m_choices.insert(correctAnswer, m_current->entry()->translation(m_practiceOptions.languageTo())->text());
65  setCorrectAnswer(correctAnswer);
66 }
67 
68 void MultipleChoiceBackendMode::setQuestion(const QString& question)
69 {
70  m_question = question;
71 }
72 
73 int MultipleChoiceBackendMode::numberOfChoices()
74 {
75  return m_numberOfChoices;
76 }
77 
78 void MultipleChoiceBackendMode::setChoices(const QStringList& choices)
79 {
80  m_choices = choices;
81 }
82 
83 void MultipleChoiceBackendMode::setCorrectAnswer(int index)
84 {
85  kDebug() << "correct: " << index << m_choices.at(index);
86  m_correctAnswer = index;
87 }
88 
89 void MultipleChoiceBackendMode::checkAnswer()
90 {
91  if (!m_frontend->userInput().isNull() && m_frontend->userInput().toInt() == m_correctAnswer) {
92  emit answerRight();
93  } else {
94  if(!m_frontend->userInput().isNull()) {
95  m_current->addUserAnswer(m_choices.at(m_frontend->userInput().toInt()));
96  }
97  emit answerWrongShowSolution();
98  }
99 }
100 
101 void MultipleChoiceBackendMode::hintAction()
102 {
103  if (m_choices.count() - m_hints.count() <= 2) {
104  // show solution
105  m_frontend->setFeedback(i18n("You revealed the answer by using too many hints."));
106  emit answerWrongShowSolution();
107  return;
108  }
109 
110  KRandomSequence randomSequence;
111  int hint = -1;
112  do {
113  hint = randomSequence.getLong(m_choices.count());
114  } while(hint == m_correctAnswer || m_hints.contains(hint));
115  m_hints.append(hint);
116  m_frontend->setHint(QVariant(hint));
117 }
118 
119 #include "multiplechoicebackendmode.moc"
Practice::MultipleChoiceBackendMode::setCorrectAnswer
void setCorrectAnswer(int index)
The correct solution, index of the choices.
Definition: multiplechoicebackendmode.cpp:83
Practice::PracticeOptions::numberMultipleChoiceAnswers
int numberMultipleChoiceAnswers() const
Definition: practiceoptions.cpp:37
Practice::PracticeOptions
Definition: practiceoptions.h:22
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...
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:78
Practice::MultipleChoiceData::question
QString question
Definition: multiplechoicedata.h:25
Practice::PracticeOptions::languageFrom
int languageFrom() const
Definition: practiceoptions.h:27
Practice::MultipleChoiceBackendMode::hintAction
virtual void hintAction()
Definition: multiplechoicebackendmode.cpp:101
Practice::AbstractFrontend::QuestionState
Definition: abstractfrontend.h:40
Practice::TestEntryManager
Definition: testentrymanager.h:31
Practice::AbstractFrontend::setSolution
virtual void setSolution(const QVariant &solution)=0
QObject
Practice::MultipleChoiceData
Definition: multiplechoicedata.h:24
multiplechoicebackendmode.h
multiplechoicedata.h
Practice::MultipleChoiceBackendMode::numberOfChoices
int numberOfChoices()
Definition: multiplechoicebackendmode.cpp:73
Practice::MultipleChoiceBackendMode::MultipleChoiceBackendMode
MultipleChoiceBackendMode(const PracticeOptions &PracticeOptions, AbstractFrontend *frontend, QObject *parent, Practice::TestEntryManager *testEntryManager)
Definition: multiplechoicebackendmode.cpp:22
Practice::MultipleChoiceData::choices
QStringList choices
Definition: multiplechoicedata.h:26
Practice::AbstractBackendMode::m_frontend
AbstractFrontend * m_frontend
Definition: abstractbackendmode.h:83
Practice::AbstractBackendMode::m_current
TestEntry * m_current
Definition: abstractbackendmode.h:84
Practice::AbstractFrontend::userInput
virtual QVariant userInput()=0
Enables access to the input of the user.
Practice::AbstractFrontend::setQuestionPronunciation
virtual void setQuestionPronunciation(const QString &pronunciationText)=0
Practice::TestEntryManager::multipleChoiceAnswers
QStringList multipleChoiceAnswers(int numberChoices)
Definition: testentrymanager.cpp:216
Practice::AbstractFrontend::setFeedback
virtual void setFeedback(const QVariant &feedback)=0
Practice::PracticeOptions::languageTo
int languageTo() const
Definition: practiceoptions.h:28
Practice::MultipleChoiceBackendMode::checkAnswer
virtual void checkAnswer()
Check if the current answer is right.
Definition: multiplechoicebackendmode.cpp:89
Practice::AbstractBackendMode::answerWrongShowSolution
void answerWrongShowSolution()
Practice::AbstractFrontend::setSolutionSound
virtual void setSolutionSound(const KUrl &soundUrl)=0
Practice::AbstractBackendMode
Definition: abstractbackendmode.h:26
Practice::AbstractFrontend
Definition: abstractfrontend.h:25
Practice::MultipleChoiceBackendMode::setTestEntry
virtual bool setTestEntry(TestEntry *current)
start practicing a new word.
Definition: multiplechoicebackendmode.cpp:30
TestEntry::addUserAnswer
void addUserAnswer(const QString &answer)
Definition: testentry.h:81
Practice::AbstractBackendMode::m_practiceOptions
PracticeOptions m_practiceOptions
Definition: abstractbackendmode.h:82
Practice::AbstractFrontend::setQuestionSound
virtual void setQuestionSound(const KUrl &soundUrl)=0
Practice::MultipleChoiceBackendMode::setQuestion
void setQuestion(const QString &question)
Set the question/original language.
Definition: multiplechoicebackendmode.cpp:68
Practice::AbstractFrontend::setSolutionPronunciation
virtual void setSolutionPronunciation(const QString &pronunciationText)=0
TestEntry::entry
KEduVocExpression * entry()
Definition: testentry.cpp:134
Practice::MultipleChoiceBackendMode::prepareChoices
virtual void prepareChoices(TestEntry *current)
set the list of possible answers.
Definition: multiplechoicebackendmode.cpp:53
TestEntry
Definition: testentry.h:22
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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