Search in sources :

Example 21 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class FormEntryActivity method saveAnswersForCurrentScreen.

// The method saves questions one by one in order to support calculations in field-list groups
private void saveAnswersForCurrentScreen(FormEntryPrompt[] mutableQuestionsBeforeSave, List<ImmutableDisplayableQuestion> immutableQuestionsBeforeSave) {
    FormController formController = getFormController();
    ODKView currentView = getCurrentViewIfODKView();
    if (formController == null || currentView == null) {
        return;
    }
    int index = 0;
    for (Map.Entry<FormIndex, IAnswerData> answer : currentView.getAnswers().entrySet()) {
        // Questions with calculates will have their answers updated as the questions they depend on are saved
        if (!isQuestionRecalculated(mutableQuestionsBeforeSave[index], immutableQuestionsBeforeSave.get(index))) {
            try {
                formController.saveOneScreenAnswer(answer.getKey(), answer.getValue(), false);
            } catch (JavaRosaException e) {
                Timber.e(e);
            }
        }
        index++;
    }
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) IAnswerData(org.javarosa.core.model.data.IAnswerData) ODKView(org.odk.collect.android.formentry.ODKView) FormIndex(org.javarosa.core.model.FormIndex) Map(java.util.Map) HashMap(java.util.HashMap) FailedConstraint(org.odk.collect.android.javarosawrapper.FormController.FailedConstraint) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 22 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class ODKView method getAnswers.

/**
 * @return a HashMap of answers entered by the user for this set of widgets
 */
public HashMap<FormIndex, IAnswerData> getAnswers() {
    HashMap<FormIndex, IAnswerData> answers = new LinkedHashMap<>();
    for (QuestionWidget q : widgets) {
        /*
             * The FormEntryPrompt has the FormIndex, which is where the answer gets stored. The
             * QuestionWidget has the answer the user has entered.
             */
        FormEntryPrompt p = q.getFormEntryPrompt();
        answers.put(p.getIndex(), q.getAnswer());
    }
    return answers;
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) FormIndex(org.javarosa.core.model.FormIndex) QuestionWidget(org.odk.collect.android.widgets.QuestionWidget) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class AuditEventCSVLine method toCSVLine.

public static String toCSVLine(AuditEvent auditEvent, boolean isTrackingLocationsEnabled, boolean isTrackingChangesEnabled, boolean isTrackingChangesReasonEnabled) {
    FormIndex formIndex = auditEvent.getFormIndex();
    AuditEvent.AuditEventType auditEventType = auditEvent.getAuditEventType();
    long start = auditEvent.getStart();
    long end = auditEvent.getEnd();
    String latitude = auditEvent.getLatitude();
    String longitude = auditEvent.getLongitude();
    String accuracy = auditEvent.getAccuracy();
    String oldValue = auditEvent.getOldValue();
    String newValue = auditEvent.getNewValue();
    String user = auditEvent.getUser();
    String changeReason = auditEvent.getChangeReason();
    String node = formIndex == null || formIndex.getReference() == null ? "" : getXPathPath(formIndex);
    String string = String.format("%s,%s,%s,%s", auditEventType.getValue(), node, start, end != 0 ? end : "");
    if (isTrackingLocationsEnabled) {
        string += String.format(",%s,%s,%s", latitude, longitude, accuracy);
    }
    if (isTrackingChangesEnabled) {
        string += String.format(",%s,%s", getEscapedValueForCsv(oldValue), getEscapedValueForCsv(newValue));
    }
    if (user != null) {
        string += String.format(",%s", getEscapedValueForCsv(user));
    }
    if (isTrackingChangesReasonEnabled) {
        if (changeReason != null) {
            string += String.format(",%s", getEscapedValueForCsv(changeReason));
        } else {
            string += ",";
        }
    }
    return string;
}
Also used : FormIndex(org.javarosa.core.model.FormIndex)

Example 24 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class FormController method getQuestionPrompts.

/**
 * Returns an array of question prompts corresponding to the current {@link FormIndex}. These
 * are the prompts that should be displayed to the user and don't include any non-relevant
 * questions.
 * <p>
 * The array has a single element if there is a question at this {@link FormIndex} or multiple
 * elements if there is a group.
 *
 * @throws RepeatsInFieldListException if there is a group at this {@link FormIndex} and it contains
 *                          elements that are not questions or regular (non-repeat) groups.
 */
public FormEntryPrompt[] getQuestionPrompts() throws RepeatsInFieldListException {
    // For questions, there is only one.
    // For groups, there could be many, but we set that below
    FormEntryPrompt[] questions = new FormEntryPrompt[0];
    IFormElement element = formEntryController.getModel().getForm().getChild(getFormIndex());
    if (element instanceof GroupDef) {
        GroupDef gd = (GroupDef) element;
        // we only display relevant questions
        List<FormEntryPrompt> questionList = new ArrayList<>();
        for (FormIndex index : getIndicesForGroup(gd)) {
            if (getEvent(index) != FormEntryController.EVENT_QUESTION) {
                throw new RepeatsInFieldListException("Repeats in 'field-list' groups " + "are not supported. Please update the form design to remove the " + "following repeat from a field list: " + index.getReference().toString(false));
            }
            // we only display relevant questions
            if (formEntryController.getModel().isIndexRelevant(index)) {
                questionList.add(getQuestionPrompt(index));
            }
            questions = new FormEntryPrompt[questionList.size()];
            questionList.toArray(questions);
        }
    } else {
        // We have a question, so just get the one prompt
        questions = new FormEntryPrompt[1];
        questions[0] = getQuestionPrompt();
    }
    return questions;
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) ArrayList(java.util.ArrayList) FormIndex(org.javarosa.core.model.FormIndex) GroupDef(org.javarosa.core.model.GroupDef)

Example 25 with FormIndex

use of org.javarosa.core.model.FormIndex in project collect by opendatakit.

the class FormController method jumpToNewRepeatPrompt.

/**
 * Jumps to the next prompt for a repeated instance of the group referenced by the current FormIndex.
 */
public void jumpToNewRepeatPrompt() {
    FormIndex repeatGroupIndex = getRepeatGroupIndex(getFormIndex(), getFormDef());
    Integer depth = repeatGroupIndex.getDepth();
    Integer promptDepth = null;
    while (!depth.equals(promptDepth)) {
        stepToNextEventType(FormEntryController.EVENT_PROMPT_NEW_REPEAT);
        promptDepth = getFormIndex().getDepth();
    }
}
Also used : FormIndex(org.javarosa.core.model.FormIndex)

Aggregations

FormIndex (org.javarosa.core.model.FormIndex)63 GroupDef (org.javarosa.core.model.GroupDef)11 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)11 FormController (org.odk.collect.android.javarosawrapper.FormController)11 ArrayList (java.util.ArrayList)10 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)10 Test (org.junit.Test)10 IFormElement (org.javarosa.core.model.IFormElement)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)6 File (java.io.File)5 FormController (org.odk.collect.android.logic.FormController)5 HierarchyElement (org.odk.collect.android.logic.HierarchyElement)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 HierarchyListAdapter (org.odk.collect.android.adapters.HierarchyListAdapter)4 FormDef (org.javarosa.core.model.FormDef)3 FormEntryModel (org.javarosa.form.api.FormEntryModel)3 Intent (android.content.Intent)2 ObjectInputStream (java.io.ObjectInputStream)2 HashMap (java.util.HashMap)2