This is a very simple application that is a quiz/test that will show the user their score after submitted.
Sample application and walk-through guide.
This type of application is a test or quiz. In this example the score is determined and shown to the user immediately after taking the quiz. I used a few different techniques in this example, let's break it down.
This quiz is made up of some simple questions using radio lists. There is nothing special about the questions or the logic of them. The real magic comes in to play once the user submits their results. The quiz has three stages: start, score and end. The form is setup to “Display in the next stage” upon submission:
Each of the questions has an image associated to it that has information about the question and it will be shown to the user after they submit the form. To accomplish this, click the Stages tab and then the Start stage then click the button to hide the image in the stage:
The label that shows the user score is also hidden in the start stage using the same mechanism.
Now let's look at how the quiz auto-marks itself. In the beforeSave event there is some code that executes to mark all the questions:
This example leverages the group of functions used to process all the items in a page, which can be found on the DevWorks Wiki. Since these functions are described in that article I will not go over them here. The one that I will describe is the definition of processItem that I used:
When walking over all the items of the form, if we find a Select One item then we get the current value and check it against the “key” that was setup in the Settings...Events...Custom Actions of the application. There are several different ways that you could implement something like this, but for simplicity sake I chose to create a simple object array where each object has an ID (the ID of the question) and value (the correct answer).
I needed to create a helper function that when given an item's ID it would return the value from the “key” array. Here is the function that was created:
I chose to show the user the correct answer if they selected the wrong answer. This was a design decision and you could modify this if it does not fit your use case:
When the code is done walking all the items in the form is stores the total score in a global variable (app.getSharedData().totalCorrect()). So the second line of code in the beforeSave event copies that totalScore into a hidden field. Once that value is in the hidden field it will be displayed by the label that is setup to mirror the content of the field:
The hidden field will also insure that the total score appears in the database.
Once the form has been submitted, I use the following code in the onShowActionButtons event of the Form to hide the submit button so that it cannot be submitted any further:
Adapting to your Use Case
- If your test contained different question types other than Select One then you would need to modify the processItem function so that only the question types get processed.
- If you need to process different types of questions differently from others then you could create different implementations of the processItem function and then call each accordingly.
Note: to change the score from the number of correct answers to a percentage change the code in the beforeSave event to: