Fixes find_next_free_number

This commit is contained in:
AliasAlreadyTaken 2024-09-13 12:47:12 +02:00
parent ed01ad4ec2
commit bd4a515544
2 changed files with 14 additions and 5 deletions

View File

@ -328,8 +328,9 @@ local function create_question(id, question, category, sort, allowed_types,
-- if sort is nil, then find next sort
local next_sort = sort or yl_survey.find_next_free_number(record, "sort")
-- Find next free q_id
local next_q_id = yl_survey.find_next_free_number(record, "id")
local next_q_id, dupe = yl_survey.find_next_free_number(record, "id")
local t_question = {
id = next_q_id,
@ -355,6 +356,6 @@ local function create_question(id, question, category, sort, allowed_types,
end
function yl_survey.create_question(id, question, category, sort, allowed_types,
answers)
return create_question(id, question, category, sort, allowed_types, answers)
answers, enabled)
return create_question(id, question, category, sort, allowed_types, answers, enabled)
end

View File

@ -105,14 +105,22 @@ end
local function find_next_free_number(record, target)
local highest_no = 1
local duplicates = {}
for key, question in pairs(record) do
if (tonumber(key) ~= nil) then
if (question[target] > highest_no) then
table.insert(duplicates, question[target])
if (question[target] >= highest_no) then
highest_no = question[target] + 1
end
end
end
return highest_no
local is_duplicate = false
for _, dupe in ipairs(duplicates) do
if (highest_no == dupe) then
is_duplicate = true
end
end
return highest_no, is_duplicate
end
function yl_survey.find_next_free_number(record, target)