This repository has been archived on 2020-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
nemubot-askweb/Question.class.php
2012-06-15 13:51:45 +02:00

177 lines
3.8 KiB
PHP

<?php
class Question
{
private $id;
private $question;
private $course;
private $answers = array();
private $added_time;
private $validated = false;
private $validator = "";
public function __construct($node = null)
{
if (isset($node))
{
$this->id = $node->getAttribute("xml: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->getElementsByTagName("answer");
for ($i = 0; $i < $answers->length; $i++)
$this->answers[] = $answers->item($i)->getAttribute("answer");
}
}
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 as $ans)
$q->answers[] = $ans;
}
else
$q->answers[] = $answers;
}
return $q;
}
public function regen_id()
{
$this->id = md5(time().$this->question.$this->validator);
}
public function set_validator($val)
{
$this->validator = $val;
$this->regen_id();
}
public function get_validator()
{
return $this->validator;
}
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) . "<br/>";
}
/**
* Generate and fill an answer node
* @param $answer The answer string
* @param $scrore The score given by this answer
*/
private function gen_anode($treeXML, $answer, $score = 1)
{
$anode = $treeXML->createElement("answer");
$anode->setAttribute("answer", $answer);
$anode->setAttribute("score", $score);
return $anode;
}
/**
* Generate XML corresponding to this question
*/
public function to_xml($root)
{
$qnode = $root->createElement("question");
$qnode->setAttribute("xml:id", $this->id);
$qnode->setAttribute("addedtime", $this->added_time);
$qnode->setAttribute("validated", intval($this->validated));
$qnode->setAttribute("validator", $this->validator);
$qnode->setAttribute("course", $this->course);
$qnode->setAttribute("question", $this->question);
foreach ($this->answers as $ans)
$qnode->appendChild($this->gen_anode($root, $ans));
return $qnode;
}
public function validated()
{
$this->validated = true;
}
public function getId()
{
return $this->id;
}
public function isValidated()
{
return $this->validated;
}
public function getCourse()
{
return $this->course;
}
public function setCourse($course)
{
$this->course = $course;
}
public function getQuestion()
{
return $this->question;
}
public function setQuestion($question)
{
$this->question = $question;
}
public function getAnswer()
{
return $this->answers;
}
public function setAnswer($answers)
{
if (!empty($answers))
{
if (is_array($answers))
{
foreach ($answers as $ans)
$q->answers[] = $ans;
}
else
$q->answers[] = $answers;
}
}
function mail_utf8($to, $subject = '(No subject)',
$message = '', $header = '')
{
$header_ = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/plain; charset=UTF-8' . "\r\n";
return (mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=',
$message, $header_ . $header));
}
}
?>