<?php
require_once(dirname(__FILE__).'/../../upgrade.php');
class populate_categoryassociations_table extends UPGRADE
{
	/**
	* IsAlreadyUpgraded
	* Do we need to run this classes upgrade script or not ?
	*
	* @return bool true if the upgrade has already run, otherwise false
	*/
	function IsAlreadyUpgraded()
	{
		$query = 'SELECT COUNT(*) FROM '.$GLOBALS['tablePrefix'].'categoryassociations';
		$num_links = $GLOBALS['AKB_DB']->FetchOne($query);

		return ($num_links > 0);
	}

	/**
	* Upgrade
	* Do the actual upgrade of this component. If this is successful all
	* subsequent calls to IsAlreadyUpgraded should return true
	*
	* @return void
	*/
	function Upgrade()
	{
		$query = 'SELECT pk_questId, qParentCatId FROM '.$GLOBALS['tablePrefix'].'questions';
		$result = $GLOBALS['AKB_DB']->Query($query);

		while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
			$query = 'INSERT INTO '.$GLOBALS['tablePrefix'].'categoryassociations (categoryId, questionId) VALUES ('.$row['qParentCatId'].', '.$row['pk_questId'].')';
			$GLOBALS['AKB_DB']->Query($query);
		}
	}

}
?>
