Ejemplo n.º 1
0
def api_create_experiment(class_id):
    """
    JSON format:
    {"questions": ["Text1", "Text2", "Text3"],
     "mins": [0, 1, 1],
     "maxs": [5, 1, 3],
     "types": ["sociometric", "sociometric", "scale"]}

    """
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    info = request.json

    if not ('questions' in info and 'mins' in info and 'maxs' in info
            and 'type' in info):
        return "Malformed input", 400

    # if info['type'] not in ('sociometric', 'scale'):
    #     return "Wrong type", 400

    experiment_id = models.create_questionnaire(info['questions'],
                                                info['mins'], info['maxs'],
                                                class_id, info['type'])

    models.push_questionnaire(experiment_id, class_id)

    return jsonify(info)
Ejemplo n.º 2
0
def prepare_demo():

    allowed_user = int(-1)

    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    models.initialize()
    models.seed_data('demoseed.sql')

    # experiment_id = models.create_questionnaire(
    #     ["Who do you want to work with this week?", "Who is your best friend?"],
    #     [0, 1], [3, 1], 1,
    #     ["sociometric", "sociometric"]
    # )
    # models.push_questionnaire(experiment_id, 1)
    #
    # models.update_results(1, experiment_id, [[5], [6]])
    # models.update_results(2, experiment_id, [[3], [1]])
    # models.update_results(3, experiment_id, [[2], [5]])
    # models.update_results(4, experiment_id, [[2], [4]])
    # models.update_results(5, experiment_id, [[4], [3]])
    # models.update_results(6, experiment_id, [[2], [2]])
    # models.update_results(7, experiment_id, [[7], [2]])

    return "Database ready for demo", 201
Ejemplo n.º 3
0
def api_class_experiments(class_id):
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    experiments = models.get_experiments(class_id)

    experiments_dict = {'experiments': []}

    for exp in experiments:
        experiments_dict['experiments'].append({
            'id':
            exp[0],
            'info':
            models.json.loads(exp[1]),
            'replies':
            models.json.loads(exp[2])['replies'],
            'date_created':
            exp[4],
            'finished':
            exp[5],
        })

    return jsonify(experiments_dict)
Ejemplo n.º 4
0
def api_list_categories(teacher_id):
    allowed_user = int(teacher_id)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    categories = models.all_categories(teacher_id)

    cat_dict = utils.query_to_dict(categories, "categories", [(0, "id"),
                                                              (1, "name")])

    return jsonify(cat_dict)
Ejemplo n.º 5
0
def api_create_category(teacher_id):
    allowed_user = int(teacher_id)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    info = request.json

    name = info['name']

    id_ = models.new_category(teacher_id, name)

    return str(id_), 201
Ejemplo n.º 6
0
def api_classes(teacher_id):
    allowed_user = int(teacher_id)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    classes = models.get_classes(teacher_id)

    classes_dict = utils.query_to_dict(classes, 'classes',
                                       [(0, 'id'), (1, 'name'),
                                        (3, 'description')])

    return jsonify(classes_dict), 201
Ejemplo n.º 7
0
def api_save_template(category_id):
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM template_categories WHERE id = ?",
        (category_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    content = json.dumps(request.json)
    # content = request.json
    models.save_template(category_id, content)
    return content, 201
Ejemplo n.º 8
0
def api_list_experiments(class_id):
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    experiments = models.list_experiments(class_id)
    # print(experiments)
    experiments_dict = utils.query_to_dict(experiments, "experiments",
                                           [(0, 'id'), (1, 'date_created')])

    return jsonify(experiments_dict)
Ejemplo n.º 9
0
def api_students(class_id):
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    students = models.get_students_in_class(class_id)

    students_dict = utils.query_to_dict(students, 'students', [(0, 'id'),
                                                               (1, 'name')])

    return jsonify(students_dict)
Ejemplo n.º 10
0
def api_experiment_replies(experiment_id):
    class_owner = models.run_simple_query(
        "SELECT c.teacher_id FROM classes c "
        "JOIN experiments e on c.id = e.class_id WHERE e.id = ?",
        (experiment_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    replies = models.get_teacher_experiment_replies(experiment_id)

    replies_dict = json.loads(replies)

    return jsonify(replies_dict)
Ejemplo n.º 11
0
def api_create_class(teacher_id):
    """
     JSON format:
     {"name": ..., "description": ...}
    """

    allowed_user = int(teacher_id)

    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    info = request.json

    class_id = models.create_class(info['name'], teacher_id,
                                   info['description'])

    message['details'] = "Class created with id %d" % class_id
    return jsonify(message), 201
Ejemplo n.º 12
0
def api_load_templates(category_id):
    class_owner = models.run_simple_query(
        "SELECT teacher_id FROM template_categories WHERE id = ?",
        (category_id, ))[0][0]

    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    templates = models.load_templates(category_id)

    res_dict = {"templates": []}

    for row in templates:
        part_dict = {"id": row[0], "template": json.loads(row[1])}
        res_dict["templates"].append(part_dict)

    return jsonify(res_dict)
Ejemplo n.º 13
0
def graph_api(experiment_id):
    # Authentication
    class_owner = models.run_simple_query(
        "SELECT c.teacher_id FROM classes c "
        "JOIN experiments e on c.id = e.class_id WHERE e.id = ?",
        (experiment_id, ))[0][0]
    allowed_user = int(class_owner)
    authorized, message = auth.authorize_teacher(allowed_user)
    if not authorized:
        return jsonify(message), 401

    # Get the question text
    questions = models.get_experiment_details(experiment_id)
    questions = json.loads(questions)

    # Get the class id and teacher's name
    teacher_query = """SELECT c.id, t.name FROM experiments e 
    JOIN classes c on e.class_id = c.id 
    JOIN teachers t on c.teacher_id = t.id
    WHERE e.id = ?"""
    class_id, teacher_name = models.run_simple_query(teacher_query,
                                                     (experiment_id, ))[0]

    # Get the raw replies
    replies = models.get_experiment_replies(experiment_id)
    replies = json.loads(replies)

    # Get the list of students for nodes
    students = models.get_students_in_class(class_id)
    students = utils.query_to_dict(students, 'nodes', [(0, 'id'), (1, 'label'),
                                                       (0, 'group')])
    # for student in students:
    #     student['shape'] = 'circularImage'
    #     student['image'] = ''

    response = {
        'teacher': teacher_name,
        'questions': [],
        'nodes': students['nodes']
    }

    for i, question in enumerate(questions['questions']):
        question_dict = {
            'text': question['text'],
            'type': question['type'],
            'edges': []
        }
        # print(question_dict)
        response['questions'].append(question_dict)

        # Bit of spaghetti, but builds the edges array
        for reply_dict in replies['replies']:
            question_reply = reply_dict['response'][i]
            if isinstance(question_reply, list):
                for reply in question_reply:
                    question_dict['edges'].append({
                        'from': reply_dict['id'],
                        'to': reply
                    })

    return jsonify(response)