changed note field in tc_accomplishment to notes
[eq/.git] / bin / parse_ward_data
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5
6 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir);
7 unshift @INC,("$mydir/../setup");
8 if( -f "$mydir/../setup/db_config.local") { require "db_config.local"; }
9 else { require "db_config"; }
10
11 %hometeaching_data = ();
12 %membership_data = ();
13 getopts('vsn:o');
14
15 ######################################################################
16 # SUBROUTINES
17 ######################################################################
18 sub csv_to_hash
19 {
20         my ($filename, $hashref) = @_;
21
22         open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
23
24         my $found_header = 0; my $index = 0;
25         while(<FILE>)
26         {
27                 $line = $_;
28                 @data = split /\",/, $line;
29                 if(!$found_header) { @header = @data; $found_header = 1; }
30                 else {
31                         foreach $i (0..$#data-1) {
32                                 $data[$i] =~ s/\"//g;
33                                 $header[$i] =~ s/\"//g;
34                                 $hashref->{$index}{$header[$i]} = $data[$i];
35                                 #print "$index: $i: $header[$i]: $data[$i]\n";
36                         }
37                         $index++;
38                 }
39         }
40
41         close(FILE);
42 }
43
44 ######################################################################
45 sub print_hash
46 {
47         my ($hashref) = @_;
48
49         foreach $key (sort {$a <=> $b} keys %$hashref) {
50                 print "Index: $key\n";
51                 foreach $field (keys %{$hashref->{$key}}) {
52                         print "$field: $hashref->{$key}{$field}\n";
53                 }
54                 print "\n";
55         }
56 }
57
58 ######################################################################
59 # MAIN
60 ######################################################################
61
62 ###################################################
63 # Open a connection to the database
64 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
65     AutoCommit=>0,
66     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
67
68 ###################################################
69 # Process command line options
70 if(defined $opt_n) { $datadir = $opt_n; }
71 else { $datadir = shift(@ARGV); }
72 print "-> Processing all ward data files in $datadir\n";
73
74 ###################################################
75 # Parse Ward Data Files
76 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
77 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
78
79 if($opt_v) {
80         print "-> Membership Data Dump\n\n";
81         &print_hash(\%membership_data);
82         print "-> HomeTeaching Data Dump\n\n";
83         &print_hash(\%hometeaching_data);
84 }
85
86 if($opt_s) { $dbh->disconnect(); exit; }
87
88 ###################################################
89 # Disconnect from the database
90 $dbh->disconnect();
91
92 ######################################################################
93
94
95
96
97
98
99
100
101
102
103
104