From 7b04204ede84cfb51b2faf69a23ffc233d6bee14 Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 18 Mar 2007 19:59:02 +0000 Subject: [PATCH] - Add judge and award type report generators (not nearly complete) - Migrate generic code into reports.inc.php --- admin/reports.inc.php | 228 +++++++++++++++++++++++++++++++-- admin/reports_awards.inc.php | 80 ++++++++++++ admin/reports_editor.php | 19 ++- admin/reports_judges.inc.php | 150 ++++++++++++++++++++++ admin/reports_students.inc.php | 214 ++----------------------------- 5 files changed, 472 insertions(+), 219 deletions(-) create mode 100644 admin/reports_awards.inc.php create mode 100644 admin/reports_judges.inc.php diff --git a/admin/reports.inc.php b/admin/reports.inc.php index a8de88a..6552238 100644 --- a/admin/reports.inc.php +++ b/admin/reports.inc.php @@ -22,6 +22,8 @@ */ require_once("reports_students.inc.php"); /* $report_students_fields */ + require_once('../lpdf.php'); + require_once('../lcsv.php'); $options = array(); @@ -94,14 +96,14 @@ foreach($stock as $n=>$v) { function report_load_field($report, $type) { - global $allow_options, $report_students_fields; + global $allow_options, $report_students_fields, $report_judges_fields, $report_awards_fields; $allow_fields = array(); switch($report['type']) { - case 'student': - $allow_fields = array_keys($report_students_fields); - break; + case 'student': $allow_fields = array_keys($report_students_fields); break; + case 'judge': $allow_fields = array_keys($report_judges_fields); break; + case 'award': $allow_fields = array_keys($report_awards_fields); break; } $ret = array(); @@ -134,12 +136,12 @@ foreach($stock as $n=>$v) { function report_save_field($report, $type) { - global $allow_options, $report_students_fields; + global $allow_options, $report_students_fields, $report_judges_fields; switch($report['type']) { - case 'student': - $allow_fields = array_keys($report_students_fields); - break; + case 'student': $allow_fields = array_keys($report_students_fields); break; + case 'judge': $allow_fields = array_keys($report_judges_fields); break; + case 'award': $allow_fields = array_keys($report_awards_fields); break; } /* First delete all existing fields */ @@ -233,12 +235,6 @@ foreach($stock as $n=>$v) { } - function report_gen($report) - { - if($report['type'] == 'student') { - return report_students_gen($report); - } - } function report_gen_by_id($report_id, $format=NULL) { @@ -263,4 +259,208 @@ foreach($stock as $n=>$v) { mysql_query("DELETE FROM reports_items WHERE `reports_id`=$r"); } + function report_gen($report) + { + global $config, $report_students_fields, $report_judges_fields, $report_awards_fields; + + //print_r($report); + switch($report['type']) { + case 'student': $fields = $report_students_fields; break; + case 'judge': $fields = $report_judges_fields; break; + case 'award': $fields = $report_awards_fields; break; + } + + $gen_mode = ''; + $fieldname = array(); + $thead = array(); + + $table['header']=array(); + $table['widths']=array(); + $table['dataalign']=array(); + $table['option']=array(); + + if($report['option']['type']=='csv') { + $rep=new lcsv(i18n($report_name)); + $gen_mode = 'table'; + + } else if($report['option']['type']=='label') { + /* Label */ + $label_stock = $stock($report['option']['stock']); + $rep=new lpdf( i18n($config['fairname']), + i18n($report_name), + $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"); + $rep->newPage($label_stock['pw'], $label_stock['ph']); + $rep->setFontSize(11); + $rep->setLabelDimensions($label_stock['w'], $label_stock['h'], + $label_stock['xs'], $label_stock['ys']); + $gen_mode = 'label'; + } else { + $rep=new lpdf( i18n($config['fairname']), + i18n($report['name']), + $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"); + $rep->newPage(); + $rep->setFontSize(11); + $gen_mode = 'table'; + if($report['option']['allow_multiline'] == 'yes') + $table['option']['allow_multiline'] = true; + } + + $sel = array(); + $x=0; + $group_by = array(); + /* Select columns to display */ + foreach($report['col'] as $f) { + $table['header'][] = i18n($fields[$f]['header']); + $table['widths'][] = $fields[$f]['width']; + $table['dataalign'][] = 'left'; + $sel[] = "{$fields[$f]['table']} AS C$x"; + $fieldname[$f] = "C$x"; + if(is_array($fields[$f]['group_by'])) + $group_by = array_merge($group_by, $fields[$f]['group_by']); + $x++; + } + /* We also want to select any column groupings, but we won't display them */ + $x=0; + foreach($report['group'] as $f) { + if(isset($fieldname[$f])) continue; + $sel[] = "{$fields[$f]['table']} AS G$x"; + $fieldname[$f] = "G$x"; + $x++; + } + + $x=0; + foreach($report['sort'] as $f) { + if(isset($fieldname[$f])) continue; + $sel[] = "{$fields[$f]['table']} AS S$x"; + $fieldname[$f] = "S$x"; + $x++; + } + + $x=0; + foreach($report['distinct'] as $f) { + if(isset($fieldname[$f])) continue; + $sel[] = "{$fields[$f]['table']} AS D$x"; + $fieldname[$f] = "D$x"; + $x++; + } + $sel = implode(",", $sel); + + $order = array(); + + /* Setup the order: groups, then sort order */ + foreach($report['group'] as $f) { + if(isset($fields[$f]['table_sort'])) { + $order[] = $fields[$f]['table_sort']; + } else { + $order[] = $fieldname[$f];//ields[$f]['table']; + } + } + foreach($report['sort'] as $f) { + if(isset($fields[$f]['table_sort'])) { + $order[] = $fields[$f]['table_sort']; + } else { + $order[] = $fieldname[$f];//$fields[$f]['table']; + } + } + $order = implode(",", $order); + + if(!isset($report['year'])) { + $report['year'] = $config['FAIRYEAR']; + } + + if(count($report['distinct'])) { + foreach($report['distinct'] as $f) { + $group_by[] = $fieldname[$f]; + } + } + + if(count($group_query)) { + $group_query = "GROUP BY ".implode(",", $group_by); + } else { + $group_query = ""; + } + + + $q = ''; + switch($report['type']) { + case 'student': $q = report_students_fromwhere($report); break; + case 'judge': $q = report_judges_fromwhere($report); break; + case 'award': $q = report_awards_fromwhere($report); break; + } + + $q = "SELECT $sel $q $group_query ORDER BY $order"; + +// print("$q"); + + $r = mysql_query($q); + echo mysql_error(); + + $ncols = count($report['col']); + $n_groups = count($report['group']); + $last_group_data = array(); + + while($i = mysql_fetch_assoc($r)) { + + if($n_groups > 0) { + $group_change = false; + for($x=0; $x<$n_groups; $x++) { + if($last_group_data["G$x"] != $i["G$x"]) { + $group_change = true; + } + $last_group_data["G$x"] = $i["G$x"]; + } + + if($group_change) { + /* Dump the last table */ + if(count($table['data'])) { + // print_r($table); + $rep->addTable($table); + $rep->nextLine(); + $table['data'] = array(); + /* Start a new page AFTER a table is + * dumped, so the first page doesn't + * end up blank */ + if($report['option']['group_new_page'] == 'yes') { + $rep->newPage(); + } else { + $rep->hr(); + $rep->vspace(-0.1); + } + } + + /* Construct a new header */ + $h = implode(" -- ", $last_group_data); + $rep->heading($h); + $rep->nextLine(); + } + + } + + $data = array(); + $x=0; + foreach($report['col'] as $c) { + if(is_array($fields[$c]['value_map'])) { + $v = $fields[$c]['value_map'][$i["C$x"]]; + } else { + $v = $i["C$x"]; + } + if($gen_mode == 'table') { + $data[] = $v; + } else if($gen_mode == 'label') { + $d = $label_data[$c]; + /* Label text: x%, y%, width%, height%, text */ + $this->addLabelText2($d['x'], $d['y'], $d['w'], + $d['h'], $v); + } + $x++; + } + $table['data'][] = $data; + } + + if(count($table['data'])) { + $rep->addTable($table); + } + $rep->output(); +} + ?> diff --git a/admin/reports_awards.inc.php b/admin/reports_awards.inc.php new file mode 100644 index 0000000..a34c7c2 --- /dev/null +++ b/admin/reports_awards.inc.php @@ -0,0 +1,80 @@ + + Copyright (C) 2005 James Grant + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation, version 2. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +$report_awards_fields = array( + 'name' => array( + 'name' => 'Award -- Name', + 'header' => 'Name', + 'width' => 2.0, + 'table' => 'award_awards.name' ), + + 'cwsfaward' => array( + 'name' => 'Award -- CWSF Award', + 'header' => 'CWSF', + 'width' => 0.5, + 'table' => 'award_awards.cwsfaward', + 'value_map' => array ('0' => 'No', '1' => 'Yes')), + +); + + function report_awards_fromwhere($report) + { + global $config, $report_awards_fields; + + $fields = $report_awards_fields; + $year = $report['year']; + +/* + $awards_join = ''; + $awards_where = ''; + + if(in_array('awards', $report['col']) || in_array('pn_awards', $report['col']) + || in_array('awards', $report['group']) || in_array('pn_awards', $report['group']) ) { + /* This requires some extra gymnastics + $awards_join = "LEFT JOIN winners ON(winners.projects_id = projects.id),award_prizes,award_awards,award_types"; + $awards_where = " AND winners.awards_prizes_id=award_prizes.id + AND award_prizes.award_awards_id=award_award_awards.id + AND award_types.id=award_award_awards.award_types_id + AND winners.year='$year' + AND award_award_awards.year='$year' + AND award_prizes.year='$year' + AND award_types.year='$year' "; + } + + $partner_join = ''; + if(in_array('bothnames', $report['col']) || in_array('partner', $report['col'])) { + $partner_join = "LEFT JOIN awards AS awards2 + ON(awards2.registrations_id=award_awards.registrations_id + AND awards2.id != award_awards.id)"; + } +*/ + $q = " FROM + award_awards + WHERE + awards_years.year='$year' + "; + + return $q; +} + +?> diff --git a/admin/reports_editor.php b/admin/reports_editor.php index ce605dc..3ba7d87 100644 --- a/admin/reports_editor.php +++ b/admin/reports_editor.php @@ -26,6 +26,8 @@ auth_required('admin'); require_once('reports_students.inc.php'); + require_once('reports_judges.inc.php'); + require_once('reports_awards.inc.php'); require_once('reports.inc.php'); $fields = array(); @@ -144,6 +146,8 @@ function reportChange() $report['distinct'] = parse_fields('distinct'); $report['option'] = parse_options('option'); +// print_r($report); + $loadaction = $_POST['loadaction']; $colaction = $_POST['colaction']; $repaction = $_POST['repaction']; @@ -188,9 +192,9 @@ function reportChange() if($colaction == 'add') $n_columns+=3; switch($report['type']) { - case 'student': - $fields = $report_students_fields; - break; + case 'student': $fields = $report_students_fields; break; + case 'judge': $fields = $report_judges_fields; break; + case 'awards': $fields = $report_awards_fields; break; } echo "<< ".i18n("Back to Reports")."
"; @@ -228,7 +232,14 @@ function reportChange() echo ""; echo ""; echo "Type: "; - echo ""; + echo ""; echo ""; echo "

Report Data

"; diff --git a/admin/reports_judges.inc.php b/admin/reports_judges.inc.php new file mode 100644 index 0000000..92c1c35 --- /dev/null +++ b/admin/reports_judges.inc.php @@ -0,0 +1,150 @@ + + Copyright (C) 2005 James Grant + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation, version 2. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +$report_judges_fields = array( + 'last_name' => array( + 'name' => 'Last Name', + 'header' => 'Judge -- Last Name', + 'width' => 1.0, + 'table' => 'judges.lastname' ), + + 'first_name' => array( + 'name' => 'First Name', + 'header' => 'Judge -- First Name', + 'width' => 1.0, + 'table' => 'judges.firstname' ), + + 'name' => array( + 'name' => 'Judge -- Full Name (last, first)', + 'header' => 'Name', + 'width' => 1.75, + 'table' => "CONCAT(judges.lastname, ', ', judges.firstname)", + 'table_sort'=> 'judges.lastname' ), + + 'namefl' => array( + 'name' => 'Judge -- Full Name (first last)', + 'header' => 'Name', + 'width' => 1.75, + 'table' => "CONCAT(judges.firstname, ' ', judges.lastname)", + 'table_sort'=> 'judges.lastname' ), + + 'email' => array( + 'name' => 'Judge -- Email', + 'header' => 'Email', + 'width' => 1.5, + 'table' => 'judges.email'), + + 'address' => array( + 'name' => 'Judge -- Address Street', + 'header' => 'Address', + 'width' => 2.0, + 'table' => "CONCAT(judges.address, ' ', judges.address2)"), + + 'city' => array( + 'name' => 'Judge -- Address City', + 'header' => 'City', + 'width' => 1.5, + 'table' => 'judges.city' ), + + 'province' => array( + 'name' => 'Judge -- Address Province', + 'header' => 'Province', + 'width' => 0.75, + 'table' => 'judges.province' ), + + 'postal' => array( + 'name' => 'Judge -- Address Postal Code', + 'header' => 'Postal', + 'width' => 0.75, + 'table' => 'judges.postalcode' ), + + 'phone_home' => array( + 'name' => 'Judges -- Phone (Home)', + 'header' => 'Phone(Home)', + 'width' => 1, + 'table' => 'judges.phonehome'), + + 'phone_work' => array( + 'name' => 'Judges -- Phone (Work)', + 'header' => 'Phone(Work)', + 'width' => 1.25, + 'table' => "CONCAT(judges.phonework, ' ', judges.phoneworkext)"), + + 'languages' => array( + 'name' => 'Judges -- Languages (REQUIRES MySQL 5.0)', + 'header' => 'Languages', + 'width' => 0.75, + 'table' => "GROUP_CONCAT(judges_languages.languages_lang ORDER BY judges_languages.languages_lang SEPARATOR ' ')", + 'group_by' => array('judges.id')), + + 'complete' => array( + 'name' => 'Judge -- Registration Complete', + 'header' => 'Complete', + 'width' => 0.5, + 'table' => 'judges.complete', + 'value_map' => array ('no' => 'No', 'yes' => 'Yes')), + +); + + function report_judges_fromwhere($report) + { + global $config, $report_judges_fields; + + $fields = $report_judges_fields; + $year = $report['year']; + +/* + $awards_join = ''; + $awards_where = ''; + + if(in_array('awards', $report['col']) || in_array('pn_awards', $report['col']) + || in_array('awards', $report['group']) || in_array('pn_awards', $report['group']) ) { + /* This requires some extra gymnastics + $awards_join = "LEFT JOIN winners ON(winners.projects_id = projects.id),award_prizes,award_awards,award_types"; + $awards_where = " AND winners.awards_prizes_id=award_prizes.id + AND award_prizes.award_awards_id=award_awards.id + AND award_types.id=award_awards.award_types_id + AND winners.year='$year' + AND award_awards.year='$year' + AND award_prizes.year='$year' + AND award_types.year='$year' "; + } + + $partner_join = ''; + if(in_array('bothnames', $report['col']) || in_array('partner', $report['col'])) { + $partner_join = "LEFT JOIN judges AS judges2 + ON(judges2.registrations_id=judges.registrations_id + AND judges2.id != judges.id)"; + } +*/ + $q = " FROM + judges, judges_years + WHERE + judges_years.judges_id = judges.id + AND judges_years.year='$year' + "; + + return $q; +} + +?> diff --git a/admin/reports_students.inc.php b/admin/reports_students.inc.php index 53f6742..cf508b6 100644 --- a/admin/reports_students.inc.php +++ b/admin/reports_students.inc.php @@ -242,123 +242,15 @@ $report_students_fields = array( ); - require_once('../lpdf.php'); - require_once('../lcsv.php'); - - function report_students_gen($report) + function report_students_fromwhere($report) { global $config, $report_students_fields; $fields = $report_students_fields; + $year = $report['year']; - //print_r($report); - - $gen_mode = ''; - $fieldname = array(); - $thead = array(); - - $table['header']=array(); - $table['widths']=array(); - $table['dataalign']=array(); - $table['option']=array(); - - if($report['option']['type']=='csv') { - $rep=new lcsv(i18n($report_name)); - $gen_mode = 'table'; - - } else if($report['option']['type']=='label') { - /* Label */ - $label_stock = $stock($report['option']['stock']); - $rep=new lpdf( i18n($config['fairname']), - i18n($report_name), - $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"); - $rep->newPage($label_stock['pw'], $label_stock['ph']); - $rep->setFontSize(11); - $rep->setLabelDimensions($label_stock['w'], $label_stock['h'], - $label_stock['xs'], $label_stock['ys']); - $gen_mode = 'label'; - } else { - $rep=new lpdf( i18n($config['fairname']), - i18n($report['name']), - $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif"); - $rep->newPage(); - $rep->setFontSize(11); - $gen_mode = 'table'; - if($report['option']['allow_multiline'] == 'yes') - $table['option']['allow_multiline'] = true; - } - - $sel = array(); - $x=0; - /* Select columns to display */ - foreach($report['col'] as $f) { - $table['header'][] = i18n($fields[$f]['header']); - $table['widths'][] = $fields[$f]['width']; - $table['dataalign'][] = 'left'; - $sel[] = "{$fields[$f]['table']} AS C$x"; - $fieldname[$f] = "C$x"; - $x++; - } - /* We also want to select any column groupings, but we won't display them */ - $x=0; - foreach($report['group'] as $f) { - if(isset($fieldname[$f])) continue; - $sel[] = "{$fields[$f]['table']} AS G$x"; - $fieldname[$f] = "G$x"; - $x++; - } - - $x=0; - foreach($report['sort'] as $f) { - if(isset($fieldname[$f])) continue; - $sel[] = "{$fields[$f]['table']} AS S$x"; - $fieldname[$f] = "S$x"; - $x++; - } - - $x=0; - foreach($report['distinct'] as $f) { - if(isset($fieldname[$f])) continue; - $sel[] = "{$fields[$f]['table']} AS D$x"; - $fieldname[$f] = "D$x"; - $x++; - } - $sel = implode(",", $sel); - - $order = array(); - - /* Setup the order: groups, then sort order */ - foreach($report['group'] as $f) { - if(isset($fields[$f]['table_sort'])) { - $order[] = $fields[$f]['table_sort']; - } else { - $order[] = $fieldname[$f];//ields[$f]['table']; - } - } - foreach($report['sort'] as $f) { - if(isset($fields[$f]['table_sort'])) { - $order[] = $fields[$f]['table_sort']; - } else { - $order[] = $fieldname[$f];//$fields[$f]['table']; - } - } - $order = implode(",", $order); - - $year = $config['FAIRYEAR']; - - $group_query = array(); - if(count($report['distinct'])) { - foreach($report['distinct'] as $f) { - $group_query[] = $fieldname[$f]; - } - } - - if(count($group_query)) { - $group_query = "GROUP BY ".implode(",", $group_query); - } else { - $group_query = ""; - } - + $awards_join = ''; + $awards_where = ''; if(in_array('awards', $report['col']) || in_array('pn_awards', $report['col']) || in_array('awards', $report['group']) || in_array('pn_awards', $report['group']) ) { @@ -367,26 +259,20 @@ $report_students_fields = array( $awards_where = " AND winners.awards_prizes_id=award_prizes.id AND award_prizes.award_awards_id=award_awards.id AND award_types.id=award_awards.award_types_id - AND winners.year=$year - AND award_awards.year=$year - AND award_prizes.year=$year - AND award_types.year=$year "; - } else { - $awards_join = ''; - $awards_where = ''; + AND winners.year='$year' + AND award_awards.year='$year' + AND award_prizes.year='$year' + AND award_types.year='$year' "; } + $partner_join = ''; if(in_array('bothnames', $report['col']) || in_array('partner', $report['col'])) { $partner_join = "LEFT JOIN students AS students2 ON(students2.registrations_id=students.registrations_id AND students2.id != students.id)"; - } else { - $partner_join = ''; - } - + } - $q = " SELECT $sel - FROM + $q = " FROM students $partner_join, schools, projects $awards_join, projectdivisions, projectcategories, registrations, emergencycontact @@ -405,83 +291,9 @@ $report_students_fields = array( AND projectdivisions.year='$year' AND emergencycontact.year='$year' $awards_where + "; - $group_query - - ORDER BY - $order"; - - //print("$q"); - - $r = mysql_query($q); - echo mysql_error(); - - $ncols = count($report['col']); - $n_groups = count($report['group']); - $last_group_data = array(); - - while($i = mysql_fetch_assoc($r)) { - - if($n_groups > 0) { - $group_change = false; - for($x=0; $x<$n_groups; $x++) { - if($last_group_data["G$x"] != $i["G$x"]) { - $group_change = true; - } - $last_group_data["G$x"] = $i["G$x"]; - } - - if($group_change) { - /* Dump the last table */ - if(count($table['data'])) { - // print_r($table); - $rep->addTable($table); - $rep->nextLine(); - $table['data'] = array(); - /* Start a new page AFTER a table is - * dumped, so the first page doesn't - * end up blank */ - if($report['option']['group_new_page'] == 'yes') { - $rep->newPage(); - } else { - $rep->hr(); - $rep->vspace(-0.1); - } - } - - /* Construct a new header */ - $h = implode(" -- ", $last_group_data); - $rep->heading($h); - $rep->nextLine(); - } - - } - - $data = array(); - $x=0; - foreach($report['col'] as $c) { - if(is_array($fields[$c]['value_map'])) { - $v = $fields[$c]['value_map'][$i["C$x"]]; - } else { - $v = $i["C$x"]; - } - if($gen_mode == 'table') { - $data[] = $v; - } else if($gen_mode == 'label') { - $d = $label_data[$c]; - /* Label text: x%, y%, width%, height%, text */ - $this->addLabelText2($d['x'], $d['y'], $d['w'], - $d['h'], $v); - } - $x++; - } - $table['data'][] = $data; - } - - if(count($table['data'])) { - $rep->addTable($table); - } - $rep->output(); + return $q; } ?>