from otree.api import * import time import random import math from decimal import Decimal import psycopg2 class C(BaseConstants): NAME_IN_URL = 'lev_o' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Group(BaseGroup): pass class Subsession(BaseSubsession): pass class Player(BasePlayer): condition = models.IntegerField() worker_id = models.CharField(initial='e') mturk_dupe = models.IntegerField(initial=0) total_bonus = models.StringField() total_compensation = models.StringField(default='Not Finished') informed_consent = models.StringField(initial=None, choices=[('Yes', 'Yes'), ('No', 'No')], verbose_name='', widget=widgets.RadioSelect()) kc1 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc2 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc3 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc4 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc5 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc6 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) kc7 = models.StringField(initial=None, choices=['True', 'False'], verbose_name='', widget=widgets.RadioSelect()) control_decision = models.IntegerField() control_decision_time = models.StringField() #"Pre" PEQ expected_cooperation = models.StringField(initial=None, choices=[('1', '1 = I do not expect any Employees to ever allocate high ' 'resources to their joint project.'), ('2', '2 '), ('3', '3 '), ('4', '4 = I expect at least some Employees to allocate high ' 'resources to their joint project at least some of the time.'), ('5', '5 '), ('6', '6 '), ('7', '7 = I expect all Employees to allocate high resources to ' 'their joint project all of the time.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) inv_expected_cooperation = models.StringField(initial=None, choices=[('1', '1 = I do not expect any Employees to ever allocate high ' 'resources to their joint project.'), ('2', '2 '), ('3', '3 '), ('4', '4 = I expect at least some Employees to allocate high ' 'resources to their joint project at least some of the time.'), ('5', '5 '), ('6', '6 '), ('7', '7 = I expect all Employees to allocate high resources to ' 'their joint project all of the time.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) #PEQ trust_managers = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) trust_employees = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) exit_team = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) teammate_employees = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) teammate_managers = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_autonomy = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_coop_right = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_coop_me = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_coop_mgr = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_coop_payoff = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) control_fairness = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) emp_want_control = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) mgr_want_control = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) control_motivation = models.StringField(verbose_name='') control_likelihood = models.StringField(initial=None, choices=[('1', '1 = Definitely would not.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Unsure.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Definitely would.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_1 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_2 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_3 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_4 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_5 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_6 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_7 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) prop_trust_8 = models.StringField(initial=None, choices=[('1', '1 = Strongly disagree.'), ('2', '2 '), ('3', '3 '), ('4', '4 = Neither agree nor disagree.'), ('5', '5 '), ('6', '6 '), ('7', '7 = Strongly agree.')], verbose_name='', widget=widgets.RadioSelectHorizontal()) gender = models.StringField(initial=None, choices=[('a', 'a. Male'), ('b', 'b. Female'), ('c', 'c. Non-binary'), ('d', 'd. Other'), ('e', 'e. Prefer not to answer')], verbose_name='', widget=widgets.RadioSelect()) age = models.IntegerField(verbose_name='', min=18, max=110) work_experience = models.IntegerField(verbose_name='', min=0) education = models.CharField(initial=None, choices=[('1', 'Less than high school'), ('2', 'High school graduate'), ('3', 'Some college'), ('4', '2 year degree'), ('5', '4 year degree'), ('6', 'Graduate degree')], verbose_name='') class OnlineWorkers(ExtraModel): worker_id = models.CharField(max_length=50) #FUNCTIONS def creating_session(subsession): session = subsession.session for player in subsession.get_players(): player.condition = session.config['condition'] player.participant.label = 'e' # PAGES class IC(Page): form_model = 'player' form_fields = ['informed_consent'] def before_next_page(player, timeout_happened): # See if that worker ID already appears in players, in which case they at least started the experiment if player.participant.label != 'e': worker_match = [w for w in OnlineWorkers.filter() if (w.worker_id == player.participant.label)] if not worker_match: OnlineWorkers.create(worker_id=player.participant.label) player.worker_id = player.participant.label else: player.mturk_dupe = 1 class IC_Decline(Page): @staticmethod def is_displayed(player: Player): return player.informed_consent == "No" class DuplicateWorker(Page): @staticmethod def is_displayed(player: Player): return player.mturk_dupe == 1 class GenInstructions(Page): pass class TaskInstructions1(Page): pass class TaskInstructions2(Page): pass class KnowledgeCheck(Page): form_model = 'player' form_fields = ['kc1', 'kc2', 'kc3', 'kc4', 'kc5', 'kc6', 'kc7'] class Decision(Page): form_model = 'player' form_fields = ['control_decision', 'control_decision_time'] @staticmethod def is_displayed(player): return player.condition == 1 class PostTask1(Page): pass class PEQ1(Page): form_model = 'player' form_fields = ['expected_cooperation', 'inv_expected_cooperation'] class PEQ2(Page): form_model = 'player' @staticmethod def get_form_fields(player): if player.condition > 1: return ['trust_managers', 'trust_employees', 'exit_team', 'teammate_employees', 'teammate_managers', 'emp_autonomy', 'emp_coop_right', 'emp_coop_me', 'emp_coop_mgr', 'emp_coop_payoff', 'control_fairness', 'emp_want_control', 'mgr_want_control', 'control_motivation', 'control_likelihood'] else: return ['trust_managers', 'trust_employees', 'exit_team', 'teammate_employees', 'teammate_managers', 'emp_autonomy', 'emp_coop_right', 'emp_coop_me', 'emp_coop_mgr', 'emp_coop_payoff', 'control_fairness', 'emp_want_control', 'mgr_want_control', 'control_motivation' ] class PEQ3(Page): form_model = 'player' form_fields = ['prop_trust_1', 'prop_trust_2', 'prop_trust_3', 'prop_trust_4', 'prop_trust_5', 'prop_trust_6', 'prop_trust_7', 'prop_trust_8', 'age', 'gender', 'education', 'work_experience'] class WkCheck(Page): # Need to check to see that all workers assigned to this manager are done with stage 2. @staticmethod def is_displayed(player): if player.workers_ready == 0: return True else: return False @staticmethod def before_next_page(player, timeout_happened): conn = psycopg2.connect(database="d843cclkr6ovgn", user="uigdr2bmq9irl", password="pc314696db376127251d8f370b2d74d5c429ca12e964769f21ac05c08fc6b7ef6", host="cf9gid2f6uallg.cluster-czrs8kj4isg7.us-east-1.rds.amazonaws.com", port=5432) #conn = sqlite3.connect("db.sqlite3") cursor = conn.cursor() # Might as well have managers wait for all participants to be done with both stages before exit. cursor.execute('select distinct e.participant_id from knowledge_of_effort_employee_s2_player e where ' 'e.active_flag = 1 and (e.s1_contribution = -1 or e.s2_contribution = -1) union all ' 'select distinct e.participant_id from knowledge_of_effort_employee_s2_lh_player e where ' 'e.active_flag = 1 and (e.s1_contribution = -1 or e.s2_contribution = -1)') unfinished_worker = cursor.fetchone() if unfinished_worker is None: player.workers_ready = 1 ##################################### #Need to pull from s2_lh_player table as well for the low/no/high conditions. cursor.execute('select sum(s1_team_contr) as total_s1, sum(s2_team_contr) as total_s2, sum(num_groups) ' 'as total_groups from (select sum(e.s1_contribution) as s1_team_contr, ' 'sum(e.s2_contribution) as s2_team_contr, count(distinct e.group_id) as num_groups from ' 'knowledge_of_effort_employee_s2_player e where e.manager_id = ' + str(player.participant_id) + ' union all select sum(e.s1_contribution) as s1_team_contr, sum(e.s2_contribution) ' 'as s2_team_contr, count(distinct e.team_id) as num_groups from ' 'knowledge_of_effort_employee_s2_lh_player e where e.manager_id = ' + str(player.participant_id) +')') mgr_results = cursor.fetchone() player.s1_team_contribution = mgr_results[0] player.s2_team_contribution = mgr_results[1] num_groups = float(mgr_results[2]) if player.s1_team_contribution / (num_groups/float(3.0)) > float(0.33): player.s1_bonus = 1.00 else: player.s1_bonus = 0.00 if player.s2_team_contribution / (num_groups/float(3.0)) > float(0.33): player.s2_bonus = 1.00 else: player.s2_bonus = 0.00 player.s1_pay = 2.00 + player.s1_bonus player.s2_pay = 2.00 + player.s2_bonus player.total_pay = player.s1_pay + player.s2_pay cursor.close() conn.commit() conn.close() class ExitResults(Page): pass page_sequence = \ [ IC, IC_Decline, DuplicateWorker, GenInstructions, TaskInstructions1, TaskInstructions2, KnowledgeCheck, Decision, PEQ1, PEQ2, PEQ3, ExitResults ]