Ejemplo n.º 1
0
 def load_questions(self, package_id):
     if package_id:
         with Connection() as con:
             with con:
                 for row in con.execute(
                         '''
                             SELECT
                             id,
                             question,
                             correct,
                             incorrect1,
                             incorrect2,
                             incorrect3
                             FROM questions
                             WHERE package_id = ?
                 ''', (str(package_id), )):
                     print(row[0])
                     self.qbank.append({
                         "id": row[0],
                         "text": row[1],
                         "correct": row[2],
                         "incorrect": [row[3], row[4], row[5]]
                     })
     else:
         with Connection() as con:
             for id, question_text, correct, b, c, d, package_id in con.execute(
                     "SELECT * from questions"):
                 self.qbank.append({
                     "text": question_text,
                     "correct": correct,
                     "incorrect": [b, c, d],
                     "id": id
                 })
Ejemplo n.º 2
0
    def ensure_table_exists(self):
        with Connection() as con:
            con.execute('''
            CREATE TABLE IF NOT EXISTS statistics(
            id INTEGER PRIMARY KEY,
            question_id INTEGER,
            quiz_format TEXT,
            corrects INTEGER,
            incorrects INTEGER,
            skips INTEGER,
            time INTEGER,
            abandons INTEGER,
            FOREIGN KEY(question_id) REFERENCES questions(id)
            )''')

        with Connection() as con:
            con.execute('''
            CREATE TABLE IF NOT EXISTS answer_stats(
            id INTEGER PRIMARY KEY,
            question_id INTEGER,
            quiz_format TEXT,
            status TEXT,
            time INTEGER,
            created_at TIMESTAMP,
            FOREIGN KEY(question_id) REFERENCES questions(id)
            )''')
Ejemplo n.º 3
0
    def ensure_table_exists():
        with Connection() as con:
            con.execute('''
                        CREATE TABLE IF NOT EXISTS passwords(
                        id INTEGER PRIMARY KEY,
                        password TEXT
                        )''')

        with Connection() as con:
            with con:
                con.execute('''
                            INSERT INTO passwords
                            (password)
                            VALUES
                            ('password')
                            ''')
Ejemplo n.º 4
0
 def load_stats2(self):
     Question = namedtuple("Question", [
         "currently_assigned", "q_id", "text", "correct", "in1", "in2",
         "in3", "time", "status", "created_at", "package_id",
         "package_name", "quiz"
     ])
     with Connection() as con:
         with con:
             for i, row in enumerate(
                     con.execute('''
             SELECT questions.id,
             question,
             correct,
             incorrect1,
             incorrect2,
             incorrect3,
             time,
             status,
             created_at,
             questions.package_id,
             packages.name,
             answer_stats.quiz_format,
             packages.quiz_format
             FROM ((answer_stats
             INNER JOIN questions
             ON answer_stats.question_id = questions.id)
             INNER JOIN packages
             ON questions.package_id = packages.package_id)
             ''')):
                 if not type(row[6]) == str and row[7] != "ongoing":
                     self.q_bank.append(
                         Question(row[-1] == row[-2], *row[:-1]))
                 elif row[7] == "ongoing":
                     print(row)
Ejemplo n.º 5
0
 def load_stats(self):
     Question = namedtuple("Question", [
         "currently_assigned", "q_id", "text", "correct", "in1", "in2",
         "in3", "successes", "failures", "skips", "abandons", "total_time",
         "quiz"
     ])
     with Connection() as con:
         with con:
             for i, row in enumerate(
                     con.execute('''
             SELECT questions.id,
             question,
             correct,
             incorrect1,
             incorrect2,
             incorrect3,
             corrects,
             incorrects,
             skips,
             abandons,
             time,
             statistics.quiz_format,
             packages.quiz_format
             FROM ((questions
             INNER JOIN statistics
             ON questions.id = statistics.question_id)
             INNER JOIN packages
             ON questions.package_id = packages.package_id)
             ''')):
                 q = Question(row[-1] == row[-2], *row[:-1])
                 self.q_bank.append(q)
                 print(q)
Ejemplo n.º 6
0
 def get_password():
     with Connection() as con:
         return con.execute('''
                     SELECT password
                     FROM passwords
                     WHERE id = '1'
                     ''').fetchone()[0]
Ejemplo n.º 7
0
 def get_hangman_qs(random = False):
     bank = []
     with Connection() as con:
         with con:
             for row in con.execute('''
                         SELECT
                         id,
                         question,
                         correct,
                         incorrect1,
                         incorrect2,
                         incorrect3
                         FROM questions
                         INNER JOIN packages
                         ON questions.package_id = packages.package_id
                         WHERE packages.quiz_format = 'Hangman'
             '''):
                 print(row[0])
                 bank.append(
                     {"id": row[0], "text": row[1], "correct": row[2], "incorrect": [row[3], row[4], row[5]]})
     if random:
         shuffle(bank)
     for question in bank:
         choices = [question["correct"]] + question["incorrect"]
         shuffle(choices)
         yield (question["id"], question["text"], choices, question["correct"])
Ejemplo n.º 8
0
 def load_packages(self):
     with Connection() as con:
         for package_id, name, quiz_format in con.execute(
                 "SELECT * from packages"):
             self.package_bank.append({
                 "name": name,
                 "package_id": package_id,
                 "quiz_format": quiz_format
             })
Ejemplo n.º 9
0
 def delete_package(package_id):
     with Connection() as con:
         with con:
             con.execute(
                 '''DELETE 
             FROM questions
             WHERE questions.package_id = ?''', (str(package_id), ))
             con.execute("DELETE from packages WHERE package_id = ?",
                         (str(package_id), ))
Ejemplo n.º 10
0
 def create_question(*, prompt, answer, incorrect1, incorrect2, incorrect3,
                     package_id):
     with Connection() as con:
         with con:
             con.execute(
                 "INSERT INTO questions(question, correct, " +
                 "incorrect1, incorrect2, incorrect3, package_id) values (?,?,?,?,?,?)",
                 (prompt, answer, incorrect1, incorrect2, incorrect3,
                  package_id))
Ejemplo n.º 11
0
 def set_password(password):
     with Connection() as con:
         with con:
             con.execute(
                 '''
                         UPDATE passwords
                         SET password = ?
                         WHERE id = '1'
                         ''', (password, ))
Ejemplo n.º 12
0
 def save_answer_stats(*, id, quiz_format, status, time, created_at):
     with Connection() as con:
         with con:
             con.execute(
                 '''
             INSERT INTO answer_stats
             (question_id, quiz_format, status, time, created_at)
             VALUES
             (?, ?, ?, ?, ?)
             ''', (str(id), quiz_format, status, str(time), created_at))
Ejemplo n.º 13
0
 def create_stats(id):
     with Connection() as con:
         with con:
             for format in ["Multi-Choice", "Hangman"]:
                 con.execute(
                     '''
                 INSERT INTO statistics
                 (question_id, quiz_format, corrects, incorrects, skips, abandons, time)
                 VALUES
                 (?, ?, 0, 0, 0, 0, 0)
                 ''', (id, format))
Ejemplo n.º 14
0
 def create_answer_stats(obj):
     with Connection() as con:
         with con:
             con.execute(
                 '''
             INSERT INTO answer_stats
             (question_id, quiz_format, status, time, created_at)
             VALUES
             (?, ?, ?, ?, ?)
             ''',
                 (obj["id"], obj["quiz_format"], obj["status"], obj["time"],
                  datetime.datetime.now()))
Ejemplo n.º 15
0
 def get_stats(id, quiz):
     with Connection() as con:
         with con:
             return con.execute(
                 '''
             SELECT question_id,
             quiz_format,
             corrects,
             incorrects,
             skips,
             time,
             abandons
             FROM statistics
             WHERE question_id = ?
             AND quiz_format = ?
             ''', (str(id), str(quiz))).fetchone()
Ejemplo n.º 16
0
 def get_package_questions(*, package_id):
     bank = []
     with Connection() as con:
         with con:
             for row in con.execute('''
                         SELECT
                         id,
                         question,
                         correct,
                         incorrect1,
                         incorrect2,
                         incorrect3
                         FROM questions
                         WHERE package_id = ?
             ''', (str(package_id),)):
                 bank.append(row)
     return sample(bank, len(bank))
