<commit_before><?php
/*
 *  UOIT/DC Computer Science Club Elections Website
 *  Copyright (C) 2012 UOIT/DC Computer Science Club
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Contains a collection of the functions that directly interact with the database
 * to provide a convenient database abstraction layer, in the future support could
 * be added to support other databases. At the moment the implementations are
 * specific to MySQL (5.1 is the version tested) and prepared statements are
 * used for all queries to provide a layer of protection against SQL injection.
 * 
 * TODO More work perhaps to add OOP features for scoping instead of just an
 * aggregation of many functions, a hierarchy of classes would be nice
 */


/**
 * A function which creates the appropriate tables for the start of a new election year
 *
 * @param mysqli $mysqli_elections The mysqli connection object for the ucsc elections DB
 */
function new_election_tables($mysqli_elections)
{
	/* The members table */
	$members_tbl = 'members_';

	/* The positions tables for the nominations and elections */
	$positions_tbls = array('positions_nom _', 'positions_elect_');

	/* The voters table to record voters and winners table to record winners */
	$voters_winners_tbls = array('voters_nom_', 'voters_elect_', 'winners_nom_', 'winners_elect_');

	/* The current election year */
	$election_year = date('Y');

	/* Create the members table for the current election year */
	if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $members_tbl . $election_year .
												" (
													access_account SMALLINT UNSIGNED NOT NULL,
													first_name VARCHAR(32),
													last_name VARCHAR(32),
													PRIMARY KEY (access_account)
											 	  )"))
	{
		/* execute query */
		$stmt->execute();

		/* close statement */
		$stmt->close();
	}

	/* Create the positions tables for the current election year */
	foreach ($positions_tbls as $position_tbl)
	{
		if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $position_tbl . $election_year .
													" (
														reference SMALLINT UNSIGNED NOT NULL REFERENCES ".
															$members_tbl . $election_year . "(access_account),
														votes SMALLINT UNSIGNED,
														position VARCHAR(32)
													  )"))
		{
			/* execute query */
			$stmt->execute();

			/* close statement */
			$stmt->close();
		}
	}

	/* Create the voters and winners tables for the current election year */
	foreach ($voters_winners_tbls as $voters_winners_tbl)
	{
		if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $voters_winners_tbl . $election_year .
													" (
														reference SMALLINT UNSIGNED NOT NULL REFERENCES ".
															$members_tbl . $election_year . "(access_account),
														position VARCHAR(32)
													  )"))
		{
			/* execute query */
			$stmt->execute();

			/* close statement */
			$stmt->close();
		}
	}
}


/**
 * A function which populates the election table with the candidates and
 * incumbents for the final phase of the election.
 * 
 * TODO Clean this method up, already have function to get candidates/incumbents
 * TODO Handle the case where there is no candidate for a position
 * TODO Handle the case where the inumbent is no longer attending UOIT
 * 
 * @param mysqli $mysqli_elections The mysqli connection object for the ucsc elections DB 
 */
