Scripting reference

From ITR Wiki

Most tests and reports in the ITR need no programming at all. The scoring matrix, the norm tables, screen dynamics and the report editor cover a lot. But sometimes you want just a little more: an adaptive test, a score that depends on two other scores, a norm per age group, or a table that is calculated for the report. That is where scripts come in.

Scripts in the ITR are small pieces of JavaScript. They run in the web browser, at a fixed moment, with a fixed set of objects that the ITR hands to them. This page tells you which script runs when, and what you can use in it.

A few friendly words of advice before you start:

  • Keep scripts short and simple. A script that only uses the objects on this page keeps working when the ITR is updated.
  • A script that fails does not stop the test. The ITR writes the error to the browser console (press F12) and carries on. So test your script well, and check the console while you do.
  • Always check that a value exists before you use it, for example with typeof x != "undefined". An answer that was skipped is often an empty string.

Overview

Script Where you write it When it runs Parameters
Screen pre script Test editor, Contents tab, per screen Just before the screen is shown session, sessiontest, candidate, testdefinition, testtakingcontroller, itsinstance, testmode, screen
Screen post script Test editor, Contents tab, per screen When the candidate leaves the screen, after the screen validations passed same as the pre script
Complex scoring script Test editor, Scoring tab When the test is scored, after the scoring matrix, before rounding session, sessiontest, candidate, testdefinition, scores, scales, itsinstance
Complex norming script Test editor, Norms tab, per norm After the norm tables of that norm have been applied session, sessiontest, candidate, testdefinition, scores, scales, itsinstance, normPostFix
Script run before the report Report editor, Report writer tab Before the report text is filled in none; use this
Pre graph script Report editor, Graph builder tab Before the graph is drawn none; use template_values and context, see the graph settings
Screen template snippets Screen template editor When the component is shown, read, validated or generated see the screen template page
Action expressions Components with a List of actions variable, such as buttons When the candidate triggers the action see Screen actions reference

The objects

These objects appear in most scripts.

session
The session of the candidate: session.Description, session.SessionTests (all tests in the session), session.PluginData and the other session fields listed in the field list. Answers that are stored at session level are in session.PluginData.sessionStorage, by component name.
sessiontest
The test as taken by this candidate: sessiontest.Results (the answers), sessiontest.Scores (the scale scores), sessiontest.CurrentPage (the index of the current screen), sessiontest.NormID1 to NormID3, and helper functions such as sessiontest.scale("ScaleVarName").
candidate
The person who takes the test: candidate.FirstName, candidate.BirthDate, candidate.Sex and so on.
testdefinition
The test definition: testdefinition.screens, testdefinition.scales, testdefinition.norms and helpers such as testdefinition.findScreenIndexByName("Question5"). In the complex norming script this parameter is the norm itself, see below.
itsinstance
The ITR application itself. You rarely need it; scripts that use it depend on the inner workings of a specific ITR version.

Answers

Answers are stored per screen and per component. Each component result has a Value. While a test is being taken or scored you can usually reach an answer by the names you gave in the test editor:

var answer = sessiontest.Results.Question5.Answer.Value;

Internally the ITR also stores the same answer under the ids of the screen and the component (keys starting with __). The scoring matrix uses those ids, so it is the most robust way to read answers. In scripts, check that the screen and the component exist before you read the value.

Scale scores

Each scale has a score object with these fields:

Score
The raw score.
NormScore, PercentileScore, AppliedNorm, AppliedNormDescription
The result of norm 1. For norm 2 and 3 the same fields end with 2 or 3, for example NormScore2.

Screen pre script and screen post script

Parameters: session, sessiontest, candidate, testdefinition, testtakingcontroller, itsinstance, testmode, screen

screen
The screen that is about to be shown (pre script) or that is being left (post script). Set screen.show = false in the pre script to skip the screen. Each component in screen.screenComponents also has a show property.
testmode
"TT" during test taking. When the answers of a test are shown or entered in paper and pencil mode, the pre script runs once per screen with testmode "PnPView".
testtakingcontroller
The controller that runs the test. Only available during test taking (it is empty in the "PnPView" mode).

The pre script runs every time the screen is about to be shown. If it sets screen.show to false, the ITR moves on to the next screen. The post script runs when the candidate leaves the screen and the screen validations are fine. If a validation fails, the candidate stays on the screen and the post script does not run yet.

Example: skip a screen when another test in the session already asked the question (the component has Store at session level switched on).

if (typeof session.PluginData.sessionStorage != "undefined" &&
    typeof session.PluginData.sessionStorage.ResearchGender != "undefined") {
  screen.show = false;
}

Complex scoring script

Parameters: session, sessiontest, candidate, testdefinition, scores, scales, itsinstance

Please note the names: for historical reasons

  • the parameter scores holds the answers of the candidate (the same object as sessiontest.Results), and
  • the parameter scales holds the scale scores (the same object as sessiontest.Scores).

The scale scores are available by the scale variable name. Scales start with their default value, then the scoring matrix runs, then this script. After the script, numeric scales are rounded to their scale precision.

Example: a total scale that is the sum of two other scales.

scales.Total.Score = Number(scales.PartA.Score) + Number(scales.PartB.Score);

Example: a textual scale based on a numeric one.

scales.Result.Score = (Number(scales.Total.Score) >= 30) ? "passed" : "not passed";

Complex norming script

Parameters: session, sessiontest, candidate, testdefinition, scores, scales, itsinstance, normPostFix

The norming script belongs to one norm, and runs right after the norm tables of that norm have been applied. The header of the script in the editor shows (TestTakingObject, TestObject, trigger); the parameters below are the ones the ITR actually passes.

testdefinition
In this script this parameter is the norm that is being applied. The test itself is testdefinition.myParent.
scores
The answers, as in the scoring script.
scales
The scale scores, as in the scoring script.
normPostFix
1, 2 or 3: whether this norm is applied as norm 1, 2 or 3 of the session. Use it to write to the right field: NormScore for norm 1, NormScore2 for norm 2, NormScore3 for norm 3.

Example: give everyone with a raw score above 60 the highest stanine, whichever norm slot is used.

var postfix = (normPostFix == 1) ? "" : normPostFix;
if (Number(scales.Total.Score) > 60) {
  scales.Total["NormScore" + postfix] = 9;
}

Script run before the report

This script has no parameters. Inside it, this is the report, with:

this.session, this.sessiontest, this.test, this.candidate
The session, the session test, the test definition and the candidate.
this.scales
The scale definitions of the test, by scale variable name. For the scores use this.sessiontest.scale("ScaleVarName").Score.

Anything you add to this can be used in the report text. In the report text the report is called instanceObj:

this.myGreeting = "Hello " + this.candidate.FirstName;

and in the report text:

%%instanceObj.myGreeting%%

Useful helper functions are normValue, makeHTMLTable and convertDateToITR; see Report editor tips.

Screen template snippets and actions

Screen templates have their own snippets with their own parameters. These are described on the Test screen template introduction page. Buttons and other components with a List of actions run actions, which can contain expressions; see the Screen actions reference.

Where to go next