Contour, display different result pages based on score
Hi everyone,
Trying to build a survey using contour, I've created the basic form fine - very easy with the contour interface. Here's the tricky part, each question has a series of multiple choice answers - I need to assign each answer with a certain score value (eg, between 1 & 4), then when the user submits the form they need to be redirected to a results page based on their score. Is there an easy way to do this? If not, I can easily display the correct information with a generic results page using jquery - but i'll still need to render out the score its self as a number somewhere on the page.
Thanks for that link! Kinda ironic I was just looking at http://www.nibble.be/?p=81 , but it wasn't quite what I needed. I'll have a read of the one you posted and see where it gets me - but it looks promising :)
Was able download and install the dll from the linked article, the score is always displaying zero. Attempting to add that snippet to ScoreCalculator.cs in Visual Studio recompiled the new dll and added it to the build folder and the score does not display at all now.
[Umbraco.Forms.Core.Attributes.Setting("Score mappings", description = "Setup how much each answer scores", control = "Contour.Addons.ScoreCalclulator.ScoreMapper", assembly = "Contour.Addons.ScoreCalclulator")]
publicstring scoremappings { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Field to update", description = "Field that will be populated with the score", control = "Umbraco.Forms.Core.FieldSetting.FieldPicker")]
publicstring field { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Session key to set", description = "If supplied a session variable will be set", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
Yes I've setup the workflow, and added a weight to each answer - however mine is setup slightly different to in the demo. In the demo you have one question per step, in mine i have 3 or 4 questions per step. I have the workflow in the approval section too.
But mine still looks the same as yours does on the workflow screen - except my weights go up to 4.
Uploaded the updated code .cs files to App_Code and it started working... not sure if that's how It was meant to be done - the only instructions on the blog post were about copying the dll to the bin folder.
Now all thats left is to figure how to redirect to a certain page based on score.
Ah success! In the thankyou section i've just put the score its self [%score], and used this jquery to redirect to the right page, it grabs the current url and adds a slug to it - on my site there will be several surveys that function in this way so that keeps it a bit more dynamic. Thanks Tim for your help!!!
Still having issues with the score display as zero regardless of the user input, any ideas as to what would cause this? Workflows are setup as specified in your article.
I am using that snippet. During testing yesterday the score started calculating again, but the next day it's all coming back as zero again. What could cause this?
No nothing on the form has changed, it was literally working at 5:30pm, then the next morning not working. Any way to debug? If you could email me I could get you access to the server to have a look.
Provider was added twice , that was the issue, once in App_code (a working one with the updated snipped) and onces compiled (without the working snippet)
Contour, display different result pages based on score
Hi everyone,
Trying to build a survey using contour, I've created the basic form fine - very easy with the contour interface. Here's the tricky part, each question has a series of multiple choice answers - I need to assign each answer with a certain score value (eg, between 1 & 4), then when the user submits the form they need to be redirected to a results page based on their score. Is there an easy way to do this? If not, I can easily display the correct information with a generic results page using jquery - but i'll still need to render out the score its self as a number somewhere on the page.
Using umbraco v 4.11.9
Any help much appreciated.
Thanks
Comment author was deleted
Hey Nathan,
Maybe this can help: http://www.nibble.be/?p=83
IT's a quiz workflow that allows you to asign weight to answers and then calculate a score
But needs a small update to work correctly on the latest Contour version, details are somewhere on the form, I'll see if I can find them :)
Hey Tim,
Thanks for that link! Kinda ironic I was just looking at http://www.nibble.be/?p=81 , but it wasn't quite what I needed. I'll have a read of the one you posted and see where it gets me - but it looks promising :)
Would appreciate the needed update.
Cheers
Comment author was deleted
Yeah looking for it now :)
Comment author was deleted
Ok this is the updated snippet to calculate a score
Comment author was deleted
So that should get you started :)
Hi Tim,
Was able download and install the dll from the linked article, the score is always displaying zero. Attempting to add that snippet to ScoreCalculator.cs in Visual Studio recompiled the new dll and added it to the build folder and the score does not display at all now.
Here's the current code with your snippet added
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Forms.Data.Storage;
using Umbraco.Forms.Core.Services;
using Umbraco.Forms.Core;
namespace Contour.Addons.ScoreCalclulator
{
publicclassEnergyQuizScoreCalculator : Umbraco.Forms.Core.WorkflowType
{
[Umbraco.Forms.Core.Attributes.Setting("Score mappings", description = "Setup how much each answer scores", control = "Contour.Addons.ScoreCalclulator.ScoreMapper", assembly = "Contour.Addons.ScoreCalclulator")]
public string scoremappings { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Field to update", description = "Field that will be populated with the score", control = "Umbraco.Forms.Core.FieldSetting.FieldPicker")]
public string field { get; set; }
[Umbraco.Forms.Core.Attributes.Setting("Session key to set", description = "If supplied a session variable will be set", control = "Umbraco.Forms.Core.FieldSetting.TextField")]
public string sessionkey { get; set; }
public EnergyQuizScoreCalculator()
{
this.Name = "Score Calculator";
this.Id = newGuid("EAFAC6F1-F2D2-43A4-8940-AB7A6F7AE83F");
this.Description = "Calculates a score based on the questions/correct answers";
}
public override List<Exception> ValidateSettings()
{
List<Exception> exceptions = new List<Exception>();
return exceptions;
}
public override Umbraco.Forms.Core.Enums.WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
{
//calc score
int score = 0;
Dictionary<string, int> scores = new Dictionary<string, int>();
foreach (string mapping in scoremappings.Split(';'))
{
if (!string.IsNullOrEmpty(mapping) && mapping.Split(',').Length > 0)
{
int weight = 0;
scores.Add(mapping.Split(',')[0], int.TryParse(mapping.Split(',')[1], out weight) ? weight : 0);
}
}
PreValueStorage pvs = newPreValueStorage();
foreach (RecordField rf in record.RecordFields.Values)
{
if (rf.Field.FieldType.GetType() == typeof (Umbraco.Forms.Core.Providers.FieldTypes.RadioButtonList))
{
if (rf.Values.Count > 0)
{
var pvkey = pvs.GetAllPreValues(rf.Field).First(p => p.Value == rf.Values[0].ToString()).Id;
if (scores.ContainsKey(pvkey.ToString()))
score += scores[pvkey.ToString()];
}
}
}
pvs.Dispose();
if (!string.IsNullOrEmpty(sessionkey))
HttpContext.Current.Session[sessionkey] = score.ToString();
FormStorage fs = new FormStorage();
Form f = fs.GetForm(record.Form);
RecordStorage rs = newRecordStorage();
rs.UpdateRecord(record, f);
rs.UpdateRecordXml(record, f);
fs.Dispose();
rs.Dispose();
return Umbraco.Forms.Core.Enums.WorkflowExecutionStatus.Completed;
}
}
}
Cheers
Any ideas on this? So close! Once the score's displaying I can use jQuery to do the rest :)
I'm not sure if I'm just placing your snippet in the wrong place? Where exactly should it be placed? What should it overwrite?
Comment author was deleted
Did you setup the workflow? So attach it to the form and set the weight for each answer?
Hi Tim,
Yes I've setup the workflow, and added a weight to each answer - however mine is setup slightly different to in the demo. In the demo you have one question per step, in mine i have 3 or 4 questions per step. I have the workflow in the approval section too.
But mine still looks the same as yours does on the workflow screen - except my weights go up to 4.
Can't see what I'm doing wrong, if you look at the entries the score field is always blank.
I'm inserting the form as Razor onto a page.
Uploaded the updated code .cs files to App_Code and it started working... not sure if that's how It was meant to be done - the only instructions on the blog post were about copying the dll to the bin folder.
Now all thats left is to figure how to redirect to a certain page based on score.
Ah success! In the thankyou section i've just put the score its self [%score], and used this jquery to redirect to the right page, it grabs the current url and adds a slug to it - on my site there will be several surveys that function in this way so that keeps it a bit more dynamic. Thanks Tim for your help!!!
if ($('.contourMessageOnSubmit').length) {
var $score = $('.contourMessageOnSubmit').html();
$('.contourMessageOnSubmit').text('Please wait...');
if($score
var current = window.location.pathname;
var newLocation = current + 'less-than-8-points'
window.location = newLocation;
}
if($score >14){
var current = window.location.pathname;
var newLocation = current + '15-or-more-points'
window.location = newLocation;
}
if($score >7){
if($score
var current = window.location.pathname;
var newLocation = current + '8-to-14-points'
window.location = newLocation;
}
}
};
Comment author was deleted
Ok will take a look at the code
Comment author was deleted
Ah replied to soon glad you got it working :)
Hi Tim,
Still having issues with the score display as zero regardless of the user input, any ideas as to what would cause this? Workflows are setup as specified in your article.
Thanks
Comment author was deleted
Yeah the code needs to be updated slightly , check page 1 of this thread to see the updated snippet
I am using that snippet. During testing yesterday the score started calculating again, but the next day it's all coming back as zero again. What could cause this?
Comment author was deleted
Did you change anything to the form between those 2 days?
No nothing on the form has changed, it was literally working at 5:30pm, then the next morning not working. Any way to debug? If you could email me I could get you access to the server to have a look.
Hi,
Any thoughts on this?
Comment author was deleted
Didn't see the last reply, you can email me tg at umbraco dot com and I'll take a further look.
Comment author was deleted
Provider was added twice , that was the issue, once in App_code (a working one with the updated snipped) and onces compiled (without the working snippet)
is working on a reply...