Add questions manager class
This commit is contained in:
parent
ef33e787a4
commit
65f5d0d2ce
82
Question.class.php
Normal file
82
Question.class.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
class Question
|
||||
{
|
||||
private var $id;
|
||||
private var $question;
|
||||
private var $answers = array();
|
||||
private var $added_time;
|
||||
private var $validated = false;
|
||||
private var $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");
|
||||
}
|
||||
|
||||
public static function new_Question($question, $answers = null)
|
||||
{
|
||||
$q = new Question();
|
||||
$q->id = md5(time().$question);
|
||||
$q->added_time = time();
|
||||
$q->question = $question;
|
||||
|
||||
if (!empty($answers))
|
||||
{
|
||||
if (is_array($answers))
|
||||
{
|
||||
foreach ($answers => $ans)
|
||||
$this->answers[] = $ans;
|
||||
}
|
||||
else
|
||||
$this->answers[] = $answers;
|
||||
}
|
||||
|
||||
return $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate and fill an answer node
|
||||
* @param $answer The answer string
|
||||
* @param $scrore The score given by this answer
|
||||
*/
|
||||
private function gen_anode($answer, $score = 1)
|
||||
{
|
||||
$anode = $this->treeXML->createElement("answer");
|
||||
|
||||
$qnode->setAttribute("answer", $answer);
|
||||
$qnode->setAttribute("score", $score);
|
||||
|
||||
return $anode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate XML corresponding to this question
|
||||
*/
|
||||
public function to_xml($root)
|
||||
{
|
||||
$qnode = $root->createElement("question");
|
||||
|
||||
$qnode->setAttribute("id", $this->id);
|
||||
$qnode->setAttribute("addedtime", $this->added_time);
|
||||
$qnode->setAttribute("validated", $this->validated);
|
||||
$qnode->setAttribute("validator", $this->validator);
|
||||
$qnode->setAttribute("question", $this->question);
|
||||
|
||||
foreach ($answers => $ans)
|
||||
$qnode->appendChild(gen_anode($ans));
|
||||
|
||||
return $qnode;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
71
QuestionsFile.class.php
Normal file
71
QuestionsFile.class.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
require("Question.class.php");
|
||||
|
||||
class QuestionsFile
|
||||
{
|
||||
private var $filename;
|
||||
private var $treeXML;
|
||||
|
||||
public function __construct($filename)
|
||||
{
|
||||
$this->filename = $filename;
|
||||
$this->reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load or reload the questions file.
|
||||
* Unsave changes will be erased.
|
||||
*/
|
||||
private function reload()
|
||||
{
|
||||
$this->treeXML = new DOMDocument('1.0', 'UTF-8');
|
||||
$this->treeXML->formatOutput = true;
|
||||
|
||||
if (isfile($this->filename))
|
||||
{
|
||||
$this->treeXML->load($this->filename);
|
||||
|
||||
$root_nodes = $this->treeXML->getElementsByTagName("questions");
|
||||
if ($root_node->length > 0)
|
||||
$this->root_node = $root_nodes->item(0);
|
||||
else
|
||||
throw Exception("Not a valid nemubot QuestionsFile.");
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->root_node = $this->treeXML->createElement("questions");
|
||||
$this->treeXML->appendChild($this->root_node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new question into the file
|
||||
* @param $question The question object
|
||||
*/
|
||||
public function add_question($question)
|
||||
{
|
||||
$this->root_node->appendChild($question->to_xml($this->root_node));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a question from its unique identifiant
|
||||
*/
|
||||
public function get_question($id)
|
||||
{
|
||||
return new Question($this->root_node->getElementById($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write changes into the real file
|
||||
*/
|
||||
public function save($newfilename = null)
|
||||
{
|
||||
if (!empty($newfilename))
|
||||
$this->filename = $newfilename;
|
||||
|
||||
$this->treeXML->save($this->filename);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user