<?

/*
 * This file is part of the Science-ation project
 * Science-ation Website: https://science-ation.ca
 *
 * This file was part of the 'Science Fair In A Box' project
 *
 *
 * Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
 * Copyright (C) 2005 James Grant <james@lightbox.org>
 * Copyright (C) 2024 AlgoLibre Inc. <science-ation@algolibre.io>
 *
 * 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');

global $pdo;
// first, lets make sure someone isnt trying to see something that they arent allowed to!
$q = $pdo->prepare("SELECT (NOW()>=?) AS test");
$q->execute([$config['dates']['postparticipants']]);
$r = $q->fetch(PDO::FETCH_OBJ);

if ($r->test) {
	$q = $pdo->prepare("SELECT  registrations.id AS reg_id,
					registrations.status,
					projects.title,
					projects.projectnumber,
					projects.projectcategories_id,
					projects.projectdivisions_id,
					projectcategories.category,
					projectdivisions.division
					
				FROM
					registrations
					LEFT JOIN projects on projects.registrations_id=registrations.id
					LEFT JOIN projectcategories ON projectcategories.id=projects.projectcategories_id
					LEFT JOIN projectdivisions ON projectdivisions.id=projects.projectdivisions_id
				WHERE
					1
					AND registrations.year=? 
					AND projectcategories.year=? 
					AND projectdivisions.year=? 
					AND (status='complete' OR status='paymentpending')
				ORDER BY
					projectcategories.id,
					projectdivisions.id,
					projects.projectnumber
				");
	$q->execute([$config['FAIRYEAR'],$config['FAIRYEAR'],$config['FAIRYEAR']]);
	show_pdo_errors_if_any($pdo);

	$lastcat = 'something_that_does_not_exist';
	$lastdiv = 'something_that_does_not_exist';
	while ($r = $q->fetch(PDO::FETCH_OBJ)) {
		if (!$r->title)
			$t = '{no title}';
		else
			$t = $r->title;

		$ret['[' . $r->projectcategories_id . '] ' . $r->category . ' - ' . $r->division][] = array('n' => $r->projectnumber, 't' => iconv('ISO-8859-1', 'UTF-8', $t));
	}
}

echo json_encode($ret);
?>