views.py 9.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33



import math
from datetime import datetime

from flask import (
    Flask, 
    render_template, 
    request, 
    session, 
    flash, 
    redirect, 
    url_for, 
    Blueprint
)

from sqlalchemy import and_ 
from flask_babel import _, lazy_gettext as _l

from app import db
from app.models import experiment
from app.models import page, question
from app.models import answer_set, answer 
from app.models import user, trial_randomization
from app.forms import Answers, TaskForm, ContinueTaskForm

task_blueprint = Blueprint("task", __name__, 
                template_folder='templates',
                static_folder='static',
                url_prefix='/task')


34
def get_randomized_page(pages):
35
36
37
38
39
40

    #this variable is feeded to the template as empty if trial randomization is set to "off"
    randomized_stimulus = ""

    #if trial randomization is on we will still use the same functionality that is used otherwise
    #but we will pass the randomized pair of the page_id from trial randomization table to the task.html
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    randomized_page_id = trial_randomization.query.filter(and_(
            trial_randomization.answer_set_idanswer_set==session['answer_set'], 
            trial_randomization.page_idpage==pages.items[0].idpage
        )).first()

    return randomized_page_id


def add_slider_answer(key, value, randomized_page_id):
    '''Insert slider value to database. If trial randomization is set to 'Off' 
    the values are inputted for session['current_idpage']. Otherwise the values 
    are set for the corresponding id found in the trial randomization table'''

    page_idpage = session['current_idpage'] if session['randomization'] == 'Off' else randomized_page_id.randomized_idpage 
    participant_answer = answer(question_idquestion=key, answer_set_idanswer_set=session['answer_set'], answer=value, page_idpage=page_idpage)
    db.session.add(participant_answer)
    db.session.commit()


@task_blueprint.route('/<int:page_num>', methods=['POST'])
def task_answer(page_num):

63
    form = TaskForm(request.form)
64
    pages = page.query.filter_by(experiment_idexperiment=session['exp_id']).paginate(per_page=1, page=page_num, error_out=True)
65

66
    # TODO: determine wheter handling POST data from slider or embody!!!
67

68
    if form.validate():
69
70
71
        #Lets check if there are answers in database already for this page_id (eg. if user returned to previous page and tried to answer again)
        #If so flash ("Page has been answered already. Answers discarded"), else insert values in to db
        #this has to be done separately for trial randomization "on" and "off" situations        
72

73
        if session['randomization'] == 'On':
74
            randomized_page_id = get_randomized_page(pages)
75
            check_answer = answer.query.filter(and_(answer.answer_set_idanswer_set==session['answer_set'], answer.page_idpage==randomized_page_id.randomized_idpage)).first()
76
        else:
77
78
79
80
81
82
83
84
85
86
87
88
89
            check_answer = answer.query.filter(and_(answer.answer_set_idanswer_set==session['answer_set'], answer.page_idpage==session['current_idpage'])).first()

        if check_answer is None:
            the_time = datetime.now()
            the_time = the_time.replace(microsecond=0)
            
            update_answer_counter = answer_set.query.filter_by(idanswer_set=session['answer_set']).first()
            update_answer_counter.answer_counter = int(update_answer_counter.answer_counter) + 1 
            update_answer_counter.last_answer_time = the_time
            db.session.commit()
        
            data = request.form.to_dict()
            for key, value in data.items():
90
                add_slider_answer(key, value, randomized_page_id)
91
92
93
94
95
96

        else:
            flash("Page has been answered already. Answers discarded")
        
        page_num=pages.next_num
        
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        if not pages.has_next:
            return redirect ( url_for('task.completed'))

    return redirect( url_for('task.task', page_num=pages.next_num))


