yl_survey/api.lua
2024-09-12 11:14:28 +02:00

202 lines
7.3 KiB
Lua

-- yl_survey.get_data
--
local function get_data()
if next(yl_survey.data) then
return true, yl_survey.data
else
return false, {}
end
end
function yl_survey.get_data() return get_data() end
-- yl_survey.get_survey
--
local function get_record(id)
local success, data = yl_survey.get_data()
if (success == false) then return nil end
return table.copy(data[id]) -- {data} or nil
end
function yl_survey.get_record(id) return get_record(id) end
-- yl_survey.create_survey
--
local function validate_nil_or_positive(value)
if (value == nil) then return true end
if ((type(value) == "number") and (value >= 0)) then return true end
return false
end
local function validate(value, expect_type, allow_nil)
if ((allow_nil == true) and (value == nil)) then return true end
if ((allow_nil == false) and (value == nil)) then return true end
if (type(value) == expect_type) then return true end
return false
end
local function create_survey(owner, allow_names, allow_anonymous, allow_changes,
timestamp_start, timestamp_end)
-- Defense
if ((type(owner) ~= "string") or (owner == "")) then
return false, yl_survey.t("owner must be string")
end
if (allow_names == nil) then allow_names = true end -- set default
if (type(allow_names) ~= "boolean") then
return false, yl_survey.t("allow_names must be boolean")
end
if (allow_anonymous == nil) then allow_anonymous = true end -- set default
if (type(allow_anonymous) ~= "boolean") then
return false, yl_survey.t("allow_anonymous must be boolean")
end
if (allow_changes == nil) then allow_changes = false end -- set default
if (type(allow_changes) ~= "boolean") then
return false, yl_survey.t("allow_changes must be boolean")
end
if (validate_nil_or_positive(timestamp_start) == false) then
return false, yl_survey.t(
"timestamp_start if given then must be number and not negative")
end
if (validate_nil_or_positive(timestamp_end) == false) then
return false, yl_survey.t(
"timestamp_end if given then must be number and not negative")
end
-- Specialcase: We don't accept surveys NOONE can take part in
--[[
if ((allow_names == false) and (allow_anonymous == false)) then
return false, yl_survey.t("Noone can take part in that survey. " ..
"Neither named nor anonymous participants are allowed")
end
]] --
if ((allow_names == false) and (allow_anonymous == false) and
(allow_changes == false)) then
return false, yl_survey.t("Noone can take part in that survey. " ..
"Neither named nor anonymous participants are allowed and you can't change it later")
end
-- Specialcase: We don't accept surveys where the enddate is smaller than the startdate
if (timestamp_start and timestamp_end and (timestamp_start >= timestamp_end) and
(allow_changes == false)) then
return false, yl_survey.t("Noone can take part in that survey. " ..
"It ends before it starts and you can't change it later")
end
-- UUID
local get_uuid_success, UUID = yl_survey.create_uuid()
if (get_uuid_success == false) then return false, UUID end
-- Add to list
local metadata = {
id = UUID,
owner = owner,
allow_names = allow_names,
allow_anonymous = allow_anonymous,
allow_changes = allow_changes,
timestamp_start = timestamp_start,
timestamp_end = timestamp_end,
creation_date = os.time(),
modification_date = os.time()
}
local record = {}
record["metadata"] = metadata
-- Store
local success = yl_survey.save(UUID, record)
if (success == true) then
yl_survey.data[UUID] = record
return true, UUID
else
return false, yl_survey.t("Could not store")
end
end
function yl_survey.create_survey(owner, allow_names, allow_anonymous,
allow_changes, timestamp_start, timestamp_end)
return create_survey(owner, allow_names, allow_anonymous, allow_changes,
timestamp_start, timestamp_end)
end
-- yl_survey.edit_survey
--
local function edit_survey(id, owner, allow_names, allow_anonymous,
allow_changes, timestamp_start, timestamp_end,
delete_responses)
-- Defense
if (validate(id, "string", false) == false) then
return false, yl_survey.t("id must be string")
end
if (validate(owner, "string", true) == false) then
return false, yl_survey.t("owner must be string")
end
if (validate(allow_names, "boolean", true) == false) then
return false, yl_survey.t("allow_names must be boolean")
end
if (validate(allow_anonymous, "boolean", true) == false) then
return false, yl_survey.t("allow_anonymous must be boolean")
end
if (validate(allow_changes, "boolean", true) == false) then
return false, yl_survey.t("allow_changes must be boolean")
end
if (validate(timestamp_start, "number", true) == false) then
return false, yl_survey.t("timestamp_start must be number")
end
if (validate(timestamp_end, "number", true) == false) then
return false, yl_survey.t("timestamp_end must be number")
end
if (validate(delete_responses, "boolean", true) == false) then
return false, yl_survey.t("delete_responses must be boolean")
end
if (validate_nil_or_positive(timestamp_start) == false) then
return false, yl_survey.t(
"timestamp_start if given then must be number and not negative")
end
if (validate_nil_or_positive(timestamp_end) == false) then
return false, yl_survey.t(
"timestamp_end if given then must be number and not negative")
end
local record = yl_survey.get_record(id)
if (record["metadata"].allow_changes == false) then
return false, yl_survey.t("no changes allowed")
end
local metadata = {
id = record["metadata"].id, -- This value cannot change
owner = owner or record["metadata"].owner,
allow_names = allow_names or record["metadata"].allow_names,
allow_anonymous = allow_anonymous or record["metadata"].allow_anonymous,
allow_changes = allow_changes or record["metadata"].allow_changes,
timestamp_start = timestamp_start or record["metadata"].timestamp_start,
timestamp_end = timestamp_end or record["metadata"].timestamp_end,
creation_date = record["metadata"].creation_date, -- This value cannot change
modification_date = os.time() -- This value must change
}
record["metadata"] = metadata
-- Store
local success = yl_survey.save(id, record)
if (success == true) then
yl_survey.data[id] = record
return true, id
else
return false, yl_survey.t("Could not store")
end
end
function yl_survey.edit_survey(id, owner, allow_names, allow_anonymous,
allow_changes, timestamp_start, timestamp_end,
delete_responses)
return edit_survey(id, owner, allow_names, allow_anonymous, allow_changes,
timestamp_start, timestamp_end, delete_responses)
end