<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewmemberall.php
 * Began: Thu December 19 2002
 *
 * $Id: viewmemberall.php 4 2006-05-08 17:01:47Z tsigo $
 *
 ******************************/

define('EQDKP_INC', true);
$eqdkp_root_path = './spdkp/';
include_once($eqdkp_root_path . 'common.php');
// MODIFICATION, ItemStat http://itemstats.free.fr === by Yahourt / Thorkal == EU Elune / Horde =========
include_once($eqdkp_root_path . 'eqdkp_config_itemstats.php');
include_once($eqdkp_root_path . path_itemstats . '/eqdkp_itemstats.php');
//========================================================================================================

$user->check_auth('u_member_view');

if ( (isset($_GET[URI_NAME])) && (strval($_GET[URI_NAME] != '')) )
{
    $sort_order = array(
        0 => array('raid_name', 'raid_name desc'),
        1 => array('raid_count desc', 'raid_count')
    );

    $current_order = switch_order($sort_order);
    
	// get the member info for each zone
	$membersql = 'select member_firstraid, member_lastraid, member_name, "sp" as zone, member_earned, member_spent, member_earned-member_spent as member_current, c.class_name from spdkp_members m, spdkp_classes c where member_name="' . $_GET[URI_NAME] . '"  and c.class_id = m.member_class_id  union distinct
		select member_firstraid, member_lastraid, member_name, "mh" as zone, member_earned, member_spent, member_earned-member_spent as member_current, c.class_name from mhdkp_members m, mhdkp_classes c where member_name="' . $_GET[URI_NAME] . '"  and c.class_id = m.member_class_id  union distinct
		select member_firstraid, member_lastraid, member_name, "bt" as zone, member_earned, member_spent, member_earned-member_spent as member_current, c.class_name from btdkp_members m, btdkp_classes c where member_name="' . $_GET[URI_NAME] . '" and c.class_id = m.member_class_id';
	
	// send the query in
	$member_data = $db->query($membersql);
	
	// the array that'll hold our member data
	$memberarray = array(	"sp" => array("name" => '-', "earned" => '-', "spent" => '-', "points" => '-', "class" => '-'),
							"mh" => array("name" => '-', "earned" => '-', "spent" => '-', "points" => '-', "class" => '-'),
							"bt" => array("name" => '-', "earned" => '-', "spent" => '-', "points" => '-', "class" => '-'));
	
	// save the name/class info
	$member_class = "";
	$member_last_raid = 0;
	$member_first_raid = time();

	// run through each row for the member. each row represents a raid zone
	while($memberdatarow = $db->fetch_record($member_data))
	{
		// check if we dont have a name/class yet
		if("" == $member_class)
			$member_class = $memberdatarow["class_name"];

		// check if we need to update our last raid
		if($member_last_raid < $memberdatarow["member_lastraid"])
			$member_last_raid = $memberdatarow["member_lastraid"];

		// check if we need to update our first raid
		if($member_first_raid > $memberdatarow["member_firstraid"])
			$member_first_raid = $memberdatarow["member_firstraid"];

		// set the member data for each zone
		$memberarray[$memberdatarow["zone"]]["name"] = $member_class;
		$memberarray[$memberdatarow["zone"]]["earned"] = (int) $memberdatarow["member_earned"];
		$memberarray[$memberdatarow["zone"]]["spent"] = (int) $memberdatarow["member_spent"];
		$memberarray[$memberdatarow["zone"]]["points"] = (int) $memberdatarow["member_current"];
		$memberarray[$memberdatarow["zone"]]["class"] = $memberdatarow["member_class"];
	}
    
    // find the percent of raids they've attended in the last 30, 60 and 90 days
    $percent_of_raids = array(
        'class'    => $member_class,
        '30'       => raid_count(mktime(0, 0, 0, date('m'), date('d')-30, date('Y')), time(), $_GET[URI_NAME]),
        '60'       => raid_count(mktime(0, 0, 0, date('m'), date('d')-60, date('Y')), time(), $_GET[URI_NAME]),
        '90'       => raid_count(mktime(0, 0, 0, date('m'), date('d')-90, date('Y')), time(), $_GET[URI_NAME]),
        'lifetime' => raid_count($member_first_raid, $member_last_raid, $_GET[URI_NAME]));





    // raid Attendance
    $rstart = ( isset($_GET['rstart']) ) ? $_GET['rstart'] : 0;

	$totalraidsql = 'SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value FROM spdkp_raids r, spdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"  union all
					SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value FROM btdkp_raids r, btdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"  union all
					SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value FROM mhdkp_raids r, mhdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"';
			
	$db->query($totalraidsql);
	$total_attended_raids = $db->num_rows();

	$sql = 'SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value, "spdkp" as folder, "Sunwell Plateau" as zone FROM spdkp_raids r, spdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"  union all
			SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value, "btdkp" as folder, "Black Temple" as zone FROM btdkp_raids r, btdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"  union all
			SELECT r.raid_id, raid_name, raid_date, raid_note, raid_value, "mhdkp" as folder, "Mount Hyjal" as zone FROM mhdkp_raids r, mhdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $_GET[URI_NAME] . '"  
			ORDER BY raid_date desc
			LIMIT '.$rstart.',25';
	        
	if(!($raids_result = $db->query($sql)))
		message_die('Could not obtain raid information', '', __FILE__, __LINE__, $sql);
	
	while( $row = $db->fetch_record($raids_result))
	{
		$tpl->assign_block_vars('raids_row', array(
			'ZONE' => $row['zone'],
			'ROW_CLASS' => $eqdkp->switch_row_class(),
			'DATE' => ( !empty($row['raid_date']) ) ? date($user->style['date_notime_short'], $row['raid_date']) : '&nbsp;',
			'U_VIEW_RAID' => '/' . $row['folder'] . '/' . 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$row['raid_id'],
			'U_VIEW_ZONE' => '/' . $row['folder'] . '/',
			'NAME' => ( !empty($row['raid_name']) ) ? stripslashes($row['raid_name']) : '&lt;<i>Not Found</i>&gt;',
			'NOTE' => ( !empty($row['raid_note']) ) ? stripslashes($row['raid_note']) : '&nbsp;',
			'EARNED' => $row['raid_value']));
	}










    // item purchase history
    $istart = ( isset($_GET['istart']) ) ? $_GET['istart'] : 0;

    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'];

	$totalraidsql = 'SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Sunwell Plateau" as zone, "spdkp" as folder
				FROM spdkp_items i, spdkp_members m, spdkp_classes c, spdkp_raids r
				WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"
				union all
				SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Mount Hyjal" as zone, "mhdkp" as folder
				FROM mhdkp_items i, mhdkp_members m, mhdkp_classes c, mhdkp_raids r
				WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"
				union all
				SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Black Temple" as zone, "btdkp" as folder
				FROM btdkp_items i, btdkp_members m, btdkp_classes c, btdkp_raids r
				WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"';
			
	$db->query($totalraidsql);
	$total_purchased_items = $db->num_rows();

    $sql = 'SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Sunwell Plateau" as zone, "spdkp" as folder
			FROM spdkp_items i, spdkp_members m, spdkp_classes c, spdkp_raids r
			WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"
			union all
			SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Mount Hyjal" as zone, "mhdkp" as folder
			FROM mhdkp_items i, mhdkp_members m, mhdkp_classes c, mhdkp_raids r
			WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"
			union all
			SELECT i.item_id, i.item_name, i.item_buyer, i.item_date, i.raid_id, i.item_value, r.raid_name, c.class_name as member_class, "Black Temple" as zone, "btdkp" as folder
			FROM btdkp_items i, btdkp_members m, btdkp_classes c, btdkp_raids r
			WHERE r.raid_id=i.raid_id and i.item_buyer=m.member_name and m.member_class_id=c.class_id and i.item_buyer="' . $_GET[URI_NAME] . '"
            ORDER BY item_date desc
            LIMIT '.$istart.','.'25';

// Regardless of which listitem page they're on, we're essentially
// outputting the same stuff. Purchase History just has a buyer column.
if ( !($items_result = $db->query($sql)) )
{
    message_die('Could not obtain item information', '', __FILE__, __LINE__, $sql);
}

while ( $item = $db->fetch_record($items_result) )
{
	
	$itemvalue =  $item['item_value'];

// MODIFICATION, ItemStat http://itemstats.free.fr === by Yahourt / Thorkal == EU Elune / Horde =========
    if (function_exists('itemstats_decorate_name'))
    {
        $tpl->assign_block_vars('items_row', array(
            'ROW_CLASS' => $eqdkp->switch_row_class(),
            'DATE' => ( !empty($item['item_date']) ) ? date($user->style['date_notime_short'], $item['item_date']) : '&nbsp;',
            'BUYER' => ( !empty($item['item_buyer']) ) ? $item['item_buyer'] : '&lt;<i>Not Found</i>&gt;',
            'BUYER_CLASS'  => $item['member_class'],
            'U_VIEW_BUYER' => '/' . 'viewmemberall.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_buyer'],
            'NAME' => itemstats_decorate_name(stripslashes($item['item_name'])),
            'U_VIEW_ITEM' => '/' . $item['folder'] . '/' . 'viewitem.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'],
            'RAID' => ( !empty($item['raid_name']) ) ? stripslashes($item['raid_name']) : '&lt;<i>Not Found</i>&gt;',
			'U_VIEW_ZONE' => '/' . $item['folder'] . '/',
			'U_VIEW_RAID' => '/' . $item['folder'] . '/' . 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$item['raid_id'],
            'VALUE' => $itemvalue)
            );
    }
