summaryrefslogtreecommitdiffstats
path: root/viewevent.php
blob: f4db06050e683a1fc16ac39f5c1cb8dcd7b62dcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * viewevent.php
 * Began: Fri December 20 2002
 * 
 * $Id: viewevent.php 46 2007-06-19 07:29:11Z tsigo $
 * 
 ******************************/

define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('u_event_view');

if ( (isset($_GET[URI_EVENT])) && (intval($_GET[URI_EVENT] > 0)) )
{
    $sort_order = array(
        0 => array('raid_date desc', 'raid_date'),
        1 => array('raid_note', 'raid_note desc'),
        2 => array('raid_value desc', 'raid_value')
    );
     
    $current_order = switch_order($sort_order);

    $sql = 'SELECT event_id, event_name, event_value, event_added_by, event_updated_by
            FROM ' . EVENTS_TABLE . "
            WHERE event_id='".$_GET[URI_EVENT]."'";
            
    if ( !($event_result = $db->query($sql)) )
    {
        message_die('Could not obtain event information', '', __FILE__, __LINE__, $sql);
    }
    
    // Check for a valid event
    if ( !$event = $db->fetch_record($event_result) )
    {
        message_die($user->lang['error_invalid_event_provided']);
    }
    
    // Init vars used to get averages and totals
    $total_drop_count = 0;
    $total_attendees_count = 0;
    $total_earned = 0;
    
    // Reduce queries
    $raids     = array();
    $raid_ids  = array('0');
    $items     = array();
    $attendees = array();
    
    $unset_raid_zero = false; // Remove the '0' from $raid_ids if raids exist for this event
    
    // Find the raids for this event
    $sql = 'SELECT raid_id, raid_date, raid_note, raid_value 
            FROM ' . RAIDS_TABLE . "
            WHERE raid_name='" . addslashes($event['event_name']) . "' 
            ORDER BY " . $current_order['sql'];
    $result = $db->query($sql);
    
    while ( $row = $db->fetch_record($result) )
    {
        $raids[ $row['raid_id'] ] = array(
            'raid_id'    => $row['raid_id'],
            'raid_date'  => $row['raid_date'],
            'raid_note'  => $row['raid_note'],
            'raid_value' => $row['raid_value']);
            
        if ( $row['raid_id'] > 0 )
        {
            if ( !$unset_raid_zero )
            {
                unset($raid_ids[0]);
                $unset_raid_zero = true;
            }
            
            $raid_ids[] = $row['raid_id'];
        }
    }
    $db->free_result($result);
    
    // Find the item drops for each raid
    $sql = 'SELECT raid_id, count(item_id) AS count 
            FROM ' . ITEMS_TABLE . ' 
            WHERE raid_id IN (' . implode(', ', $raid_ids) . ')
            GROUP BY raid_id';
    $result = $db->query($sql);
    
    while ( $row = $db->fetch_record($result) )
    {
        $items[ $row['raid_id'] ] = $row['count'];
    }
    $db->free_result($result);
    
    // Find the attendees at each raid
    $sql = 'SELECT raid_id, count(member_name) AS count 
            FROM ' . RAID_ATTENDEES_TABLE . ' 
            WHERE raid_id IN (' . implode(', ', $raid_ids) . ')
            GROUP BY raid_id';
    $result = $db->query($sql);
    
    while ( $row = $db->fetch_record($result) )
    {
        $attendees[ $row['raid_id'] ] = $row['count'];
    }
    $db->free_result($result);
    
    // Loop through the raids for this event
    $total_raid_count = sizeof($raids);
    foreach ( $raids as $raid_id => $raid )
    {
        $drop_count = ( isset($items[ $raid['raid_id'] ]) ) ? $items[ $raid['raid_id'] ] : '0';
        $attendees_count = ( isset($attendees[ $raid['raid_id'] ]) ) ? $attendees[ $raid['raid_id'] ] : '0';

        $tpl->assign_block_vars('raids_row', array(
            'ROW_CLASS'   => $eqdkp->switch_row_class(),
            'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$raid['raid_id'],
            'DATE'        => ( !empty($raid['raid_id']) ) ? date($user->style['date_notime_short'], $raid['raid_date']) : '&nbsp;',
            'ATTENDEES'   => $attendees_count,
            'DROPS'       => $drop_count,
            'NOTE'        => ( !empty($raid['raid_note']) ) ? stripslashes($raid['raid_note']) : '&nbsp;',
            'VALUE'       => $raid['raid_value'])
        );
        
        // Add the values of this row to our totals
        $total_drop_count += $drop_count;
        $total_attendees_count += $attendees_count;
        $total_earned += $raid['raid_value'];       
    }
 
    // Prevent div by 0
    $average_attendees = ( $total_raid_count > 0 ) ? floor($total_attendees_count / $total_raid_count) : '0';
    $average_drops     = ( $total_drop_count > 0 ) ? floor($total_drop_count / $total_raid_count)      : '0';
    
    //
    // Items
    //
    $sql = 'SELECT item_date, raid_id, item_name, item_buyer, item_id, item_value
            FROM ' . ITEMS_TABLE . '
            WHERE raid_id IN (' . implode(', ', $raid_ids) . ')
            ORDER BY item_date DESC';
    $result = $db->query($sql);
    while ( $row = $db->fetch_record($result) )
    {
        $tpl->assign_block_vars('items_row', array(
            'ROW_CLASS'     => $eqdkp->switch_row_class(),
            'DATE'          => date($user->style['date_notime_short'], $row['item_date']),
            'U_VIEW_RAID'   => 'viewraid.php' . $SID . '&amp;' . URI_RAID . '=' . $row['raid_id'],
            'BUYER'         => stripslashes($row['item_buyer']),
            'U_VIEW_MEMBER' => 'viewmember.php' . $SID . '&amp;' . URI_NAME . '=' . $row['item_buyer'],
            'NAME'          => stripslashes($row['item_name']),
            'U_VIEW_ITEM'   => 'viewitem.php' . $SID . '&amp;' . URI_ITEM . '=' . $row['item_id'],
            'SPENT'         => sprintf("%.2f", $row['item_value']))
        );
    }
    $total_items = $db->num_rows($result);
    $db->free_result($result);
    
    $tpl->assign_vars(array(
        'L_RECORDED_RAID_HISTORY' => sprintf($user->lang['recorded_raid_history'], stripslashes($event['event_name'])),
        'L_ADDED_BY'              => $user->lang['added_by'],
        'L_UPDATED_BY'            => $user->lang['updated_by'],
        'L_DATE'                  => $user->lang['date'],
        'L_ATTENDEES'             => $user->lang['attendees'],
        'L_DROPS'                 => $user->lang['drops'],
        'L_NOTE'                  => $user->lang['note'],
        'L_VALUE'                 => $user->lang['value'],
        'L_AVERAGE'               => $user->lang['average'],
        'L_TOTAL_EARNED'          => $user->lang['total_earned'],
        'L_ITEMS'                 => $user->lang['items'],
        'L_BUYER'                 => $user->lang['buyer'],
        'L_NAME'                  => $user->lang['name'],
        'L_SPENT'                 => $user->lang['spent'],
        
        'O_DATE'  => $current_order['uri'][0],
        'O_NOTE'  => $current_order['uri'][1],
        'O_VALUE' => $current_order['uri'][2],
        
        'U_VIEW_EVENT' => 'viewevent.php'.$SID.'&amp;' . URI_EVENT . '='.$_GET[URI_EVENT].'&amp;',
        
        'EVENT_ADDED_BY'      => ( !empty($event['event_added_by']) ) ? $event['event_added_by'] : 'N/A',
        'EVENT_UPDATED_BY'    => ( !empty($event['event_updated_by']) ) ? $event['event_updated_by'] : 'N/A',
        'ROW_CLASS'           => $eqdkp->switch_row_class(),
        'AVERAGE_ATTENDEES'   => $average_attendees,
        'AVERAGE_DROPS'       => $average_drops,
        'TOTAL_EARNED'        => sprintf("%.2f", $total_earned),
        'VIEWEVENT_FOOTCOUNT' => sprintf($user->lang['viewevent_footcount'], $total_raid_count),
        'ITEM_FOOTCOUNT'      => sprintf($user->lang['viewitem_footcount'], $total_items, $total_items))
    );
    
    $eqdkp->set_vars(array(
        'page_title'    => sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.sprintf($user->lang['viewevent_title'], stripslashes($event['event_name'])),
        'template_file' => 'viewevent.html',
        'display'       => true)
    );
}
else
{
    message_die($user->lang['error_invalid_event_provided']);
}
?>