Passed
Push — main ( 1e34ac...e89ccc )
by Máté
01:12
created

src.moodle_to_vikwikiquiz.quiz.Quiz.__init__()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 6
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 4
1
import os
2
3
from bs4 import BeautifulSoup
4
5
from src.moodle_to_vikwikiquiz.grading_types import GradingType
6
from src.moodle_to_vikwikiquiz.question_types import QuestionType
7
from src.moodle_to_vikwikiquiz.quiz_helpers import *
8
9
10 View Code Duplication
class Quiz:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
    def __init__(
12
        self, parent_article: str, title: str, grading: GradingType | None = None
13
    ):
14
        self.parent_article = parent_article
15
        self.title = title
16
        self.grading = grading
17
18
        self.questions: set[Question] = set()
19
20
    def __str__(self) -> str:
21
        text = f"{{{{Vissza | {self.parent_article}}}}}"
22
        text += f"""
23
24
{{{{Kvízoldal
25
|cím={self.title}"""
26
        if self.grading:
27
            text += f"\n|pontozás={self.grading.value}"
28
        text += "\n}}"
29
        for question in self.questions:
30
            text += f"\n\n\n{question}"
31
        text += "\n"
32
        return text
33
34
    def import_files(self, directory: str) -> None:
35
        for subdir, dirs, files in os.walk(directory):
36
            for file in files:
37
                self.import_questions(file, subdir)
38
39
    def import_questions(self, file: str, subdir: str) -> None:
40
        file_path = os.path.join(subdir, file)
41
        with open(file_path, "rb") as source_file:
42
            webpage = BeautifulSoup(source_file, "html.parser")
43
44
            multichoice_questions = webpage.find_all("div", class_="multichoice")
45
            for question in multichoice_questions:
46
                self.import_question(question)
47
48
    def import_question(self, question: Tag) -> None:
49
        correctly_answered, grade, maximum_points = get_grading_of_question(question)
50
        question_text = get_question_text(question)
51
        answer_texts, correct_answers = get_answers(question)
52
        if not correctly_answered:
53
            complete_correct_answers(
54
                answer_texts, correct_answers, grade, maximum_points, question_text
55
            )
56
        has_illustration = (
57
            True if question.find("img", class_="img-responsive") else False
58
        )
59
        self.add_question_no_duplicates(
60
            answer_texts, correct_answers, has_illustration, question_text
61
        )
62
63
    def add_question_no_duplicates(
64
        self,
65
        answer_texts: list[str],
66
        correct_answers: list[int],
67
        has_illustration: bool,
68
        question_text: str,
69
    ) -> None:
70
        for existing_question in self.questions:
71
            if question_already_exists(existing_question, question_text):
72
                add_answers_to_existing_question(
73
                    answer_texts, correct_answers, existing_question
74
                )
75
                break
76
        else:
77
            self.questions.add(
78
                Question(
79
                    q_type=QuestionType.MultipleChoice,
80
                    text=question_text,
81
                    illustration=has_illustration,
82
                    answers=answer_texts,
83
                    correct_answers=correct_answers,
84
                )
85
            )
86