def insert_new_questions_from_file(*args): """Imports data from txt file into the selected_database table. *args: only needed to delete the gui entry_field after passing of data """ with open("import_questions.txt", encoding="utf8") as f: r = f.read() test_questions = r.split("Exercise") test_questions = test_questions[1:] questions_list = [] for i in range(0, len(test_questions)): questions_list.append(test_questions[i].split("\n\n")) for ii in range(0, len(questions_list)): states = [] answer_states = questions_list[ii][1] for i in range(0, len(answer_states)): answer_state = answer_states[i] if answer_state == "*": state = 1 states.append(state) elif answer_state == "-": state = 0 states.append(state) question = questions_list[ii][0].replace("\n", ".") answer1 = questions_list[ii][2].replace("\n", "."), answer2 = questions_list[ii][3].replace("\n", "."), answer3 = questions_list[ii][4].replace("\n", "."), answer4 = questions_list[ii][5].replace("\n", "."), answer5 = questions_list[ii][6].replace("\n", "."), right_answers = [ states[0], states[1], states[2], states[3], states[4] ] sql_m = SqlManager() table_content = sql_m.sql_get_database_content( SettingVariables().selected_database) if len(table_content) > 0: row_id = table_content[-1].row_id + 1 else: row_id = 1 variables = Question(row_id, question, answer1, answer2, answer3, answer4, answer5, right_answers) variables = [variables] sql_m = SqlManager() sql_m.sql_insert_new_questions( variables, SettingVariables().selected_database)
def save_new_question(self, *args): """Passes the user information input (Question object) from GUI to SqlManager to save data in selected_database table. """ question, answer1, answer2, answer3, answer4, answer5, state_var1, state_var2, state_var3, state_var4, state_var5 = args right_answers_list = [ state_var1, state_var2, state_var3, state_var4, state_var5 ] settings_obj = SettingVariables() selected_database = settings_obj.selected_database row_id = SqlManager().sql_get_last_row_id(selected_database) + 1 new_question = Question(row_id, question.replace("\n", "."), answer1.replace("\n", "."), answer2.replace("\n", "."), answer3.replace("\n", "."), answer4.replace("\n", "."), answer5.replace("\n", "."), right_answers_list, 2) new_question_list = [new_question] sql_m = SqlManager() sql_m.sql_insert_new_questions(new_question_list, selected_database) try: self.insert_top.destroy() except AttributeError as ae: print("Exception occurred in save_new_question - due to " + str(ae)) finally: self.gui_insert_menu()
def create_results_tables(table_name, *args): """Creates a results table with the passed table name. *args is used only in the button widget to delete input inside the entry widget. *args: only needed to delete the gui entry_field after passing of data """ try: # creates 2 tables: 1. for saving results and 2. a copy of the selected database settings_obj = SettingVariables() selected_database = settings_obj.selected_database sql_m = SqlManager() sql_m.sql_create_results_table(table_name) sql_m.sql_create_database_copy(selected_database) except sqlite3.OperationalError as oe: print("Exception occurred in create_results_tables: " + str(oe))
def next_question(self, row_id, question, right_answers, user_answers, start_time, firsts): """Sets the score variable to 1 if right_answers and user_answers are equally. Creates the time_track_variable to save how long user took to answer a question. Adds or subtracts firsts variable accordingly to the score(add if score=0 add subtract if score=1). Checks if any firsts variable is lower than one and deletes that row if true. Passes all variables to the selected_results table. Then call the start_testing function again. """ try: if right_answers == user_answers: score = 1 else: score = 0 end_time = time.time() time_track = round(((end_time - start_time) / 60), 2) results = Results(row_id, question, right_answers, user_answers, score, time_track, firsts) results_list = [results] sql_m = SqlManager() sql_m.sql_insert_new_results( results_list, SettingVariables().selected_results_table) if score == 1: sql_m.sql_subtract_firsts( row_id, SettingVariables().selected_database_copy) elif score == 0: sql_m.sql_add_firsts(row_id, SettingVariables().selected_database_copy) if score == 1: firsts = sql_m.sql_get_row( SettingVariables().selected_database_copy, row_id)[0].firsts if firsts < 1: sql_m.sql_delete_row(SettingVariables().selected_database_copy, row_id) except IndexError as ie: print(str(ie) + " in next_question") print( "Possible reason might be that the database itself is empty or you choose an empty database_copy" ) try: self.testing_top.destroy() self.start_testing(SettingVariables().selected_database_copy) except Exception: print("Another exception in next_question occurred")
def results_overview(self, top): """Calculates the average score and passes it as well as the last score to the gui_results_overview.""" try: sql_m = SqlManager() results_list = sql_m.sql_get_results_content( SettingVariables().selected_results_table) last_score = results_list[-1].score score_list = [score.score for score in results_list] score_average = 0 for score in score_list: score_average += score score_average = str(round(score_average / len(score_list) * 100, 2)) + " %" self.gui_results_overview(top, score_average, last_score) except IndexError as ie: print(str(ie) + " in results_overview") print( "This is OK, if our results table is empty. Otherwise you should inform your admin." )
def start_testing(self, table_name=SettingVariables().selected_database_copy): """Queries content of the selected_database_copy table and selects randomly some row. Creates a random sequence of 5 digits in range 0 and 4 to sort newly the possible answers as well as right_answers sequence. Then pass all variables to the testing gui. """ sql_m = SqlManager() all_questions = sql_m.sql_get_database_content(table_name) random_row_id = random.randint(1, len(all_questions)) random_answer_seq = random.sample([0, 1, 2, 3, 4], 5) question_vars = all_questions[random_row_id - 1] random_question = [ question_vars.row_id, question_vars.question, question_vars.answer1, question_vars.answer2, question_vars.answer3, question_vars.answer4, question_vars.answer5, question_vars.right_answers, question_vars.firsts ] row_id = random_question[0] question_var = random_question[1] answer_vars = random_question[2:7] answer_vars = [answer_vars[i] for i in random_answer_seq] # transform right_answer variable from str to list with 5 integers right_answers = random_question[7] right_answers = [ int(right_answers[i]) for i in range(1, len(right_answers), 3) ] # save the right_answer variable in same order as the random_answer_seq right_answers_new = [right_answers[i] for i in random_answer_seq] firsts = random_question[8] question_obj = Question(row_id, question_var, answer_vars[0], answer_vars[1], answer_vars[2], answer_vars[3], answer_vars[4], right_answers_new, firsts) self.gui_testing_menu(question_obj) return random_answer_seq, random_row_id, question_obj
def show_remaining_questions(self): """Opens a window which shows contents of selected_database_copy table.""" sql_m = SqlManager() database_copy = sql_m.sql_get_database_content( SettingVariables().selected_database_copy) self.gui_show_remaining_questions(database_copy)
def show_testing_results(self): """Opens a window which shows contents of selected_results table.""" sql_m = SqlManager() results_list = sql_m.sql_get_results_content( SettingVariables().selected_results_table) self.gui_show_testing_results(results_list)