PerBonomi
10 years agoBoss
Give users badges for solutions in specific boards
Thought I'd share this code to give users badges for getting solutions in specific boards. Can alos be fairly easily re-written to look for posts or replies, instead.
Feedback is welcome.
<#if !user.anonymous> <#assign boardLookIn = ["boardid1","boardid2"]/> <#-- Include all the board ids you want to look for --> <#assign brd = "${coreNode.id}"/> <#-- Check current board id --> <#if boardLookIn?seq_contains(brd) == true> <#-- Only continue if user is in one of the boards you want to look for (Avoid running the entire script on every page.) --> <#assign postsInBoard = rest("/users/id/${user.id}/posts/in/boards/id/${brd}/count").value?number/> <#if (postsInBoard >= 10)> <#-- Only continue if user has more than 10 posts in this board --> <#if brd == "board1"> <#-- check first board --> <#assign has_Sol_board1_10 = "false" /> <#-- Variable to check against user roles, call it anything you like --> <#assign has_Sol_board1_20 = "false" /> <#assign has_Sol_board1_30 = "false" /> <#assign has_Sol_board1_50 = "false" /> <#assign has_Sol_board1_100 = "false" /> <#assign has_Sol_board1_200 = "false" /> <#assign selfRoles = restadmin("/users/self/roles").roles.role /> <#list selfRoles as selfRole> <#if selfRole.name == "has_Sol_board1_10"><#assign has_Sol_board1_10 = "true" /></#if> <#-- start checking if user already has certain roles --> <#if selfRole.name == "has_Sol_board1_20"><#assign has_Sol_board1_20 = "true" /></#if> <#if selfRole.name == "has_Sol_board1_30"><#assign has_Sol_board1_30 = "true" /></#if> <#if selfRole.name == "has_Sol_board1_50"><#assign has_Sol_board1_50 = "true" /></#if> <#if selfRole.name == "has_Sol_board1_100"><#assign has_Sol_board1_100 = "true" /></#if> <#if selfRole.name == "has_Sol_board1_200"><#assign has_Sol_board1_200 = "true" /></#if> </#list> <#assign x= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE is_solution = true AND author.id = '${user.id}' AND board.id = '${brd}'"?url) /> <#assign userSolutions = x.data.count/> <#if (userSolutions >= 10) && has_Sol_board1_10 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_10/users/add?role.user=id/${user.id}") /></#if> <#-- if user has required amount of solutions and doesn't already have the role, assign it now --> <#if (userSolutions >= 20) && has_Sol_board1_20 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_20/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 30) && has_Sol_board1_30 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_30/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 50) && has_Sol_board1_50 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_50/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 100) && has_Sol_board1_100 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_100/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 200) && has_Sol_board1_200 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board1_200/users/add?role.user=id/${user.id}") /></#if> </#if> <#if brd == "board2"> <#-- check second board; rinse, repeat --> <#assign has_Sol_board2_10 = "false" /> <#assign has_Sol_board2_20 = "false" /> <#assign has_Sol_board2_30 = "false" /> <#assign has_Sol_board2_50 = "false" /> <#assign has_Sol_board2_100 = "false" /> <#assign has_Sol_board2_200 = "false" /> <#assign selfRoles = restadmin("/users/self/roles").roles.role /> <#list selfRoles as selfRole> <#if selfRole.name == "has_Sol_board2_10"><#assign has_Sol_board2_10 = "true" /></#if> <#if selfRole.name == "has_Sol_board2_20"><#assign has_Sol_board2_20 = "true" /></#if> <#if selfRole.name == "has_Sol_board2_30"><#assign has_Sol_board2_30 = "true" /></#if> <#if selfRole.name == "has_Sol_board2_50"><#assign has_Sol_board2_50 = "true" /></#if> <#if selfRole.name == "has_Sol_board2_100"><#assign has_Sol_board2 = "true" /></#if> <#if selfRole.name == "has_Sol_board2_200"><#assign has_Sol_board2 = "true" /></#if> </#list> <#assign x= rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE is_solution = true AND author.id = '${user.id}' AND board.id = '${brd}'"?url) /> <#assign userSolutions = x.data.count/> <#if (userSolutions >= 10) && has_Sol_board2_10 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_10/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 20) && has_Sol_board2_20 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_20/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 30) && has_Sol_board2_30 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_30/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 50) && has_Sol_board2_50 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_50/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 100) && has_Sol_board2_100 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_100/users/add?role.user=id/${user.id}") /></#if> <#if (userSolutions >= 200) && has_Sol_board2_200 == "false"> <#assign result = restadmin("/roles/name/has_Sol_board2_200/users/add?role.user=id/${user.id}") /></#if> </#if> </#if> </#if> </#if>