diff options
Diffstat (limited to 'interesting_defendants/interesting_defendants.php')
| -rw-r--r-- | interesting_defendants/interesting_defendants.php | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/interesting_defendants/interesting_defendants.php b/interesting_defendants/interesting_defendants.php new file mode 100644 index 0000000..485eb8d --- /dev/null +++ b/interesting_defendants/interesting_defendants.php @@ -0,0 +1,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); + +?> |
