<?php
/******************************
 * EQdkp
 * Copyright 2002-2005
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * listallplayers.php
 * begin: Wed December 18 2002
 * 
 * $Id: listallplayers.php 44 2006-07-28 20:52:02Z tsigo $
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './spdkp/';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('u_member_list');

$sort_order = array(
    0 => array('member_class', 'member_class desc'),
    1 => array('member_name', 'member_name desc'),
    2 => array('member_spent desc', 'member_spent'),
    3 => array('member_adjustment desc', 'member_adjustment'),
    4 => array('member_current desc', 'member_current'),
);

$current_order = switch_order($sort_order);

$cur_hash = hash_filename("listallplayers.php");

    $member_count = 0;
    
    // just get the names
    $sql = "select member_name, member_class_id from mhdkp_members union distinct
			select member_name, member_class_id from spdkp_members union distinct
			select member_name, member_class_id from btdkp_members
			order by member_class_id, member_name";

    if ( !($members_result = $db->query($sql)) )
    {
        message_die('Could not obtain member information', '', __FILE__, __LINE__, $sql);
    }

	// the array that'll hold our member data
	$memberclasscount = array("Druid" => 0, "Hunter" => 0, "Mage" => 0, "Paladin" => 0, "Priest" => 0, "Rogue" => 0, "Shaman" => 0, "Warlock" => 0, "Warrior" => 0);

    while ( $row = $db->fetch_record($members_result) )
    {
    
		// get the member info for each zone
		$membersql = 'select member_name, "mh" as zone, member_earned-member_spent as member_current, c.class_name from mhdkp_members m, mhdkp_classes c where member_name="' . $row['member_name'] . '"  and c.class_id = m.member_class_id  union distinct
			select member_name, "sp" as zone, member_earned-member_spent as member_current, c.class_name from spdkp_members m, spdkp_classes c where member_name="' . $row['member_name'] . '"  and c.class_id = m.member_class_id  union distinct
			select member_name, "bt" as zone, member_earned-member_spent as member_current, c.class_name from btdkp_members m, btdkp_classes c where member_name="' . $row['member_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(
			"mh" => array("name" => '-', "points" => '-', "class" => '-'),
			"sp" => array("name" => '-', "points" => '-', "class" => '-'),
			"bt" => array("name" => '-', "points" => '-', "class" => '-'));
		
		// save the name/class info
		$member_class = "";
		
		// 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"];

			// set the member data for each zone
			$memberarray[$memberdatarow["zone"]]["name"] = $member_class;
			$memberarray[$memberdatarow["zone"]]["points"] = (int) $memberdatarow["member_current"];
			$memberarray[$memberdatarow["zone"]]["class"] = $memberdatarow["member_class"];
		}
			
		// setup the raid attendance shit
		$individual_raid_count_30 = 0;
		$thirty_days = mktime(0, 0, 0, date('m'), date('d')-30, date('Y'));
		
		// the raid attendance query
		$rc_sql = 'select * from mhdkp_raids r, mhdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $row['member_name'] . '" and (r.raid_date between ' . $thirty_days . ' and ' . time() . ') union all
			select * from spdkp_raids r, spdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $row['member_name'] . '" and (r.raid_date between ' . $thirty_days . ' and ' . time() . ') union all
			select * from btdkp_raids r, btdkp_raid_attendees a where r.raid_id=a.raid_id and a.member_name="' . $row['member_name'] . '" and (r.raid_date between ' . $thirty_days . ' and ' . time() . ')';
			
		// 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 mhdkp_raids where raid_date between ' . $thirty_days . ' and ' . time() . ' union all
			select * from spdkp_raids where raid_date between ' . $thirty_days . ' and ' . time() . ' union all
			select * from btdkp_raids where raid_date between ' . $thirty_days . ' and ' . time();
			
		// 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;
    
		// if they meet the attendance req we can show them
        if(0 < $percent_of_raids_30)
        {
			// count em up boys
            $member_count++;
            
            // sum the counted players
            $memberclasscount[$member_class] = $memberclasscount[$member_class] + 1;

            $tpl->assign_block_vars($member_class . '_row', array(
                'ROW_CLASS'     => $eqdkp->switch_row_class(),
                'COUNT'         => $member_count,
                'NAME'          => $row['member_name'] . '</a> <font style="font-size:10px;letter-spacing:-1px;font-family:verdana;color:gray;">(<a target="_blank" href="http://armory.worldofwarcraft.com/character-sheet.xml?r=Mal\'Ganis&n=' . $row['member_name'] . '">...</a>)</font>',
                'CLASS'         => $member_class,
                
                'SPCURRENT'       => $memberarray['sp']['points'],
                'BTCURRENT'       => $memberarray['bt']['points'],
                'MHCURRENT'       => $memberarray['mh']['points'],

                'C_SPCURRENT'       => color_item($memberarray['sp']['points']),
                'C_BTCURRENT'       => color_item($memberarray['bt']['points']),
                'C_MHCURRENT'       => color_item($memberarray['mh']['points']),
                
				'C_RAIDS_30_DAYS' => color_item($percent_of_raids_30, true),
	            'RAIDS_30_DAYS' => $percent_of_raids_30,
                'U_VIEW_MEMBER' => 'viewmemberall.php' . $SID . '&amp;' . URI_NAME . '='.$row['member_name'])
            );
        }
    }
    
    $uri_addon  = ''; // Added to the end of the sort links
    $uri_addon .= '&amp;filter=' . urlencode($filter);
    $uri_addon .= ( isset($_GET['show']) ) ? '&amp;show=' . $_GET['show'] : '';
   
    $footcount_text = sprintf($user->lang['listmembers_footcount'], $member_count);
    $db->free_result($members_result);

$tpl->assign_vars(array(
    'F_MEMBERS' => 'listallplayers.php'.$SID,
    'V_SID'     => str_replace('?' . URI_SESSION . '=', '', $SID),
    
    'L_FILTER'        => $user->lang['filter'],
    'L_NAME'          => $user->lang['name'],
    'L_RANK'          => $user->lang['rank'],
    'L_LEVEL'         => $user->lang['level'],
    'L_CLASS'         => $user->lang['class'],
    'L_ARMOR'         => $user->lang['armor'],
    'L_EARNED'        => $user->lang['earned'],
    'L_SPENT'         => $user->lang['spent'],
    'L_ADJUSTMENT'    => $user->lang['adjustment'],
    'L_CURRENT'       => $user->lang['current'],
    'L_LASTRAID'      => $user->lang['lastraid'],
    'L_LASTLOOT'      => $user->lang['lastloot'],
    'L_RAIDS_30_DAYS' => sprintf($user->lang['raids_x_days'], 30),
    'L_RAIDS_90_DAYS' => sprintf($user->lang['raids_x_days'], 90),
    'BUTTON_NAME'     => 'submit',
    'BUTTON_VALUE'    => $user->lang['compare_members'],

    'L_DRUID'    => "Druids <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Druid'] . ")</font>",
    'L_HUNTER'    => "Hunters <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Hunter'] . ")</font>",
    'L_MAGE'    => "Mages <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Mage'] . ")</font>",
    'L_PALADIN'    => "Paladins <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Paladin'] . ")</font>",
    'L_PRIEST'    => "Priests <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Priest'] . ")</font>",
    'L_ROGUE'    => "Rogues <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Rogue'] . ")</font>",
    'L_SHAMAN'    => "Shaman <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Shaman'] . ")</font>",
    'L_WARLOCK'    => "Warlocks <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Warlock'] . ")</font>",
    'L_WARRIOR'    => "Warriors <font style='font-weight:normal;color:#F6F2C8;font-size:12px;'>(" . $memberclasscount['Warrior'] . ")</font>",
    
    'O_NAME'       => $current_order['uri'][1],
    'O_CLASS'      => $current_order['uri'][0],
    'O_SPENT'      => $current_order['uri'][2],
    'O_ADJUSTMENT' => $current_order['uri'][3],
    'O_CURRENT'    => $current_order['uri'][4],
    
    'URI_ADDON'      => $uri_addon,
    'PAGE_HASH'		=> $cur_hash,
    'U_LIST_MEMBERS' => 'listallplayers.php' . $SID . '&amp;',
    
    'S_COMPARE' => $s_compare,
    'S_NOTMM'   => true,
    
    'LISTMEMBERS_FOOTCOUNT' => ( isset($_GET['compare']) ) ? sprintf($footcount_text, sizeof(explode(',', $compare))) : $footcount_text)
);

$eqdkp->set_vars(array(
    'page_title'    => sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listmembers_title'],
    'template_file' => 'listallplayers.html',
    'display'       => true)
);
?>
