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 class Question
{ {
private var $id; private $id;
private var $question; private $question;
private var $answers = array(); private $course;
private var $added_time; private $answers = array();
private var $validated = false; private $added_time;
private var $validator = ""; private $validated = false;
private $validator = "";
public function __construct($node = null) public function __construct($node = null)
{ {
$this->id = $node->getAttribute("id"); if (isset($node))
$this->added_time = $node->getAttribute("addedtime"); {
if (intval($node->getAttribute("validated"))) $this->id = $node->getAttribute("id");
$this->validated = true; $this->added_time = $node->getAttribute("addedtime");
$this->validator = $node->getAttribute("validator"); if (intval($node->getAttribute("validated")))
$this->question = $node->getAttribute("question"); $this->validated = true;
$this->validator = $node->getAttribute("validator");
$answers = $node->getElementByTagName("answer"); $this->question = $node->getAttribute("question");
for ($i = 0; $i < $answers->length; $i++) $this->course = $node->getAttribute("course");
$this->answers[] = $answers->item($i)->getAttribute("answer");
$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 = new Question();
$q->id = md5(time().$question); $q->id = md5(time().$question);
$q->added_time = time(); $q->added_time = time();
$q->question = $question; $q->question = $question;
$q->course = $course;
if (!empty($answers)) if (!empty($answers))
{ {
if (is_array($answers)) if (is_array($answers))
{ {
foreach ($answers => $ans) foreach ($answers as $ans)
$this->answers[] = $ans; $q->answers[] = $ans;
} }
else else
$this->answers[] = $answers; $q->answers[] = $answers;
} }
return $q; 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 * Generate and fill an answer node
* @param $answer The answer string * @param $answer The answer string
@ -70,9 +83,10 @@ class Question
$qnode->setAttribute("addedtime", $this->added_time); $qnode->setAttribute("addedtime", $this->added_time);
$qnode->setAttribute("validated", $this->validated); $qnode->setAttribute("validated", $this->validated);
$qnode->setAttribute("validator", $this->validator); $qnode->setAttribute("validator", $this->validator);
$qnode->setAttribute("course", $this->course);
$qnode->setAttribute("question", $this->question); $qnode->setAttribute("question", $this->question);
foreach ($answers => $ans) foreach ($answers as $ans)
$qnode->appendChild(gen_anode($ans)); $qnode->appendChild(gen_anode($ans));
return $qnode; return $qnode;

View File

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

View File

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

View File

@ -2,24 +2,46 @@
define("FILENAME", "questions_file.nemubot"); define("FILENAME", "questions_file.nemubot");
include("Question.class.php");
if (isset ($_POST['send'])) 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 else
{ {
$question = $_POST['question']; $quest = Question::new_Question($question, $answers, $course);
$answer = $_POST['answer']; $quest->print_test();
echo 'La question est : ' . $question . "<br/>"; // @TODO: Create/Load a QuestionFile and add the question (it must be unique)
echo 'La réponse est : ' . $answer;
// @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"; $to = "bertrand.cournaud@gmail.com";
$subject = "Nemubot"; $subject = "[Nemubot] Validation d'une question";
$message = "Ceci est un test"; $headers = "From: Nemubot <bot@nemunai.re>";
$headers = "From: bertrand.cournaud@gmail.com"; $message = "Bonjour,\n";
if (mail($to, $subject, $message, $headers)) if (mail($to, $subject, $message, $headers))
{ {
@ -27,7 +49,10 @@ if (isset ($_POST['send']))
} }
else else
echo ("Error with the email"); echo ("Error with the email");
//*/
} }
} }
else
header("Location: ./");
?> ?>