I just tested out polls for the first time in a while, and it works quite well and I like it.
But I would like to have an open poll so we can see who voted for which option. Is this possible? Is there any way to see who voted for what?
I just tested out polls for the first time in a while, and it works quite well and I like it.
But I would like to have an open poll so we can see who voted for which option. Is this possible? Is there any way to see who voted for what?
You can run the following query to get a list of users who voted and their selected poll options.
Please choose the appropriate query for your Discourse version.
-- [params]
-- post_id :post_id
-- text :poll_name = poll
SELECT u.id AS user_id, u.username, v.poll_option_id AS option_id, o.html AS option_html
FROM polls p
JOIN poll_votes v ON (p.id = v.poll_id)
JOIN poll_options o ON (o.id = v.poll_option_id)
JOIN users u ON (v.user_id = u.id)
WHERE p.post_id = :post_id AND p.name = :poll_name
ORDER BY u.username, v.poll_option_id
-- [params]
-- post_id :post_id
-- text :poll_name = poll
SELECT votes.user_id, users.username, votes.option_id, options.html AS option_html
FROM (
SELECT value1 ->> 'id' AS id,
value1 ->> 'html' AS html
FROM (
SELECT json_array_elements(value :: JSON -> :poll_name -> 'options') AS value1
FROM post_custom_fields
WHERE post_id = :post_id AND name = 'polls' AND value :: JSON -> :poll_name ->> 'name' = :poll_name
) option_values
) options
JOIN (
SELECT key :: INTEGER AS user_id,
trim(json_array_elements(value :: JSON -> :poll_name) :: TEXT, '"') AS option_id
FROM json_each((
SELECT value :: JSON
FROM post_custom_fields
WHERE post_id = :post_id AND name LIKE 'polls-votes'
))
) votes ON (options.id = votes.option_id)
JOIN users ON (votes.user_id = users.id)
ORDER BY users.username, votes.option_id
You can use the following query if you need to find the right post_id.
-- [params]
-- topic_id :topic_id
-- int :post_number = 1
SELECT id
FROM posts
WHERE topic_id = :topic_id AND post_number = :post_number
it works! thanks so much. 
is there a query for quickly identifying the post ID containing polls? I used the .json URL method but it was surprisingly cumbersome to find the right post within it.
Default values are specified like this:
I updated the query. It’s now a lot simpler. ![]()
有没有办法根据账号注册时间来筛选结果?
例如,如果账号注册时间少于 60 天,则不会在结果中显示?或者,是否有办法创建一个投票,限制只有符合特定账号注册时长的用户才能投票?
我们试图防止即将进行的重要投票中出现由新创建账号进行的虚假投票。
这是一个非常有趣的使用场景。我确实认为我们应该提供一个选项,防止 TL0 用户在投票中投票 @zogstrip!
我能否在 @gerhard 提供的搜索字符串中添加一个参数,以便在查询中不显示信任等级或账户年龄低于特定阈值的用户?
即使我需要手动统计结果中的投票,这也会比手动检查每个账户的年龄及其投票更快。
以下是为设置用户最低信任等级而编写的查询。
-- [params]
-- post_id :post_id
-- text :poll_name = poll
-- integer :min_trust_level
SELECT u.id AS user_id, u.username, v.poll_option_id AS option_id, o.html AS option_html
FROM polls p
JOIN poll_votes v ON (p.id = v.poll_id)
JOIN poll_options o ON (o.id = v.poll_option_id)
JOIN users u ON (v.user_id = u.id)
WHERE p.post_id = :post_id AND p.name = :poll_name
AND u.trust_level >= :min_trust_level
ORDER BY u.username, v.poll_option_id
以下是仅显示在特定天数前注册用户的查询。
-- [params]
-- post_id :post_id
-- text :poll_name = poll
-- integer :days_ago = 30
SELECT u.id AS user_id, u.username, v.poll_option_id AS option_id, o.html AS option_html
FROM polls p
JOIN poll_votes v ON (p.id = v.poll_id)
JOIN poll_options o ON (o.id = v.poll_option_id)
JOIN users u ON (v.user_id = u.id)
WHERE p.post_id = :post_id AND p.name = :poll_name
AND u.created_at < NOW() - INTERVAL ':days_ago days'
ORDER BY u.username, v.poll_option_id
太棒了
@gerhard 谢谢!
请注意,此查询现已包含在自动安装的默认数据浏览器查询中,因此您无需手动输入此代码!
另一种查找帖子 ID 的方法(尤其是当它是首帖时)是查看 <topic_url>.json(例如:https://meta.discourse.org/t/is-it-possible-to-see-who-voted-in-polls/27064.json)。在该 JSON 文件的开头部分,可以很容易地找到帖子 ID。
@nbianca 能否在你的列表中新增一个投票参数 min_trust_level,以便让投票创建者根据用户的信任等级限制谁能参与投票?
该选项应出现在投票构建器界面中,并且需要显示一条友好的提示信息,说明某些用户为何无法对该投票进行投票。
你甚至不需要查找帖子 ID,因为现在工作人员在投票中已有导出按钮。
此功能已实现,但实现方式与请求略有不同。投票现在接受 groups 选项,而非 min_trust_level。该选项接受逗号分隔的组名列表(例如 trust_level_2,staff,表示允许信任等级 2 及以上用户及所有工作人员投票)。
抱歉,这对我来说太令人困惑了,你能帮我分解一下吗?