Break survey

Sep 10, 2007 at 3:35 PM
In multiple page survey, Is that possible to stop the survey at the middle and continue later using sstchur survey control? If yes how can I manage it. How does the control commit data to answer.xml? is that at the end of the survey or at the end of each page?
Coordinator
Sep 12, 2007 at 5:24 PM
Sisayd,

Unfortunately, there is not (currently anyway) any mechanism to allow the use to stop mid-survey and continue later. The data is only committed to the AnswersFile when the user submits at the very end.

-Steve
Nov 11, 2007 at 12:00 PM
I needed the same feature so I extended the control. Here is some code to do this.

Steve: you are free to use this in a future release if you want to. If you are interested: i implemented a few other small extra features to your control: the current logged in user name is stored in the answer file in the AnswerSet. And i've created an ExpectedAnswer feature which is useful for online tests, it is now very easy to retrieve a users score on the test. Let me know if you're interested in this code.

This code uses Profiles to store and load the current answers of a user. This makes it easy to implement and users can continue their survey where they left it at any computer.

Implementation



Add this code to the WebSurvey.cs file (And add using System.Collections.Specialized; to the beginning of the file)

public NameValueCollection GetCurrentAnswers()
{
NameValueCollection antwoorden = new NameValueCollection();

foreach (Control control in this.Controls)
{
Panel panel = (Panel)control;
foreach (Control control2 in panel.Controls)
{
if (control2 is WebQuestion)
{
WebQuestion question = (WebQuestion)control2;
question.SetAnswer();

antwoorden.Add(question.Id, question.Answer);
}
}
}
return antwoorden;
}

public void SetAnswers(NameValueCollection answers)
{
foreach (Control control in this.Controls)
{
Panel panel = (Panel)control;
foreach (Control control2 in panel.Controls)
{
if (control2 is WebQuestion)
{
WebQuestion question = (WebQuestion)control2;

if (answersquestion.Id != null)
{
string answer = answersquestion.Id;
question.UpdateAnswer(answer);
}
}
}
}
}


Now add this code to WebQuestion.cs
public virtual void UpdateAnswer(string answer)
{
}

And for every question you allow saving implement this. For MCssWebQuestion this implementation is:
public override void UpdateAnswer(string answer)
{
base.UpdateAnswer(answer);

this.m_listControl.SelectedValue = answer;
}

Using the code


Create a file named SerializableNameValueCollection.cs
using System;
using System.Xml.Schema;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Specialized;

namespace sstchur.web.survey
{
Serializable
public class SerializableNameValueCollection : IXmlSerializable
{
NameValueCollection _collection;

public SerializableNameValueCollection()
{

}

public SerializableNameValueCollection(NameValueCollection collection)
{
this._collection = collection;
}

XmlIgnore
public NameValueCollection Collection
{
get { return _collection; }
set { _collection = value; }
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader r)
{
_collection = new NameValueCollection();
while (r.MoveToNextAttribute())
_collection.Add(r.Name, r.Value);

r.Read();
}

public void WriteXml(XmlWriter w)
{
foreach (string key in _collection.Keys)
{
string value = _collectionkey;
w.WriteAttributeString(key, value);
}
}
}
}

Add a profile property to save the data:
<profile>
<properties>
<group name="User">
<add name="Antwoorden" type="sstchur.web.survey.SerializableNameValueCollection"/>
</group>
</properties>
</profile>

Now you can load this data by using this code:
NameValueCollection antwoorden = Profile.User.Antwoorden.Collection;
poll.SetAnswers(antwoorden);

And save the data using:
NameValueCollection antwoorden = poll.GetCurrentAnswers();
Profile.User.Antwoorden.Collection = antwoorden;
Profile.Save();

(assumes the WebSurvey control is named "poll").


PS The source code is a little messed up (especially the attributes) you might need to add brackets there.
Coordinator
Nov 13, 2007 at 6:03 PM
neokoen,

Awesome job! Definitely I would be interested in this, as I think it has great value to others.

Tell you what I'd like to do. I'm going to see if I can grant you access to enlist in the source. Then, perhaps you create a shelf set and send it to me so that I can do a diff to review the code?