@task_blueprint.route('/<int:page_num>', methods=['GET'])
def task(page_num):

    experiment_info = experiment.query.filter_by(idexperiment=session['exp_id']).first()
    rating_instruction = experiment_info.single_sentence_instruction
    stimulus_size = experiment_info.stimulus_size
    
    #for text stimuli the size needs to be calculated since the template element utilises h1-h6 tags.
    #A value of stimulus size 12 gives h1 and value of 1 gives h6
    stimulus_size_text = 7-math.ceil((int(stimulus_size)/2))

    print(stimulus_size_text)
    
    pages = page.query.filter_by(experiment_idexperiment=session['exp_id']).paginate(per_page=1, page=page_num, error_out=True)
    progress_bar_percentage = round((pages.page/pages.pages)*100)

    #this variable is feeded to the template as empty if trial randomization is set to "off"
    randomized_stimulus = ""

    # if trial randomization is on we will still use the same functionality that is used otherwise
    # but we will pass the randomized pair of the page_id from trial randomization table to the task.html
    if session['randomization'] == 'On':

        #set the stimulus to be shown if randomization is on
        randomized_page_id = get_randomized_page(pages)
        randomized_stimulus = page.query.filter_by(idpage=randomized_page_id.randomized_idpage).first()
129
        
130
131
132
133
134
135
136
137
138
139
140
141
142
    for p in pages.items:
        session['current_idpage'] = p.idpage
    
    #slider set
    form = TaskForm(request.form)
    categories_and_scales = {}
    categories = question.query.filter_by(experiment_idexperiment=session['exp_id']).all()

    for cat in categories:    
        scale_list = [(cat.left, cat.right)]
        categories_and_scales[cat.idquestion, cat.question]  = scale_list
    
    form.categories1 = categories_and_scales
143

144
145
146
147
148
149
150
151
152
153
    return render_template(
                        'task.html', 
                        pages=pages, 
                        progress_bar_percentage=progress_bar_percentage, 
                        form=form, 
                        randomized_stimulus=randomized_stimulus, 
                        rating_instruction=rating_instruction, 
                        stimulus_size=stimulus_size, 
                        stimulus_size_text=stimulus_size_text
                        )
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


@task_blueprint.route('/completed')
def completed():
    
    session.pop('user', None)
    session.pop('exp_id', None)    
    session.pop('agree', None)
    session.pop('answer_set', None)
    session.pop('type', None)
    session.pop('randomization', None)

    return render_template('task_completed.html')


@task_blueprint.route('/continue', methods=['GET', 'POST'])
def continue_task():
    
    exp_id = request.args.get('exp_id', None)
    form = ContinueTaskForm()
    
    if form.validate_on_submit():
        
        #check if participant ID is found from db and that the answer set is linked to the correct experiment
        participant = answer_set.query.filter(and_(answer_set.session==form.participant_id.data, answer_set.experiment_idexperiment==exp_id)).first()
        if participant is None:
            flash('Invalid ID')
181
            return redirect(url_for('task.continue_task', exp_id=exp_id))        
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
        
        #flash('Login requested for participant {}'.format(form.participant_id.data))
        
        #if correct participant_id is found with the correct experiment ID; start session for that user
        session['exp_id'] = exp_id
        session['user'] = form.participant_id.data 
        session['answer_set'] = participant.idanswer_set
        mediatype = page.query.filter_by(experiment_idexperiment=session['exp_id']).first()
        
        rand = experiment.query.filter_by(idexperiment=session['exp_id']).first()
        
        session['randomization'] = rand.randomization
    
        if mediatype:
            session['type'] = mediatype.type
        else:
            flash('No pages or mediatype set for experiment')
            return redirect('/')

        #If participant has done just the registration redirect to the first page of the experiment        
        if participant.answer_counter == 0:
            #flash("Ei vastauksia ohjataan ekalle sivulle")
            return redirect( url_for('task', page_num=1))
        
        
        redirect_to_page = participant.answer_counter + 1
        
        #flash("redirect to page:")
        #flash(redirect_to_page)

        experiment_page_count = db.session.query(page).filter_by(experiment_idexperiment=session['exp_id']).count()
        
        #If participant has ansvered all pages allready redirect to task completed page
        if experiment_page_count == participant.answer_counter:
            
            return redirect( url_for('task.completed'))
        
        return redirect( url_for('task.task', page_num=redirect_to_page))
        
    return render_template('continue_task.html', exp_id=exp_id, form=form)


@task_blueprint.route('/quit')
def quit():
    
    user_id = session['user']
    session.pop('user', None)
    session.pop('exp_id', None)    
    session.pop('agree', None)
    session.pop('answer_set', None)
    session.pop('type', None)
    
    return render_template('quit_task.html', user_id=user_id)


# TODO: removable?
@task_blueprint.route('/create')
def create():
    return render_template('create_task.html')