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
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;