id = $id; } $this->load($this->id); $this->setActions(); } function load($id) { if (is_numeric($id)) { $this->id = $id; } $data = false; if (is_numeric($this->id)) { $data = query_row("SELECT * FROM " . DB_PREFIX . $this->getTable() . " WHERE id='{$id}'"); } if ($data) { $this->id = $id; $this->setTitle($data->title); $this->setDescription($data->description); $this->setLanguage($data->language); $this->setCreated($data->created); $this->setModified($data->modified); $this->setCreator($data->creator); $this->setStatus($data->status); $this->setSecurity($data->security); $this->setLocked($data->locked); return $data; } return false; } function create() { $this->setCreator(get_logged_in_user()->getId()); $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (title, description, language, status, security, creator, created, modified) values ('".mysql_real_escape_string($this->getTitle())."', '".mysql_real_escape_string($this->getDescription())."', '".$this->getLanguage()."', '".$this->getStatus()."', '".$this->getSecurity()."',".$this->getCreator().", NOW(), NOW())"); if ($insert) { $this->saveTags(); //$this->setId($insert); } return $insert; } function save() { $update_query = "UPDATE " . DB_PREFIX . $this->getTable() . " SET"; $update_query .= " title='" . mysql_real_escape_string($this->getTitle()) . "',"; $update_query .= " description='" . mysql_real_escape_string($this->getDescription()) . "',"; $update_query .= " language='" . $this->getLanguage() . "',"; $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); if ($update) { $this->saveTags(); return $this->getId(); } return $update; } function copy($locked=false) { $crt = $this->create(); if ($crt && $locked) { $lock = $this->lock($crt); $this->copyTestSections($crt); } return $crt; } function copyTestSections($test_id) { global $TeKe; $test_sections = $this->getSections(); foreach ($test_sections as $section) { $id = $section["id"]; $obj = $TeKe->plugin->loadType("testSection", $id); $new_id = $obj->copy(true); $new_obj = $TeKe->plugin->loadType("testSection", $new_id); $new_obj->setTest($test_id); $new_obj->saveTest(); //$test_position++; //$new_obj->setPosition($test_position, $new_id); } } /*function copyItems($items) { global $TeKe; $test_position = 0; foreach ($items as $item) { $id = $item->id; $obj = $TeKe->plugin->loadType("", $id); $new_id = $obj->copy(true); $new_obj = $TeKe->plugin->loadType("", $new_id); $new_obj->setTest($this->getId(), $new_id); $test_position++; $new_obj->setPosition($test_position, $new_id); } }*/ function lock($id=null) { if (!$id) $id = $this->getId(); return query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET locked=1 WHERE id=".$id); } function setAssessment($assessment_id, $id=null) { if (!$id) $id = $this->getId(); return query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET assessment={$assessment_id} WHERE id=".$id); } function saveTestSections($sections_count, $sections_data, $test_id) { global $TeKe; $position = 0; foreach ($sections_data as $section => $section_data) { $position++; if ($position <= $sections_count) { $add = $this->addTestSection($section_data, $position, $test_id); if (!$add) { $position--; } } else { $this->deleteTestSection($section_data); } } } function addTestSection($section_data, $position, $test_id) { global $TeKe; $id = 0; if ($section_data["id"] && $section_data["id"] != "") { $id = $section_data["id"]; if (!$section_data["title"]) { $this->deleteTestSection($section_data); return false; } } $obj = $TeKe->plugin->loadType("testSection", $id); $obj->setTest($test_id); $obj->setPosition($position); $section_id = $obj->build($section_data); return $section_id; } function deleteTestSection($section_data) { global $TeKe; if ($section_data["id"]) { $obj = $TeKe->plugin->loadType("testSection", $section_data["id"]); $obj->delete(); } } function isTitleProvidedForAllSections($sections_count, $sections_data) { global $TeKe; $position = 0; foreach ($sections_data as $section => $section_data) { $position++; if ($position <= $sections_count) { if (trim($section_data["title"]) == "") { return false; } } } return true; } function build($data) { if (!isset($data["title"]) || empty($data["title"])) { return false; } else if (!$this->isTitleProvidedForAllSections($data["sections_count"], $data["sections"])) { return false; } else { $this->setTitle($data["title"]); 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 ($this->getId() > 0) { $this->saveTestSections($data["sections_count"], $data["sections"], $this->getId()); return $this->save(); } else { $id = $this->create(); $this->saveTestSections($data["sections_count"], $data["sections"], $id); return $id; } } 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(), "test"); $TeKe->plugin->tags->deleteOldTagRelations($current_tags, $this->getId(), "test"); return query("DELETE FROM " . DB_PREFIX . $this->getTable() . " WHERE id=".$this->getId()); } } return false; } public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } 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($actions) { $this->actions = $actions; }*/ function setActions() { if (!$this->getId()) { $this->actions ["add"] = array("title" => _("Metadata"), "disabled" => false); $this->actions ["items"] = array("title" => _("Items"), "disabled" => true); } else { $this->actions ["edit"] = array("title" => _("Metadata"), "disabled" => false); $this->actions ["items"] = array("title" => _("Items"), "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 getDescription() { return $this->description; } function setDescription($description) { $this->description = $description; } function getTags() { global $TeKe; if ($this->getId()) { $tags = $TeKe->plugin->tags->getTagRelations($this->getId(), "test"); 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(), "test"); $new_tags = array_diff($tags_array, $current_tags); $old_tags = array_diff($current_tags, $tags_array); $TeKe->plugin->tags->addNewTagRelations($new_tags, $this->getId(), "test"); $TeKe->plugin->tags->deleteOldTagRelations($old_tags, $this->getId(), "test"); } public function getLanguage() { return $this->language; } function setLanguage($language) { $this->language = $language; } 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; } public function getSectionsCount() { $count = count($this->getSections()); if ($count < 1) { return 1; } return $count; } public function getSections() { $sections = array(); if ($this->getId()) { $res = query_rows("SELECT * FROM ". DB_PREFIX ."test_sections WHERE test=".$this->getId()." ORDER BY position"); foreach ($res as $section) { $section_data = array(); $section_data ["id"]= $section->id; $section_data ["title"]= $section->title; $sections [$section->position] = $section_data; } } return $sections; } function addBuildError($err) { $this->build_error []= $err; } function getBuildError() { return $this->build_error; } public function getItems() { global $TeKe; $sections = $this->getSections(); $test_items = array(); foreach ($sections as $section_data) { $section_obj = $TeKe->plugin->loadType('testSection', $section_data['id']); $items = $section_obj->getItems(); foreach ($items as $item) { $test_items []= $item; } } return $test_items; } function viewImage($id, $thumb=false) { $image = new Image($id); return $image->view($thumb); } public function isLocked() { return false; } function buildFromXML($obj, $folder=0, $dir, $version=1) { $this->setFolder($folder); $this->setTitle($obj->title[0][0]); if (isset($obj->description[0][0])) { $this->setInstructions($obj->description[0][0]); if (isset($obj->descriptionIsTop) && $obj->descriptionIsTop[0][0] && $obj->descriptionIsTop[0][0] != "False") { $this->setInstructionsTop(1); } else { $this->setInstructionsTop(0); } } else if (isset($obj->instructions[0][0])) { $this->setInstructions($obj->instructions[0][0]); $this->setInstructionsTop($obj->instructionsTop[0][0]); } $this->setAuthors($obj->author[0][0]); if (!empty($obj->rights[0][0])) $this->setRights(implode("\n", $obj->rights[0][0])); if (!empty($obj->info[0][0])) $this->setInfo(implode("\n", $obj->info[0][0])); $this->setCreated($obj->created[0][0]); } /*public function getContextNavigation() { $navigation = array(); $navigation []= array('title'=>_('Metadata'), 'url'=>"assessments/view", 'current'=>$this->is_current_page($this->page[0], "view")); }*/ public function getBaseXML() { $xml = "\n\n"; $xml .= "".$this->getType()."\n"; $xml .= "getCreated()."]]>\n"; $xml .= "<![CDATA[".$this->getTitle()."]]>\n"; $xml .= "getInstructions()."]]>\n"; $xml .= "getInstructionsTop()."]]>\n"; $xml .= "getCreator()."]]>\n"; $xml .= "getRights()."]]>\n"; $xml .= "getInfo()."]]>\n"; $xml .= $this->getObjectsXML(); $xml .= "\n"; return $xml; } public function getNavigationTree() { $nav = array(); $folder = $this->getFolder(); if ($folder>0) { $fd = query_row("SELECT title FROM " . DB_PREFIX . "test_folder WHERE id=".$folder); $nav []= array('title' => $fd->title, 'url'=>"library/TestFolder/".$folder."/view", 'current'=>false, 'level'=>1); } $nav []= array('title' => $this->getTitle(), 'url'=>"library/".$this->getType()."/".$this->getId()."/view", 'current'=>true, 'level'=>2); return $nav; } } ?>