StatFunctions plugin
From LimeSurvey Manual
Usage
Functions for the Expression Script engine to count statistical data:
statCount(QuestionCode.sgqa[, submitted = 1][, self = true]): counts the number of responses that have already answered (submitted) the question identified by QuestionCode. For example, you can count the number of submitted (completed) responses to the survey withstatCount('id').statCountIf(QuestionCode.sgqa, value[, submitted = 1][, self = true]): counts the number of responses where the answer to question QuestionCode equals value. The comparison uses Yii's compare; you can prefix value with an operator such as<,>,>=,<=, or<>.
An example showing both functions in use can be found in the test directory: survey_archive_statCountFunctionsTest.
This plugin relies on the ExpressionManagerStart event, and adds these two functions to it. Neither function has a JavaScript equivalent, so the calculation is only performed when the respondent moves to the next page.
Expression Script does not have true and false as reserved words, so these strings are interpreted as variable names rather than as booleans. For the submitted and self parameters, use 1, 0, or the result of an expression instead — keep in mind that the string "false" evaluates to true in a boolean context.
Example
- Count the number of submitted responses:
{statCount("id")} - Count the number of responses, submitted or not:
{statCount("id", 0)} - Number of responses where the respondent stopped on page 1:
{statCountIf("lastpage", 1, 0)} - Number of submitted responses with start language "en":
{statCountIf("startlanguage", "en", 1)} - Number of incomplete responses to this survey:
{sum(statCount("id", 0),statCount("id")*-1)} - Number of responses submitted in 2022:
{statCountIf("submitted", ">=2022-01-01")} - Number of submitted responses with answer Y to the question titled YesNo:
{statCountIf(YesNo.sgqa, "Y")} - Number of responses, submitted or not, with answer A1 to the question titled Q01:
{statCountIf(Q01.sgqa, "A1", 0)} - Check whether a short-text answer is unique among responses, evaluated only when moving to the next page (not submitted):
{statCountIf(self.sgqa, self.NAOK, 0, 1)}
