Program rule, assign value by summing boolean data elements

Hi @Natalie_Tibbels.

I have created a quick demo on the https://play.dhis2.org/2.34.0/ but I guess it will be deleted tonight; so I am adding a metadata package (remove the .pdf extension as it is a .zip)metadata.json.zip.pdf (3.3 KB) that you could import in the play server.

To be honest I was a bit more difficult that I initially thought because of the differences between the PR in Android and web.

The result in web|android is (what I think you wanted); note that I have added a “3” by default so it can be appreciated the changes.
vokoscreen-2020-05-09_10-55-40
vokoscreen-2020-05-09_10-59-37

You will need several Program Rules variable, in the form:
2 * (number of boolean fields)
Because you will need two program rules variables to assign either 0 or 1 according to the boolean state:
image

Then you will need the same amount of program rules pluse one, in the form:
2 * (number of boolean fields) + 1
image

Basically you are having two program rules per boolean to assign the number 0 or 1 so calculations can be made later:

PR1 condtion: !d2:hasValue(‘prv_boolean_one’) || !#{prv_boolean_one}
PR1 action: Assign value: “0” to field “#{prv_bool_one_to_number}”

PR2 condition: d2:hasValue(‘prv_boolean_one’) && #{prv_boolean_one}
PR2 action: Assign value: “1” to field “#{prv_bool_one_to_number}”

(repeat these two PR for every boolean)

Program rule to calculate (this is a bit tricky as we want it to be evaluated always so condition is true):

PR1 condtion: true
PR1 action: Assign value: “#{prv_bool_one_to_number} + #{prv_bool_two_to_number} + 3” to field “jb_test_boolean_calculation”

Please note that the program rules require to have priorities. The important thing is that the calculation is executed after the other ones. In the example I used Priority 1 for the assignations and 5 for the calculation.

Hope it helps.

2 Likes