classroom/gui.lua

248 lines
6.3 KiB
Lua

local infos = {
{
title = "Shout?",
type = "priv",
privs = { shout = true },
},
{
title = "Fly?",
type = "priv",
privs = { fly = true },
},
{
title = "Fast?",
type = "priv",
privs = { fast = true },
},
}
local function get_group(context)
if context and context.groupname then
return classroom.get_group_students(context.groupname)
else
return classroom.get_students()
end
end
sfinv.register_page("classroom:edu", {
title = "Classroom",
check_perm = function(self, player)
return minetest.check_player_privs(player:get_player_name(), { teacher = true })
end,
is_in_nav = function(self, player, context)
return self:check_perm(player)
end,
get = function(self, player, context)
if not self:check_perm(player) then
return "label[0,0;Access denied]"
end
local fs = {
"tablecolumns[color;text"
}
context.select_toggle = context.select_toggle or "all"
for i, col in pairs(infos) do
fs[#fs + 1] = ";color;text,align=center"
if i == 1 then
fs[#fs + 1] = ",padding=2"
end
end
fs[#fs + 1] = "]"
fs[#fs + 1] = "tabheader[0.3,1.1;group;All,Group 1;1]"
fs[#fs + 1] = "table[0,0.8;5,8;students;,Name"
for _, col in pairs(infos) do
fs[#fs + 1] = ",," .. col.title
end
local students = get_group(context)
local selection_id = ""
context.students = table.copy(students)
for i, student in pairs(students) do
fs[#fs + 1] = ",,"
fs[#fs + 1] = minetest.formspec_escape(student)
if student == context.selected_student then
selection_id = tostring(i + 1)
end
for _, col in pairs(infos) do
local color, value
if col.type == "priv" then
local has_priv = minetest.check_player_privs(student, col.privs)
color = has_priv and "green" or "red"
value = has_priv and "Yes" or "No"
end
fs[#fs + 1] = ","
fs[#fs + 1] = color
fs[#fs + 1] = ","
fs[#fs + 1] = minetest.formspec_escape(value)
end
end
fs[#fs + 1] = ";"
fs[#fs + 1] = selection_id
fs[#fs + 1] = "]"
fs[#fs + 1] = "container[5.25,1.1]"
fs[#fs + 1] = "box[-0.25,-0.3;3.1,1.15;#666]"
fs[#fs + 1] = "label[0,-0.3;Run actions on:]"
if context.select_toggle == "all" then
fs[#fs + 1] = "box[0,0.1;0.8,0.7;#53ac56]"
fs[#fs + 1] = "label[0.3,0.25;All]"
else
fs[#fs + 1] = "button[0,0;1,1;select_all;All]"
end
if not context.groupname then
fs[#fs + 1] = "box[0.9,0.1;0.8,0.7;#333]"
fs[#fs + 1] = "label[1.05,0.25;Group]"
fs[#fs + 1] = "tooltip[0.9,0;1,1;Please select a group first]"
elseif context.select_toggle == "group" then
fs[#fs + 1] = "box[0.9,0.1;0.8,0.7;#53ac56]"
fs[#fs + 1] = "label[1.05,0.25;Group]"
else
fs[#fs + 1] = "button[0.9,0;1,1;select_group;Group]"
end
if not context.selected_student then
fs[#fs + 1] = "box[1.8,0.1;0.8,0.7;#333]"
fs[#fs + 1] = "label[1.85,0.25;Selected]"
fs[#fs + 1] = "tooltip[1.8,0;1,1;Please select a student first]"
elseif context.select_toggle == "selected" then
fs[#fs + 1] = "box[1.8,0.1;0.8,0.7;#53ac56]"
fs[#fs + 1] = "label[1.85,0.25;Selected]"
else
fs[#fs + 1] = "button[1.8,0;1,1;select_selected;Selected]"
end
fs[#fs + 1] = "label[0,0.9;Actions]"
local x = 0
local y = 1.2
for aname, action in pairs(classroom.get_actions()) do
fs[#fs + 1] = "button["
fs[#fs + 1] = tostring(x)
fs[#fs + 1] = ","
fs[#fs + 1] = tostring(y)
fs[#fs + 1] = ";1,1;action_"
fs[#fs + 1] = aname
fs[#fs + 1] = ";"
fs[#fs + 1] = minetest.formspec_escape(action.title)
fs[#fs + 1] = "]"
fs[#fs + 1] = "tooltip[action_"
fs[#fs + 1] = aname
fs[#fs + 1] = ";"
fs[#fs + 1] = minetest.formspec_escape(action.description)
fs[#fs + 1] = "]"
if x < 1.5 then
x = x + 0.9
else
x = 0
y = y + 0.8
end
end
y = y + 0.25
if context.selected_student then
fs[#fs + 1] = "container[0,"
fs[#fs + 1] = tostring(y)
fs[#fs + 1] = "]"
fs[#fs + 1] = "label[0,-0.1;Selected user: "
fs[#fs + 1] = minetest.formspec_escape(context.selected_student)
fs[#fs + 1] = "]"
fs[#fs + 1] = "button[0,0.3;1.5,1;teleport;Teleport To]"
fs[#fs + 1] = "container_end[]"
end
fs[#fs + 1] = "container_end[]"
return sfinv.make_formspec(player, context, table.concat(fs, ""), false)
end,
on_player_receive_fields = function(self, player, context, fields)
if not self:check_perm(player) then
sfinv.set_page(player, sfinv.get_homepage_name(player))
return true
end
if fields.students then
local evt = minetest.explode_table_event(fields.students)
local i = (evt.row or 0) - 1
if evt.type == "CHG" and i >= 1 and i <= #context.students then
context.selected_student = context.students[i]
sfinv.set_player_inventory_formspec(player)
return true
end
end
if fields.select_all then
context.select_toggle = "all"
sfinv.set_player_inventory_formspec(player)
return true
elseif fields.select_group then
context.select_toggle = "group"
sfinv.set_player_inventory_formspec(player)
return true
elseif fields.select_selected then
context.select_toggle = "selected"
sfinv.set_player_inventory_formspec(player)
return true
end
if fields.teleport and context.selected_student then
local student = minetest.get_player_by_name(context.selected_student)
if student then
player:set_pos(student:get_pos())
return true
else
context.selected_student = nil
sfinv.set_player_inventory_formspec(player)
return true
end
end
for aname, action in pairs(classroom.get_actions()) do
if fields["action_" .. aname] then
local selector
if context.select_toggle == "all" then
selector = "*"
elseif context.select_toggle == "group" then
selector = "group:" .. context.groupname
elseif context.select_toggle == "selected" then
selector = "user:" .. context.selected_student
else
error("Unknown selector")
end
classroom.run_action(aname, player, selector)
sfinv.set_player_inventory_formspec(player)
return true
end
end
end,
})
local function on_grant_revoke(grantee, granter, priv)
if priv ~= "teacher" then
return
end
local player = minetest.get_player_by_name(grantee)
if not player then
return
end
minetest.after(0, function()
sfinv.set_player_inventory_formspec(player)
end)
end
minetest.register_on_priv_grant(on_grant_revoke)
minetest.register_on_priv_revoke(on_grant_revoke)