$pagetexts = ouwiki_get_pages($subwiki->id);
$contexts = get_related_contexts_string($context);
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$allroles = get_roles_used_in_context($context, true);
$header->grouptabletitle = get_string('report_grouptabletitle', 'ouwiki');
if (!$csv) {
    print <<<EOF
<div class='ouw_grouplist'>
\t<h3>{$header->grouptabletitle}</h3> 
\t<table>
\t<tr class="ouw_dodgyextrarow">
    \t\t<td>&nbsp;</td>\t
\t\t\t
EOF;
} else {
    print $csv->quote($header->grouptabletitle) . $csv->line() . $csv->sep();
}
$rolenames = array();
$roleids = array();
foreach ($allroles as $role) {
    if (!ouwiki_reports_include_role($role)) {
        continue;
    }
    $rolename = role_get_name($role, $coursecontext);
    $usercount = 0;
    foreach ($groups as $group) {
        $usercount = max(count(ouwiki_get_users($contexts, $group->id, $role->id)), $usercount);
    }
    if ($usercount > 0) {
        $rolenames[$role->id] = $rolename;
        if (!$csv) {
    $choices = array();
    if ($allgroups) {
        $choices[0] = get_string('all');
    }
    foreach ($groups as $group) {
        $choices[$group->id] = $group->name;
    }
    if (!$csv) {
        print '<form method="get" action="reportsgroup.php" class="ouw_reportsgroups"><div>' . ouwiki_display_wiki_parameters(null, $subwiki, $cm, OUWIKI_PARAMS_FORM) . '<label for="ouw_viewgroup">' . get_string('report_grouplabel', 'ouwiki') . '</label> <select name="viewgroup" id="ouw_viewgroup">';
        foreach ($choices as $id => $value) {
            $selected = $id == $viewgroupid ? ' selected="selected"' : '';
            print '<option value="' . $id . '"' . $selected . '>' . htmlspecialchars($value) . '</option>';
        }
        print '</select><input type="submit" value="' . get_string('changebutton', 'ouwiki') . '" /></div></form>' . '<div class="clearer"></div>';
    } else {
        print $csv->quote(htmlspecialchars($choices[$viewgroupid])) . $csv->line();
    }
}
// get data from database
$pagesinfo = ouwiki_get_pages($subwiki->id);
$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
$contexts = get_related_contexts_string($context);
$allroles = get_roles_used_in_context($context, true);
// get all the role ids and names for this group as well as the number of users in each role
$rolenames = array();
$usercounts = array();
foreach ($allroles as $role) {
    // TODO Roles to provide reports for will be selectable
    // For now restrict ('hardcode') reports to student and tutor/teacher roles only
    if (ouwiki_reports_include_role($role)) {
        $roleid = $role->id;