Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Wright 23 posts 54 karma points
    Jun 27, 2013 @ 08:43
    Nathan Wright
    0

    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

    Jun 27, 2013 @ 08:46

    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 :)

     

  • Nathan Wright 23 posts 54 karma points
    Jun 27, 2013 @ 08:49
    Nathan Wright
    0

    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

    Jun 27, 2013 @ 08:50

    Yeah looking for it now :)

  • Comment author was deleted

    Jun 27, 2013 @ 09:56

    Ok this is the updated snippet to calculate a 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 = new PreValueStorage();
    
                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();
  • Comment author was deleted

    Jun 27, 2013 @ 10:00

    So that should get you started :)

  • Nathan Wright 23 posts 54 karma points
    Jun 28, 2013 @ 04:43
    Nathan Wright
    0

    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

  • Nathan Wright 23 posts 54 karma points
    Jul 01, 2013 @ 01:44
    Nathan Wright
    0

    Any ideas on this? So close! Once the score's displaying I can use jQuery to do the rest :) 

  • Nathan Wright 23 posts 54 karma points
    Jul 01, 2013 @ 09:27
    Nathan Wright
    0

    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

    Jul 01, 2013 @ 09:49

    Did you setup the workflow? So attach it to the form and set the weight for each answer?

  • Nathan Wright 23 posts 54 karma points
    Jul 02, 2013 @ 01:38
    Nathan Wright
    0

    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.

     

  • Nathan Wright 23 posts 54 karma points
    Jul 02, 2013 @ 02:15
    Nathan Wright
    0

    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. 

  • Nathan Wright 23 posts 54 karma points
    Jul 02, 2013 @ 02:34
    Nathan Wright
    0

    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. 

  • Nathan Wright 23 posts 54 karma points
    Jul 02, 2013 @ 03:45
    Nathan Wright
    0

    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

    Jul 02, 2013 @ 11:34

    Ok will take a look at the code

  • Comment author was deleted

    Jul 02, 2013 @ 11:34

    Ah replied to soon glad you got it working :) 

  • Nathan Wright 23 posts 54 karma points
    Oct 29, 2014 @ 05:43
    Nathan Wright
    0

    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

    Oct 29, 2014 @ 09:25

    Yeah the code needs to be updated slightly , check page 1 of this thread to see the updated snippet

  • Nathan Wright 23 posts 54 karma points
    Oct 30, 2014 @ 00:52
    Nathan Wright
    0

    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

    Oct 30, 2014 @ 09:27

    Did you change anything to the form between those 2 days?

  • Nathan Wright 23 posts 54 karma points
    Oct 30, 2014 @ 10:06
    Nathan Wright
    0

    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.

  • Nathan Wright 23 posts 54 karma points
    Nov 12, 2014 @ 23:17
    Nathan Wright
    0

    Hi,

     

    Any thoughts on this?

  • Comment author was deleted

    Nov 13, 2014 @ 10:00

    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

    Nov 14, 2014 @ 12:31

    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) 

Please Sign in or register to post replies

Write your reply to:

Draft