<?php // $Id: user_access_details.php 9707 2007-12-12 13:46:31Z mlaurent $
/**
 * CLAROLINE
 *
 * @version 1.6 *
 *
 * @copyright 2001-2007 Universite catholique de Louvain (UCL)
 * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE
 * @author see CREDITS.txt
 *
 */
require '../inc/claro_init_global.inc.php';

$nameTools = get_lang('User access details');

$interbredcrump[]= array ("url"=>"courseLog.php", "name"=> get_lang('Statistics'));

$tbl_mdb_names       = claro_sql_get_main_tbl();
$TABLEUSER           = $tbl_mdb_names['user'  ];
$tbl_cdb_names       = claro_sql_get_course_tbl();
$TABLETRACK_ACCESS        = $tbl_cdb_names['track_e_access'];
$TABLETRACK_DOWNLOADS        = $tbl_cdb_names['track_e_downloads'];

include get_path('incRepositorySys') . '/lib/statsUtils.lib.inc.php';

$toolTitle['mainTitle'] = $nameTools;

$is_allowedToTrack = claro_is_course_manager();

include get_path('incRepositorySys') . '/claro_init_header.inc.php';

if( $is_allowedToTrack && get_conf('is_trackingEnabled') )
{
     if( isset($_REQUEST['cmd']) && ( $_REQUEST['cmd'] == 'tool' && !empty($_REQUEST['id']) ) )
    {
            // set the subtitle for the echo claro_html_tool_title function
            $sql = "SELECT `access_tlabel` AS `label`
                    FROM `" . $TABLETRACK_ACCESS . "`
                    WHERE `access_tid` = ". (int)$_REQUEST['id']."
                    GROUP BY `access_tid`" ;

            $viewedToolLabel = claro_sql_query_get_single_row($sql);

            if( isset($viewedToolLabel['label']) && isset($toolNameList[$viewedToolLabel['label']]) )
                    $toolTitle['subTitle'] = get_lang('Tool')." : ".$toolNameList[$viewedToolLabel['label']];


            // prepare SQL query
            $sql = "SELECT `nom` AS `lastName`,
                        `prenom` AS `firstName`,
                        MAX(UNIX_TIMESTAMP(`access_date`)) AS `data`,
                        COUNT(`access_date`) AS `nbr`
                    FROM `".$TABLETRACK_ACCESS."`
                    LEFT JOIN `".$TABLEUSER."`
                    ON `access_user_id` = `user_id`
                    WHERE `access_tid` = '". (int)$_REQUEST['id']."'
                    GROUP BY `nom`, `prenom`
                    ORDER BY `nom`, `prenom`";
    }
    elseif( isset($_REQUEST['cmd']) && ( $_REQUEST['cmd'] == 'doc' && !empty($_REQUEST['path']) ) )
    {
            // set the subtitle for the echo claro_html_tool_title function
            $toolTitle['subTitle'] = get_lang('Documents and Links')." : ". htmlspecialchars($_REQUEST['path']);
            // prepare SQL query
            $sql = "SELECT `nom` as `lastName`,
                        `prenom` as `firstName`,
                        MAX(UNIX_TIMESTAMP(`down_date`)) AS `data`,
                        COUNT(`down_date`) AS `nbr`
                    FROM `".$TABLETRACK_DOWNLOADS."`
                    LEFT JOIN `".$TABLEUSER."`
                    ON `down_user_id` = `user_id`
                    WHERE `down_doc_path` = '". addslashes($_REQUEST['path']) ."'
                    GROUP BY `nom`, `prenom`
                    ORDER BY `nom`, `prenom`";
    }
    else
    {
        $dialogBox = get_lang('Wrong operation');
    }

    echo claro_html_tool_title($toolTitle);

    if( isset($dialogBox) ) echo claro_html_message_box($dialogBox);


    // TODO  use datagrid
    echo '<br />' . "\n\n"
    .    '<table class="claroTable" border="0" cellpadding="5" cellspacing="1">' . "\n"
    .    '<tr class="headerX">'."\n"
    .    '<th>' . get_lang('Username') . '</th>' . "\n"
    .    '<th>' . get_lang('Last access') . '</th>' . "\n"
    .    '<th>' . get_lang('Access count') . '</th>' . "\n"
    .    '</tr>' . "\n"
    .    '<tbody>' . "\n\n"
    ;

    $i = 0;
    $anonymousCount = 0;
    if( isset($sql) )
    {
        $accessList = claro_sql_query_fetch_all($sql);
        // display the list
        foreach ( $accessList as $userAccess )
        {
            $userName = $userAccess['lastName']." ".$userAccess['firstName'];
            if( empty($userAccess['lastName']) )
            {
                 $anonymousCount = $userAccess['nbr'];
                continue;
            }
            $i++;
            echo '<tr>' . "\n"
            .    '<td>' . $userName . '</td>' . "\n"
            .    '<td>' . claro_html_localised_date(get_locale('dateTimeFormatLong'), $userAccess['data']) . '</td>' . "\n"
            .    '<td>' . $userAccess['nbr'] . '</td>' . "\n"
            .    '</tr>' . "\n\n"
            ;
        }
    }
    // in case of error or no results to display
    if( $i == 0 || !isset($sql) )
    {
        echo '<td colspan="3">' . "\n"
        .    '<center>' . get_lang('No result') . '</center>' . "\n"
        .    '</td>' . "\n\n"
        ;
    }

    echo '</tbody>' . "\n\n"
    .    '</table>' . "\n\n"
    ;

    if( $anonymousCount != 0 )
    {
        echo '<p>'.get_lang('Anonymous users access count : ').' '.$anonymousCount.'</p>'."\n";
    }

}
// not allowed
else
{
    if(!get_conf('is_trackingEnabled'))
    {
        echo get_lang('Tracking has been disabled by system administrator.');
    }
    else
    {
        echo get_lang('Not allowed');
    }
}

// footer
include get_path('incRepositorySys') . '/claro_init_footer.inc.php';
?>