71 lines
1.5 KiB
PHP
71 lines
1.5 KiB
PHP
|
<?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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|