Skip to content

Commit

Permalink
Merge pull request #1615 from dimagi/customRepeatCaptions
Browse files Browse the repository at this point in the history
Custom Repeat Captions Support
  • Loading branch information
shubham1g5 authored Aug 22, 2024
2 parents 2715320 + 1835516 commit 3aef186
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
17 changes: 3 additions & 14 deletions src/main/java/org/commcare/formplayer/api/json/PromptToJson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.commcare.formplayer.api.json;

import org.commcare.cases.util.StringUtils;
import org.commcare.formplayer.exceptions.ApplicationConfigException;
import org.javarosa.core.model.Constants;
import org.javarosa.core.model.FormIndex;
Expand Down Expand Up @@ -131,20 +132,8 @@ public static JSONObject parseQuestionType(FormEntryModel model, JSONObject obj)
}

private static String getRepeatAddText(FormEntryCaption prompt) {
String promptText = prompt.getLongText();
if (prompt.getNumRepetitions() > 0) {
try {
return Localization.get("repeat.dialog.add.another", promptText);
} catch (NoLocalizedTextException e) {
return "Add another " + promptText;
}
} else {
try {
return Localization.get("repeat.dialog.add.new", promptText);
} catch (NoLocalizedTextException e) {
return "Add a new " + promptText;
}
}
boolean hasRepetitions = prompt.getNumRepetitions() > 0;
return prompt.getRepeatText(hasRepetitions ? "add" : "add-empty");
}

private static void parseRepeatJuncture(FormEntryModel model, JSONObject obj, FormIndex ix) {
Expand Down
28 changes: 21 additions & 7 deletions src/test/java/org/commcare/formplayer/tests/RepeatTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void testRepeatNonCountedSimple() throws Exception {
NewFormResponse newSessionResponse = startNewForm("requests/new_form/new_form.json",
"xforms/repeat.xml");
QuestionBean[] tree = newSessionResponse.getTree();
assert (tree.length == 2);
assert (tree.length == 3);
QuestionBean dummyNode = tree[1];
assertEquals("false", dummyNode.getExists());
assertEquals("Add a new question3", dummyNode.getAddChoice());
assertEquals("Add Empty Caption Label", dummyNode.getAddChoice());
assertEquals("false", dummyNode.getExists());
assertEquals(false, dummyNode.isDelete());

Expand All @@ -53,7 +53,7 @@ public void testRepeatNonCountedSimple() throws Exception {

// Verify the repeat has been added to form tree correctly
tree = newRepeatResponseBean.getTree();
assert (tree.length == 3);
assert (tree.length == 4);
QuestionBean firstRepeat = tree[1];
assertEquals("true", firstRepeat.getExists());
assertEquals(true, firstRepeat.isDelete());
Expand All @@ -68,12 +68,12 @@ public void testRepeatNonCountedSimple() throws Exception {
assertEquals("false", secondRepeat.getExists());
assertEquals(false, secondRepeat.isDelete());
assert (secondRepeat.getChildren().length == 0);
assertEquals("Add another question3", secondRepeat.getAddChoice());
assertEquals("Add Caption Label", secondRepeat.getAddChoice());

// Add another repeat and verify the form tree accordingly
newRepeatResponseBean = newRepeatRequest(sessionId, "1_1");
tree = newRepeatResponseBean.getTree();
assert (tree.length == 4);
assert (tree.length == 5);
secondRepeat = tree[2];
assertEquals("true", secondRepeat.getExists());
assert (secondRepeat.getChildren().length == 1);
Expand All @@ -94,7 +94,7 @@ public void testRepeatNonCountedSimple() throws Exception {

// Verify that we deleted the repeat at right index
tree = deleteRepeatResponseBean.getTree();
assert (tree.length == 4);
assert (tree.length == 5);
firstRepeat = tree[1];
secondRepeat = tree[2];
thirdRepeat = tree[3];
Expand All @@ -106,7 +106,7 @@ public void testRepeatNonCountedSimple() throws Exception {
// delete second repeat again from the new tree
deleteRepeatResponseBean = deleteRepeatRequest(sessionId, "1_1,0");
tree = deleteRepeatResponseBean.getTree();
assert (tree.length == 3);
assert (tree.length == 4);
firstRepeat = tree[1];
secondRepeat = tree[2];
assertEquals(firstRepeat.getChildren()[0].getAnswer(),"repeat 1");
Expand Down Expand Up @@ -239,4 +239,18 @@ public void testNestedRepeatDeletionRegression() throws Exception {
assertEquals("bed 2", beds[0].getChildren()[0].getChildren()[0].getAnswer());
assertEquals("false", beds[1].getExists());
}

@Test
public void testDefaultRepeatCaption() throws Exception {
NewFormResponse newSessionResponse = startNewForm("requests/new_form/new_form.json",
"xforms/repeat.xml");
QuestionBean[] tree = newSessionResponse.getTree();
assertEquals("Add a new question4", tree[2].getAddChoice());
String sessionId = newSessionResponse.getSessionId();
FormEntryResponseBean newRepeatResponseBean = newRepeatRequest(sessionId, "2_0");

// Verify the repeat has been added to form tree correctly
tree = newRepeatResponseBean.getTree();
assertEquals("Add another question4", tree[3].getAddChoice());
}
}
27 changes: 26 additions & 1 deletion src/test/resources/xforms/repeat.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<question3 jr:template="">
<repeat_text/>
</question3>
<question4 jr:template="">
<repeat_text/>
</question4>
<orx:meta xmlns:cc="http://commcarehq.org/xforms"><orx:deviceID/><orx:timeStart/><orx:timeEnd/><orx:username/><orx:userID/><orx:instanceID/><cc:appVersion/><cc:location/></orx:meta></data>
</instance><instance id="commcaresession" src="jr://instance/session"/>
<bind nodeset="/data/q_name" type="xsd:string" required="true()"/>
Expand All @@ -25,6 +28,18 @@
<text id="question3/repeat_text-label">
<value>repeat_text</value>
</text>
<text id="question3/add-caption-label">
<value>Add Caption Label</value>
</text>
<text id="question3/add-empty-caption-label">
<value>Add Empty Caption Label</value>
</text>
<text id="question4-label">
<value>question4</value>
</text>
<text id="question4/repeat_text-label">
<value>repeat_text</value>
</text>
</translation>
<translation lang="hin">
<text id="q_name-label">
Expand All @@ -47,10 +62,20 @@
<group>
<label ref="jr:itext('question3-label')"/>
<repeat nodeset="/data/question3">
<jr:addCaption ref="jr:itext('question3/add-caption-label')" />
<jr:addEmptyCaption ref="jr:itext('question3/add-empty-caption-label')" />
<input ref="/data/question3/repeat_text">
<label ref="jr:itext('question3/repeat_text-label')"/>
</input>
</repeat>
</group>
<group>
<label ref="jr:itext('question4-label')"/>
<repeat nodeset="/data/question4">
<input ref="/data/question4/repeat_text">
<label ref="jr:itext('question4/repeat_text-label')"/>
</input>
</repeat>
</group>
</h:body>
</h:html>
</h:html>

0 comments on commit 3aef186

Please sign in to comment.