diff --git a/api.lua b/api.lua index 4a4f667..92b4670 100644 --- a/api.lua +++ b/api.lua @@ -312,8 +312,8 @@ local function create_question(id, question, category, sort, allowed_types, -- Specialcase : answers cannot be nil or empty if allowedtypes has either multiplechoice or singlechoice if (((answers == nil) or (next(answers) == nil)) and - ((allowed_types["multiplechoice"] ~= nil) or - (allowed_types["singlechoice"] ~= nil))) then + ((yl_survey.table_contains(allowed_types, "multiplechoice") == true) or + (yl_survey.table_contains(allowed_types, "singlechoice") == true))) then return false, yl_survey.t( "answers cannot be nil or empty if allowedtypes has either multiplechoice or singlechoice") end @@ -415,8 +415,8 @@ local function edit_question(id, q_id, question, sort, category, allowed_types, -- Specialcase : answers cannot be nil or empty if allowedtypes has either multiplechoice or singlechoice if ((answers ~= nil) and (next(answers) ~= nil) and - ((allowed_types["multiplechoice"] ~= nil) or - (allowed_types["singlechoice"] ~= nil))) then + ((yl_survey.table_contains(allowed_types, "multiplechoice") == true) or + (yl_survey.table_contains(allowed_types, "singlechoice") == true))) then return false, yl_survey.t( "answers cannot be nil or empty if allowedtypes has either multiplechoice or singlechoice") end @@ -446,9 +446,7 @@ local function edit_question(id, q_id, question, sort, category, allowed_types, local no = 0 local n_responses = t_question["responses"] or {} if (delete_responses == true) then - for _,_ in pairs(n_responses) do - no = no + 1 - end + for _, _ in pairs(n_responses) do no = no + 1 end n_responses = {} end diff --git a/internal.lua b/internal.lua index 4719d28..d28597c 100644 --- a/internal.lua +++ b/internal.lua @@ -30,6 +30,24 @@ end function yl_survey.priv_exists(priv) return priv_exists(priv) end +-- table.contains + +local function table_contains(t, m) + if ((type(m) ~= "number") and (type(m) ~= "string") and (type(m) ~= "boolean")) then + return nil, yl_survey.t("member must be primitive") + end + for _, v in pairs(t) do + if (v == m) then + return true + end + end + return false +end + +function yl_survey.table_contains(t, m) + return table_contains(t, m) +end + -- Hash name local function hash_name(name)