from otree.api import * doc = """ Part1b Instructions """ class C(BaseConstants): NAME_IN_URL = 'Part1b_Instructions' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): Question1 = models.StringField( label='Is the number of tokens in the pot independent of the number of tokens in the pot in any other part?', choices=['Yes', 'No'], widget=widgets.RadioSelect, ) Question2 = models.StringField( label='The warning system functions correctly in... ', choices=['... 0 out of 10 cases', '... 1 out of 10 cases', '... 2 out of 10 cases', '... 3 out of 10 cases', '... 4 out of 10 cases', '... 5 out of 10 cases', '... 6 out of 10 cases', '... 7 out of 10 cases', '... 8 out of 10 cases', '... 9 out of 10 cases', '... 10 out of 10 cases'], widget=widgets.RadioSelect, ) Question3 = models.StringField( label='This is an attention check, please select the top option. Are you a person that likes cats?', choices=['Yes', 'No', 'Maybe','Irrelevant'], widget=widgets.RadioSelect, ) Question4 = models.StringField( label='When you receive a warning, do you learn exactly how many tokens are in the pot?', choices=['Yes', 'No'], widget=widgets.RadioSelect, ) blength = models.IntegerField() noise = models.BooleanField() class IncorrectResponse(ExtraModel): player = models.Link(Player) field_name = models.StringField() response = models.StringField() def range_def(player): player.blength = player.in_round(1).participant.vars.get('box_length', None) player.noise = player.in_round(1).participant.vars.get('noise', None) return{ 'length': player.blength,'noise': player.noise } # PAGES class Start(Page): pass class Rules(Page): @staticmethod def vars_for_template(player): player.participant.vars['length','noise'] = range_def(player) class Quiz(Page): form_model = 'player' form_fields = ['Question1', 'Question2', 'Question3', 'Question4'] @staticmethod def error_message(player, values): if player.noise == True: solutions = dict(Question1='Yes', Question2='... 8 out of 10 cases', Question3='Yes', Question4='No') else: solutions = dict(Question1='Yes', Question2='... 10 out of 10 cases', Question3='Yes', Question4='No') errors = {f: 'This answer is not correct.' for f in solutions if values[f] != solutions[f]} if errors: for name in errors: response = values[name] IncorrectResponse.create(player=player, field_name=name, response=str(response)) return errors class Quizsuccess(Page): pass page_sequence = [Start, Rules, Quiz, Quizsuccess] def custom_export(players): """For data export page""" yield ['participant_code', 'id_in_session', 'round_number', 'field_name', 'response'] responses = IncorrectResponse.filter() for resp in responses: player = resp.player participant = player.participant yield [participant.code, participant.id_in_session, player.round_number, resp.field_name, resp.response]