summaryrefslogtreecommitdiffstats
path: root/classes/invite_tree.class.php
blob: e424f2e35c9981774b0200f2ee54f0b2f70a07fd (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?
/**************************************************************************/
/*-- Invite tree class -----------------------------------------------------



***************************************************************************/

class INVITE_TREE {
	var $UserID = 0;
	var $Visible = true;

	// Set things up
	function INVITE_TREE($UserID, $Options = array()) {
		$this->UserID = $UserID;
		if ($Options['visible'] === false) {
			$this->Visible = false;
		}
	}

	function make_tree() {
		$QueryID = G::$DB->get_query_id();

		$UserID = $this->UserID;
?>
		<div class="invitetree pad">
<?
		G::$DB->query("
			SELECT TreePosition, TreeID, TreeLevel
			FROM invite_tree
			WHERE UserID = $UserID");
		list($TreePosition, $TreeID, $TreeLevel) = G::$DB->next_record(MYSQLI_NUM, false);

		if (!$TreeID) {
			return;
		}
		G::$DB->query("
			SELECT TreePosition
			FROM invite_tree
			WHERE TreeID = $TreeID
				AND TreeLevel = $TreeLevel
				AND TreePosition > $TreePosition
			ORDER BY TreePosition ASC
			LIMIT 1");
		if (G::$DB->has_results()) {
			list($MaxPosition) = G::$DB->next_record(MYSQLI_NUM, false);
		} else {
			$MaxPosition = false;
		}
		$TreeQuery = G::$DB->query("
			SELECT
				it.UserID,
				Enabled,
				PermissionID,
				Donor,
				Uploaded,
				Downloaded,
				Paranoia,
				TreePosition,
				TreeLevel
			FROM invite_tree AS it
				JOIN users_main AS um ON um.ID = it.UserID
				JOIN users_info AS ui ON ui.UserID = it.UserID
			WHERE TreeID = $TreeID
				AND TreePosition > $TreePosition".
				($MaxPosition ? " AND TreePosition < $MaxPosition" : '')."
				AND TreeLevel > $TreeLevel
			ORDER BY TreePosition");

		$PreviousTreeLevel = $TreeLevel;

		// Stats for the summary
		$MaxTreeLevel = $TreeLevel; // The deepest level (this changes)
		$OriginalTreeLevel = $TreeLevel; // The level of the user we're viewing
		$BaseTreeLevel = $TreeLevel + 1; // The level of users invited by our user
		$Count = 0;
		$Branches = 0;
		$DisabledCount = 0;
		$DonorCount = 0;
		$ParanoidCount = 0;
		$TotalUpload = 0;
		$TotalDownload = 0;
		$TopLevelUpload = 0;
		$TopLevelDownload = 0;

		$ClassSummary = array();
		global $Classes;
		foreach ($Classes as $ClassID => $Val) {
			$ClassSummary[$ClassID] = 0;
		}

		// We store this in an output buffer, so we can show the summary at the top without having to loop through twice
		ob_start();
		while (list($ID, $Enabled, $Class, $Donor, $Uploaded, $Downloaded, $Paranoia, $TreePosition, $TreeLevel) = G::$DB->next_record(MYSQLI_NUM, false)) {

			// Do stats
			$Count++;

			if ($TreeLevel > $MaxTreeLevel) {
				$MaxTreeLevel = $TreeLevel;
			}

			if ($TreeLevel == $BaseTreeLevel) {
				$Branches++;
				$TopLevelUpload += $Uploaded;
				$TopLevelDownload += $Downloaded;
			}

			$ClassSummary[$Class]++;
			if ($Enabled == 2) {
				$DisabledCount++;
			}
			if ($Donor) {
				$DonorCount++;
			}

			// Manage tree depth
			if ($TreeLevel > $PreviousTreeLevel) {
				for ($i = 0; $i < $TreeLevel - $PreviousTreeLevel; $i++) {
					echo "\n\n<ul class=\"invitetree\">\n\t<li>\n";
				}
			} elseif ($TreeLevel < $PreviousTreeLevel) {
				for ($i = 0; $i < $PreviousTreeLevel - $TreeLevel; $i++) {
					echo "\t</li>\n</ul>\n";
				}
				echo "\t</li>\n\t<li>\n";
			} else {
				echo "\t</li>\n\t<li>\n";
			}
			$UserClass = $Classes[$Class]['Level'];
?>
		<strong><?=Users::format_username($ID, true, true, ($Enabled != 2 ? false : true), true)?></strong>
<?
			if (check_paranoia(array('uploaded', 'downloaded'), $Paranoia, $UserClass)) {
				$TotalUpload += $Uploaded;
				$TotalDownload += $Downloaded;
?>
		&nbsp;Uploaded: <strong><?=Format::get_size($Uploaded)?></strong>
		&nbsp;Downloaded: <strong><?=Format::get_size($Downloaded)?></strong>
		&nbsp;Ratio: <strong><?=Format::get_ratio_html($Uploaded, $Downloaded)?></strong>
<?
			} else {
				$ParanoidCount++;
?>
		&nbsp;Hidden
<?
			}
?>

<?
			$PreviousTreeLevel = $TreeLevel;
			G::$DB->set_query_id($TreeQuery);
		}

		$Tree = ob_get_clean();
		for ($i = 0; $i < $PreviousTreeLevel - $OriginalTreeLevel; $i++) {
			$Tree .= "\t</li>\n</ul>\n";
		}

		if ($Count) {

?>
		<p style="font-weight: bold;">
			This tree has <?=number_format($Count)?> entries, <?=number_format($Branches)?> branches, and a depth of <?=number_format($MaxTreeLevel - $OriginalTreeLevel)?>.
			It has
<?
			$ClassStrings = array();
			foreach ($ClassSummary as $ClassID => $ClassCount) {
				if ($ClassCount == 0) {
					continue;
				}
				$LastClass = Users::make_class_string($ClassID);
				if ($ClassCount > 1) {
					if ($LastClass == 'Torrent Celebrity') {
						 $LastClass = 'Torrent Celebrities';
					} else {
						$LastClass.='s';
					}
				}
				$LastClass = "$ClassCount $LastClass (" . number_format(($ClassCount / $Count) * 100) . '%)';

				$ClassStrings[] = $LastClass;
			}
			if (count($ClassStrings) > 1) {
				array_pop($ClassStrings);
				echo implode(', ', $ClassStrings);
				echo ' and '.$LastClass;
			} else {
				echo $LastClass;
			}
			echo '. ';
			echo $DisabledCount;
			echo ($DisabledCount == 1) ? ' user is' : ' users are';
			echo ' disabled (';
			if ($DisabledCount == 0) {
				echo '0%)';
			} else {
				echo number_format(($DisabledCount / $Count) * 100) . '%)';
			}
			echo ', and ';
			echo $DonorCount;
			echo ($DonorCount == 1) ? ' user has' : ' users have';
			echo ' donated (';
			if ($DonorCount == 0) {
				echo '0%)';
			} else {
				echo number_format(($DonorCount / $Count) * 100) . '%)';
			}
			echo '. </p>';

			echo '<p style="font-weight: bold;">';
			echo 'The total amount uploaded by the entire tree was '.Format::get_size($TotalUpload);
			echo '; the total amount downloaded was '.Format::get_size($TotalDownload);
			echo '; and the total ratio is '.Format::get_ratio_html($TotalUpload, $TotalDownload).'. ';
			echo '</p>';

			echo '<p style="font-weight: bold;">';
			echo 'The total amount uploaded by direct invitees (the top level) was '.Format::get_size($TopLevelUpload);
			echo '; the total amount downloaded was '.Format::get_size($TopLevelDownload);
			echo '; and the total ratio is '.Format::get_ratio_html($TopLevelUpload, $TopLevelDownload).'. ';

			echo "These numbers include the stats of paranoid users and will be factored into the invitation giving script.\n\t\t</p>\n";

			if ($ParanoidCount) {
				echo '<p style="font-weight: bold;">';
				echo $ParanoidCount;
				echo ($ParanoidCount == 1) ? ' user (' : ' users (';
				echo number_format(($ParanoidCount / $Count) * 100);
				echo '%) ';
				echo ($ParanoidCount == 1) ? ' is' : ' are';
				echo ' too paranoid to have their stats shown here, and ';
				echo ($ParanoidCount == 1) ? ' was' : ' were';
				echo ' not factored into the stats for the total tree.';
				echo '</p>';
			}
		}
?>
			<br />
<?=			$Tree?>
		</div>
<?
		G::$DB->set_query_id($QueryID);
	}
}
?>