Skip to content

faq002_finalpage dynamic

Johannes Brachem edited this page Apr 29, 2019 · 1 revision

How can I define different final pages for my Alfred experiment, depending e.g. on the answers of participants to specific questions?

If you already know how to create dynamic content in Alfred, this one is quite straightforward:

  1. Define a custom class in Section 3 that creates a dynamic page, depending on your chosen conditions. Lets say that class is called FinalQuestion.
  2. Use this class to define a page in Section 4. Lets say you call that page p_fin.
  3. Add the command exp.questionController.appendItemToFinishQuestionGroup(p_fin) to the definition of the method generate_experiment in Section 4. If your page has a different name, make sure to enter the correct name instead of p_fin.

This is a minimal example of what your Sections 3 and 4 would look like with a dynamic customized final page. In this example, the content of the final page depends participants' answer to the informed consent question.

#################################
### Section 3: Custom classes ###
#################################


class FinalQuestion(CompositeQuestion):
    def onShowingWidget(self):
        consent = self._experiment.dataManager.findExperimentDataByUid('p1')['consent']

        if consent == 1:
            p_fin_text = TextElement("Text if consent is equal to 1.")

        elif consent == 2:
            p_fin_text = TextElement("Text if consent is equal to 2.")

        self.addElements(p_fin_text)


########################################
### Section 4: Experiment generation ###
########################################

class Script(object):
    def generate_experiment(self):
        exp = Experiment('web', expName, expVersion)

        # --- FIRST PAGE --- #
        p1 = CompositeQuestion(title=u"Hello World", uid="p1")
        p1_text = TextElement("This is a minimal example.", alignment="center")
        p1_consent = SingleChoiceElement(u"Do you agree?",
                                         itemLabels=["Yes", "No"], name="consent")

        p1.addElements(p1_text, p1_consent)

        # --- LAST PAGE --- #
        p_fin = FinalQuestion(title=u"Title of last page.")


        # --- EXPERIMENT CONTROLS --- #
        # ------------------------------------------------------------------- #
        exp.questionController.appendItems(p1)

        exp.questionController.appendItemToFinishQuestionGroup(p_fin)

        return exp


generate_experiment = Script().generate_experiment