alum_import.php

Go to the documentation of this file.
00001 #!/usr/bin/php -q
00002 <?php
00012 // {{{ Configuration
00013 
00014 // Do a dry run only?
00015 $b_dryRun = true;
00016 
00017 // The DSN for the import database
00018 $s_dsnImport = 'mysql://username:password@localhost/studentinfo';
00019 
00020 // The import table with alumni info?
00021 $s_importTable = 'importstudents';
00022 
00023 // The DSN for the FastFrame database
00024 $s_dsnFF = 'mysql://username:password@localhost/fastframe_alumni';
00025 
00026 // FastFrame alumni data table 
00027 $s_alumTable = 'alumni_alums';
00028 
00029 // FastFrame profile table 
00030 $s_profileTable = 'profile';
00031 
00032 // }}}
00033 // {{{ Setup
00034 
00035 require_once 'DB.php';
00036 error_reporting(E_ALL);
00037 set_time_limit(0);
00038 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error');
00039 $o_dataImport =& DB::connect($s_dsnImport);
00040 $o_dataFF =& DB::connect($s_dsnFF);
00041 
00042 // }}}
00043 // {{{ handle_pear_error()
00044 
00045 function handle_pear_error (&$error_obj) 
00046 {
00047     die ($error_obj->getMessage()."\n".$error_obj->getDebugInfo());
00048 }
00049 
00050 // }}}
00051 // {{{ Loop through imported alums
00052 
00053 print  "Importing alums...\n";
00054 // Get all graduates and incompletes that were here > 1 year
00055 $s_query = sprintf('SELECT * FROM %s 
00056             WHERE status = %s OR 
00057             (status = %s AND (TO_DAYS(grad_date) - TO_DAYS(start_date)) > 365)', 
00058             $s_importTable, $o_dataImport->quote('GRAD'), $o_dataImport->quote('INC'));
00059 $o_result = $o_dataImport->query($s_query);
00060 $s_insertCount = 0;
00061 $s_updateCount = 0;
00062 while ($a_row = $o_result->fetchRow(DB_FETCHMODE_ASSOC)) {
00063     // See if they are already in the FastFrame database in which case we update
00064     $s_query = sprintf('SELECT %s FROM %s WHERE %s = %s', 
00065             'id', $s_alumTable, 'student_id', $o_dataFF->quote($a_row['student_id']));
00066     $a_row['email'] = empty($a_row['email2']) ? $a_row['email'] : $a_row['email2'];
00067     if (!is_null($s_id = $o_dataFF->getOne($s_query))) {
00068         $s_updateCount++;
00069         if ($b_dryRun) {
00070             echo "Student ID {$a_row['student_id']} would be updated\n";
00071         }
00072         else {
00073             $s_query = sprintf('UPDATE %s SET %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s WHERE %s=%s', 
00074                     $s_profileTable,
00075                     'firstname', $o_dataFF->quote($a_row['first_name']),
00076                     'lastname', $o_dataFF->quote($a_row['last_name']),
00077                     'email', $o_dataFF->quote($a_row['email']),
00078                     'phone', $o_dataFF->quote($a_row['phone']),
00079                     'username', $o_dataFF->quote($a_row['student_id']),
00080                     'password', $o_dataFF->quote(md5($a_row['student_id'])),
00081                     'id', $s_id);
00082             $o_dataFF->query($s_query);
00083 
00084             $s_query = sprintf('UPDATE %s SET %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s WHERE %s=%s', 
00085                     $s_alumTable,
00086                     'student_id', $o_dataFF->quote($a_row['student_id']),
00087                     'address', $o_dataFF->quote($a_row['street']),
00088                     'city', $o_dataFF->quote($a_row['city']),
00089                     'state', $o_dataFF->quote($a_row['state']),
00090                     'major', $o_dataFF->quote($a_row['program']),
00091                     'country', $o_dataFF->quote($a_row['citizenship']),
00092                     'postal_code', $o_dataFF->quote($a_row['zip']),
00093                     // Not syncing grad year because it's incorrect in c2k for <78 alums
00094                     // 'grad_year', $o_dataFF->quote(substr($a_row['grad_date'], 0, 4)),
00095                     // NOTE: a bit of a hack, we save status in fax
00096                     'fax', $o_dataFF->quote($a_row['status']),
00097                     'id', $s_id);
00098             $o_dataFF->query($s_query);
00099         }
00100     }
00101     else {
00102         $s_insertCount++;
00103         if ($b_dryRun) {
00104             echo "Student ID {$a_row['student_id']} would be inserted\n";
00105         }
00106         else {
00107             $s_id = $o_dataFF->nextId($s_profileTable);
00108             $s_query = sprintf('INSERT INTO %s SET %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, receive_emails=1', 
00109                     $s_profileTable,
00110                     'id', $s_id,
00111                     'firstname', $o_dataFF->quote($a_row['first_name']),
00112                     'lastname', $o_dataFF->quote($a_row['last_name']),
00113                     'email', $o_dataFF->quote($a_row['email']),
00114                     'username', $o_dataFF->quote($a_row['student_id']),
00115                     'password', $o_dataFF->quote(md5($a_row['student_id'])),
00116                     'phone', $o_dataFF->quote($a_row['phone']),
00117                     // because authsource is empty they won't be able to log in
00118                     'authsource', '\'\'',
00119                     'theme', '\'\'', 
00120                     'list_mode', '\'\'', 
00121                     'language', '\'\'', 
00122                     'initial_page', '\'\'');
00123             $o_dataFF->query($s_query);
00124 
00125             $s_query = sprintf('INSERT INTO %s SET %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s, %s=%s', 
00126                     $s_alumTable,
00127                     'id', $s_id,
00128                     'student_id', $o_dataFF->quote($a_row['student_id']),
00129                     'address', $o_dataFF->quote($a_row['street']),
00130                     'city', $o_dataFF->quote($a_row['city']),
00131                     'state', $o_dataFF->quote($a_row['state']),
00132                     'major', $o_dataFF->quote($a_row['program']), 
00133                     'grad_year', $o_dataFF->quote(substr($a_row['grad_date'], 0, 4)),
00134                     'title', $o_dataFF->quote($a_row['title']), 
00135                     'country', $o_dataFF->quote($a_row['citizenship']), 
00136                     'postal_code', $o_dataFF->quote($a_row['zip']), 
00137                     // NOTE: a bit of a hack, we save status in fax
00138                     'fax', $o_dataFF->quote($a_row['status']), 
00139                     'company', '\'\'',
00140                     'web_site', '\'\'');
00141             $o_dataFF->query($s_query);
00142         }
00143     }
00144 }
00145 
00146 // }}}
00147 $s_total = $s_updateCount + $s_insertCount;
00148 print "Import Done.  Inserted: $s_insertCount.  Updated: $s_updateCount.  Total: $s_total.\n";
00149 ?>

Generated on Fri Jun 23 11:38:14 2006 for FastFrame by  doxygen 1.4.4