//========================================================================================================
    else
	{
		$tpl->assign_block_vars('items_row', array(
				'ROW_CLASS' => $eqdkp->switch_row_class(),
				'DATE' => ( !empty($item['item_date']) ) ? date($user->style['date_notime_short'], $item['item_date']) : '&nbsp;',
				'BUYER' => ( !empty($item['item_buyer']) ) ? $item['item_buyer'] : '&lt;<i>Not Found</i>&gt;',
				'BUYER_CLASS'  => $item['member_class'],
				'U_VIEW_BUYER' => 'viewmemberall.php'.$SID.'&amp;' . URI_NAME . '='.$item['item_buyer'],
				'NAME' => stripslashes($item['item_name']),
				'U_VIEW_ITEM' => 'viewitem.php'.$SID.'&amp;' . URI_ITEM . '='.$item['item_id'],
				'RAID' => ( !empty($item['raid_name']) ) ? stripslashes($item['raid_name']) : '&lt;<i>Not Found</i>&gt;',
				'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$item['raid_id'],
				'VALUE' => $itemvalue)
		);
	}
}

$db->free_result($items_result);


    $tpl->assign_vars(array(
        'GUILDTAG' => $eqdkp->config['guildtag'],
        'NAME'     => $_GET[URI_NAME],
        'CLASS'     => $member_class,

        'L_EARNED'                        => $user->lang['earned'],
        'L_SPENT'                         => $user->lang['spent'],
        'L_ADJUSTMENT'                    => $user->lang['adjustment'],
        'L_CURRENT'                       => $user->lang['current'],
        'L_RAIDS_30_DAYS'                 => sprintf($user->lang['raids_x_days'], 30),
        'L_RAIDS_60_DAYS'                 => sprintf($user->lang['raids_x_days'], 60),
        'L_RAIDS_90_DAYS'                 => sprintf($user->lang['raids_x_days'], 90),
        'L_RAIDS_LIFETIME'                => sprintf($user->lang['raids_lifetime'],
                                                date($user->style['date_notime_short'], $member_first_raid),
                                                date($user->style['date_notime_short'], $member_last_raid)),
        'L_RAID_ATTENDANCE_HISTORY'       => $user->lang['raid_attendance_history'],
        'L_DATE'                          => $user->lang['date'],
        'L_NAME'                          => $user->lang['name'],
        'L_NOTE'                          => $user->lang['note'],
        'L_EARNED'                        => $user->lang['earned'],
        'L_CURRENT'                       => $user->lang['current'],
        'L_ITEM_PURCHASE_HISTORY'         => $user->lang['item_purchase_history'],
        'L_RAID'                          => $user->lang['raid'],
        'L_INDIVIDUAL_ADJUSTMENT_HISTORY' => $user->lang['individual_adjustment_history'],
        'L_REASON'                        => $user->lang['reason'],
        'L_ADJUSTMENT'                    => $user->lang['adjustment'],
        'L_ATTENDANCE_BY_EVENT'           => $user->lang['attendance_by_event'],
        'L_EVENT'                         => $user->lang['event'],
        'L_PERCENT'                       => $user->lang['percent'],

        'O_EVENT'   => $current_order['uri'][0],
        'O_PERCENT' => $current_order['uri'][1],

        'SPEARNED'         => $memberarray['sp']['earned'],
        'SPSPENT'          => $memberarray['sp']['spent'],
        'SPCURRENT'        => $memberarray['sp']['points'],

        'BTEARNED'         => $memberarray['bt']['earned'],
        'BTSPENT'          => $memberarray['bt']['spent'],
        'BTCURRENT'        => $memberarray['bt']['points'],

        'MHEARNED'         => $memberarray['mh']['earned'],
        'MHSPENT'          => $memberarray['mh']['spent'],
        'MHCURRENT'        => $memberarray['mh']['points'],
        
        'RAIDS_30_DAYS'  => sprintf($user->lang['of_raids'], $percent_of_raids['30']),
        'RAIDS_60_DAYS'  => sprintf($user->lang['of_raids'], $percent_of_raids['60']),
        'RAIDS_90_DAYS'  => sprintf($user->lang['of_raids'], $percent_of_raids['90']),
        'RAIDS_LIFETIME' => sprintf($user->lang['of_raids'], $percent_of_raids['lifetime']),

        'C_RAIDS_30_DAYS'  => color_item($percent_of_raids['30'], true),
        'C_RAIDS_60_DAYS'  => color_item($percent_of_raids['60'], true),
        'C_RAIDS_90_DAYS'  => color_item($percent_of_raids['90'], true),
        'C_RAIDS_LIFETIME' => color_item($percent_of_raids['lifetime'], true),

        'RAID_FOOTCOUNT'       => sprintf($user->lang['viewmember_raid_footcount'], $total_attended_raids, '25'),
        'RAID_PAGINATION'      => generate_pagination('viewmemberall.php'.$SID.'&amp;name='.$_GET[URI_NAME].'&amp;istart='.$istart, $total_attended_raids, '25', $rstart, 'rstart'),
        'ITEM_FOOTCOUNT'       => sprintf($user->lang['viewmember_item_footcount'], $total_purchased_items, '25'),
        'ITEM_PAGINATION'      => generate_pagination('viewmemberall.php'.$SID.'&amp;name='.$_GET[URI_NAME].'&amp;rstart='.$rstart, $total_purchased_items, '25', $istart, 'istart'),

        'U_VIEW_MEMBER' => 'viewmemberall.php' . $SID . '&amp;' . URI_NAME . '=' . $_GET[URI_NAME] . '&amp;')
    );

    $db->free_result($adjustments_result);

    $pm->do_hooks('/viewmemberall.php');

    $eqdkp->set_vars(array(
        'page_title'    => sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.sprintf($user->lang['viewmember_title'], $_GET[URI_NAME]),
        'template_file' => 'viewmemberall.html',
        'display'       => true)
    );
}
else
{
    message_die($user->lang['error_invalid_name_provided']);
}

