def next_exercise():
    assignment = get_assignment(session['current_assignment_id'])
    total_exercises = DEFAULT_E_NUM
    answered_exercises = session.get('answered_exercises', 0)

    if 'answered_exercises' not in session or not session['answered_exercises']:
        session['answered_exercises'] = answered_exercises
        session['total_exercises'] = total_exercises
        session['exercises_correct'] = 0
        session['exercises_incorrect'] = 0

    if not session['is_reading'] and session['answered_exercises'] == 6:
        return redirect('{0}?{1}'.format(url_for('exp.reading'),
                                         'status=complete'))

    if session['answered_exercises'] == total_exercises:
        return redirect('{0}?{1}'.format(url_for('exp.reading'),
                                         'status=complete'))

    if 'last_answer' in session and session['last_answer']:
        exercise = get_exercise(session['current_exercise_id'])
    else:

        session['is_reading'] = False

        exercise = get_assignment_next_exercise(assignment)
        session['current_exercise_id'] = exercise.id
        session['exercise_start'] = datetime.utcnow()

    return render_template('exercise.html',
                           feedback=False,
                           total_exercises=total_exercises,
                           answered_exercises=answered_exercises,
                           exercise=exercise.data['simple_question'])
def show_feedback():
    if request.method == 'POST' or ('skip_feedback' in session
                                    and session['skip_feedback']):
        session['current_exercise_id'] = None
        session['exercise_start'] = None
        session['last_answer'] = None
        return redirect(url_for('exp.next_exercise'))

    assignment = get_assignment(session['current_assignment_id'])
    answered_exercises = session['answered_exercises']
    total_exercises = session['total_exercises']
    exercise = get_exercise(session['current_exercise_id'])
    choices = exercise.data['simple_question']['answer_choices']
    correct_choices = [
        i for i, c in enumerate(choices) if float(c['credit']) > 0
    ]
    incorrect_choices = [
        i for i, c in enumerate(choices) if float(c['credit']) == 0
    ]

    answer = session['last_answer']
    correct_answer = answer in correct_choices

    return render_template('exercise.html',
                           feedback=True,
                           answer=answer,
                           correct_answer=correct_answer,
                           answered_exercises=answered_exercises,
                           total_exercises=total_exercises,
                           correct_choices=correct_choices,
                           incorrect_choices=incorrect_choices,
                           exercise=exercise.data['simple_question'])
def finalize():
    form = FinalizeForm()
    assignment = get_assignment(session['current_assignment_id'])
    if form.validate_on_submit():
        form.populate_obj(assignment)
        # TODO: move this to its own function
        assignment_results = dict()
        correct = session['exercises_correct']
        incorrect = session['exercises_incorrect']
        total = session['total_exercises']
        score = ((float(correct)) / total) * 100

        assignment_results['correct'] = correct
        assignment_results['incorrect'] = incorrect
        assignment_results['total'] = total
        assignment_results['score'] = score
        assignment_results['phase'] = assignment.assignment_phase

        log.info('A score of {0} was recorded for subject {1}'.format(
            score,
            assignment.subject_id))

        assignment.mturk_completion_code = id_generator()
        assignment.is_complete = True
        assignment.completed_on = datetime.utcnow()
        assignment.assignment_results = assignment_results
        db.session.add(assignment)
        save_session_record(assignment.id, 'Completed')

        db.session.commit()

        return redirect(url_for('exp.summary'))

    return render_template('exam_finalize.html', form=form)
def timed_out():
    if request.method == 'POST':
        posted = request.get_json()
        assignment_id = posted['assignment_id']
        assignment = get_assignment(session['current_assignment_id'])
        if assignment:
            assignment.did_timeout = True
            db.session.add(assignment)
            save_session_record(assignment.id, 'Finalizing')
            db.session.commit()
            return jsonify(
                dict(success=True,
                     message='User set to timed_out successfully'))
        else:
            raise ExperimentError('no_assignment')
    elif request.method == 'GET':
        assignment = get_assignment(session['current_assignment_id'])
        assignment.did_timeout = True
        db.session.add(assignment)
        save_session_record(assignment.id, 'Finalizing')
        db.session.commit()
    return render_template('timeout.html')
