from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants, compute_rows, return_all_vals from django.http import HttpRequest # from django.utils.translation import ugettext_lazy as _ class Instructions(Page): form_model = 'player' def get_form_fields(self): fields = ['question1', 'question2', 'question3', 'question4', 'question5', 'question6', 'question7', 'question8'] if self.session.config['current_treatment'] == 2: fields.append('question9') elif self.session.config['current_treatment'] == 3: fields.append('question10') return fields def is_displayed(self): return self.round_number == 1 and Constants.instructions def get_timeout_seconds(self): if 'timed_out' in self.participant.vars: return 0 return 60 * Constants.timer_minutes_instructions def vars_for_template(self): string_num_1, string_num_2, preferences_row, priorities_row, students, schools, \ ex_preferences_row, ex_priorities_row, ex_preferences_row_waived, ex_students, ex_schools = return_all_vals() return {'point': c(1), 'exchange_rate': str(c(1).to_real_world_currency(self.session)), 'participant_fee': str(self.session.config['participation_fee']), 'treatment': self.session.config['current_treatment'], 'rounds': Constants.num_rounds, 'stringnum1': string_num_1, 'stringnum2': string_num_2, 'students': students, 'preferences_row': preferences_row, 'schools': schools, 'priorities_row': priorities_row, 'ex_students': ex_students, 'ex_schools': ex_schools, 'ex_preferences_row': ex_preferences_row, 'ex_preferences_row_waived': ex_preferences_row_waived, 'ex_priorities_row': ex_priorities_row, 'soft_timer': Constants.soft_timer_seconds_instructions * 1000, 'timer_message': "The time is about to run out. Please make a choice."} def error_message(self, values): count = 0 errors = [] equal_fields = 8 for i in range(0, equal_fields): if int(values[self.get_form_fields()[i]]) == int(Constants.expected_control_answer[i]): count += 1 else: errors.append("Bitte berichtigen Sie Ihre Antwort auf Frage " + str(i + 1) + ".") if self.session.config['current_treatment'] == 2: if int(values[self.get_form_fields()[8]]) == int(Constants.expected_control_answer[8]): count += 1 else: errors.append("Bitte berichtigen Sie Ihre Antwort auf Frage 9.") if self.session.config['current_treatment'] == 3: if int(values[self.get_form_fields()[8]]) == int(Constants.expected_control_answer[9]): count += 1 else: errors.append("Bitte berichtigen Sie Ihre Antwort auf Frage 9.") if count != len(self.get_form_fields()): return errors def before_next_page(self): if self.timeout_happened: self.player.question1 = None self.player.question2 = None self.player.question3 = None self.player.question4 = None self.player.question5 = None self.player.question6 = None self.player.question7 = None self.player.question8 = None self.player.question9 = None self.player.question10 = None self.participant.vars['timed_out'] = True class Survey(Page): form_model = 'player' form_fields = ['first_choice', 'second_choice', 'third_choice', 'fourth_choice', 'fifth_choice', 'consent'] timer_text = "Please make a choice within: " def get_timeout_seconds(self): if 'timed_out' in self.participant.vars: return 0 return 60 * Constants.timer_minutes_survey def vars_for_template(self): string_num_1, string_num_2, preferences_row, priorities_row, students, schools, ex_preferences_row, \ ex_priorities_row, ex_preferences_row_waived, ex_students, ex_schools = return_all_vals() return {'point': c(1), 'exchange_rate': str(c(1).to_real_world_currency(self.session)), 'participant_fee': str(self.session.config['participation_fee']), 'treatment': self.session.config['current_treatment'], 'rounds': Constants.num_rounds, 'stringnum1': string_num_1, 'stringnum2': string_num_2, 'type': self.player.id_in_group, 'students': students, 'preferences_row': preferences_row, 'schools': schools, 'priorities_row': priorities_row, 'ex_students': ex_students, 'ex_schools': ex_schools, 'ex_preferences_row': ex_preferences_row, 'ex_preferences_row_waived': ex_preferences_row_waived, 'ex_priorities_row': ex_priorities_row, 'additional_table': Constants.additional_table, 'round': self.round_number, 'soft_timer': Constants.soft_timer_seconds_survey * 1000, 'dropped_out': self.player.check_droupped_out(), 'timer_message': "The time is about to run out. Please make a choice." } def error_message(self, values): total_string = '' for i in range(0, len(self.form_fields) - 1): # -1 because we don't want 'consent' total_string += values[self.form_fields[i]] for school in Constants.priorities: if school not in total_string: return 'Each school can be selected only once.' def before_next_page(self): if self.timeout_happened: self.participant.vars['timed_out'] = True self.player.first_choice = 'A' self.player.second_choice = 'B' self.player.third_choice = 'C' self.player.fourth_choice = 'D' self.player.fifth_choice = 'E' # TODO: Do the timed out ones consent by default? self.player.consent = 1 # Set consent variables default_consent = False current_treatment = self.session.config['current_treatment'] if current_treatment == 3 or current_treatment == 4: default_consent = True if self.player.consent == 1 and current_treatment > 1: self.participant.vars['tick_one_round'] = True self.participant.vars['given_consent'] = not default_consent else: self.participant.vars['given_consent'] = default_consent # Save the preferences self.participant.vars['preferences'] = [self.player.first_choice, self.player.second_choice, self.player.third_choice, self.player.fourth_choice, self.player.fifth_choice] class OutcomeWaitPage(WaitPage): def after_all_players_arrive(self): self.group.set_payoffs() class Outcome(Page): timer_text = "Please continue within: " def get_timeout_seconds(self): if 'timed_out' in self.participant.vars: return 0 return 60 * Constants.timer_minutes_result def vars_for_template(self): schools = self.participant.vars['school_allocation'] points = self.participant.vars['points_allocation'] schools_points = zip(schools, points) school = schools[self.player.id_in_group - 1] index_preference = {} for i in range(0, len(self.participant.vars['preferences'])): index_preference[Constants.positional_array[i]] = self.participant.vars['preferences'][i] return {'round': self.player.round_number, 'type': self.player.id_in_group, 'payoff': self.player.payoff, 'preferences': index_preference, 'assigned': school, 'school_points_allocation': schools_points} def before_next_page(self): if 'timed_out' in self.participant.vars: self.player.payoff = 0 class Demographics(Page): form_model = 'player' def is_displayed(self): return self.round_number == Constants.num_rounds and 'timed_out' not in self.participant.vars def get_form_fields(self): fields = ['gender', 'age', 'study', 'truthful', 'comments'] if self.session.config['current_treatment'] == 3 and 'tick_one_round' in self.participant.vars: fields.append('object_reason') fields.append('reason_free_text') fields.append('object_other') elif self.session.config['current_treatment'] == 2 and 'tick_one_round' in self.participant.vars: fields.append('consent_reason') fields.append('reason_free_text') fields.append('consent_other') return fields def error_message(self, values): fields = self.get_form_fields() if 'reason_free_text' in fields: if 'object_reason' in fields and values['object_reason'] == 4 and values['reason_free_text'] == '': return "Please add a reason." if 'consent_reason' in fields and values['consent_reason'] == 4 and values['reason_free_text'] == '': return "Please add a reason." def vars_for_template(self): show_consent = ((self.session.config['current_treatment'] == 3 or self.session.config[ 'current_treatment'] == 2) and 'tick_one_round' in self.participant.vars) return {'treatment': self.session.config['current_treatment'], 'show_consent': show_consent, } def before_next_page(self): self.player.draw_sample() two_rounds = self.player.participant.vars['drawn_sample'] payoff1 = self.player.participant.vars['possible_payoffs'][two_rounds[0]] payoff2 = self.player.participant.vars['possible_payoffs'][two_rounds[1]] self.participant.payoff = payoff1 + payoff2 self.player.participant_vars_dump = str(self.participant.vars) self.player.session_vars_dump = str(self.session.vars) class Results(Page): def is_displayed(self): return self.round_number == Constants.num_rounds and Constants.results and \ 'timed_out' not in self.participant.vars def vars_for_template(self): two_rounds = self.player.participant.vars['drawn_sample'] payoff1 = self.player.participant.vars['possible_payoffs'][two_rounds[0]] payoff2 = self.player.participant.vars['possible_payoffs'][two_rounds[1]] payoff_euros = self.participant.payoff.to_real_world_currency(self.session) total_euros = self.participant.payoff_plus_participation_fee() return {'type': self.player.id_in_group, 'round1': two_rounds[0] + 1, 'payoff1': payoff1, 'round2': two_rounds[1] + 1, 'payoff2': payoff2, 'sum': self.participant.payoff, 'payoff_euros': payoff_euros, 'participant_fee': str(self.session.config['participation_fee']), 'total_euros': total_euros } class TimedOut(Page): def is_displayed(self): return self.round_number == Constants.num_rounds and 'timed_out' in self.participant.vars def vars_for_template(self): iban = True if 'no_iban' in self.participant.vars: iban = False return {'iban': iban} page_sequence = [ Instructions, Survey, OutcomeWaitPage, Outcome, Demographics, Results, TimedOut ]