Friday, September 14, 2018

SurveyGizmo custom script has a function (sgapiGetQuestionOptionSelected) to return the selected option for a single or multiple select question. For a single select question it always returns a single number. Unfortunately for a multiple select question the return value depends on the number of options selected. If multiple items are selected an array is returned but if only one item is selected just that option's id is returned. Makes it harder to write code to check if certain items were checked.

So I wrote the following function to always return an array regardless of the number of items selected.

Friday, July 21, 2017

Colorful serialization

I was writing some code to serialize a C# class to JSON and needed to export a System.Drawing.Color field as a hex string. Thankfully it's easy to customize the output using Json.NET. I created the following custom converter to save colors as hex strings (like #FFF0B6)

And example usage:

Monday, March 13, 2017

Exclusive checkbox column

We had a customer that had a checkbox grid and wanted the checkboxes in one of the rows to be exclusive (e.g. when checked no other items in that column are checked). Most survey platforms have this functionality for a multiple choice checkbox question but not for columns in a checkbox grid. Some quick jQuery and JavaScript coding I had the following mostly generic function working.

Working demo: JSFiddle

Saturday, April 2, 2016

Find and Replace Placeholders in a Word Template

We have a template file that we use as a baseline in our report generation (see here for help creating a new Word document from a template). The template file has some placeholders for title, subtitle, author, etc. I wanted to be able to dynamically replace those with content when the rest of the document was generated. This took me way longer than I expected to figure out so here is hopefully some info to help the next person. First I found the relevant section in the XML (the Open XML Productivity Tools are a great help for this). Then with a bunch of trial and error I figured out how to find and replace the '[Document title]' placeholder.

Thursday, March 17, 2016

Custom validation in SurveyGizmo

SurveyGizmo allows you to add JavaScript directly to a survey page with the JavaScript Action. They use jQuery for survey interaction so that's already loaded on the page and available. Using this feature it's easy to customize a survey page.

One of the possible uses is for custom validation. For example to validate user input in some way that SurveyGizmo does not support out of the box. To do this you could start with the JavaScript below. It hooks the Next button click event and then hides or shows the error message based on a custom check. Note, we use the Next button click event and not the page submit event because that is called even when the Back button is selected.

Wednesday, March 16, 2016

Working with Arrays in SurveyGizmo Custom Scripting

As mentioned in my previous posts (here) we do a lot of work with SurveyGizmo's custom scripting feature. It's kind of like PHP and kind of like JavaScript but different enough that I forget how to do basic things like create arrays. So thought I would post some examples of working with arrays.

Some examples of working with single dimension arrays:

// Declare array with values
%%locations = array('Jacksonville, FL',
        'Toronto, Canada',
        'Orlando, FL',
        'Chicago, IL');

// Declare new empty array
%%copied = array();

foreach(%%locations as %%location) {
        // Add element to array
        %%copied[] = %%location;

And with a for loop (NOTE: You must use curly brackets when using a variable as the key).
for (%%i=0; %%i<%%count; %%i++) {
 %%output .= "<br>".%%locations{%%i};

And with a 2-dimension array (or hashtable):

// Declare array with values
%%locations = array('Toronto' => 'Ontario', 'Jacksonville' => 'FL',
        'Orlando' => 'FL', 'Chicago' => 'IL');

foreach (%%locations as %%key => %%value) {
        %%output .= "City: ".%%key." State/Province".%%value;

Tuesday, January 5, 2016

SurveyGizmo Custom Scripting

One nice feature of SurveyGizmo is their server-side custom scripting functionality (example below). It’s a PHP-like scripting language that allows you to modify the survey contents and operation at runtime. The script runs server-side before the page loads and offers a lot options for customizing a survey.

There are several things I would love to see with this feature:

  1. Syntax validation in the editor. Having to go through several survey pages to test your script and then find out you are missing a semi-colon or have a typo is no fun. A basic eval of the script would catch most of these errors.
  2. Using full PHP instead of PHP-lite. Having programmed a survey system I can understand why Survey Gizmo went with a more controlled subset of the language but when I need to use a function that isn’t there I’m not happy about it.
  3. Adding an option to run a script on page save. Currently, the scripts only run on page load so custom logic, validation and other items need to be on the next page. Sometimes it would be nice to just have them on the same page.

Having said that all in all its still pretty powerful functionality that we use with a lot of surveys at Researchscape.

Example of mapping the state question to another question that tracks the four main US Census regions.