// ---------------------------------------------------------
// Page-specific functions
// ---------------------------------------------------------
function raid_count($start_date, $end_date, $member_name)
{
	// dbdbdb!
    global $db;

	// setup the raid attendance shit
	$individual_raid_count_30 = 0;
	
	// the raid attendance query
	$rc_sql = 'select * from spdkp_raids r, spdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $member_name . '" and (r.raid_date between ' . $start_date . ' and ' . $end_date . ') union all
		select * from mhdkp_raids r, mhdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $member_name . '" and (r.raid_date between ' . $start_date . ' and ' . $end_date . ') union all
		select * from btdkp_raids r, btdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $member_name . '" and (r.raid_date between ' . $start_date . ' and ' . $end_date . ')';
		
	// query baby, query
	$db->query($rc_sql);
	$individual_raid_count_30 = $db->num_rows();

	// the total raid count query
	$total_raid_count_query = 'select * from spdkp_raids where raid_date between ' . $start_date . ' and ' . $end_date . ' union all
		select * from mhdkp_raids where raid_date between ' . $start_date . ' and ' . $end_date . ' union all
		select * from btdkp_raids where raid_date between ' . $start_date . ' and ' . $end_date;
		
	// do the total raid count query
	$db->query($total_raid_count_query);
	$raid_count_30 = $db->num_rows();
	
	// finally make the calculation
	$percent_of_raids_30 = ( $raid_count_30 > 0 ) ? round(($individual_raid_count_30 / $raid_count_30) * 100) : 0;

	// return the %
    return $percent_of_raids_30;
}
?>
