your-land/yl_survey#3 Implements get_question

This commit is contained in:
AliasAlreadyTaken 2024-09-21 17:11:05 +02:00
parent b2f8dbfae7
commit f9664dff5d
2 changed files with 67 additions and 26 deletions

View File

@ -143,7 +143,7 @@ yl_survey.get_question(s_id, q_id, include_responses, playername)
* s_id: string, required. This is the survey/UUID the question is added to.
* q_id: number, required.
* include_responses: boolean, optional, defaults to `false`. If `true`, the responses to that question are included.
* playername: string, optional. If given, returns only the responses of this player. May be empty `{}` if the player did not answer this question yet. No effect if include_responses is not `true`.
* playername: string, optional. If given, adds only the responses of this player. May be empty `{}` if the player did not answer this question yet. No effect if include_responses is not `true`.
Returns `true, {question}` if successful, `false, "errormessage"` if not.

91
api.lua
View File

@ -19,7 +19,11 @@ function yl_survey.get_data() return get_data() end
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
if (type(data[id]) == "table") then
return table.copy(data[id]) -- {data} or nil
else
return nil
end
end
function yl_survey.get_record(id) return get_record(id) end
@ -77,17 +81,11 @@ end
local function validate_responses(t_answers, responses)
-- Defense
if (validate(t_answers, "table", false) == false) then
return false
end
if (validate(responses, "table", false) == false) then
return false
end
if (validate(t_answers, "table", false) == false) then return false end
if (validate(responses, "table", false) == false) then return false end
-- If the responses are not JSON-able, return false
if (minetest.write_json(responses) == nil) then
return false
end
if (minetest.write_json(responses) == nil) then return false end
-- If there is a response not matched in t_answers, return false
for k, v in pairs(responses) do
@ -95,13 +93,9 @@ local function validate_responses(t_answers, responses)
if r_id and (r_id > 0) then
local found = false
for kk, vv in pairs(t_answers) do
if (v == vv) then
found = true
end
end
if (found == false) then
return false
if (v == vv) then found = true end
end
if (found == false) then return false end
end
end
@ -357,15 +351,16 @@ local function create_question(id, question, category, sort, allowed_types,
local record = yl_survey.get_record(id)
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
-- Specialcase : given sort is a duplicate
if ((type(sort) == "number") and
(yl_survey.is_duplicate(record, sort) == true)) then
return false, yl_survey.t("sort is duplicate")
end
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
if (record["metadata"].allow_changes == false) then
return false, yl_survey.t("no changes allowed")
end
@ -459,15 +454,16 @@ local function edit_question(id, q_id, question, sort, category, allowed_types,
local record = yl_survey.get_record(id)
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
-- Specialcase : given sort is a duplicate
if ((type(sort) == "number") and
(yl_survey.is_duplicate(record, sort) == true)) then
return false, yl_survey.t("sort is duplicate")
end
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
if (record["metadata"].allow_changes == false) then
return false, yl_survey.t("no changes allowed")
end
@ -592,9 +588,6 @@ local function list_questions(id, include_responses)
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
if (record["metadata"].allow_changes == false) then
return false, yl_survey.t("no changes allowed")
end
-- Payload
local questions = {}
@ -699,3 +692,51 @@ end
function yl_survey.answer_question(id, q_id, playername, responses, overwrite)
return answer_question(id, q_id, playername, responses, overwrite)
end
-- get_question
--
local function get_question(id, q_id, include_responses, playername)
-- Defense
if (validate(id, "string", false) == false) then
return false, yl_survey.t("id must be string")
end
if (validate(q_id, "number", false) == false) then
return false, yl_survey.t("q_id must be number")
end
if (q_id <= 0) then return false, "q_id must be positive number" end
if (validate(playername, "string", true) == false) then
return false, yl_survey.t("playername must be string")
end
if (validate(include_responses, "boolean", true) == false) then
return false, yl_survey.t("include_responses must be boolean")
end
local record = yl_survey.get_record(id)
if (record == nil) then
return false, yl_survey.t("record does not exist")
end
-- Payload
local question = record[tostring(q_id)] or {}
if (playername ~= nil) then
local responses = {}
responses[playername] = question["responses"] and
question["responses"][playername] or {}
question["responses"] = responses
end
if (include_responses ~= true) then question["responses"] = nil end
if (next(question) ~= nil) then
return true, question
else
return false, {}
end
end
function yl_survey.get_question(s_id, q_id, include_responses, playername)
return get_question(s_id, q_id, include_responses, playername)
end