def finalize():
    from jobs import (assign_worker_qualification,
                      schedule_check_for_start_assessment)
    PART_2_QUAL = app.config['MTURK_PT2_QUALIFICATION']
    DLOGIC_QUAL = app.config['MTURK_DL_QUALIFICATION']

    form = FinalizeForm()
    assignment = get_assignment(session['current_assignment_id'])
    if form.validate_on_submit():
        form.populate_obj(assignment)
        subject = get_subject_by_user_id(current_user.id)
        # TODO: move this to its own function
        assignment_results = dict()
        correct = session['exercises_correct']
        incorrect = session['exercises_incorrect']
        total = session['total_exercises']
        score = ((float(correct)) / total) * 100

        assignment_results['correct'] = correct
        assignment_results['incorrect'] = incorrect
        assignment_results['total'] = total
        assignment_results['score'] = score
        assignment_results['phase'] = assignment.assignment_phase

        __logs__.info('A score of {0} was recorded for subject {1}'.format(
            score, assignment.subject_id))

        assignment.mturk_completion_code = id_generator()
        assignment.is_complete = True
        assignment.assignment_results = assignment_results
        assignment.completed_on = datetime.utcnow()
        db.session.add(assignment)
        save_session_record(assignment.id, 'Completed')

        db.session.commit()

        qualified = qualify_assignment(assignment.id)

        if qualified:
            assign_worker_qualification(PART_2_QUAL, subject.mturk_worker_id,
                                        1, True)
            schedule_periodic_check_for_start_assessment(subject.id)

        assign_worker_qualification(DLOGIC_QUAL, subject.mturk_worker_id, 1,
                                    True)

        return redirect(url_for('exp.summary'))

    return render_template('finalize.html', form=form)
def summary():
    # TODO: Finish summary page
    qualified = False

    assignments = all_subject_assignments(session['current_subject_id'])

    current_assignment = get_assignment(session['current_assignment_id'])

    completion_code = current_assignment.mturk_completion_code

    if (current_assignment.assignment_phase == 'Practice'
            and current_assignment.mturk_assignment_status == 'Accepted'):
        qualified = True

    return render_template('summary.html',
                           assignments=assignments,
                           completion_code=completion_code,
                           qualified=qualified)
def prediction_task():
    form = PredictionsForm(request.form)
    assignment = get_assignment(session['current_assignment_id'])
    if request.method == 'GET':
        exp_session = get_current_assignment_session()
        if exp_session.status is not 'Predicting':
            save_session_record(assignment.id, 'Predicting')
    if form.validate_on_submit():
        prediction_results = dict(assignment_id=assignment.id,
                                  basic_recall=form.basics.data,
                                  expressions=form.expressions.data,
                                  circuits=form.circuits.data,
                                  truth_tables=form.truth_tables.data,
                                  overall=form.overall.data
                                  )
        save_assignment_predictions(assignment.id, prediction_results)
        save_session_record(assignment.id, 'Assessment')
        return redirect(url_for('exam.next_exercise'))

    return render_template('predicting.html', form=form)
def submit_response():
    exercise = get_exercise(session['current_exercise_id'])
    assignment = get_assignment(session['current_assignment_id'])
    started_on = session['exercise_start']

    answer = request.form.get('choice', None)

    if answer:
        session['last_answer'] = int(answer)
        credit = float(
            exercise.data['simple_question']['answer_choices'][int(answer)][
                'credit'])

        save_assignment_response(assignment.id, exercise.id, credit, answer,
                                 started_on)
        if credit == 0:
            session['exercises_incorrect'] += 1
        else:
            session['exercises_correct'] += 1

        session['answered_exercises'] += 1
    return redirect(url_for('exam.next_exercise'))
def next_exercise():
    assignment = get_assignment(session['current_assignment_id'])
    total_exercises = len(assignment.exercise_pool)
    answered_exercises = session.get('answered_exercises', 0)

    if 'answered_exercises' not in session or not session['answered_exercises']:
        session['answered_exercises'] = answered_exercises
        session['total_exercises'] = total_exercises
        session['exercises_correct'] = 0
        session['exercises_incorrect'] = 0

    elif session['answered_exercises'] == total_exercises:
        return redirect(url_for('exam.finalize'))

    exercise = get_assignment_next_exercise(assignment)
    session['current_exercise_id'] = exercise.id
    session['exercise_start'] = datetime.utcnow()

    return render_template('exam_exercise.html',
                           feedback=False,
                           total_exercises=total_exercises,
                           answered_exercises=answered_exercises,
                           exercise=exercise.data['simple_question'])
Beispiel #10
0
def test_get_assignment(db):
    assignment_id = 1
    assignment = get_assignment(assignment_id)
    assert assignment.mturk_assignment_id == 'debug45678'