Ejemplo n.º 17
0
 def update_stats(obj):
     obj = {k: str(v) for k, v in obj.items()}
     with Connection() as con:
         with con:
             con.execute(
                 '''
             UPDATE statistics
             SET corrects = ?,
             incorrects = ?,
             skips = ?,
             time = ?,
             abandons = ?
             WHERE question_id = ?
             AND quiz_format = ?
             ''', (obj["corrects"], obj["incorrects"], obj["skips"],
                   obj["time"], obj["abandons"], obj["id"],
                   obj["quiz_format"]))
Ejemplo n.º 18
0
 def get_quiz_questions(*, quiz="Multi-Choice"):
     bank = []
     with Connection() as con:
         with con:
             for row in con.execute('''
                         SELECT
                         id,
                         question,
                         correct,
                         incorrect1,
                         incorrect2,
                         incorrect3
                         FROM questions
                         INNER JOIN packages
                         ON questions.package_id = packages.package_id
                         WHERE packages.quiz_format = ?
             ''', (str(quiz),)):
                 bank.append(row)
     return sample(bank, len(bank))
Ejemplo n.º 19
0
 def get_answer_stats(id):
     with Connection() as con:
         with con:
             return con.execute(
                 '''
             SELECT questions.id,
             quiz_format,
             status,
             question,
             correct,
             incorrect1,
             incorrect2,
             incorrect3,
             time,
             created_at
             FROM (answer_stats
             INNER JOIN questions
             ON answer_stats.question_id = questions.id)
             WHERE question_id = ?
             ''', (str(id))).fetchall()
Ejemplo n.º 20
0
 def delete_question(id):
     with Connection() as con:
         with con:
             con.execute("DELETE from questions WHERE id = ?", (str(id), ))
Ejemplo n.º 21
0
 def save_question(id, question, correct, inc1, inc2, inc3):
     with Connection() as con:
         with con:
             con.execute(
                 "UPDATE questions SET question = ?, correct = ?, incorrect1 = ?, incorrect2 = ?, incorrect3 = ? WHERE id = ?",
                 (question, correct, inc1, inc2, inc3, str(id)))
Ejemplo n.º 22
0
 def get_question(id):
     with Connection() as con:
         with con:
             return con.execute(
                 "SELECT id, question, correct, incorrect1, incorrect2, incorrect3 from questions WHERE id = ?",
                 (str(id), )).fetchone()
Ejemplo n.º 23
0
 def ensure_table_exists(self):
     with Connection() as con:
         with con:
             con.execute(
                 "CREATE TABLE IF NOT EXISTS packages (package_id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, quiz_format text UNIQUE)"
             )
Ejemplo n.º 24
0
 def save_package(package_id, name, quiz_format):
     with Connection() as con:
         with con:
             con.execute(
                 "UPDATE packages SET name = ?, quiz_format = ? WHERE package_id = ?",
                 (name, quiz_format, str(package_id)))
Ejemplo n.º 25
0
 def ensure_table_exists(self):
     with Connection() as con:
         con.execute(
             "CREATE TABLE IF NOT EXISTS questions (id INTEGER PRIMARY KEY, question TEXT, correct TEXT, incorrect1 TEXT, incorrect2 TEXT, incorrect3 TEXT, package_id INTEGER, FOREIGN KEY (package_id) REFERENCES packages (package_id))"
         )
Ejemplo n.º 26
0
 def search_format(package_id):
     with Connection() as con:
         with con:
             return con.execute(
                 "SELECT package_id, name, quiz_format FROM packages WHERE package_id = ?",
                 (str(package_id))).fetchone()
Ejemplo n.º 27
0
 def add_package(name):
     with Connection() as con:
         with con:
             return con.execute("INSERT INTO packages(name) values (?)",
                                (name, )).lastrowid
Ejemplo n.º 28
0
from Quiz.multiplechoice import Multiplechoice
from Quiz.package import Package
from Quiz.statistics import Statistics
from data.connection import Connection
import random

# !!Nukes current data!!
for t_name in ["questions", "statistics", "packages"]:
    with Connection() as con:
        with con:
            con.execute("DROP TABLE IF EXISTS " + t_name)

# Ensures that databases are created
Multiplechoice()
Statistics()
Package()


# helper function
def gen_num():
    return str(random.randint(100, 999))


data = [{
    "Harry Potter trivia":
    [[
        "What was Nearly Headless Nick's last name?", "de Mimsy-Porpington",
        "von Grieve", "van Orton", "Delaney-Podmore"
    ],
     [
         "How old was Nicholas Flamel in the Sorcerer's Stone?", "655",