id = $id;
//$this->setActions(array("view" => _("View"), "edit" => _("Edit")));
}
$this->setActions();
}
function load($id) {
$join = " ";
if ($this->table) {
$join = " LEFT JOIN " . DB_PREFIX . $this->getTable() . " t ON b.id=t.base_id ";
}
$data = query_row("SELECT * FROM " . DB_PREFIX . "base_item b" . $join . "WHERE b.id='{$id}'");
if ($data) {
$this->id = $id;
$this->setTitle($data->title);
$this->setItemBody($data->item_body);
$this->setDescription($data->description);
$this->setLanguage($data->language);
$this->setDifficulty($data->difficulty);
$this->setCategory($data->category);
$this->setCreated($data->created);
$this->setModified($data->modified);
$this->setCreator($data->creator);
$this->setStatus($data->status);
$this->setSecurity($data->security);
$this->setLocked($data->locked);
//$this->initialiseTags();
return $data;
}
return false;
}
function create() {
$this->setCreator(get_logged_in_user()->getId());
$category = "NULL";
if ($this->getCategory()) {
$category = $this->getCategory();
}
$insert_query = "INSERT INTO " . DB_PREFIX . "base_item (type, title, item_body, description, language, difficulty, category, status, security, creator, created, modified) values ('".mysql_real_escape_string($this->getType())."', '".mysql_real_escape_string($this->getTitle())."', '".mysql_real_escape_string($this->getItemBody())."', '".mysql_real_escape_string($this->getDescription())."', '".$this->getLanguage()."', ".$this->getDifficulty().", ".$category.",'".$this->getStatus()."', '".$this->getSecurity()."',".$this->getCreator().", NOW(), NOW())";
$insert = query_insert($insert_query);
$this->setId($insert);
$this->saveTags();
return $insert;
}
function save() {
$update_query = "UPDATE " . DB_PREFIX . "base_item SET";
$update_query .= " title='" . mysql_real_escape_string($this->getTitle()) . "',";
$update_query .= " item_body='" . mysql_real_escape_string($this->getItemBody()) . "',";
$update_query .= " description='" . mysql_real_escape_string($this->getDescription()) . "',";
$update_query .= " language='" . $this->getLanguage() . "',";
$update_query .= " difficulty='" . $this->getDifficulty() . "',";
if ($this->getCategory()) {
$update_query .= " category='" . $this->getCategory() . "',";
}
$update_query .= " status='" . $this->getStatus() . "',";
$update_query .= " security='" . $this->getSecurity() . "',";
$update_query .= " modified=NOW()";
$update_query .= " WHERE id=" . $this->getId();
$update = query_update($update_query);
$this->saveTags();
return $update;
}
function copy($locked=false) {
$id = $this->create();
if ($id && $locked) {
$lock = $this->lock($id);
}
return $id;
}
function lock($id=null) {
if (!$id) $id = $this->getId();
return query_update("UPDATE " . DB_PREFIX . "base_item SET locked=1 WHERE id=".$id);
}
function setTestSection($test_section_id, $id=null) {
if (!$id) $id = $this->getId();
return query_update("UPDATE " . DB_PREFIX . "base_item SET test_section={$test_section_id} WHERE id=".$id);
}
function setPosition($position, $id=null) {
if (!$id) $id = $this->getId();
return query_update("UPDATE " . DB_PREFIX . "base_item SET position={$position} WHERE id=".$id);
}
function build($data) {
if (!isset($data["title"]) || empty($data["title"])) {
return false;
} else {
$this->setTitle($data["title"]);
if (isset($data["item_body"])) $this->setItemBody($data["item_body"]);
if (isset($data["description"])) $this->setDescription($data["description"]);
if (isset($data["tags"])) {
$this->setTags($data["tags"]);
}
if (isset($data["language"])) $this->setLanguage($data["language"]);
if (isset($data["difficulty"])) $this->setDifficulty($data["difficulty"]);
if (isset($data["category"])) $this->setCategory($data["category"]);
if (isset($data["status"])) $this->setStatus($data["status"]);
if (isset($data["security"])) $this->setSecurity($data["security"]);
}
return true;
}
function delete() {
global $TeKe;
if (is_numeric($this->getId()) && $this->getId() > 0) {
if ($TeKe->plugin->is_test_constructor() || $this->creator==get_logged_in_user()->getId()) {
$current_tags = $TeKe->plugin->tags->getTagRelations($this->getId(), "item");
$TeKe->plugin->tags->deleteOldTagRelations($current_tags, $this->getId(), "item");
return query("DELETE FROM " . DB_PREFIX . "base_item WHERE id=".$this->getId());
}
}
return false;
}
function isOwnerOrAdmin() {
global $TeKe;
if (!$this->getId()) {
return true;
} else if ($TeKe->is_admin()) {
return true;
} else if ($this->getCreator() == get_logged_in_user()->getId()) {
return true;
}
return false;
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getType() {
return $this->type;
}
function setType($type) {
$this->type = $type;
}
public function getInteractionType() {
return $this->interaction_type;
}
function setInteractionType($interaction_type) {
$this->interaction_type = $interaction_type;
}
public function getTable() {
return $this->table;
}
function setTable($table) {
$this->table = $table;
}
public function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function getActions() {
return $this->actions;
}
function setActions() {
if (!$this->getId()) {
$this->actions ["add"] = array("title" => _("Edit"), "disabled" => false);
$this->actions ["view"] = array("title" => _("Preview"), "disabled" => true);
} else {
$this->actions ["edit"] = array("title" => _("Edit"), "disabled" => false);
$this->actions ["view"] = array("title" => _("Preview"), "disabled" => false);
}
}
public function getLocked() {
return $this->locked;
}
public function setLocked($locked) {
if ($locked) {
$this->locked = 1;
} else {
$this->locked = 0;
}
}
public function getTitle() {
return $this->title;
}
function setTitle($title) {
$this->title = $title;
}
public function getItemBody() {
return $this->item_body;
}
function setItemBody($item_body) {
$this->item_body = $item_body;
}
public function getDescription() {
return $this->description;
}
function setDescription($description) {
$this->description = $description;
}
function getTags() {
global $TeKe;
if ($this->getId()) {
$tags = $TeKe->plugin->tags->getTagRelations($this->getId(), "item");
return implode(", ", $tags);
}
return "";
}
public function getNewTags() {
return $this->tags;
}
function setTags($tags) {
$this->tags = $tags;
}
function saveTags() {
global $TeKe;
$tags = explode(",", $this->getNewTags());
$tags_array = array();
foreach ($tags as $tag) {
$tag = trim($tag);
$tags_array []= $tag;
}
$current_tags = $TeKe->plugin->tags->getTagRelations($this->getId(), "item");
$new_tags = array_diff($tags_array, $current_tags);
$old_tags = array_diff($current_tags, $tags_array);
$TeKe->plugin->tags->addNewTagRelations($new_tags, $this->getId(), "item");
$TeKe->plugin->tags->deleteOldTagRelations($old_tags, $this->getId(), "item");
}
function addNewTagRelations($tags) {
global $TeKe;
foreach ($tags as $tag) {
$tag = trim($tag);
if ($tag == "") {
continue;
}
$obj = $TeKe->plugin->loadType("tag", $tag);
if ($obj->getId()) {
$tag_id = $obj->getId();
} else {
$tag_id = $obj->build(array("name"=>$tag));
}
$TeKe->plugin->tags->addTagRelation($tag_id, $this->getId(), "item");
}
}
public function getLanguage() {
return $this->language;
}
function setLanguage($language) {
$this->language = $language;
}
public function getDifficulty() {
return $this->difficulty;
}
function setDifficulty($difficulty) {
$this->difficulty = $difficulty;
}
public function getCategory() {
return $this->category;
}
function setCategory($category) {
$this->category = $category;
}
public function getCreated() {
return $this->created;
}
function setCreated($created) {
$this->created = $created;
}
public function getModified() {
return $this->modified;
}
function setModified($modified) {
$this->modified = $modified;
}
public function getCreator() {
return $this->creator;
}
function setCreator($creator) {
$this->creator = $creator;
}
public function getStatus() {
return $this->status;
}
function setStatus($status) {
$this->status = $status;
}
public function getSecurity() {
return $this->security;
}
function setSecurity($security) {
$this->security = $security;
}
function addBuildError($err) {
$this->build_error []= $err;
}
function getBuildError() {
return $this->build_error;
}
function viewImage($id, $thumb=false) {
$image = new Image($id);
return $image->view($thumb);
}
public function isLocked() {
return false;
}
public function answer($data) {
return true;
}
function collect($examinees) {
return array();
}
function buildFromXML($dom) {
$language = $this->getLanguageFromXML($dom);
if ($language) {
$this->setLanguage($language);
}
$description = $this->getDescriptionFromXML($dom, $language);
$this->setDescription($description);
$tags = $this->getTagsFromXML($dom);
$this->setTags($tags);
}
function getLanguageFromXML($dom) {
$language_node = $dom->getElementsByTagName("language")->item(0);
$lang = $language_node->textContent;
if (in_array($lang, array("et", "en"))) {
return $lang;
}
return false;
}
function getDescriptionFromXML($dom, $language) {
$description = "";
$description_node = $dom->getElementsByTagName("description")->item(0);
$langstrings = $description_node->getElementsByTagName("langstring");
for ($i=0; $i<$langstrings->length; $i++) {
$langstring = $langstrings->item($i);
$lang = $langstring->getAttribute("xml:lang");
if ($lang == $language) {
return $langstring->textContent;
} else {
$description = $langstring->textContent;
}
}
return $description;
}
function getTagsFromXML($dom) {
$keywords = array();
$keywords_node = $dom->getElementsByTagName("keyword")->item(0);
$langstrings = $keywords_node->getElementsByTagName("langstring");
for ($i=0; $i<$langstrings->length; $i++) {
$langstring = $langstrings->item($i);
$keywords []= $langstring->textContent;
}
return implode(", ", $keywords);
}
/*public function getBaseXML() {
$xml = "\n\n";
return $xml;
}*/
/*Äfunction getBaseXML() {
$xml = "\n";
}*/
function match_correct_xml() {
$resp = "
1
0
";
$resp = sprintf($resp, $this->getId(), $this->getId());
return $resp;
}
function map_response_xml() {
$map_resp= "
0.0
";
$map_resp = sprintf($map_resp, $this->getId(), $this->getId());
return $map_resp;
}
function getPackage() {
global $TeKe;
$language = $this->getLanguage();
$difficulties = $TeKe->plugin->getDifficulties();
$difficulty = $difficulties[$this->getDifficulty()];
$tags = $this->getTags();
$tags_splitted = explode(",", $tags);
$tags_xml = "\n";
foreach ($tags_splitted as $tag) {
$tag = trim($tag);
if ($tag != "") {
$tag_xml .= "\t\t\t%s\n";
$tag_xml .= sprintf($tag_xml, $language, htmlentities($tag));
}
}
$tags_xml .= "\t\t\t\t";
$pack = "getId()."\" type=\"imsqti_item_xmlv2p1\" href=\"".$this->getId().".xml\">
".$this->getId()."
getLanguage()."\">".htmlentities($this->getTitle())."
getLanguage()."\">".htmlentities($this->getDescription())."
".$tags_xml."
".$this->getLanguage()."
".$difficulty."
Final
false
".$this->getInteractionType()."
none
true
Tester
getId().".xml\"/>
\n";
return $pack;
}
}
?>