I created two functions: GetScore() & GetTotalScore().
GetScore() takes the document variable of each question as a single parameter and returns the value between the two parentheses (i.e. the answer's score). It does this utilizing the MEL match() function, finding a match for the left parentheses, grabbing the index just after it, and returning the value found at that index.
{fn GetScore(docVar){
return ( docVar[match(docVar, "(") + 1] )
}}
GetTotalScore() also takes a single parameter - a collection (array) of the questions - loops through each of the questions, grabs its score (utilizing our GetScore() function), totals them all up, and returns the total score.
{fn GetTotalScore(questionCollection){
local questions = questionCollection
local totalScore = 0
for i = 1, i <= size(questions), i = i + 1 do
if (questions[i] <> "") then
totalScore = totalScore + GetScore(questions[i])
endif
endfor
return totalScore
}}
We can then create a Watcher Expression to automatically update the View (the form) when a selection on a question is made. We do this by creating our questionCollection array, passing it to our GetTotalScore() function, and saving the value in a global variable questionTotalScore.
{
global questionCollection = array(DOCUMENT.Question1, DOCUMENT.Question2, DOCUMENT.Question3, DOCUMENT.Question4, DOCUMENT.Question5)
global questionTotalScore = GetTotalScore(questionCollection)
}
We can then use this global variable inside of a Data Display item on our form labeled TOTAL, with a Connection Type of "Mel expression," and placing the following inside the expression view:
{questionTotalScore}
Now when each question is answered, our Watcher Expression will fire our GetTotalScore() function, which in turn will tally up the score of our questions, and return them to the TOTAL Data Display.
Here is our Function View all put together:
/* Watcher Expression to automatically update the total as selections are made */
{
global questionCollection = array(DOCUMENT.Question1, DOCUMENT.Question2, DOCUMENT.Question3, DOCUMENT.Question4, DOCUMENT.Question5)
global questionTotalScore = GetTotalScore(questionCollection)
}
{fn GetTotalScore(questionCollection){
local questions = questionCollection
local totalScore = 0
for i = 1, i <= size(questions), i = i + 1 do
if (questions[i] <> "") then
totalScore = totalScore + GetScore(questions[i])
endif
endfor
return totalScore
}}
/* Returns the value found at the index just after the left parentheses (i.e. the score) */
{fn GetScore(docVar){
return ( docVar[match(docVar, "(") + 1] )
}}
Hope that helps! Or at least steers you in the right direction =)
[caption id="attachment_164546" align="alignnone" width="300"] Form with running total tallied from questionnaire.[/caption]
Caleb Bergman
Systems Analyst
Valley Medical Center, PLLC
Posted : October 15, 2014 10:23 am