summaryrefslogtreecommitdiff
path: root/db/db.py
blob: fd2e8c64a639aac466ef8985b592aa1034194cec (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import text

from app import app

from db.user import DBUser

class DB:
    def __init__(self):
        self.db = SQLAlchemy()
        self.db.init_app(app)
        self.user = DBUser(self.db)
        
    def question_new(self, question, neg_ans, pos_ans ):
        sql = "INSERT \
                INTO questions (question, neg_answer, pos_answer) \
                VALUES (:question, :neg_answer, :pos_answer) \
                RETURNING id ;"
        result = self.db.session.execute(
                text(sql), { 
                        "question":question, 
                        "neg_answer":neg_ans,
                        "pos_answer":pos_ans }
            )
        self.db.session.commit()
        return result.fetchone()[0]

    def quiz_new(self, user_id):
        sql = "INSERT \
                INTO questionaires (creator_id) \
                VALUES (:creator_id) \
                RETURNING id ;"
        result = self.db.session.execute( text(sql),
            { "creator_id":user_id } )
        self.db.session.commit()
        return result.fetchone()[0]

    def quiz_add(self, quiz_id, question_id ):
        sql = "UPDATE questionaires \
                SET questionset = ARRAY_APPEND(questionset, :question_id) \
                WHERE id=:quiz_id;"
        self.db.session.execute(text(sql), {
                "quiz_id":quiz_id,
                "question_id":question_id
            })
        self.db.session.commit()
    

    def set_quiz_link( self, quiz_id, link ):
        sql = "INSERT \
                INTO quiz_links (quiz_id, link) \
                VALUES (:quiz_id, :link)"
        result = self.db.session.execute( text(sql),
            {  "quiz_id":quiz_id, "link":link } )
        self.db.session.commit()


    def find_quiz_by_link( self, link ):
        sql = "SELECT quiz_id \
                FROM quiz_links \
                WHERE link=:link;"
        result = self.db.session.execute(text(sql), { "link":link }).fetchone()
        return result[0] if result else False


    def get_quiz_link( self, quiz_id ):
        sql = "SELECT link \
                FROM quiz_links \
                WHERE quiz_id=:quiz_id;"
        result = self.db.session.execute(text(sql), { "quiz_id":quiz_id }).fetchone()
        return result[0] if result else result

    
    def answer_new(self, user_id, question_id, answer):
        sql = "INSERT \
                INTO answers (user_id, question_id, answer ) \
                VALUES (:user_id, :question_id, :answer);"
        self.db.session.execute( text(sql), {
                "user_id":user_id,
                "question_id":question_id, 
                "answer":answer
            } )
        self.db.session.commit()


    def get_questions(self, quiz_id):
        sql = "SELECT q.id, q.question, q.neg_answer, q.pos_answer, a.answer \
                FROM questionaires quiz \
                JOIN questions q ON q.id = ANY(quiz.questionset) \
                JOIN answers a ON a.user_id = quiz.creator_id \
                WHERE a.question_id = q.id AND quiz.id = (:quiz_id);"
        return self.db.session.execute( text(sql), { "quiz_id":quiz_id } ).fetchall()

    def get_comparable(self, quiz_id,user1,user2):
        sql = "SELECT q.question, q.neg_answer, q.pos_answer, \
                a1.answer, a2.answer, \
                100 - ABS( a1.answer - a2.answer ) / 10 \
                FROM questionaires quiz \
                JOIN questions q ON q.id = ANY(quiz.questionset) \
                JOIN answers a1 ON a1.user_id = (:user1) \
                JOIN answers a2 ON a2.user_id = (:user2) \
                WHERE a1.question_id = q.id \
                AND a2.question_id = q.id \
                AND quiz.id = (:quiz_id);"
        return self.db.session.execute( text(sql), {
                "quiz_id":quiz_id,
                "user1":user1,
                "user2":user2
            } ).fetchall()


    def get_user_answer(self, user_id, question_id):
        sql = "SELECT answer \
            FROM answers \
            WHERE question_id = (:question_id) AND user_id = (:user_id);"
        result = self.db.session.execute( text(sql), { 
                'question_id': question_id,
                'user_id': user_id 
                } ).fetchone()
        return result[0] if result else -1


    def get_user_answers_for_quiz(self, quiz_id, user_id):
        sql = "SELECT a.question_id, a.answer \
                FROM questionaires quiz \
                JOIN answers a ON a.question_id = ANY(quiz.questionset) \
                WHERE a.user_id = (:user_id) AND quiz.id = (:quiz_id);"
        return self.db.session.execute( text(sql), { 
                'user_id': user_id,
                'quiz_id': quiz_id
                } ).fetchall()

    def get_users_answered(self, quiz_id):
        sql = "SELECT DISTINCT a.user_id, u.nick \
                FROM questionaires quiz \
                JOIN answers a ON a.question_id = quiz.questionset[1] \
                JOIN users u ON u.id = a.user_id \
                WHERE quiz.id = (:quiz_id);"
        return self.db.session.execute( text(sql), { 
                'quiz_id': quiz_id
                } ).fetchall()

    def is_user_answered(self, quiz_id, user_id):
        sql = "SELECT a.answer \
                FROM questionaires quiz \
                JOIN answers a ON a.question_id = quiz.questionset[1] \
                WHERE quiz.id = (:quiz_id) AND a.user_id = (:user_id);"
        results = self.db.session.execute( text(sql), { 
                'quiz_id': quiz_id,
                'user_id': user_id
                } ).fetchone()
        return True if results else False


    def get_all_answers_for_quiz(self, quiz_id):
        sql = "SELECT a.question_id, a.user_id, a.answer \
                FROM questionaires quiz \
                JOIN answers a ON a.question_id = ANY(quiz.questionset) \
                WHERE quiz.id = (:quiz_id);"
        return self.db.session.execute( text(sql), { 
                'quiz_id': quiz_id
                } ).fetchall()