Добавлен метод generateRandomQuestionsCountDict для корректного распределения...

Добавлен метод generateRandomQuestionsCountDict для корректного распределения количества получаемых вопросов по темам БТЗ
parent 8ec7d420
...@@ -397,6 +397,47 @@ def generateRandomCountList(count_1, count_2): ...@@ -397,6 +397,47 @@ def generateRandomCountList(count_1, count_2):
return count_list return count_list
def generateRandomQuestionsCountDict(context, theme_list, count):
td = dict()
td_corr = dict()
# Получаем количество вопросов в каждой теме
QtiQuestion = QtiQuestionCursor(context)
questions = 0
for item in theme_list:
QtiQuestion.setRange('ThemeID', item)
c = QtiQuestion.count()
td[item] = c
questions += c
QtiQuestion.close()
# Уменьшаем количество вопросов в теме для получения требуемого количества
rc = 0
for item in theme_list:
qc = int(round(td[item] * count / float(questions)))
rc += qc
td_corr[item] = qc
# Сумма округленных значений может отличаться
if rc > count:
n = rc - count
sorted_td_corr = OrderedDict(sorted(td_corr.items(), key=operator.itemgetter(1)))
for key in reversed(sorted_td_corr):
value = sorted_td_corr[key]
if value > 0:
td_corr[key] = value - 1
n -= 1
if n == 0:
break
elif rc < count:
n = count - rc
for key, value in td_corr.iteritems():
if (td[key] - value) > 0:
td_corr[key] = value + 1
n -= 1
if n == 0:
break
return td_corr
def generateVariant(context, packageId, variantId, debugFlag=False): def generateVariant(context, packageId, variantId, debugFlag=False):
u'''Функция генерации варианта''' u'''Функция генерации варианта'''
# Получение режима выбора воропросов: '1' - выбор по порядку, '0' - случайным образом # Получение режима выбора воропросов: '1' - выбор по порядку, '0' - случайным образом
...@@ -447,10 +488,13 @@ def generateVariant(context, packageId, variantId, debugFlag=False): ...@@ -447,10 +488,13 @@ def generateVariant(context, packageId, variantId, debugFlag=False):
else: else:
tmpList = list() tmpList = list()
getPackageThemes(context, package_id, tmpList) getPackageThemes(context, package_id, tmpList)
count_list = generateRandomCountList(test_item.question_count, len(tmpList)) tmpDict = generateRandomQuestionsCountDict(context, tmpList, test_item.question_count)
for i,item in enumerate(tmpList):
themeDict[item] = count_list[i-1] for k in tmpDict:
themeDict[k] = tmpDict[k]
themeList.extend(tmpList) themeList.extend(tmpList)
if is_btz: if is_btz:
# Если не из Матрицы тем, то в packageId передан ИД БТЗ # Если не из Матрицы тем, то в packageId передан ИД БТЗ
getPackageThemes(context, packageId, themeList) getPackageThemes(context, packageId, themeList)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment