/*
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 "kqtiassessmentitem.h"

#include "kqtichoiceinteraction.h"
#include "kqtiresponsedeclaration.h"
#include "kqtiitembody.h"

#include <kdebug.h>

KQtiAssessmentItem::KQtiAssessmentItem(const QString& identifier, const QString& title, bool timeDependent) : mIdentifier(identifier), mTitle(title), mTimeDependent(timeDependent)/*, mChoiceInteraction(0)*/, mItemBody(0)
{
}

KQtiAssessmentItem::KQtiAssessmentItem(const QDomDocument&  document)
{
    mTitle = document.documentElement().attribute("title");
    mIdentifier = document.documentElement().attribute("identifier");
    mTimeDependent = document.documentElement().attribute("timeDependent") == "true" ? true : false;
    
    for(uint i=0; i<document.elementsByTagName("responseDeclaration").length(); i++)
    {
        QDomElement childElement = document.elementsByTagName("responseDeclaration").item(i).toElement();
        KQtiResponseDeclaration* responseDeclaration = new KQtiResponseDeclaration(childElement);
        addResponseDeclaration(responseDeclaration);
    }
    
    mItemBody = new KQtiItemBody(document.documentElement().firstChildElement("itemBody"));
}

KQtiAssessmentItem::~KQtiAssessmentItem()
{
    delete mItemBody;
}

QDomDocument KQtiAssessmentItem::document() const
{
    QDomDocument doc;
    
    QDomElement assessmentItemElement = doc.createElement("assessmentItem");
    assessmentItemElement.setAttribute("title", mTitle);
    assessmentItemElement.setAttribute("identifier", mIdentifier);
    doc.appendChild(assessmentItemElement);
    
    foreach(KQtiResponseDeclaration* responseDeclaration, mResponseDeclarations)
    {
        assessmentItemElement.appendChild(responseDeclaration->element());
    }
    assessmentItemElement.appendChild(mItemBody->element());
    
    return doc;
}

void KQtiAssessmentItem::addItemBody(KQtiItemBody* itemBody)
{
    mItemBody = itemBody;
}

void KQtiAssessmentItem::addResponseDeclaration(KQtiResponseDeclaration* responseDeclaration)
{
    mResponseDeclarations.append(responseDeclaration);
}
