generated from your-land/yl_template
Fixes find_next_free_number
This commit is contained in:
parent
ed01ad4ec2
commit
bd4a515544
7
api.lua
7
api.lua
@ -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
|
||||
|
||||
12
internal.lua
12
internal.lua
@ -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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user