<?php
/*
This is an example how to work with Kreatura v1.0 class. First launch and see:)
(C) Tomek Glinski 2004
tomekglinski at o2.pl
www.kreatura.prv.pl
www.kreatura.go.pl
www.actuosus.prv.pl
www.actuosus.go.pl
www.tomekglinski.prv.pl
www.tomekglinski.go.pl
*/
session_start();
require_once('kreatura.php');
// Let's extend the BaseCell to cope with our demandings:
class QuestionCell extends BaseCell{
var $Question;
var $Answers;
// Our constructor
function QuestionCell($x, $y, $w, $h, $Question, $Answers){
$this->Answers = Array();
if ($Question == "") die("What about the question?"); //check
if ( !is_array($Answers)) die("Answers must be an array!"); //check
// Launch BaseCell constructor (IMPORTANT)
$this->BaseCell($x, $y, $w, $h);
// Of course..
$this->Question = $Question;
$this->Answers = $Answers;
// Let's be sure, that size of letters doesn't matter
@array_map(strtolower, $this->Answers);
// QuestionCells will always have orange background by default
$this->setTdTag("bgcolor", "orange");
}
// generate() overwrites this method from BaseCell. We care only for things that are between <td> and </td> tags
function generate(){
$ret = '<center>'.$this->Question;
$ret.= ' <input type="text" ';
// "Input" name is name of Cell - this trick will make things easier (see the check method)
$ret.= 'name="' . $this->Name . '" /></center>';
//remember to RETURN the string!
return $ret;
}
// Check the answer. Good returns 1, bad are 0. It will be used to count proper answers
function check(){
// If cell is invisible, then in the past user put the right answer
if(!$this->Visible) return 1;
// We send answers through GET: index.php?NameOfCell=UsersAnswer&...
// So lets check, if in Answers table exists the users answer (it should be one of the values)
if (in_array(strtolower($_POST[$this->Name]), $this->Answers)){
// Set cell invisible
$this->Visible = false;
return 1;
}
return 0;
}
}
//******************************************************************************************************
// FIRST LOAD ON THIS PAGE (Kreatura object is not in session or we do the test once more). We build our Kreatura...
if (!@array_key_exists("kreatura", $_SESSION) || !is_object(unserialize($_SESSION["kreatura"])) || $_GET["oncemore"]==1){
// New Kreatura with table consisting of 10 x 15 <td> cells
$kreatura = new Kreatura(10, 15);
// Each <td> cell dimensions
$kreatura->setCellsDims("70px", "30px");
// Some tags to put inside <table .. >
$kreatura->setTableTag("bgcolor", "#F5F5F5");
$kreatura->setTableTag("rules", "none");
$kreatura->setTableTag("border", "0");
//Now, let's create some Cells and add them to our Kreatura
// BaseCell - we need only to put some text
$title = new BaseCell(0,0,10,1);
$title->Content = '<center><b>For every country enter one color, that appears in the flag of this country</b></center>';
$title->Content.= '<form action="index.php" method="post">';
// Naming cells is important - i.e. to handle Cell that are already in Kreatura
$title->Name = "title";
// Add Cell to Kreatura
$kreatura->addCell($title);
// Score Cell - will change dynamically.
$score = new BaseCell(0,14, 1, 1);
// At the beginning score is..
$score->Content = "0/6";
$score->Name = "score";
$kreatura->addCell($score);
// Now our extended Cells:)
$pol = new QuestionCell(0, 2, 5, 1, "Poland", Array(0 => "white", "red"));
$pol->Name = "pol";
$kreatura->addCell($pol);
$eng = new QuestionCell(1, 4, 5, 1, "England", Array(0 => "white", "red", "blue"));
$eng->Name = "eng";
$kreatura->addCell($eng);
$ire = new QuestionCell(2, 6, 5, 1, "Ireland", Array(0 => "white", "green", "yellow"));
$ire->Name = "ire";
$kreatura->addCell($ire);
$ger = new QuestionCell(3, 8, 5, 1, "Germany", Array(0 => "black", "red", "yellow"));
$ger->Name = "ger";
$kreatura->addCell($ger);
$fra = new QuestionCell(4, 10, 5, 1, "France", Array(0 => "white", "red", "blue"));
$fra->Name = "fra";
$kreatura->addCell($fra);
$cze = new QuestionCell(5, 12, 5, 1, "Czech Republic", Array(0 => "white", "red", "blue"));
$cze->Name = "cze";
$kreatura->addCell($cze);
// Submit button - done by BaseCell
$submit = new BaseCell(1, 14, 9, 1);
$submit->Content='<center><input type="submit" value="Check"></center></form>';
$submit->Name = "submit";
$kreatura->addCell($submit);
} // END OF FIRST TIME SECTION
// AFTER CLICKING "check"
// All we'll do here is checking the answers and presenting the score
else{
// Get Kreatura from session
$kreatura = unserialize($_SESSION["kreatura"]);
$sum = 0;
//cannot use 'foreach' - we need the references to the objects...
for($i = 0; $i < count($kreatura->Content); ++$i){
// We are interested only with QuestionCells
if (strtolower(get_class($kreatura->Content[$i])) != "questioncell") continue;
// Perform checking. Sum is the amount of good answers
$sum += $kreatura->Content[$i]->check();
}
// To present the score..
if ($sum > 0 and $sum < 6){
// ..delete old Cell with score (we need the Cells name!)..
$kreatura->removeCell("score");
// ..and create new, with new dimensions and content
$score=new BaseCell(0, 15 - $sum, 1, $sum);
$score->Content = "$sum/6";
$score->setTdTag("bgcolor", "greenyellow");
$score->setTdTag("valign", "top");
// The same name, to make future deletion possible
$score->Name = "score";
// Finito
$kreatura->addCell($score);
// We could also just call BaseCell method to change the dimensions of "score" Cell
}
// If it's end of the test
if ($sum == 6){
// Brutally delete all Cells in Kreatura
$kreatura->Content = Array();
// Create one new
$finish = new BaseCell(3, 3, 4, 10);
$finish->Content = '<center><h1><font color="white"> GOOD:) </font></h2><br> <a href="index.php?oncemore=1">Once more!</a></center>';
$finish->setTdTag("bgcolor", "gray");
$finish->Name = "finish";
$kreatura->addCell($finish);
$cel=&$kreatura->getCell("finish");
$cel->setTdTag("bgcolor", "green");
}
} // END OF "AFTER CLICKING"
// DO IT ALWAYS:
// Generate the table
echo $kreatura->generate();
// Save Kreatura
$_SESSION["kreatura"] = serialize($kreatura);
?>
|