What's the right way to use the value of a variable in a User Macro
Asked by Brainshark Operations:

I"ve got a user macro that has some code like this in it:  

```
#set($containerManagerClass = $content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod = $containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager = $getInstanceMethod.invoke(null,null))
#set($containerContext = $containerManager.containerContext)
#set($loginManager = $containerContext.getComponent('loginManager'))
#set($users = $userAccessor.getUsers())
#set($numUsers = $users.size())

<div class="aui-message closeable">
Total Licenses Being Used: ${numUsers}
</div>



```

I know that $users has elements in it, because later on when I loop over it, I can print out each user on the page. I've tried a number of different reasonable looking syntaxes for trying to print out the value of numUsers, but I'm not getting that value on my page.  

What's the proper way to reference a variable in my User Macro?   

I've tried the above as well as:  
$numUsers  
#$numUsers  
$!{numUsers} - which was just blank  

<br />


Answers:
author: JP _AC Bielefeld Leader_
body: Hi,

the number of users of the userAccessor.getUsers() does not necessarily reflect the number of consumed licenses. You should count the users of the user groups granted access to Confluence.

Try:

```
## @param group:title=Group|type=string|required=true|desc=User group|default=confluence-users

#set($containerManagerClass = $content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod = $containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager = $getInstanceMethod.invoke(null,null))
#set($containerContext = $containerManager.containerContext)
#set($loginManager = $containerContext.getComponent('loginManager'))

#set($now=$content.currentDate.time)
#set($group = $userAccessor.getGroup($paramgroup))
#if($group)
#set($usernames = $userAccessor.getMemberNames($group))
#end
#set($totalactiveusers = 0)
<table class="confluenceTable" width="660px">
<tr>
<th class="confluenceTh">Name</th>
<th class="confluenceTh">Last login</th> 
<th class="confluenceTh" style="text-align:center;">Days/Status</th>
</tr>
#foreach($username in $usernames)
#set($t3="9999")
#set($user = $userAccessor.getUser($username))
<tr>
<td class="confluenceTd">#usernameLink($user.name)</td>
<td class="confluenceTd" style="text-align:center;">$action.dateFormatter.formatGivenString('yyyy-MM-dd HH:mm',$loginManager.getLoginInfo($user).lastSuccessfulLoginDate)</td>
#if($loginManager.getLoginInfo($user).lastSuccessfulLoginDate)
#set($t3= $now - $loginManager.getLoginInfo($user).lastSuccessfulLoginDate.time)
#set($t3= $t3/1000/24/60/60)
#end
<td class="confluenceTd" style="text-align:center;">
#if($userAccessor.isDeactivated($user))
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Red</ac:parameter>
<ac:parameter ac:name="title">Locked</ac:parameter>
</ac:macro>
#else
#set($totalactiveusers = $totalactiveusers + 1)
#if($t3 < 7) 
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Green</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#elseif($t3 < 31) 
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Blue</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#elseif($t3 < 180) 
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Yellow</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#else 
<ac:macro ac:name="status">
<ac:parameter ac:name="colour">Red</ac:parameter>
<ac:parameter ac:name="title">$t3</ac:parameter>
</ac:macro>
#end
#end
</td>
</tr>
#end
</table>
<p>Active users in group <b>$paramgroup</b>: $totalactiveusers</p>
```

Best

JP


Tags: confluence-server

server