function pop_election_table($mysqli_elections)
{
	/* An array mapping the positions to the candidate access_account */
	$candidates = array('President'         => 0,
						'Vice President'    => 0,
						'Coordinator'       => 0,
						'Treasurer'         => 0
	);
	
	/* An array mapping the positions to the incumbent access_account */
	$incumbents = array('President'         => 0,
						'Vice President'    => 0,
						'Coordinator'       => 0,
						'Treasurer'         => 0
	);
	<commit_msg>Added support to determine if the elections tables exist<commit_after><?php
/*
 *  UOIT/DC Computer Science Club Elections Website
 *  Copyright (C) 2012 UOIT/DC Computer Science Club
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Contains a collection of the functions that directly interact with the database
 * to provide a convenient database abstraction layer, in the future support could
 * be added to support other databases. At the moment the implementations are
 * specific to MySQL (5.1 is the version tested) and prepared statements are
 * used for all queries to provide a layer of protection against SQL injection.
 * 
 * TODO More work perhaps to add OOP features for scoping instead of just an
 * aggregation of many functions, a hierarchy of classes would be nice
 */


/**
 * A function which creates the appropriate tables for the start of a new election year
 *
 * @param mysqli $mysqli_elections The mysqli connection object for the ucsc elections DB
 */
function new_election_tables($mysqli_elections)
{
	/* The members table */
	$members_tbl = 'members_';

	/* The positions tables for the nominations and elections */
	$positions_tbls = array('positions_nom_', 'positions_elect_');

	/* The voters table to record voters and winners table to record winners */
	$voters_winners_tbls = array('voters_nom_', 'voters_elect_', 'winners_nom_', 'winners_elect_');

	/* The current election year */
	$election_year = date('Y');

	/* Create the members table for the current election year */
	if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $members_tbl . $election_year .
												" (
													access_account SMALLINT UNSIGNED NOT NULL,
													first_name VARCHAR(32),
													last_name VARCHAR(32),
													PRIMARY KEY (access_account)
											 	  )"))
	{
		/* execute query */
		$stmt->execute();

		/* close statement */
		$stmt->close();
	}

	/* Create the positions tables for the current election year */
	foreach ($positions_tbls as $position_tbl)
	{
		if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $position_tbl . $election_year .
													" (
														reference SMALLINT UNSIGNED NOT NULL REFERENCES ".
															$members_tbl . $election_year . "(access_account),
														votes SMALLINT UNSIGNED,
														position VARCHAR(32)
													  )"))
		{
			/* execute query */
			$stmt->execute();

			/* close statement */
			$stmt->close();
		}
	}

	/* Create the voters and winners tables for the current election year */
	foreach ($voters_winners_tbls as $voters_winners_tbl)
	{
		if ($stmt = $mysqli_elections->prepare("CREATE TABLE ". $voters_winners_tbl . $election_year .
													" (
														reference SMALLINT UNSIGNED NOT NULL REFERENCES ".
															$members_tbl . $election_year . "(access_account),
														position VARCHAR(32)
													  )"))
		{
			/* execute query */
			$stmt->execute();

			/* close statement */
			$stmt->close();
		}
	}
}


/**
 * A function which determines if the election tables for the current year already exist
 * the purpose of this function is to determine if the daemon has already created the tables
 * for the current election year.
 * 
 * TODO CLEAN THIS METHOD UP, I DON'T LIKE THE IMPLEMENTATION OF ALL THE "IF STATEMENTS"
 * 
 * @param mysqli $mysqli_elections The mysqli connection object for the ucsc elections DB 
 */
function election_tables_exist($mysqli_elections)
{
	/* Election tables that exist for the current year */
	$tables_cur_year = array();
	
	/* The current election year */
	$election_year = date('Y');
	
	/* Get the election tables for the current year */
	if ($stmt = $mysqli_elections->prepare("SHOW TABLES LIKE '%_?'"))
	{
		/* bind parameters for markers */
		$stmt->bind_param('s', $election_year);
	
		/* execute query */
		$stmt->execute();
	
		/* bind result variables */
		$stmt->bind_result($table);
	
		while ($stmt->fetch())
		{
			$tables_cur_year[] = $table;
		}
		
		/* close statement */
		$stmt->close();
	}
	
	/* Verify that all of the tables for the current election year exist */
	if (count($tables_cur_year) === 7)
	{
		if (in_array('members_' . $election_year, $tables_cur_year)
			&& in_array('positions_nom_' . $election_year, $tables_cur_year)
			&& in_array('positions_elect_' . $election_year, $tables_cur_year)
			&& in_array('voters_nom_' . $election_year, $tables_cur_year)
			&& in_array('voters_elect_' . $election_year, $tables_cur_year)
			&& in_array('winners_nom_' . $election_year, $tables_cur_year)
			&& in_array('winners_elect_' . $election_year, $tables_cur_year))
		{
			return true;
		}
	}
	
	return false;
}


/**
 * A function which populates the election table with the candidates and
 * incumbents for the final phase of the election.
 * 
 * TODO Clean this method up, already have function to get candidates/incumbents
 * TODO Handle the case where there is no candidate for a position
 * TODO Handle the case where the inumbent is no longer attending UOIT
 * 
 * @param mysqli $mysqli_elections The mysqli connection object for the ucsc elections DB 
 */
function pop_election_table($mysqli_elections)
{
	/* An array mapping the positions to the candidate access_account */
	$candidates = array('President'         => 0,
						'Vice President'    => 0,
						'Coordinator'       => 0,
						'Treasurer'         => 0
	);
	
	/* An array mapping the positions to the incumbent access_account */
	$incumbents = array('President'         => 0,
						'Vice President'    => 0,
						'Coordinator'       => 0,
						'Treasurer'         => 0
	);
	
{'fraction_non_alphanumeric': 0.0947280178837556, 'fraction_numerical': 0.00437779433681073, 'mean_word_length': 3.028893058161351, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7618547', 'n_tokens_mistral': 3723, 'n_tokens_neox': 3011, 'n_words': 1134}