Author: Not specified | Language: php |
Description: Not specified | Timestamp: 2017-01-29 15:20:16 +0000 |
View raw paste | Reply |
<?php
require_once './Page.php';
class Quiz extends Page
{
private $isQuestion = false;
protected function __construct() {
parent::__construct();
}
protected function __destruct() {
// to do: if necessary, destruct attribute objects representing substructures/blocks
parent::__destruct();
}
public static function main() {
try {
$page = new Quiz();
$page->processReceivedData();
$page->generateView();
}
catch (Exception $e) {
header("Content-type: text/plain; charset=UTF-8");
echo $e->getMessage();
}
}
protected function getViewData() {
try{
$sql = "SELECT question, aid, answer, count
FROM question
INNER JOIN answer
ON question.qid = answer.qid
WHERE selected = True
ORDER BY aid";
$recordset = $this->_database->query($sql);
if (!$recordset)
throw new Exception("Fehler in DB-Abfrage: ".$this->_database->error);
$answers = array();
while ($record = $recordset->fetch_assoc()){
$answers[]=$record;
}
$recordset->free();
return $answers;
}
catch(Exception $ex){
echo $ex->getMessage();
}
}
protected function generateView() {
$answers = $this->getViewData();
$this->generatePageHeader();
echo "<h1> EWA Quiz</h1>";
$question = htmlspecialchars($answers[0]['question']);
echo "<p>$question</p>
<form action='Quiz.php' method='post' onsubmit='return testRadios();'>";
echo "<table>";
for ($i=0; $i<count($answers); $i++) {
$aid = $answers[$i]['aid'];
$answer = htmlspecialchars($answers[$i]['answer']);
$count = $answers[$i]['count'];
$countEm = $count * 0.2;
echo "<tr>";
if ($this->isQuestion) {
echo "<td class='Name'>
<input type='radio' name='aid' value='$aid' onclick='setBold(this);'/></td>";
}
else{
echo "<td><div style='width:{$countEm}em;'></div></td>";
}
echo "<td>$answer</td>";
echo" </tr>\r\n";
}
echo "</table>";
if ($this->isQuestion){
echo "<p><input type='submit' name='submit_Btn' value='Senden'/>";
echo "</p>";
}
echo "</form>";
$this->generatePageFooter();
}
/*
processReceivedData: Auswertung der (z.B. von einem Formular)
übermittelten Daten und Schreiben dieser Daten in die Datenbank:
*/
protected function processReceivedData() {
parent::processReceivedData();
if(isset($_POST['aid'])) {
$this->isQuestion = false;
$aid = $this->_database->real_escape_string($_POST['aid']);
$sql =
"UPDATE answer
SET count = count+1
WHERE aid = '$aid'
AND qid IN (SELECT qid FROM question WHERE selected=TRUE);";
$ok = $this->_database->query($sql);
if (!$ok)
throw new Exception($this->_database->error);
}
else {
$this->isQuestion = true;
}
}
//class end
}
Quiz::main();
require_once './Page.php';
class Quiz extends Page
{
private $isQuestion = false;
protected function __construct() {
parent::__construct();
}
protected function __destruct() {
// to do: if necessary, destruct attribute objects representing substructures/blocks
parent::__destruct();
}
public static function main() {
try {
$page = new Quiz();
$page->processReceivedData();
$page->generateView();
}
catch (Exception $e) {
header("Content-type: text/plain; charset=UTF-8");
echo $e->getMessage();
}
}
protected function getViewData() {
try{
$sql = "SELECT question, aid, answer, count
FROM question
INNER JOIN answer
ON question.qid = answer.qid
WHERE selected = True
ORDER BY aid";
$recordset = $this->_database->query($sql);
if (!$recordset)
throw new Exception("Fehler in DB-Abfrage: ".$this->_database->error);
$answers = array();
while ($record = $recordset->fetch_assoc()){
$answers[]=$record;
}
$recordset->free();
return $answers;
}
catch(Exception $ex){
echo $ex->getMessage();
}
}
protected function generateView() {
$answers = $this->getViewData();
$this->generatePageHeader();
echo "<h1> EWA Quiz</h1>";
$question = htmlspecialchars($answers[0]['question']);
echo "<p>$question</p>
<form action='Quiz.php' method='post' onsubmit='return testRadios();'>";
echo "<table>";
for ($i=0; $i<count($answers); $i++) {
$aid = $answers[$i]['aid'];
$answer = htmlspecialchars($answers[$i]['answer']);
$count = $answers[$i]['count'];
$countEm = $count * 0.2;
echo "<tr>";
if ($this->isQuestion) {
echo "<td class='Name'>
<input type='radio' name='aid' value='$aid' onclick='setBold(this);'/></td>";
}
else{
echo "<td><div style='width:{$countEm}em;'></div></td>";
}
echo "<td>$answer</td>";
echo" </tr>\r\n";
}
echo "</table>";
if ($this->isQuestion){
echo "<p><input type='submit' name='submit_Btn' value='Senden'/>";
echo "</p>";
}
echo "</form>";
$this->generatePageFooter();
}
/*
processReceivedData: Auswertung der (z.B. von einem Formular)
übermittelten Daten und Schreiben dieser Daten in die Datenbank:
*/
protected function processReceivedData() {
parent::processReceivedData();
if(isset($_POST['aid'])) {
$this->isQuestion = false;
$aid = $this->_database->real_escape_string($_POST['aid']);
$sql =
"UPDATE answer
SET count = count+1
WHERE aid = '$aid'
AND qid IN (SELECT qid FROM question WHERE selected=TRUE);";
$ok = $this->_database->query($sql);
if (!$ok)
throw new Exception($this->_database->error);
}
else {
$this->isQuestion = true;
}
}
//class end
}
Quiz::main();
View raw paste | Reply |