<?

/*
 * 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_once ('../user.inc.php');
user_auth_required('committee', 'admin');
require ('../lpdf.php');
require ('../lcsv.php');
require ('../questions.inc.php');

if (!$_GET['type'])
	$type = 'csv';
else
	$type = $_GET['type'];

if ($type == 'pdf') {
	$rep = new lpdf(i18n($config['fairname']),
		i18n('Judge List'),
		$_SERVER['DOCUMENT_ROOT'] . $config['SFIABDIRECTORY'] . '/data/logo-200.gif');

	$rep->newPage();
	$rep->setFontSize(11);
} else if ($type == 'csv') {
	$rep = new lcsv(i18n('Judge List'));
}

$table = array();
$table['header'] = array(
	i18n('ID'),
	i18n('Unique ID'),
	i18n('Last Name'),
	i18n('First Name'),
	i18n('Email'),
	i18n('Phone Home'),
	i18n('Phone Work'),
	i18n('Phone Work Ext'),
	i18n('Phone Cell'),
	i18n('Languages'),
	i18n('Organization'),
	i18n('Address 1'),
	i18n('Address 2'),
	i18n('City'),
	i18n($config['provincestate']),
	i18n($config['postalzip']),
	i18n('Highest PostSecDeg'),
	i18n('Professional Quals'),
	i18n('Expertise Other')
);

/* Append headers for all the custom questions */
$qs = questions_load_questions('judgereg', $config['FAIRYEAR']);
$keys = array_keys($qs);
foreach ($keys as $qid) {
	$table['header'][] = i18n($qs[$qid]['db_heading']);
}

// grab the list of divisions, because the last fields of the table will be the sub-divisions
$q = $pdo->prepare("SELECT * FROM projectcategories WHERE year=? ORDER BY id");
$q->execute([$config['FAIRYEAR']]);
$numcats = $q->rowCount();
$catheadings = array();
while ($r = $q->fetch(PDO::FETCH_OBJ)) {
	$cats[] = $r->id;
	$catheadings[] = "$r->category (out of 5)";
}
// grab the list of divisions, because the last fields of the table will be the sub-divisions
$q = $pdo->prepare("SELECT * FROM projectdivisions WHERE year=? ORDER BY id");
$q->execute([$config['FAIRYEAR']]);
$divheadings = array();
while ($r = $q->fetch(PDO::FETCH_OBJ)) {
	$divs[] = $r->id;
	$divheadings[] = "$r->division (out of 5)";
	$divheadings[] = "$r->division subdivisions";
}

// now append the arrays together
$table['header'] = array_merge($table['header'], array_merge($catheadings, $divheadings));

$times = array();
$datetimeheadings = array();

/* Load the judging rounds */
$q = $pdo->prepare("SELECT date,starttime,endtime,name FROM judges_timeslots WHERE round_id='0' AND year=? ORDER BY starttime,type");
$q->execute([$config['FAIRYEAR']]);
$x = 0;
while ($r = $q->fetch(PDO::FETCH_OBJ)) {
	$found = false;
	foreach ($times as $xx => $t) {
		if ($t['date'] == $r->date && $t['starttime'] == $r->starttime && $t['endtime'] == $r->endtime) {
			$times[$xx]['name'] .= ", {$r->name}";
			$found = true;
			break;
		}
	}
	if (!$found) {
		$times[$x] = array('date' => $r->date,
			'starttime' => $r->starttime,
			'endtime' => $r->endtime,
			'name' => $r->name);
		$datetimeheadings[] = $r->name;
		$x++;
	}
}

$table['header'] = array_merge($table['header'], $datetimeheadings);

// fill these in if we ever make this PDFable
$table['widths'] = array();
$table['dataalign'] = array();

$q = $pdo->prepare("SELECT 
				users.*,
				users_judge.*
			FROM 
				users
				JOIN users_judge ON users.id=users_judge.users_id
			WHERE 
				users.deleted='no' AND 
				users.year=?
				AND users.types LIKE '%judge%'

			ORDER BY 
				lastname,
				firstname");
$q->execute([$config['FAIRYEAR']]);
show_pdo_errors_if_any($pdo);
while ($r = $q->fetch(PDO::FETCH_OBJ)) {
	$u = user_load($r->id);

	$expertise_other = str_replace("\n", ' ', $r->expertise_other);
	$expertise_other = str_replace("\r", '', $expertise_other);

	if (isset($divdata))
		unset($divdata);
	$divdata = array();
	if (isset($catdata))
		unset($catdata);
	$catdata = array();
	$languages = '';

	foreach ($u['cat_prefs'] AS $c) {
		$catdata[] = $c + 2;
	}

	foreach ($u['div_prefs'] AS $d) {
		$divdata[] = $d;
		// FIXME: 2010-01-22 - James - get the sub divisions for now we use a placeholder
		$divdata[] = '';
	}

	foreach ($u['languages'] AS $k => $v) {
		$languages .= "$v/";
	}
	$languages = substr($languages, 0, -1);

	$qarray = array();
	$qans = questions_load_answers('judgereg', $r->id, $config['FAIRYEAR']);
	$keys = array_keys($qans);
	foreach ($keys as $qid) {
		$qarray[] = $qans[$qid];
	}

	$tq = $pdo->prepare('SELECT * FROM judges_availability WHERE users_id=? ORDER BY `start`');
	$tq->execute([$r->id]);

	$sel = array();
	$timedata = array();
	while ($tr = $tq->fetch(PDO::FETCH_OBJ)) {
		foreach ($times as $x => $t) {
			if ($tr->start == $t['starttime'] && $tr->end == $t['endtime'] && $tr->date == $t['date']) {
				$sel[$x] = true;
			}
		}
	}

	foreach ($times as $x => $t) {
		if (get_value_from_array($sel, $x) == true) {
			$timedata[] = 'yes';
		} else {
			$timedata[] = 'no';
		}
	}

	$tmp = array(
		$r->id,
		$r->uid,
		$r->lastname,
		$r->firstname,
		$r->email,
		$r->phonehome,
		$r->phonework,
		get_value_property_or_default($r, 'phoneworkext'),
		$r->phonecell,
		$languages,
		$r->organization,
		$r->address,
		$r->address2,
		$r->city,
		$r->province,
		$r->postalcode,
		$r->highest_psd,
		get_value_property_or_default($r, 'professional_quals'),
		$expertise_other
	);
	$tmp = array_merge($tmp, $qarray);

	$extradata = array_merge($catdata, $divdata);
	$table['data'][] = array_merge(array_merge($tmp, $extradata), $timedata);
}

$rep->addTable($table);
$rep->output();

?>