from otree.api import *
import random
doc = """
This app is intended to facilitate the delivery of discrete choice experiments & conjoint analysis
by offering the capability to generate randomized choice sets
"""
#Note for DA, it may be problematic if player prefer Vendor A, but select vendor B more turstworthy
class C(BaseConstants):
NAME_IN_URL = 'DCE'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 5
vendors = ['Vendor A', 'Vendor B']
Trust_level = [
'Not at all trustworthy',
'Not trustworthy',
'Neutral',
'Trustworthy',
'Extremely trustworthy'
]
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
attribute1level1 = models.StringField()
attribute2level1 = models.StringField()
attribute3level1 = models.StringField()
attribute4level1 = models.StringField()
attribute1level2 = models.StringField()
attribute2level2 = models.StringField()
attribute3level2 = models.StringField()
attribute4level2 = models.StringField()
choices = models.StringField(label="Which external professional learning vendor would you prefer?",
choices=C.vendors,
widget=widgets.RadioSelectHorizontal) # actual choice between choice 1 or choice 2
rating1 = models.StringField(label="How trustworthy do you find Vendor A?",
choices=C.Trust_level, widget=widgets.RadioSelectHorizontal)
rating2 = models.StringField(label="How trustworthy do you find Vendor B?",
choices=C.Trust_level, widget=widgets.RadioSelectHorizontal)
firstrating = models.StringField()
secondrating = models.StringField()
# PAGES
class Choice(Page):
form_model = "player"
form_fields = ["choices", "rating1", "rating2"]
@staticmethod
def vars_for_template(player: Player):
attribute1 = ['
',
'
',
'
',
'
',
'
']
attribute2 = ['
',
'
',
'
',
'
',
'
']
attribute3 = ['
',
'
',
'
',
'
',
'
']
attribute4 = ['
',
'
',
'
',
'
',
'
']
# manually adjust below to reflect listed attributes above
ratingorder = random.randint(1,2)
if ratingorder == 1:
fund1 = "Vendor A"
fund2 = "Vendor B"
else:
fund1 = "Vendor B"
fund2 = "Vendor A"
player.firstrating = fund1
player.secondrating = fund2
choiceuniverse = [attribute1, attribute2, attribute3, attribute4] ##create a list containing all possible attributes & levels
choiceset = []
for i in choiceuniverse:
rand = random.randint(0,(len(i)-1))
selected = i[rand]
choiceset.append(selected)
for i in choiceuniverse:
rand = random.randint(0,(len(i)-1))
selected = i[rand]
choiceset.append(selected)
player.attribute1level1 = choiceset[0]
player.attribute1level2 = choiceset[1]
player.attribute2level1 = choiceset[2]
player.attribute2level2 = choiceset[3]
player.attribute3level1 = choiceset[4]
player.attribute3level2 = choiceset[5]
player.attribute4level1 = choiceset[6]
player.attribute4level2 = choiceset[7]
result = {"attribute1level1": choiceset[0],
"attribute1level2": choiceset[1],
"attribute2level1": choiceset[2],
"attribute2level2": choiceset[3],
"attribute3level1": choiceset[4],
"attribute3level2": choiceset[5],
"attribute4level1": choiceset[6],
"attribute4level2": choiceset[7],
"firstfund": fund1,
"secondfund": fund2}
return result
page_sequence = [Choice]