summaryrefslogtreecommitdiff
path: root/interesting_defendants/interesting_defendants.php
blob: 485eb8d173f2eaf03fbf2afa0586896a8ff2c340 (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
<?php

error_reporting(E_ALL ^ E_NOTICE);

$dbh = new PDO('mysql:host=localhost;dbname=mdcc', 'mdcc', 'Mdcc.');

$fatal_dispositions = file('./fatal_dispositions',  FILE_IGNORE_NEW_LINES);
$excludable_charges = file('./excludable_charges',  FILE_IGNORE_NEW_LINES);
$safe_dispositions = file('./safe_dispositions',  FILE_IGNORE_NEW_LINES);
$disposition_groups = file('./disposition_groups',  FILE_IGNORE_NEW_LINES);

// CSV files
$def_no_grp_csv = fopen('./output/interesting_defendants_no_group.csv', 'w');
$def_grp1_csv = fopen('./output/interesting_defendants_group_1.csv', 'w');
$def_grp2_csv = fopen('./output/interesting_defendants_group_2.csv', 'w');
$def_grp3_csv = fopen('./output/interesting_defendants_group_3.csv', 'w');
$def_grp4_csv = fopen('./output/interesting_defendants_group_4.csv', 'w');
$def_multiple_cases_csv = fopen('./output/interesting_defendants_with_multiple_cases.csv', 'w');
$def_akas_csv = fopen('./output/interesting_defendants_with_akas.csv', 'w');
$def_akas_multiple_cases_csv = fopen('./output/interesting_defendants_with_akas_and_multiple_cases.csv', 'w');

// HTML files
$def_no_grp_html = fopen('./output/interesting_defendants_no_group.html', 'w');
$def_grp1_html = fopen('./output/interesting_defendants_group_1.html', 'w');
$def_grp2_html = fopen('./output/interesting_defendants_group_2.html', 'w');
$def_grp3_html = fopen('./output/interesting_defendants_group_3.html', 'w');
$def_grp4_html = fopen('./output/interesting_defendants_group_4.html', 'w');
$def_multiple_cases_html = fopen('./output/interesting_defendants_with_multiple_cases.html', 'w');
$def_akas_html = fopen('./output/interesting_defendants_with_akas.html', 'w');
$def_akas_multiple_cases_html = fopen('./output/interesting_defendants_with_akas_and_multiple_cases.html', 'w');

// HTML template start
$html_start = <<<HTML
<html>
<head>
	<title>Interesting defendants</title>
</head>
<body>
<table border="1">
HTML;

// HTML template end
$html_end = <<<HTML
</table>
</body>
</html>
HTML;

// Initialize the HTML files
fwrite($def_no_grp_html, $html_start);
fwrite($def_grp1_html, $html_start);
fwrite($def_grp2_html, $html_start);
fwrite($def_grp3_html, $html_start);
fwrite($def_grp4_html, $html_start);
fwrite($def_multiple_cases_html, $html_start);
fwrite($def_akas_html, $html_start);

$sql = "SELECT * FROM cases";

$st = $dbh->prepare($sql);
if ( $st->execute() ) {
	while ( $case = $st->fetch(PDO::FETCH_ASSOC) ) {

		$has_multiple_cases = false;
		$has_akas = false;

		$defendant_csv = array(
			$case['court_case_no'],
			$case['name'],
			$case['date_birth'],
		);

		$defendant_html = <<<HTML

<tr>
	<td>
		<a href='http://li554-21.members.linode.com/search.php?case_id={$case['id']}'>{$case['court_case_no']}</a>
	</td>
	<td>{$case['name']}</td>
	<td>{$case['date_birth']}</td>
	<td>[<a href='http://li554-21.members.linode.com/charges.php?case_id={$case['id']}&case_no={$case['court_case_no']}'>charges</a>]</td>
</tr>

HTML;

		$sql_charge = "SELECT * FROM charges WHERE case_id = '{$case['id']}'";
		$st_charge = $dbh->prepare($sql_charge);
		$st_charge->execute();
		$charges = $st_charge->fetchAll(PDO::FETCH_ASSOC);

		// The default group is no group (i.e. 0)
		$disposition_group = 0;

		foreach ( $charges as $charge ) {

			// Figure out into which, if any, disposition group
			// this charge puts the case
			foreach ( $disposition_groups as $group_and_disposition ) {
				list($group,$disposition) = explode(',', $group_and_disposition);
				if ( $charge['disposition'] == $disposition ) {
					// Only move a case into a higher group, never backward
					if ( $disposition_group == '0' || $group < $disposition_group ) {
						$disposition_group = $group;
					}

				}
			}
			
			// Don't process this case if there is an empty disposition 
			if ( ! trim($charge['disposition']) ) {
				continue(2);
			}

			// If the disposition is a "fatal" one then go to next case
			if ( in_array( $charge['disposition'], $fatal_dispositions) ) {
				continue(2);
			}

			// If the charge is excludable and the disposition is not a safe one then go to the next case
			if ( in_array($charge['charge'], $excludable_charges) && ! in_array($charge['disposition'], $safe_dispositions) ) {
				continue(2);
			}

		}

		// Does this person have multiple cases, based on the same name and date of birth?
		$sql_def = "SELECT count(*) from cases WHERE name = '{$case['name']}' AND date_birth = '{$case['date_birth']}' AND id != '{$case['id']}'";
		$st_def = $dbh->prepare($sql_def);
		$st_def->execute();
		$case_count = $st_def->fetchColumn();
		if ( $case_count != '0' ) {
			$has_multiple_cases = true;
		}

		// Does this person has any AKAs?
		$sql_akas = "SELECT count(*) FROM akas WHERE case_id = '{$case['id']}'";
		$st_akas = $dbh->prepare($sql_akas);
		$st_akas->execute();
		if ( $st_akas->fetchColumn() != '0' ) {
			$has_akas = true;
		}

		if ( $has_multiple_cases && ! $has_akas ) {
			fputcsv($def_multiple_cases_csv, $defendant_csv);
			fwrite($def_multiple_cases_html, $defendant_html);
		}

		if ( $has_akas && ! $has_multiple_cases ) {
			fputcsv($def_akas_csv, $defendant_csv);
			fwrite($def_akas_html, $defendant_html);
		}

		if ( $has_multiple_cases && $has_akas ) {
			fputcsv($def_akas_multiple_cases_csv, $defendant_csv);
			fwrite($def_akas_multiple_cases_html, $defendant_html);
		}


		// The highest quality list
		if ( ! $has_multiple_cases && ! $has_akas ) {
			switch ( $disposition_group ) {
				case '1':
					fputcsv($def_grp1_csv, $defendant_csv);
					fwrite($def_grp1_html, $defendant_html);
					break;
				case '2':
					fputcsv($def_grp2_csv, $defendant_csv);
					fwrite($def_grp2_html, $defendant_html);
				case '3':
					fputcsv($def_grp3_csv, $defendant_csv);
					fwrite($def_grp3_html, $defendant_html);
					break;
				case '4':
					fputcsv($def_grp4_csv, $defendant_csv);
					fwrite($def_grp4_html, $defendant_html);
					break;
				default:
					fputcsv($def_no_grp_csv, $defendant_csv);
					fwrite($def_no_grp_html, $defendant_html);
			}
		}

	}
}

// Finalize HTML files
fwrite($def_no_grp_html, $html_stop);
fwrite($def_grp1_html, $html_stop);
fwrite($def_grp2_html, $html_stop);
fwrite($def_grp3_html, $html_stop);
fwrite($def_grp4_html, $html_stop);
fwrite($def_multiple_cases_html, $html_stop);
fwrite($def_akas_html, $html_stop);
fwrite($def_akas_multiple_cases_html, $html_stop);

fclose($def_no_group_csv);
fclose($def_grp1_csv);
fclose($def_grp2_csv);
fclose($def_grp3_csv);
fclose($def_grp4_csv);
fclose($def_multiple_cases_csv);
fclose($def_akas_csv);
fclose($def_akas_multiple_cases_csv);
fclose($def_no_group_html);
fclose($def_grp1_html);
fclose($def_grp2_html);
fclose($def_grp3_html);
fclose($def_grp4_html);
fclose($def_multiple_cases_html);
fclose($def_akas_html);
fclose($def_akas_multiple_cases_html);

?>