science-ation/admin/reports_students.inc.php

488 lines
13 KiB
PHP

<?
/*
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.
*/
$report_students_fields = array(
'pn' => array(
'name' => 'Project Number',
'header' => '#',
'width' => 0.5,
'table' => 'projects.projectnumber' ),
'last_name' => array(
'name' => 'Last Name',
'header' => 'Last Name',
'width' => 1.0,
'table' => 'students.lastname' ),
'first_name' => array(
'name' => 'First Name',
'header' => 'First Name',
'width' => 1.0,
'table' => 'students.firstname' ),
'name' => array(
'name' => 'Full Student Name (last, first)',
'header' => 'Name',
'width' => 1.75,
'table' => "CONCAT(students.lastname, ', ', students.firstname)",
'table_sort'=> 'students.lastname' ),
'partner' => array(
'name' => 'Partner Name',
'header' => 'Partner',
'width' => 1.5,
'table' => "CONCAT(students2.lastname, ', ', students2.firstname)" ),
'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'),
'grade' => array(
'name' => 'Grade',
'header' => 'Grade',
'width' => 0.5,
'table' => 'students.grade'),
'gender' => array(
'name' => 'Gender',
'header' => 'Gender',
'width' => 0.5,
'table' => 'students.sex',
'value_map' =>array ('male' => 'Male', 'female' => 'Female')),
'birthdate' => array(
'name' => 'Birthdate',
'header' => 'Birthdate',
'width' => 1,
'table' => 'students.dateofbirth'),
'title' => array(
'name' => 'Project Title',
'header' => 'Project Title',
'width' => 2.75,
'table' => 'projects.title' ),
'division' => array(
'name' => 'Project Division',
'header' => 'Division',
'width' => 3.0,
'table' => 'projectdivisions.division' ),
'div' => array(
'name' => 'Project Division Short Form' ,
'header' => 'Div',
'width' => 0.4,
'table' => 'projectdivisions.division_shortform' ),
'category' => array(
'name' => 'Project Category',
'header' => 'Category',
'width' => 1,
'table_sort' => 'projectcategories.id',
'table' => 'projectcategories.category' ),
'categorydivision' => array(
'name' => 'Project Category - Project Division',
'header' => 'Category/Division',
'width' => 3.5,
'table_sort' => 'projectcategories.id',
'table' => "CONCAT(projectcategories.category',' - ', projectdivisions.division)"),
'address' => array(
'name' => 'Student Address -- Street Address',
'header' => 'Address',
'width' => 2.0,
'table' => 'students.address'),
'city' => array(
'name' => 'Student Address -- City',
'header' => 'City',
'width' => 1.5,
'table' => 'students.city' ),
'province' => array(
'name' => 'Student Address -- Province',
'header' => 'Province',
'width' => 0.75,
'table' => 'students.province' ),
'postal' => array(
'name' => 'Student Address -- Postal Code',
'header' => 'Postal',
'width' => 0.75,
'table' => 'students.postalcode' ),
'school' => array(
'name' => 'School -- Name',
'header' => 'School Name',
'width' => 2.25,
'table' => 'schools.school' ),
'schooladdr' => array(
'name' => 'School -- Full Address',
'header' => 'School Address',
'width' => 3.0,
'table' => "CONCAT(schools.address, ', ', schools.city, ', ', schools.province_code, ', ', schools.postalcode)" ),
'teacher' => array(
'name' => 'School -- Teacher Name',
'header' => 'Teacher',
'width' => 1.5,
'table' => 'students.teachername' ),
'teacheremail' => array(
'name' => 'School -- Teacher Email',
'header' => 'Teacher Email',
'width' => 2.0,
'table' => 'students.teacheremail' ),
'school_phone' => array(
'name' => 'School -- Phone',
'header' => 'School Phone',
'width' => 1,
'table' => 'schools.phone' ),
'school_fax' => array(
'name' => 'School -- Fax',
'header' => 'School Fax',
'width' => 1,
'table' => 'schools.fax' ),
'paid' => array(
'name' => 'Paid',
'header' => 'Paid',
'width' => '0.4',
'table' => 'registrations.status',
'value_map' => array ('complete' => '', 'paymentpending' => 'No')),
'tshirt' => array(
'name' => 'T-Shirt Size',
'header' => 'T-Shirt',
'width' => 0.55,
'table' => 'students.tshirt',
'value_map' => array ('none' => '', 'small' => 'Small', 'medium' => 'Medium',
'large' => 'Large', 'xlarge' => 'X-Large')),
'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'),
'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'),
'req_elec' => array(
'name' => 'If the project requires electricity',
'header' => 'Elec',
'width' => .5,
'table' => "projects.req_electricity",
'value_map' => array ('no' => '', 'yes' => 'Yes')),
'req_table' => array(
'name' => 'If the project requires a table',
'header' => 'Table',
'width' => .5,
'table' => "projects.req_table",
'value_map' => array ('no' => '', 'yes' => 'Yes')),
'req_special' => array(
'name' => 'Any special requirements the project has',
'header' => 'Special Requirements',
'width' => 3,
'table' => "projects.req_special"),
'emerg_name' => array(
'name' => 'Emergency Contact -- Name',
'header' => 'Emerg. Name',
'width' => 1.5,
'table' => "CONCAT(emergencycontact.firstname, ' ', emergencycontact.lastname)"),
'emerg_relation' => array(
'name' => 'Emergency Contact -- Relationship',
'header' => 'Emerg. Rlt',
'width' => 1,
'table' => "emergencycontact.relation"),
'emerg_phone' => array(
'name' => 'Emergency Contact -- Phone',
'header' => 'Emerg. Phone',
'width' => 1,
'table' => "CONCAT(emergencycontact.phone1, ' ', emergencycontact.phone2, ' ', emergencycontact.phone3, ' ', emergencycontact.phone4)"),
);
require_once('../lpdf.php');
require_once('../lcsv.php');
function report_students_gen($report)
{
global $config, $report_students_fields;
$fields = $report_students_fields;
//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 = "";
}
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 ";
} else {
$awards_join = '';
$awards_where = '';
}
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
students $partner_join,
schools, projects $awards_join, projectdivisions,
projectcategories, registrations, emergencycontact
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 emergencycontact.students_id=students.id
AND students.year='$year'
AND projects.year='$year'
AND registrations.year='$year'
AND projectcategories.year='$year'
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();
}
?>