/*
Copyright (c) 2007 Matt Williams matt@milliams.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License or (at
your option) version 3 or, at the discretion of KDE e.V. (which shall
act as a proxy as in section 14 of the GPLv3), any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "kqtichoiceinteraction.h"

#include "kqtisimplechoice.h"
#include "kqtiflowstatic.h"

#include <kdebug.h>

KQtiChoiceInteraction::KQtiChoiceInteraction(const QString& responseIdentifier, int maxChoices, bool shuffle) : mMaxChoices(maxChoices), mShuffle(shuffle)
{
    mResponseIdentifier = responseIdentifier;
}

KQtiChoiceInteraction::KQtiChoiceInteraction(const QDomElement& element) : KQtiBlockInteraction(element)
{
    mMaxChoices = element.attribute("maxChoices").toInt();
    mMinChoices = element.attribute("minChoices", "0").toInt();
    mShuffle = element.attribute("shuffle") == "true" ? true : false;
    
    for(int i=0; i<element.elementsByTagName("simpleChoice").size(); i++)
    {
        QDomNode simpleChoice = element.elementsByTagName("simpleChoice").item(i);
        addChoice(new KQtiSimpleChoice(simpleChoice.toElement()));
    }
}

KQtiChoiceInteraction::~KQtiChoiceInteraction()
{
}

void KQtiChoiceInteraction::addChoice(KQtiSimpleChoice* choice)
{
    KQtiSimpleChoice* simpleChoice(choice);
    mChoices.append(simpleChoice);
}

QDomElement KQtiChoiceInteraction::element() const
{
    QDomDocument doc;
    
    QDomElement choiceInteractionElement = doc.createElement("choiceInteraction");
    choiceInteractionElement.setAttribute("responseIdentifier", mResponseIdentifier);
    choiceInteractionElement.setAttribute("maxChoices", mMaxChoices);
    choiceInteractionElement.setAttribute("shuffle", mShuffle ? "true" : "false");
    
    QDomElement promptElement = doc.createElement("prompt");
    QDomText promptElementText = doc.createTextNode(mPrompt);
    promptElement.appendChild(promptElementText);
    choiceInteractionElement.appendChild(promptElement);
    
    KQtiSimpleChoice* choice;
    foreach(choice, mChoices)
    {
        choiceInteractionElement.appendChild(choice->element());
    }
    
    return choiceInteractionElement;
}
