- Commit the report generator.. it still needs work, but it's quite usable now.

This commit is contained in:
dave 2007-03-13 06:10:27 +00:00
parent eb0481b2a1
commit 453a3666f1

614
admin/reports_students.php Normal file
View File

@ -0,0 +1,614 @@
<?
/*
This file is part of the 'Science Fair In A Box' project
SFIAB Website: http://www.sfiab.ca
Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
Copyright (C) 2005 James Grant <james@lightbox.org>
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.
*/
?>
<?
require("../common.inc.php");
require("../lpdf.php");
require("../lcsv.php");
auth_required('admin');
$fields = array();
$fields['pn'] = array( 'name' => 'Project Number',
'header' => '#',
'width' => 0.5,
'table' => 'projects.projectnumber' );
$fields['last_name'] = array( 'name' => 'Last Name',
'header' => 'Last Name',
'width' => 1.0,
'table' => 'students.lastname' );
$fields['first_name'] = array( 'name' => 'First Name',
'header' => 'First Name',
'width' => 1.0,
'table' => 'students.firstname' );
$fields['name'] = array( 'name' => 'Full Student Name (last, first)',
'header' => 'Name',
'width' => 1.5,
'table' => "CONCAT(students.lastname, ', ', students.firstname)",
'table_sort'=> 'students.lastname' );
$fields['partner'] = array( 'name' => 'Partner Name',
'header' => 'Partner',
'width' => 1.5,
'table' => "CONCAT(students2.lastname, ', ', students2.firstname)" );
$fields['bothnames'] = array( 'name' => "Both Student Names",
'header' => 'Student(s)',
'width' => 3.0,
'table' => "CONCAT(students.firstname, ' ', students.lastname, IF(students2.lastname IS NULL,'', CONCAT(', ', students2.firstname, ' ', students2.lastname)))",
'table_sort' => 'students.lastnmae');
$fields['grade'] = array( 'name' => 'Grade',
'header' => 'Grade',
'width' => 0.5,
'table' => 'students.grade');
$fields['gender'] = array( 'name' => 'Gender',
'header' => 'Gender',
'width' => 0.5,
'table' => 'students.sex',
'value_map' =>array ('male' => 'Male', 'female' => 'Female'));
$fields['title'] = array( 'name' => 'Project Title',
'header' => 'Project Title',
'width' => 3.0,
'table' => 'projects.title' );
$fields['division'] = array( 'name' => 'Project Division',
'header' => 'Division',
'width' => 3.0,
'table' => 'projectdivisions.division' );
$fields['div'] = array( 'name' => 'Project Division Short Form' ,
'header' => 'Div',
'width' => 0.4,
'table' => 'projectdivisions.division_shortform' );
$fields['category'] = array( 'name' => 'Project Category',
'header' => 'Category',
'width' => 1,
'table_sort' => 'projectcategories.id',
'table' => 'projectcategories.category' );
$fields['address'] = array( 'name' => 'Student Address -- Street Address',
'header' => 'Address',
'width' => 2.0,
'table' => 'students.address');
$fields['city'] = array( 'name' => 'Student Address -- City',
'header' => 'City',
'width' => 1.5,
'table' => 'students.city' );
$fields['province'] = array( 'name' => 'Student Address -- Province',
'header' => 'Province',
'width' => 0.75,
'table' => 'students.province' );
$fields['postal'] = array( 'name' => 'Student Address -- Postal Code',
'header' => 'Postal',
'width' => 0.75,
'table' => 'students.postalcode' );
$fields['school'] = array( 'name' => 'School -- Name',
'header' => 'School Name',
'width' => 2.0,
'table' => 'schools.school' );
$fields['teacher'] =array( 'name' => 'School -- Teacher Name',
'header' => 'Teacher',
'width' => 2.0,
'table' => 'students.teachername' );
$fields['school_phone'] =array( 'name' => 'School -- Phone',
'header' => 'School Phone',
'width' => 1.0,
'table' => 'schools.phone' );
$fields['school_fax'] =array( 'name' => 'School -- Fax',
'header' => 'School Fax',
'width' => 1.0,
'table' => 'schools.fax' );
$fields['paid'] = array( 'name' => 'Paid',
'header' => 'Paid',
'width' => '0.4',
'table' => 'registrations.status',
'value_map' =>array ('complete' => '', 'paymentpending' => 'No'));
$fields['tshirt'] = array( 'name' => 'T-Shirt Size',
'header' => 'T-Shirt',
'width' => 0.75,
'table' => 'students.tshirt' );
$fields['awards'] = array( 'name' => 'Awards (warning: duplicates student for multiple awards!)',
'header' => 'Award Name',
'width' => 4,
'table' => "CONCAT(IF(award_types.type='Other','Special',award_types.type), ' ', award_awards.name)",
'table_sort' => 'award_awards.order');
$fields['pn_awards'] = array( 'name' => 'Project Num + Award (will be unique)',
'header' => 'Award Name',
'width' => 4,
'table' => "CONCAT(projects.projectnumber,' ', award_awards.name)",
'table_sort' => 'award_awards.order');
$reports = array();
$reports[] = array( 'name' => "Student Name, Project Num and Title, Category sorted by Last Name",
'cols' => array('pn', 'name', 'title', 'category'),
'sort' => array('last_name'),
'group' => array()
);
$reports[] = array( 'name' => "Student Name, Project Num and Title, Category sorted by Project Number",
'cols' => array('pn', 'name', 'title', 'category'),
'sort' => array('pn'),
'group' => array()
);
$reports[] = array( 'name' => "Student Name, Project Num and Title, Category sorted by Last Name, grouped by Category",
'cols' => array('pn', 'name', 'title'),
'sort' => array('last_name'),
'group' => array('category')
);
$reports[] = array( 'name' => "Student Name, Project Num, School Name sorted by Last Name",
'cols' => array('pn', 'name', 'school'),
'sort' => array('last_name'),
'group' => array()
);
$reports[] = array( 'name' => "Student Name, Project Num and Name sorted by Last Name, grouped by School Name",
'cols' => array('pn', 'name', 'title', 'category'),
'sort' => array('last_name'),
'group' => array('school'),
);
$reports[] = array( 'name' => "Teacher, School Info sorted by Teacher Name",
'cols' => array('teacher', 'school','school_phone','school_fax'),
'sort' => array('teacher'),
'group' => array(),
'distinct' => array('teacher'),
);
$reports[] = array( 'name' => "Teacher, School Info sorted by Teacher Name grouped by School Name",
'cols' => array('teacher', 'school','school_phone','school_fax'),
'sort' => array('teacher'),
'group' => array('school'),
'distinct' => array('teacher'),
);
$reports[] = array( 'name' => "Project Checkin",
'cols' => array('paid', 'pn', 'title', 'name', 'partner','tshirt', 'div'),
'sort' => array('pn'),
'group' => array('category'),
'option' => array('group_new_page' => 'yes'),
);
$reports[] = array( 'name' => "Student Pairs, Project Name/Num Grouped by School",
'cols' => array('pn', 'name', 'partner','title'),
'sort' => array('pn'),
'group' => array('school'),
'distinct' => array('pn'),
);
$reports[] = array( 'name' => "Individual Students, Project Name/Num Grouped by School",
'cols' => array('pn', 'name', 'title'),
'sort' => array('pn'),
'group' => array('school'),
);
$reports[] = array( 'name' => "Individual Students, Project Num, TShirt, Grouped by School",
'cols' => array('pn', 'name', 'tshirt'),
'sort' => array('pn'),
'group' => array('school'),
);
$reports[] = array( 'name' => "Program Guide",
'cols' => array('pn', 'bothnames', 'title'),
'sort' => array('pn'),
'group' => array('school'),
'distinct' => array('pn'),
);
$reports[] = array( 'name' => "Project Name/Num, Grade Grouped by School",
'cols' => array('pn', 'title', 'grade'),
'sort' => array('pn'),
'group' => array('school'),
'distinct' => array('pn'),
);
$reports[] = array( 'name' => "Award List (Media)",
'cols' => array('pn', 'name', 'address','city','province','postal'),
'sort' => array('pn'),
'group' => array('awards'),
);
$options = array();
$options['type'] = array( 'desc' => 'Report Format',
'values' => array('pdf'=>'PDF', 'csv'=>'CSV')
);
$options['group_new_page'] = array( 'desc' => 'Start each new grouping on a new page',
'values' => array('no'=>'No', 'yes'=>'Yes')
);
$allow_fields = array_keys($fields);
function check_fields_array($a, $name)
{
global $allow_fields;
$n = array();
foreach($a as $f) {
if($f=='') continue;
if(!in_array($f, $allow_fields)) {
echo "$name [$f] not allowed.\n";
exit;
}
$n[] = $f;
}
return $n;
}
/* ---- Generator -----*/
$action= $_GET['action'];
if($action == 'gen') {
/* Scrub the data */
$col = check_fields_array($_GET['col'], "Column");
$group = check_fields_array($_GET['group'], "Group");
$sort = check_fields_array($_GET['sort'], "Sort");
$distinct = check_fields_array($_GET['distinct'], "Distinct");
$option = $_GET['option'];
//print_r($option);
foreach($option as $k=>$v) {
$keys = array_keys($options[$k]['values']);
if(!in_array($v, $keys)) {
echo "option $k [$v] not allowed.\n";
exit;
}
}
$fieldname = array();
$thead = array();
if($option['type']=="pdf") {
$rep=new lpdf( i18n($config['fairname']),
i18n($report_name),
$_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif");
$rep->newPage();
$rep->setFontSize(11);
} else if($option['type']=="csv") {
$rep=new lcsv(i18n($report_name));
}
$table['header']=array();
$table['widths']=array();
$table['dataalign']=array();
$sel = array();
$x=0;
/* Select columns to display */
foreach($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($group as $f) {
if(isset($fieldnmae[$f])) continue;
$sel[] = "{$fields[$f]['table']} AS G$x";
$fieldname[$f] = "G$x";
$x++;
}
$x=0;
foreach($distinct as $f) {
if(isset($fieldname[$f])) continue;
$sel[] = "{$fields[$f]['table']} AS D$x";
$fieldname[$f] = "D$x";
$x++;
}
$sel = implode(",", $sel);
$n_groups = count($group);
$last_group_data = array();
$order = array();
/* Setup the order: groups, then sort order */
foreach($group as $f) {
if(isset($fields[$f]['table_sort'])) {
$order[] = $fields[$f]['table_sort'];
} else {
$order[] = $fieldname[$f];//ields[$f]['table'];
}
}
foreach($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 = "";
if(count($distinct)) {
$f = $distinct[0];
$group_query = "GROUP BY {$fieldname[$f]}";//$$fields[$f]['table']}";
}
if(in_array('awards', $col) || in_array('pn_awards', $col)
|| in_array('awards', $group) || in_array('pn_awards', $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 ";
} else {
$awards_join = '';
$awards_where = '';
}
if(in_array('bothnames', $col) || in_array('partner', $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
students $partner_join,
schools, projects $awards_join, projectdivisions,
projectcategories, registrations
WHERE
schools.id=students.schools_id
AND projects.registrations_id=students.registrations_id
AND projectdivisions.id=projects.projectdivisions_id
AND projectcategories.id=projects.projectcategories_id
AND registrations.id=students.registrations_id
AND (registrations.status='complete' OR registrations.status='paymentpending')
AND students.year='$year'
AND projects.year='$year'
AND registrations.year='$year'
AND projectcategories.year='$year'
AND projectdivisions.year='$year'
$awards_where
$group_query
ORDER BY
$order";
//print("$q");
$r = mysql_query($q);
echo mysql_error();
$ncols = count($col);
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($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($col as $c) {
if(is_array($fields[$c]['value_map'])) {
$v = $fields[$c]['value_map'][$i["C$x"]];
} else {
$v = $i["C$x"];
}
$data[] = $v;
$x++;
}
$table['data'][] = $data;
}
if(count($table['data'])) {
$rep->addTable($table);
}
$rep->output();
exit;
}
send_header("Administration - Reports > Student Report Generator");
?>
<script type="text/javascript">
function reportChange()
{
var index = document.forms.report.report.selectedIndex;
/* Clean out existing settings */
<?foreach($fields as $k=>$f) {
echo "document.getElementById('$k').checked=false;\n";
}?>
document.getElementById('group0').value = '';
// document.getElementById('group1').value = '';
// document.getElementById('group2').value = '';
document.getElementById('sort0').value = '';
document.getElementById('sort1').value = '';
document.getElementById('sort2').value = '';
document.getElementById('distinct0').value = '';
document.getElementById('type').value = 'pdf';
document.getElementById('group_new_page').value = 'no';
/* Load appropriate settings */
if(index == 0) {
/* Do nothing */
}
<?$x=1;
foreach($reports as $r) {
echo "else if(index == $x) {\n";
foreach($r['cols'] as $c) {
echo "document.getElementById('$c').checked=true;\n";
}
$i = 0;
foreach($r['sort'] as $c) {
echo "document.getElementById('sort$i').value='$c';\n";
}
$i = 0;
foreach($r['group'] as $c) {
echo "document.getElementById('group$i').value='$c';\n";
}
if(is_array($r['distinct'])) {
$i = 0;
foreach($r['distinct'] as $c) {
echo "document.getElementById('distinct$i').value='$c';\n";
}
}
if(is_array($r['option'])) {
foreach($r['option'] as $k=>$v) {
echo "document.getElementById('$k').value='$v';\n";
}
}
$x++;
echo "}\n";
}
?>
}
</script>
<?
/* ---- Setup ------ */
echo "<a href=\"reports.php\">&lt;&lt; ".i18n("Back to Reports")."</a><br />";
echo "<br />";
echo "<form method=\"get\" name=\"report\" action=\"reports_students.php\">";
echo "<input type=\"hidden\" name=\"action\" value=\"gen\">";
echo "<select name=\"report\" onchange=\"reportChange()\">";
echo "<option value=\"Custom::::::\">".i18n("Choose Report")."</option>\n";
$x=0;
foreach($reports as $r) {
echo "<option value=\"$x\">{$r['name']}</option>\n";
}
echo "</select>";
echo "<h4>Table Columns</h4>";
echo "<table>";
$x=0;
foreach($fields as $k=>$f) {
if($x%2==0) echo "<tr>";
echo "<td align=right><input type=\"checkbox\" type=\"text\" name=\"col[]\" id=\"$k\" value=\"$k\" /></td><td>{$f['name']}</td>";
if($x%2 == 1) echo "</tr>";
$x++;
}
echo "</table>";
echo "<h4>Grouping</h4>";
echo "Group By: ";
echo "<select name=\"group[]\" id=\"group0\">";
echo "<option value=\"\" />-- None --</option>";
foreach($fields as $k=>$f) {
echo "<option value=\"$k\" />{$f['name']}</option>";
}
echo "</select>";
echo "<h4>Sorting</h4>";
for($x=0;$x<3;$x++) {
echo "Sort By".($x + 1).": ";
echo "<select name=\"sort[]\" id=\"sort$x\">";
echo "<option value=\"\" />-- None --</option>";
foreach($fields as $k=>$f) {
echo "<option value=\"$k\" />{$f['name']}</option>";
}
echo "</select><br />";
}
echo "<h4>Distinct</h4>";
echo "Distinct Column: ";
echo "<select name=\"distinct[]\" id=\"distinct0\">";
echo "<option value=\"\" />-- None --</option>";
foreach($fields as $k=>$f) {
echo "<option value=\"$k\" />{$f['name']}</option>";
}
echo "</select>";
echo "<h4>Options</h4>";
foreach($options as $ok=>$o) {
echo "{$o['desc']}: <select name=\"option[$ok]\" id=\"$ok\">";
foreach($o['values'] as $k=>$v) {
echo "<option value=\"$k\">$v</option>";
}
echo "</select><br />\n";
}
echo "<br />";
echo "<input type=\"submit\" value=\"Generate Report\">";
echo "</form>";
send_footer();
?>