diff --git a/ai/src/generator/bubble_sheet_generator.py b/ai/src/generator/bubble_sheet_generator.py index 8ae67155690ec70fd16c3c527ccd094bd9a1e65d..0626f9b9367f909aefe43ac9d31a5c31f2c42fd6 100644 --- a/ai/src/generator/bubble_sheet_generator.py +++ b/ai/src/generator/bubble_sheet_generator.py @@ -130,7 +130,7 @@ def draw_bubbles(ax, config, rect_x, rect_y, rect_type, last_rect_q, student_id) face_color = "none" if rect_type == "student_id_rect": y = rect_y + height - (grid_height * (j + 1)) - if student_id[i] == str(j): + if student_id != "empty" and student_id[i] == str(j): face_color = rect_color # Draw the bubble @@ -260,7 +260,10 @@ def draw_rect(ax, config, rect_x, rect_y, rect_type="answer_rect", gray_columns= gray_out(ax, config, rect_x, rect_y, rect_type=rect_type, gray_columns=gray_columns) # Draw the bubbles - draw_bubbles(ax, config, rect_x, rect_y, rect_type, last_rect_q, str(student_id).zfill(4)) + if student_id == "empty": + draw_bubbles(ax, config, rect_x, rect_y, rect_type, last_rect_q, student_id) + else: + draw_bubbles(ax, config, rect_x, rect_y, rect_type, last_rect_q, str(student_id).zfill(4)) # Draw labels draw_labels(ax, config, rect_x, rect_y, rect_type, last_rect_q=last_rect_q) @@ -324,6 +327,9 @@ def generate_bubble_sheet(test_id, student_id): :param test_id: Test ID :param student_id: Student ID (number from 0 to 9999) """ + global question_number + question_number = 1 + # Load the configuration file config = load_config() diff --git a/ai/src/generator/generator_handler.py b/ai/src/generator/generator_handler.py index 257a34846b5c4e2c1df5c1509d57073b25951630..f1364e055b1b2fd6cf6c79c05c19b7552bbed49e 100644 --- a/ai/src/generator/generator_handler.py +++ b/ai/src/generator/generator_handler.py @@ -1,6 +1,5 @@ import os import fitz -import random import uuid import numpy as np @@ -9,7 +8,17 @@ from ai.src.generator.question_paper_generator import generate_question_paper class Student: + """ + Class representing a student + """ def __init__(self, id, name, surname, student_number): + """ + Initialize the student + :param id: Our internal ID + :param name: Name of the student + :param surname: Surname of the student + :param student_number: Student number (os_cislo) + """ self.id = id self.name = name self.surname = surname @@ -17,6 +26,10 @@ class Student: self.shuffle = [] def to_dict(self): + """ + Convert the student to a dictionary + :return: Dictionary representation of the student + """ return { "id": self.id, "name": self.name, @@ -27,7 +40,19 @@ class Student: class Question: + """ + Class representing a question + """ def __init__(self, question_id, type, text, answers, default_grade=None, penalty=None): + """ + Initialize the question + :param question_id: Question ID + :param type: Type of the question + :param text: Text of the question + :param answers: Answers to the question + :param default_grade: Default grade + :param penalty: Penalty + """ self.question_id = question_id self.type = type self.text = text @@ -36,6 +61,10 @@ class Question: self.penalty = penalty def to_dict(self): + """ + Convert the question to a dictionary + :return: Dictionary representation of the question + """ return { "question_id": self.question_id, "type": self.type, @@ -47,6 +76,12 @@ class Question: def preprocess_data(students_json, questions_json): + """ + Preprocess the data from the JSON files from the request + :param students_json: Students JSON + :param questions_json: Questions JSON + :return: Students and questions (as objects) + """ student_id = 0 students = [] for student in students_json: @@ -61,6 +96,11 @@ def preprocess_data(students_json, questions_json): def shuffled_questions(questions_list): + """ + Shuffle the questions + :param questions_list: List of questions + :return: Shuffler and shuffled list + """ shuffled_list = questions_list.copy() shuffler = np.random.permutation(len(shuffled_list)) shuffled_list = [shuffled_list[i] for i in shuffler] @@ -68,6 +108,12 @@ def shuffled_questions(questions_list): def generate_sheets(collection, questions_json, students_json): + """ + Generate bubble sheets and question papers for the students + :param collection: DB collection + :param questions_json: Questions JSON (from the request) + :param students_json: Students JSON (from the request) + """ students, questions = preprocess_data(students_json, questions_json) test_id = uuid.uuid4().hex test_length = len(questions) @@ -87,6 +133,7 @@ def generate_sheets(collection, questions_json, students_json): generate_question_paper(student.id, questions_text, answers_text) + # Save the data to the database collection.insert_one( { "test_id": test_id, @@ -96,8 +143,11 @@ def generate_sheets(collection, questions_json, students_json): } ) + # Generate one student-less bubble sheet + generate_bubble_sheet(test_id, "empty") + pdfs_q = [f"generated_pdfs/{student.id}_question_paper.pdf" for student in students] - pdfs_a = [f"generated_pdfs/{student.id}_bubble_sheet.pdf" for student in students] + pdfs_a = ["generated_pdfs/empty_bubble_sheet.pdf"] + [f"generated_pdfs/{student.id}_bubble_sheet.pdf" for student in students] merged_pdf_q = fitz.open() merged_pdf_a = fitz.open()