/**
 * Show the table for the user's edit and comment (etc) summary info
 *
 * @param unknown_type $subwiki  the subwiki info
 * @param unknown_type $userid   the id of the user
 * @param unknown_type $header   the header text for the tables
 */
function reportsuser_usersummarytable($subwikiid, $userid, $header, $csv)
{
    // get info from database:
    // get the number of pages created by each user in this subwiki
    $usercreates = ouwiki_get_userspagecreate($subwikiid);
    // get the user name, number of edits, number of pages edited,
    //    start and finish days for editing by this user, in this subwiki
    $useredits = ouwiki_get_useredits($subwikiid, $userid);
    // get all comments by this user on this subwiki as well as
    //  the page title and the time the comment was posted
    $usercomments = ouwiki_get_usercomments($subwikiid, $userid);
    if (count($useredits) == 0) {
        $inactive = get_string('report_user_is_inactive', 'ouwiki', fullname(get_record('user', 'id', $userid)));
        if (!$csv) {
            print <<<EOF
<div class='ouw_userlist'>
\t<h3>{$header->user}</h3>
\t<p>{$inactive}</p>
EOF;
        } else {
            print $csv->quote($header->user) . $csv->line() . $csv->quote($inactive) . $csv->line();
        }
        return;
    }
    // get the last element in the array holding the user edits -
    //  there should be only one entry and this is the easy way of getting it
    $userinfo = array_pop($useredits);
    // the full name of the user
    $name = fullname($userinfo);
    // the number of second in a day to calculate days from epoch time
    $day = 60 * 60 * 24;
    // the time of the first edit by the user
    $mintime = $userinfo->startday;
    // the time of the last edit by the user
    $maxtime = $userinfo->lastday;
    // round to the day
    $mintime -= $mintime % $day;
    $maxtime -= $maxtime % $day;
    // get the dates from the times
    $startday = userdate($mintime, get_string('strftimedate'));
    $lastday = userdate($maxtime, get_string('strftimedate'));
    // calculate the number of days between first and last edits
    $daycount = ceil(($maxtime - $mintime) / $day);
    // the number of pages created by the user - if there is no entry for the user, then it must be zero
    $createdpages = array_key_exists($userid, $usercreates) ? $usercreates[$userid]->createdcount : 0;
    // get the number of edited pages
    $editedpages = $userinfo->editedpagecount;
    // get the number of edits
    $edits = $userinfo->editcount;
    // count the number of comments
    $comments = count($usercomments);
    // calculate the number of contributions
    $contributions = $edits + $comments;
    // print out the html for the table - one header row and one data row
    if (!$csv) {
        print <<<EOF
<div class='ouw_userlist'>
\t<h3>{$header->user}</h3>
\t<table>
\t\t<tr>\t\t\t
\t\t\t<th class="ouw_leftcol" scope="col">{$header->username}<div class='ouw_namecolumn'></div></th>
\t\t\t<th scope="col" class='ouw_firstingroup'>{$header->startday}<div class='ouw_datecolumn'></div></th>\t
\t\t\t<th scope="col">{$header->lastday}<div class='ouw_datecolumn'></div></th>
\t\t\t<th scope="col">{$header->timeonwiki}</th>
\t\t\t<th scope="col" class='ouw_firstingroup'>{$header->createdpages}</th>
\t\t\t<th scope="col">{$header->editedpages}</th>
\t\t    <th scope="col">{$header->edits}</th>
\t\t    <th scope="col">{$header->comments}</th>
\t\t\t<!--th scope="col" class='ouw_firstingroup'>{$header->additions}</th>
\t\t\t<th scope="col">{$header->deletes}</th>
\t\t\t<th scope="col">{$header->otheredits}</th-->
\t\t    <th scope="col" class='ouw_rightcol'>{$header->contributions}</th>
\t\t </tr>\t\t 
\t\t<tr>
\t\t\t<td class='ouw_leftcol'>{$name}</td>
\t\t\t<td class='ouw_firstingroup'>{$startday}</td>
\t\t\t<td>{$lastday}</td>
\t\t\t<td>{$daycount}</td>
\t\t\t<td class='ouw_firstingroup'>{$createdpages}</td>\t\t\t        \t
        \t<td>{$editedpages}</td>
        \t<td>{$edits}</td>
        \t<td>{$comments}</td>
\t\t\t<!--td class='ouw_firstingroup'></td>        \t
        \t<td></td>
        \t<td></td-->
        \t<td class='ouw_rightcol'>{$contributions}</td>
        </tr>
   \t</table>
</div>\t
EOF;
    } else {
        print $csv->quote($header->user) . $csv->line() . $csv->quote($header->username) . $csv->sep() . $csv->quote($header->startday) . $csv->sep() . $csv->quote($header->lastday) . $csv->sep() . $csv->quote($header->timeonwiki) . $csv->sep() . $csv->quote($header->createdpages) . $csv->sep() . $csv->quote($header->editedpages) . $csv->sep() . $csv->quote($header->edits) . $csv->sep() . $csv->quote($header->comments) . $csv->sep() . $csv->quote($header->contributions) . $csv->line() . $csv->quote($name) . $csv->sep() . $csv->quote($startday) . $csv->sep() . $csv->quote($lastday) . $csv->sep() . $csv->quote($daycount) . $csv->sep() . $csv->quote($createdpages) . $csv->sep() . $csv->quote($editedpages) . $csv->sep() . $csv->quote($edits) . $csv->sep() . $csv->quote($comments) . $csv->sep() . $csv->quote($contributions) . $csv->line();
    }
}
/**
 * This prints out the table showing user activity info
 *
 * @param $header     the object containing the header text
 * @param $rolenames  the array containing the role names
 */
