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