<?

/*
 * 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.
 */
?>
<?
echo "To run this script, edit it and comment out the 'exit' (and this message) at the beginning";
exit;

include '../common.inc.php';

$projq = $pdo->prepare("SELECT id FROM registrations WHERE status='complete' OR status='paymentpending' AND year='2008'");
$projq->execute();
while ($projr = $projq->fetch(PDO::FETCH_OBJ)) {
	$reg_id = $projr->id;
	$q = $pdo->prepare("SELECT projects.projectcategories_id, projects.projectdivisions_id FROM projects WHERE registrations_id='$reg_id'");
	$q->execute();
	$r = $q->fetch(PDO::FETCH_OBJ);

	$projectnumber = $config['project_num_format'];
	// first replace the division and category
	$projectnumber = str_replace('D', $r->projectdivisions_id, $projectnumber);
	$projectnumber = str_replace('C', $r->projectcategories_id, $projectnumber);

	// now change the N to a % so we can use it as a wildcard
	$querynum = str_replace('N', '%', $projectnumber);
	$searchq = $pdo->prepare("SELECT projectnumber FROM projects WHERE year='" . $config['FAIRYEAR'] . "' AND projectnumber LIKE '$querynum'");
	$searchq->execute();
	print ("SELECT projectnumber FROM projects WHERE year='" . $config['FAIRYEAR'] . "' AND projectnumber LIKE '$querynum'\n");
	$searchnum = $searchq->rowCount();
	echo "searchnum=$searchnum \n";
	if ($searchq->rowCount()) {
		// first, put them all in an array
		$proj_nums = array();
		while ($searchr = $searchq->fetch(PDO::FETCH_OBJ)) {
			$proj_nums[] = $searchr->projectnumber;
		}
		print_r($proj_nums);

		// we will eventually find a good number, so lets loop forever until we find a good one
		$testnum = 1;
		$Nnum = 1;
		$ok = false;
		do {
			$Nnum = sprintf('%02d', $testnum);
			$test_projectnumber = str_replace('N', $Nnum, $projectnumber);
			echo "seeing if $test_projectnumber is in proj_nums\n";
			if (!in_array($test_projectnumber, $proj_nums))
				$ok = true;
			$testnum++;
		} while (!$ok);
	} else {
		$Nnum = '01';
	}

	$projectnumber = str_replace('N', $Nnum, $projectnumber);
	$stmt = $pdo->prepare("UPDATE projects SET projectnumber='$projectnumber' WHERE registrations_id='$reg_id' AND year='" . $config['FAIRYEAR'] . "'");
	$stmt->execute();
	if ($projectnumber) {
		echo "Assigned new project number $projectnumber\n";
	} else {
		echo "Eeek! couldnt assign project number for $reg_id";
		exit;
	}
	$assigned++;
}
echo "assigned=$assigned";

?>