function reportssummary_usertable($cm, $wikiid, $contexts, $subwiki, $viewgroupid, $header, $rolenames, $csv)
{
    if (count($rolenames) == 0) {
        return;
    }
    $doneheaders = false;
    $usercreates = ouwiki_get_userspagecreate($subwiki->id);
    // Get list of users with required roles (default Tutor and Student)
    // within required contexts [and group] for this wiki
    $usersinfo = array();
    if ($usersroles = ouwiki_get_usersin($contexts, $viewgroupid)) {
        // Get lists of users who can edit and/or comment on this wiki
        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
        if (!($usersedit = get_users_by_capability($context, 'mod/ouwiki:edit', 'u.id', '', '', '', $viewgroupid, '', false))) {
            $usersedit = array();
        }
        if (!($userscomment = get_users_by_capability($context, 'mod/ouwiki:comment', 'u.id', '', '', '', $viewgroupid, '', false))) {
            $userscomment = array();
        }
        // Build list of users who can edit and/or comment
        // and have required role [and group] for this wiki
        $userlist = '';
        foreach ($usersroles as $userid => $user) {
            if (isset($usersedit[$userid]) || isset($userscomment[$userid])) {
                $userlist .= ',' . $userid;
            }
        }
        // If there are any users get user edits and user comments
        if ($userlist) {
            $userlist = substr($userlist, 1);
            $usersinfo = ouwiki_get_usersedits($userlist, $subwiki->id, $viewgroupid);
            $usercomments = ouwiki_get_userscomments($userlist, $subwiki->id, $viewgroupid);
        }
    }
    // for each user in this wiki, use their info
    foreach ($usersinfo as $userinfo) {
        // the user's id number
        $userid = $userinfo->userid;
        // the user's name
        $name = fullname($userinfo);
        $usernamedisp = !class_exists('ouflags') ? '' : $userinfo->username;
        // number of seconds in a day
        $day = 60 * 60 * 24;
        // end day of editing by this user rounded down to the start of the day
        $starttime = $userinfo->startday;
        // end day of editing by this user rounded down to the start of the day
        $endtime = $userinfo->lastday;
        // start day of editing by user in date form
        $startday = $starttime ? userdate($starttime, get_string('strftimedate')) : '';
        // start day of editing by user in date form
        $lastday = $endtime ? userdate($endtime, get_string('strftimedate')) : '';
        // number of days between first and last edits
        $daycount = ceil(($endtime - $starttime) / $day);
        // number of pages created by this user
        $createdpages = array_key_exists($userid, $usercreates) ? $usercreates[$userid]->createdcount : 0;
        // number of pages edited by this user
        $editedpages = $userinfo->editedpagecount;
        // number of edits by this user
        $edits = $userinfo->editcount;
        // number of comments by this user
        $comments = array_key_exists($userid, $usercomments) ? $usercomments[$userid]->commentcount : 0;
        // number of contributions by this user
        $contributions = $edits + $comments;
        if (!$doneheaders) {
            // OK we now know we're printing something, so print headers
            $doneheaders = true;
            if (!$csv) {
                print <<<EOF
<div class="ouw_userlist">
    <h3>{$header->userstabletitle}</h3>    
\t<table>
\t\t<tr>\t\t\t
\t\t\t<th class="ouw_leftcol" scope="col">{$header->username}<div class='ouw_namecolumn'></div></th>
\t\t\t<th scope="col" class='ouw_firstingroup'>{$header->startday}<div class='ouw_datecolumn'></div></th>\t
\t\t\t<th scope="col">{$header->lastday}<div class='ouw_datecolumn'></div></th>
\t\t\t<th scope="col">{$header->timeonwiki}</th>
\t\t\t<th scope="col" class='ouw_firstingroup'>{$header->createdpages}</th>
\t\t\t<th scope="col">{$header->editedpages}</th>
\t\t    <th scope="col">{$header->edits}</th>
\t\t    <th scope="col">{$header->comments}</th>
\t\t\t<!--th scope="col" class='ouw_firstingroup'>{$header->additions}</th>
\t\t\t<th scope="col">{$header->deletes}</th>
\t\t\t<th scope="col">{$header->otheredits}</th-->
\t\t    <th scope="col" class='ouw_rightcol'>{$header->contributions}</th>
\t\t </tr>\t
EOF;
            } else {
                print $csv->line() . $csv->quote($header->userstabletitle) . $csv->line() . $csv->quote($header->username) . $csv->sep() . $csv->quote($header->startday) . $csv->sep() . $csv->quote($header->lastday) . $csv->sep() . $csv->quote($header->timeonwiki) . $csv->sep() . $csv->quote($header->createdpages) . $csv->sep() . $csv->quote($header->editedpages) . $csv->sep() . $csv->quote($header->edits) . $csv->sep() . $csv->quote($header->comments) . $csv->sep() . $csv->quote($header->contributions);
            }
        }
        // print the row of info for this user which includes name, start day of editing, last day,
        //  number of days on wiki, number of created pages, edited pages, edits and commnets and total contributions
        if (!$csv) {
            print <<<EOF
\t\t<tr>
\t\t\t<td class='ouw_leftcol'><a href="reportsuser.php?id={$wikiid}&amp;userid={$userid}">{$name} {$usernamedisp}</a></td>
\t\t\t<td class='ouw_firstingroup'>{$startday}</td>
\t\t\t<td>{$lastday}</td>
\t\t\t<td>{$daycount}</td>
\t\t\t<td class='ouw_firstingroup'>{$createdpages}</td>\t\t\t        \t
        \t<td>{$editedpages}</td>
        \t<td>{$edits}</td>
        \t<td>{$comments}</td>
\t\t\t<!--td class='ouw_firstingroup'></td>        \t
        \t<td></td>
        \t<td></td-->
        \t<td class='ouw_rightcol'>{$contributions}</td>
        </tr>        \t
EOF;
        } else {
            print $csv->line() . $csv->quote($name) . $csv->sep() . $csv->quote($startday) . $csv->sep() . $csv->quote($lastday) . $csv->sep() . $csv->quote($daycount) . $csv->sep() . $csv->quote($createdpages) . $csv->sep() . $csv->quote($editedpages) . $csv->sep() . $csv->quote($edits) . $csv->sep() . $csv->quote($comments) . $csv->sep() . $csv->quote($contributions);
        }
    }
    if ($doneheaders) {
        if (!$csv) {
            // close the table and div tags
            print '
   	</table>
</div>';
        } else {
            print $csv->line();
        }
    }
}