2024-05-28 17:47:08 +03:00
|
|
|
|
from db.object_pool import ObjectPool
|
2024-06-01 13:49:13 +03:00
|
|
|
|
from db.response_option import ResponseOptionPool
|
2024-05-25 19:18:26 +03:00
|
|
|
|
|
|
|
|
|
class Question:
|
|
|
|
|
def init(self, sID, title, max_time, test_id):
|
|
|
|
|
self.id = sID
|
|
|
|
|
self.title = title
|
|
|
|
|
self.max_time = max_time
|
|
|
|
|
self.test_id = test_id
|
|
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def init_from_data(self, data):
|
|
|
|
|
return self.init(*data)
|
|
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
|
|
def get_title(self):
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
def get_max_time(self):
|
|
|
|
|
return self.max_time
|
|
|
|
|
|
|
|
|
|
def get_test_id(self):
|
|
|
|
|
return self.test_id
|
|
|
|
|
|
2024-06-01 13:49:13 +03:00
|
|
|
|
def get_time_label(self):
|
|
|
|
|
if int(self.max_time) == 0:
|
|
|
|
|
return "Час на відповідь не обмежений"
|
|
|
|
|
|
2024-05-28 17:47:08 +03:00
|
|
|
|
total_time = self.max_time
|
|
|
|
|
|
|
|
|
|
hours = total_time // 3600
|
|
|
|
|
total_time -= hours * 3600
|
|
|
|
|
|
|
|
|
|
minutes = total_time // 60
|
|
|
|
|
total_time -= minutes * 60
|
|
|
|
|
|
|
|
|
|
seconds = total_time
|
|
|
|
|
|
|
|
|
|
total_label = []
|
2024-06-01 13:49:13 +03:00
|
|
|
|
|
2024-05-28 17:47:08 +03:00
|
|
|
|
if hours:
|
|
|
|
|
total_label.append(f"{hours} год.")
|
|
|
|
|
if minutes:
|
|
|
|
|
total_label.append(f"{minutes} хв.")
|
|
|
|
|
if seconds:
|
|
|
|
|
total_label.append(f"{seconds} c.")
|
|
|
|
|
|
2024-06-01 13:49:13 +03:00
|
|
|
|
return "На відповідь є " + " ".join(total_label)
|
|
|
|
|
|
|
|
|
|
def get_response_option_short_list(self, cur):
|
|
|
|
|
rop = ResponseOptionPool(cur)
|
|
|
|
|
return "<br>".join([i.render_short() for i in rop.select_by_question_id(self.id)])
|
2024-05-28 17:47:08 +03:00
|
|
|
|
|
2024-06-01 19:45:47 +03:00
|
|
|
|
def get_correct_response_percentage(self, cur):
|
|
|
|
|
rop = ResponseOptionPool(cur)
|
|
|
|
|
response_options = rop.select_by_question_id(self.id)
|
|
|
|
|
return sum([int(i.get_correctness()) for i in response_options]) / len(response_options) * 100
|
|
|
|
|
|
2024-06-01 13:49:13 +03:00
|
|
|
|
def render_short(self, cur):
|
|
|
|
|
time_label = self.get_time_label()
|
|
|
|
|
response_options = self.get_response_option_short_list(cur)
|
2024-06-01 19:45:47 +03:00
|
|
|
|
correct_percentage = round(self.get_correct_response_percentage(cur))
|
2024-05-28 17:47:08 +03:00
|
|
|
|
|
2024-06-01 19:45:47 +03:00
|
|
|
|
return f'<div class="question-short"><a class="question-link" href="/index.py?mode=view-question&id={self.id}"><span class="sub-label">#{self.id}</span><span class="main-label">{self.title}</span></a><span class="sub-title">{time_label}<br>{correct_percentage}% відповідей правильні</span><div class="response-option-short-list">{response_options}</div><div class="controls"><a class="magic-button" href="?mode=generate-response-options&id={self.id}">Згенерувати відповіді</a><a class="sub-button" href="/index.py?mode=edit-question&id={self.id}">Редагувати</a><a class="scary-button" href="/index.py?mode=delete-question&id={self.id}">Видалити</a></div></div>'
|
2024-05-25 19:18:26 +03:00
|
|
|
|
|
|
|
|
|
class QuestionPool:
|
2024-05-28 17:47:08 +03:00
|
|
|
|
def __init__(self, db):
|
2024-05-25 19:18:26 +03:00
|
|
|
|
self.object_pool = ObjectPool("question", Question)
|
|
|
|
|
|
|
|
|
|
if db:
|
|
|
|
|
self.object_pool.load_from_db(db)
|
2024-05-28 17:47:08 +03:00
|
|
|
|
|
|
|
|
|
def iter(self):
|
|
|
|
|
return iter(self.object_pool.pool)
|
|
|
|
|
|
|
|
|
|
def select_by_test_id(self, test_id):
|
|
|
|
|
return [i for i in self.object_pool.pool if i.get_test_id() == int(test_id)]
|