Fix simples errors in the two classes

JavaScript now generates inputs with a unique name
Add todo in questions.php and manage multiple answers; start using Question class
This commit is contained in:
Némunaire 2012-05-22 03:34:24 +02:00
parent 91d2b8471c
commit 48a77a0d10
4 changed files with 76 additions and 35 deletions

View File

@ -2,48 +2,61 @@
class Question
{
private var $id;
private var $question;
private var $answers = array();
private var $added_time;
private var $validated = false;
private var $validator = "";
private $id;
private $question;
private $course;
private $answers = array();
private $added_time;
private $validated = false;
private $validator = "";
public function __construct($node = null)
{
$this->id = $node->getAttribute("id");
$this->added_time = $node->getAttribute("addedtime");
if (intval($node->getAttribute("validated")))
$this->validated = true;
$this->validator = $node->getAttribute("validator");
$this->question = $node->getAttribute("question");
$answers = $node->getElementByTagName("answer");
for ($i = 0; $i < $answers->length; $i++)
$this->answers[] = $answers->item($i)->getAttribute("answer");
if (isset($node))
{
$this->id = $node->getAttribute("id");
$this->added_time = $node->getAttribute("addedtime");
if (intval($node->getAttribute("validated")))
$this->validated = true;
$this->validator = $node->getAttribute("validator");
$this->question = $node->getAttribute("question");
$this->course = $node->getAttribute("course");
$answers = $node->getElementByTagName("answer");
for ($i = 0; $i < $answers->length; $i++)
$this->answers[] = $answers->item($i)->getAttribute("answer");
}
}
public static function new_Question($question, $answers = null)
public static function new_Question($question, $answers, $course = null)
{
$q = new Question();
$q->id = md5(time().$question);
$q->added_time = time();
$q->question = $question;
$q->course = $course;
if (!empty($answers))
{
if (is_array($answers))
{
foreach ($answers => $ans)
$this->answers[] = $ans;
foreach ($answers as $ans)
$q->answers[] = $ans;
}
else
$this->answers[] = $answers;
$q->answers[] = $answers;
}
return $q;
}
public function print_test()
{
echo 'Cours concerné : ' . $this->course . "<br>";
echo 'La question est : ' . $this->question . "<br>";
echo 'Les réponses sont : ' . print_r($this->answers, true);
}
/**
* Generate and fill an answer node
* @param $answer The answer string
@ -70,9 +83,10 @@ class Question
$qnode->setAttribute("addedtime", $this->added_time);
$qnode->setAttribute("validated", $this->validated);
$qnode->setAttribute("validator", $this->validator);
$qnode->setAttribute("course", $this->course);
$qnode->setAttribute("question", $this->question);
foreach ($answers => $ans)
foreach ($answers as $ans)
$qnode->appendChild(gen_anode($ans));
return $qnode;

View File

@ -1,11 +1,11 @@
<?php
require("Question.class.php");
require_once("Question.class.php");
class QuestionsFile
{
private var $filename;
private var $treeXML;
private $filename;
private $treeXML;
public function __construct($filename)
{

View File

@ -6,12 +6,14 @@
<title>Every Questions</title>
<script laguage="javascript">
var nbAnswer = 1;
function add()
{
var element = document.createElement("input");
element.setAttribute("type", "text");
element.setAttribute("name", "answer");
element.setAttribute("name", "answer" + nbAnswer);
nbAnswer++;
element.setAttribute("placeholder", "Enter the new answer");
var foo = document.getElementById("answerList");
@ -106,7 +108,7 @@
</p>
<p id="answerList">
<label for="answer">Quelle est la réponse ? </label>
<input id="answer" name="answer" type="text" />
<input id="answer" name="answer0" type="text" />
</p>
<p>

View File

@ -2,24 +2,46 @@
define("FILENAME", "questions_file.nemubot");
include("Question.class.php");
if (isset ($_POST['send']))
{
if (empty($_POST['question']) || empty($_POST['answer']))
//Gets parameters: course, question and answers
$course = "";
$answers = array();
foreach ($_POST as $key => $value)
{
echo "Il manque la question ou la réponse !";
if ($key == "question")
$question = $value;
else if ($key == "course")
$course = $value;
else if (preg_match("#^answer#", $key))
$answers[] = $value;
}
//Check we have at least a question and an answer
if (empty($question))
die("Veuillez indiquer une question !");
else if (count($answers) <= 0)
die("Veuillez indiquer au moins une réponse correcte !");
else
{
$question = $_POST['question'];
$answer = $_POST['answer'];
$quest = Question::new_Question($question, $answers, $course);
$quest->print_test();
echo 'La question est : ' . $question . "<br/>";
echo 'La réponse est : ' . $answer;
// @TODO: Create/Load a QuestionFile and add the question (it must be unique)
// @TODO: Find a validator (from the list of previous senders for example?)
// @TODO: Update the question and save the file
// @TODO: Send mail to the selected validator
/* This code works, perhaps should be placed in a method in Question class?
$to = "bertrand.cournaud@gmail.com";
$subject = "Nemubot";
$message = "Ceci est un test";
$headers = "From: bertrand.cournaud@gmail.com";
$subject = "[Nemubot] Validation d'une question";
$headers = "From: Nemubot <bot@nemunai.re>";
$message = "Bonjour,\n";
if (mail($to, $subject, $message, $headers))
{
@ -27,7 +49,10 @@ if (isset ($_POST['send']))
}
else
echo ("Error with the email");
//*/
}
}
else
header("Location: ./");
?>