generated from your-land/yl_template
Implements skip button
This commit is contained in:
parent
edf1c9ddd1
commit
c4d08f8d78
1
api.lua
1
api.lua
@ -28,6 +28,7 @@ local function deliver_survey(playername, survey_id)
|
||||
-- Init cache
|
||||
yl_survey_fs.data[playername] = {}
|
||||
yl_survey_fs.data[playername].survey_id = survey_id
|
||||
yl_survey_fs.data[playername].sort = 0
|
||||
yl_survey_fs.data[playername].q_id = 0
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
|
||||
|
144
internal.lua
144
internal.lua
@ -9,27 +9,60 @@ function yl_survey_fs.log(text) return log(text) end
|
||||
|
||||
-- Helper
|
||||
|
||||
local function get_next_q_id(questions, current_q_id)
|
||||
local function get_next_q_id_and_sort(questions, current_sort)
|
||||
--core.log("action","meow0=".. dump(questions))
|
||||
--core.log("action","meow0=" .. dump(current_q_id))
|
||||
if current_q_id == nil then current_q_id = 0 end
|
||||
if current_sort == nil then current_sort = 0 end
|
||||
--core.log("action","meow1=" .. dump(current_q_id))
|
||||
local ret
|
||||
local ret_id, ret_sort
|
||||
local ret_maxsort = 0
|
||||
local temp = math.huge
|
||||
for _, q in pairs(questions) do
|
||||
--core.log("action","meow2=" .. dump(q))
|
||||
if ((q.sort > current_q_id) and (q.sort < temp) and (q.enabled == true)) then
|
||||
if ((q.sort > current_sort) and (q.sort < temp) and (q.enabled == true)) then
|
||||
--core.log("action","meow3=" .. dump(q.sort))
|
||||
temp = q.sort
|
||||
ret = q.id
|
||||
ret_id = q.id
|
||||
ret_sort = q.sort
|
||||
end
|
||||
if (q.sort >= ret_maxsort) then
|
||||
ret_maxsort = q.sort
|
||||
end
|
||||
end
|
||||
--core.log("action","meow4" .. dump(ret))
|
||||
return ret
|
||||
return ret_id, ret_sort, ret_maxsort
|
||||
end
|
||||
|
||||
function yl_survey_fs.get_next_q_id(questions, current_q_id)
|
||||
return get_next_q_id(questions, current_q_id)
|
||||
function yl_survey_fs.get_next_q_id_and_sort(questions, current_sort)
|
||||
return get_next_q_id_and_sort(questions, current_sort)
|
||||
end
|
||||
|
||||
local function get_previous_q_id_and_sort(questions, current_sort)
|
||||
--core.log("action","meow0=".. dump(questions))
|
||||
--core.log("action","meow0=" .. dump(current_sort))
|
||||
if current_sort == nil then current_sort = 0 end
|
||||
--core.log("action","meow1=" .. dump(current_sort))
|
||||
local ret_id, ret_sort
|
||||
local ret_minsort = math.huge
|
||||
local temp = 0
|
||||
for _, q in pairs(questions) do
|
||||
--core.log("action","meow2=" .. dump(q))
|
||||
if ((q.sort < current_sort) and (q.sort > temp) and (q.enabled == true)) then
|
||||
--core.log("action","meow3=" .. dump(q.sort))
|
||||
temp = q.sort
|
||||
ret_id = q.id
|
||||
ret_sort = q.sort
|
||||
end
|
||||
if (q.sort <= ret_minsort) then
|
||||
ret_minsort = q.sort
|
||||
end
|
||||
end
|
||||
--core.log("action","meow4" .. dump(ret))
|
||||
return ret_id, ret_sort, ret_minsort
|
||||
end
|
||||
|
||||
function yl_survey_fs.get_previous_q_id_and_sort(questions, current_sort)
|
||||
return get_previous_q_id_and_sort(questions, current_sort)
|
||||
end
|
||||
|
||||
-- Validation
|
||||
@ -100,7 +133,7 @@ end
|
||||
--
|
||||
|
||||
yl_survey_fs.single_answer_formspec = table.concat({
|
||||
"checkbox[0,%s;%s;;%s]", -- answer ID
|
||||
"checkbox[0,%s;checkbox_%s;;%s]", -- answer ID
|
||||
"textarea[0.5,%s;14,1;;;%s]" -- answer text
|
||||
}, "")
|
||||
|
||||
@ -150,7 +183,7 @@ local function construct_answer_formspec(playername, survey_id, question_id)
|
||||
if (r_succeess == false) then
|
||||
return
|
||||
end
|
||||
local answers = q.answers
|
||||
local answers = q.answers or {}
|
||||
local freetext_default = yl_survey_fs.t("freetext_default")
|
||||
local fs_freetext = minetest.formspec_escape(responses["0"] or freetext_default)
|
||||
|
||||
@ -237,11 +270,16 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
return
|
||||
end
|
||||
local current_q_id = 0
|
||||
local next_q_id = yl_survey_fs.get_next_q_id(questions, current_q_id)
|
||||
local current_sort = 0
|
||||
local next_q_id, next_sort = yl_survey_fs.get_next_q_id_and_sort(questions, current_sort)
|
||||
if (next_q_id == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find next q_id"))
|
||||
return
|
||||
end
|
||||
if (next_sort == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find next sort"))
|
||||
return
|
||||
end
|
||||
local formspec = yl_survey_fs.construct_answer_formspec(playername, survey_id, next_q_id)
|
||||
|
||||
if (formspec == nil) then
|
||||
@ -249,10 +287,92 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
return
|
||||
end
|
||||
|
||||
yl_survey_fs.data[playername].survey_id = survey_id
|
||||
-- The survey ID won't change. We know it already and if it's different, THEN there's a problem
|
||||
-- yl_survey_fs.data[playername].survey_id = survey_id
|
||||
yl_survey_fs.data[playername].q_id = next_q_id
|
||||
yl_survey_fs.data[playername].sort = next_sort
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
minetest.show_formspec(playername, formname, formspec)
|
||||
elseif fields.btn_back then
|
||||
|
||||
local q_success, questions = yl_survey.list_questions(survey_id, false)
|
||||
if (q_success == false) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find questions"))
|
||||
return
|
||||
end
|
||||
local current_q_id = yl_survey_fs.data[playername].q_id
|
||||
local current_sort = yl_survey_fs.data[playername].sort
|
||||
local previous_q_id, previous_sort, min_sort = yl_survey_fs.get_previous_q_id_and_sort(questions, current_sort)
|
||||
|
||||
local formspec
|
||||
if (current_sort == min_sort) then
|
||||
-- Back button on first question
|
||||
yl_survey_fs.data[playername].q_id = 0
|
||||
yl_survey_fs.data[playername].sort = 0
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
formspec = yl_survey_fs.construct_frame_formspec(playername, survey_id)
|
||||
else
|
||||
if (previous_q_id == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find previous q_id"))
|
||||
return
|
||||
end
|
||||
if (previous_sort == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find previous sort"))
|
||||
return
|
||||
end
|
||||
formspec = yl_survey_fs.construct_answer_formspec(playername, survey_id, previous_q_id)
|
||||
yl_survey_fs.data[playername].q_id = previous_q_id
|
||||
yl_survey_fs.data[playername].sort = previous_sort
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
end
|
||||
|
||||
if (formspec == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot create formspec"))
|
||||
return
|
||||
end
|
||||
|
||||
minetest.show_formspec(playername, formname, formspec)
|
||||
elseif fields.btn_skip then
|
||||
|
||||
local q_success, questions = yl_survey.list_questions(survey_id, false)
|
||||
if (q_success == false) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find questions"))
|
||||
return
|
||||
end
|
||||
local current_q_id = yl_survey_fs.data[playername].q_id
|
||||
local current_sort = yl_survey_fs.data[playername].sort
|
||||
local next_q_id, next_sort, max_sort = yl_survey_fs.get_next_q_id_and_sort(questions, current_sort)
|
||||
core.log("action","max_sort="..dump(max_sort))
|
||||
core.log("action","current_sort="..dump(current_sort))
|
||||
local formspec
|
||||
if (current_sort == max_sort) then
|
||||
-- Skip button on last question
|
||||
yl_survey_fs.data[playername].q_id = 0
|
||||
yl_survey_fs.data[playername].sort = 0
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
formspec = yl_survey_fs.construct_frame_formspec(playername, survey_id)
|
||||
else
|
||||
if (next_q_id == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find next q_id"))
|
||||
return
|
||||
end
|
||||
if (next_sort == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot find next sort"))
|
||||
return
|
||||
end
|
||||
formspec = yl_survey_fs.construct_answer_formspec(playername, survey_id, next_q_id)
|
||||
yl_survey_fs.data[playername].q_id = next_q_id
|
||||
yl_survey_fs.data[playername].sort = next_sort
|
||||
yl_survey_fs.data[playername].response = {}
|
||||
end
|
||||
|
||||
if (formspec == nil) then
|
||||
minetest.log("warning",yl_survey_fs.t("cannot create formspec"))
|
||||
return
|
||||
end
|
||||
|
||||
minetest.show_formspec(playername, formname, formspec)
|
||||
|
||||
end
|
||||
|
||||
end)
|
Loading…
Reference in New Issue
Block a user