<? include 'xmlheader.php'; ?>
<?echo "<?xml version=\"1.0\" ?>";?>
//This file returns the Word Web, in a recursive XML format.
//If the returnwebtobuild flag is set, it does this in a way
//which allows the two users to build the web,
//contributing alternately. Specifically:
//* The same user's word web will be returned to both users.
//* A status flag is used to let each user know whether it's
// their turn to add to the web. If it's "active", it's that
// user's turn to add to the web; if it's "passive", that
// user must wait until the other user has added something.
//* Once a user has added something, the status flag switches,
// so that it becomes the other user's turn to add something.
//* Once the first web is complete, we move on to the second
// web. Once the second web is complete, the status flag
// changes to "complete", indicating that both webs are
// complete and we should move to the next stage of the game.
//If the returnwebtobuildflag is not set, we just return all
//the web items in this user's word web. So:
//* Each user will get their own word web: the one
// which started with their favourite colour.
//* The status is not returned.
//The file wordweb.htm will call this file with the flag set
//(since it wants to build the web). The file traits.htm
//will call this file without the flag set (since it
//just wants to see the completed web).
$returnwebtobuild = getAndCheckParameter("returnwebtobuild", PATTERN_NUMERIC, "There's been an error. [returnwebtobuild is invalid]");
//This defines the maximum number of items in the word web
define ("WORDWEB_ITEMS", 12);
$gameid = getGameId();
$otheruserid = getOtherUserId();
//This currentuserid refers to the user whose
//word web we're returning. If returnwebtobuild is 0,
//it'll be the same as this user's userid.
$currentuserid = -1;
if ($returnwebtobuild != "1")
//This user sees their own word web
$currentuserid = $_SESSION['userid'];
//First, let's set $currentuserid, which tells us
//whose word web we're working on. $currentuserid
//will either equal $_SESSION['userid'] or $otheruserid: it'll
//equal the *lower* of these two values until
//that word web is complete; then it'll equal the higher value.
//So, which is the lower user ID?
//(For simplicity of code, later, we'll also get the higher one.)
$loweruserid = $_SESSION['userid'] < $otheruserid ? $_SESSION['userid']: $otheruserid;
$higheruserid = $_SESSION['userid'] < $otheruserid ? $otheruserid : $_SESSION['userid'];
//How many items are in the corresponding word web?
$numWebItemsLower = getNumWebItems($loweruserid);
$numWebItemsHigher = getNumWebItems($higheruserid);
//If both webs are complete, then we need only return that they're complete
if ($numWebItemsLower + $numWebItemsHigher >= WORDWEB_ITEMS * 2)
//This is also for simplicity of code: a variable for
//the number of items in the current word web. It will
//equal either $numWebItemsLower or $numWebItemsHigher
$numWebItemsCurrent = -1;
if ($numWebItemsLower < WORDWEB_ITEMS)
//This word web isn't complete. Let's work on that.
$currentuserid = $loweruserid;
$numWebItemsCurrent = $numWebItemsLower;
//The first word web is complete: let's work on the other one.
$currentuserid = $higheruserid;
$numWebItemsCurrent = $numWebItemsHigher;
$status = "";
$mywordweb = "";
//If we're working on this user's word web, the status
//will be active when there's an even number of items in the web
//and passive otherwise. If it's the other user's word web,
//it's the other way round.
//There's also a special case: if there's zero items in the word
//web, then it means that no favourite colour's been entered:
//in other words, we've entered our favourite colour, but we're
//working on the other user's word web, and they haven't entered
//their favourite colour yet. So we return "passive": in other
//words, we're waiting for the other user.
if ($currentuserid == $_SESSION['userid'])
$status = ($numWebItemsCurrent % 2 == 0 || $numWebItemsCurrent == 0) ? "passive" : "active";
$mywordweb = "yes";
$status = ($numWebItemsCurrent % 2 != 0 || $numWebItemsCurrent == 0) ? "passive" : "active";
$mywordweb = "no";
//This outputs the entire word web, for the appropriate
//user, as XML.
//This function outputs a single word from the word web.
//It does this by calling queryDatabase, which will call
//outputRow with any results, which may call this function
$sGetWebQuery = "SELECT DISTINCT webitemid, name FROM webitem WHERE userid = ".$currentuserid." AND previouswebitemid = ".$idLastId." AND gameid = ".$gameid;
//This function is called by the queryDatabase function.
$sGetCountQuery = "SELECT COUNT(1) AS numwebitems FROM webitem WHERE userid = ".$id." AND gameid = ".$gameid;
$rowResults = queryDatabaseForSingleRow($sGetCountQuery);