• Resolved joaquinpn

    (@joaquinpn)


    When I obtain user history, it appears in the dashboard but not in the CSV file when exporting. Would it be possible to add it somehow?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Faiyaz Alam

    (@faiyazalam)

    file path:
    wordpress\wp-content\plugins\WordPress-plugin-user-login-history\admin\class-user-login-history-admin.php

    just replace the following function:

    `private function export_to_CSV() {
    global $wpdb;
    $get_values = array();
    $table = $wpdb->prefix . ULH_TABLE_NAME;
    $table_usermeta = $wpdb->usermeta;
    $table_users = $wpdb->users;

    $sql = “select ”
    . “DISTINCT(FaUserLogin.id) as id, ”
    . “User.user_login,”
    . “FaUserLogin.*, ”
    . “UserMeta.* ”
    . “FROM $table AS FaUserLogin ”
    . ” INNER JOIN $table_users as User ON User.ID = FaUserLogin.user_id”
    . ” INNER JOIN $table_usermeta AS UserMeta ON UserMeta.user_id=FaUserLogin.user_id where UserMeta.meta_key = ‘{$wpdb->prefix}capabilities’ AND 1 “;

    $User_Login_History_List_Table = new User_Login_History_List_Table();
    $where_query = $User_Login_History_List_Table->prepare_where_query();

    if ($where_query) {
    $sql .= $where_query[‘sql_query’];
    $get_values = $where_query[‘values’];
    }
    $sql .= ‘ ORDER BY id DESC’;
    $data = $wpdb->get_results($wpdb->prepare($sql, $get_values), ‘ARRAY_A’);

    //date string to suffix the file nanme: month – day – year – hour – minute
    $suffix = date(‘n-j-y_H-i’);
    // send response headers to the browser
    header(‘Content-Type: text/csv’);
    header(‘Content-Disposition: attachment;filename=login_log_’ . $suffix . ‘.csv’);

    if (!$data) {
    _e(‘No Records Found!’, ‘user-login-history’);
    exit;
    }

    $fp = fopen(‘php://output’, ‘w’);
    $unknown = ‘Unknown’;
    $i = 0;

    foreach ($data as $row) {
    $row[‘current_role’] = implode(”, array_keys(unserialize($row[‘meta_value’])));
    unset($row[‘meta_value’]);
    unset($row[‘meta_key’]);
    unset($row[‘umeta_id’]);

    //add duration column – start
    $start_time_str = strtotime($row[‘time_login’]);
    $logout_time_str = strtotime($row[‘time_logout’]);
    $end_time_str = $logout_time_str > 0 ?$logout_time_str: strtotime($row[‘time_last_seen’]) ;

    $row[‘duration’] = human_time_diff($start_time_str, $end_time_str);
    //add duration column – end

    $row_keys = array_keys($row);

    //output header row
    if (0 == $i) {
    fputcsv($fp, $row_keys);
    }

    foreach ($row_keys as $row_key) {
    if (“” == $row[$row_key]) {
    $row[$row_key] = $unknown;
    }
    }

    fputcsv($fp, $row);
    $i++;
    }
    fclose($fp);
    die();
    }`

    Thread Starter joaquinpn

    (@joaquinpn)

    Hi,

    It gives an error on this line and the whole web does not work:
    $table = $wpdb->prefix . ULH_TABLE_NAME;

    Thank you,

    Joaquín Pérez

    Thread Starter joaquinpn

    (@joaquinpn)

    Sorry, the error is related to the use of &gt in the code.
    $table = $wpdb->prefix . ULH_TABLE_NAME;

    In previous code it appears > character

    Plugin Author Faiyaz Alam

    (@faiyazalam)

    Is it worked?
    I have also added this column ‘duration’ in latest version 1.6.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Session Duration not included in CSV file’ is closed to new replies.