setActions(array("view"=>_("View"), "edit"=>_("Edit"), "contents"=>_("Contents"))); if ($id) { $data = $this->load($id); } } function load($id) { if (is_numeric($id)) { $data = query_row("SELECT * FROM " . DB_PREFIX . "test WHERE id='{$id}'"); if ($data) { $this->id = $id; $this->setTitle($data->title); $this->setDescription($data->description); $this->setCreated($data->created); $this->setModified($data->modified); $this->setCreator($data->creator); $this->setActive($data->active); $this->setNumbered($data->numbered); $groups = $this->load_groups(); $this->setGroups($groups); return $data; } } return false; } function load_groups() { $gs = array(); foreach (query_rows("SELECT group_id FROM " . DB_PREFIX . "test_groups WHERE test_id=".$this->id) as $g) { $gs []= $g->group_id; } return $gs; } function create() { $this->setCreator(get_logged_in_user()->getId()); $insert = query_insert("INSERT INTO " . DB_PREFIX . "test (title, description, created, creator, active, numbered) values ('".mysql_real_escape_string($this->getTitle())."', '".mysql_real_escape_string($this->getDescription())."', NOW(), ".$this->getCreator().", ".$this->getActive().", ".$this->getNumbered().")"); return $insert; } function save() { $update = query_update("UPDATE " . DB_PREFIX . "test SET title='".$this->getTitle()."', description='".$this->getDescription()."', modified=NOW(), active=".$this->getActive().", numbered=".$this->getNumbered()." WHERE id=".$this->getId()); if ($update) { $this->update_positions(); return $this->id; } return false; } function update_positions() { $counter = 1; foreach ($this->getContents() as $content) { query_update("UPDATE " . DB_PREFIX . "test_folder SET position = " . $counter . " WHERE locked AND id = " . $content->id . " AND test=".$this->getId()); $counter++; } } function save_groups($gs) { if (!is_array($gs)) $gs = array($gs); $groups = $this->getGroups(); foreach($groups as $g) { if (!in_array($g, $gs)) query("DELETE FROM " . DB_PREFIX . "test_groups WHERE test_id=".$this->getId()." AND group_id=".$g); } foreach($gs as $g) { if (!in_array($g, $groups)) $gid = query_insert("INSERT INTO " . DB_PREFIX . "test_groups (test_id, group_id) values (".$this->getId().", ".$g.")"); } } function copy() { return false; } function build($data) { if (!isset($data["title"]) || empty($data["title"])) { return false; } else { $this->setTitle($data["title"]); if (isset($data["description"])) $this->setDescription($data["description"]); $this->setActive(0); if (isset($data["active"])) $this->setActive($data["active"]); $this->setNumbered(0); if (isset($data["numbered"])) $this->setNumbered($data["numbered"]); } if ($this->id > 0) { $test_id = $this->save(); } else { $test_id = $this->create(); $this->load($test_id); } if (isset($data["groups"])) { $this->save_groups($data["groups"]); } else { $this->save_groups(array()); } return $test_id; } function delete() { if (is_numeric($this->getId()) && $this->getId() > 0) { if (is_admin() || $this->creator==get_logged_in_user()->getId()) { global $psyhvel; $contents = $this->getContents(); foreach($contents as $contents) { $psyhvel->deleteType("TestFolder", $content['id']); } return query("DELETE FROM " . DB_PREFIX . "test WHERE id=".$this->getId()); } } return false; } public function getId() { return $this->id; } public function getType() { return $this->type; } public function getName() { return $this->name; } function getActions() { return $this->actions; } function setActions($actions) { $this->actions = $actions; } public function getActive() { return $this->active; } function setActive($act) { $this->active = $act; } public function getNumbered() { return $this->numbered; } function setNumbered($numbered) { $this->numbered = $numbered; } public function getTitle() { return $this->title; } function setTitle($title) { $this->title = $title; } public function getDescription() { return $this->description; } function setDescription($desc) { $this->description = $desc; } 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 getGroups() { return $this->groups; } function setGroups($groups) { $this->groups = $groups; } function repositionContents($positions) { $positionCounter = 1; foreach ($positions as $position_id) { query_update("UPDATE " . DB_PREFIX . "test_folder SET position = " . $positionCounter . " WHERE locked AND id = " . $position_id." AND test=".$this->getId()); $positionCounter = $positionCounter + 1; } return true; } function getContents() { if (is_numeric($this->getId())) { $rows = query_rows("SELECT * FROM " . DB_PREFIX . "test_folder WHERE locked AND test=".$this->getId()." ORDER BY position ASC"); return $rows; } return false; } function getContentLocked($id) { $examinee = get_examinee()->getId(); if (is_numeric($this->getId()) && $examinee > 0) { $row = query_row("SELECT locked FROM " . DB_PREFIX . "chapter WHERE chapter=".$id." AND examinee=".$examinee); if ($row) { return $row->locked; } } return false; } public function getNavigationTree() { $nav = array(); $nav []= array('title' => $this->getTitle(), 'url'=>"tests/".$this->getType()."/".$this->getId()."/view", 'current'=>true, 'level'=>1); return $nav; } public function renderTestTable($width=800, $owidth=200) { $objects = array(); $table = array(); $user = get_logged_in_user(); $counter = 1; $testObjects = $this->getContents(); $nrOfObjs = count($testObjects); $notActive = false; $table []= ''; $isEditor = false; if (is_admin() || $this->creator==get_logged_in_user()->getId()) { $isEditor = true; } foreach ($testObjects as $obj) { $url = ""; if ($isEditor) { $url = WWW_ROOT."tests/TestFolder/".$obj->id."/preview"; $cached = $obj->cached; if ($cached == "0000-00-00 00:00:00") { $cached = _('not yet'); } $objects []= ''; } else { if ($this->getContentLocked($obj->id)) { $objects []= ''; } else if ($notActive) { $objects []= ''; } else { $notActive = true; $url = WWW_ROOT."test/TestFolder/".$obj->id; $objects []= ''; } } if (isset($obj->current_page) && $obj->current_page == 0) { $notActive = true; } $counter++; } if ($isEditor) { $url = PLUGIN_ROOT."actions/psyhvel_object_actions.php?parent=Tests&parent_id=".$this->id."&test_to_put=".$this->id; $objects []= ''; } $nrOfObjs = count($objects); $rows = round($nrOfObjs*$owidth/$width); $max_objs = ($width/$owidth); if ($nrOfObjs < $max_objs) { $max_objs = $nrOfObjs; } for ($line = 0; $line<=$rows; $line++) { $table []= ''; $lines = array(); $spacer = array(); for ($i=0; $i<=($max_objs*2-2); $i++) { if (count($objects) > 0 && $i%2 == 0) { $ob = array_shift($objects); $lines []= $ob; if ($i==$max_objs*2-2) { $spacer []= ''; } else { $spacer []= ''; } } else if (count($objects) > 0 && $i%2 != 0 && $line%2 == 0) { $lines []= ''; $spacer []= ''; } else if (count($objects) > 0 && $i%2 != 0 && $line%2 != 0) { $lines []= ''; $spacer []= ''; } else { $lines []= ''; $spacer []= ''; } } if ($line%2 != 0) { $lines = array_reverse($lines); $spacer = array_reverse($spacer); } foreach ($lines as $li) { $table []= $li; } $table []= ''; if ($line+1 <= $rows) { $table []= ''; foreach ($spacer as $sp) { $table []= $sp; } $table []= ''; } } $table []= '
'._('Test').' '.$counter.''. '
Cached: '.$cached.'
'. '
'._('Test').' '.$counter.''._('Test').' '.$counter.''._('Test').' '.$counter.''._('Add Test').'
'; return implode("", $table); } public function zipContents(&$zip) { $psy = new Psyhvel(); $conts = $this->getContents(); $nr = 1; foreach ($conts as $cont) { $o = $psy->loadType("TestFolder", $cont->id); $dirname = sanitize_file_name($nr."_".$o->getTitle()); $o->zipContents($zip, $dirname."/"); $nr++; } } public function zipScreenshots() { $zip_filename = sanitize_file_name("Screenshots_" . $this->getTitle()); $zip_name = SCREENSHOT_STORE . $zip_filename; $zip = new ZipArchive; $res = $zip->open($zip_name.".zip", ZipArchive::CREATE); if ($res === TRUE) { $this->zipContents($zip); $zip->close(); header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=\"".$zip_filename.".zip\""); header("Content-Length: ".filesize($zip_name.".zip")); header("Pragma: no-cache"); header("Expires: 0"); readfile($zip_name.".zip"); unlink($zip_name.".zip"); } } public function setupTestShooter() { if (isset($_SESSION['test_shooter'][$this->getId()])) { unset($_SESSION['test_shooter'][$this->getId()]); } $conts = $this->getContents(); $objects = array(); foreach ($conts as $cont) { $objects []= $cont->id; } $_SESSION['test_shooter'][$this->getId()] = $objects; $this->nextTestShooter(); } public function nextTestShooter() { $test = $this->getNextTestShooter(); if ($test) { unset($_SESSION['test_preview'][$test]); forward("tests/TestFolder/{$test}/preview?test_shooter_id={$this->id}"); } forward("tests/Test/{$this->id}?download_screenshots=1"); //forward("includes/psyhvel/actions/download_file.php?type=Test&id={$this->id}&download_screenshots=1"); } public function getNextTestShooter() { if (isset($_SESSION['test_shooter']) && isset($_SESSION['test_shooter'][$this->getId()]) && is_array($_SESSION['test_shooter'][$this->getId()]) && count($_SESSION['test_shooter'][$this->getId()]) > 0) { return array_shift($_SESSION['test_shooter'][$this->getId()]); } return false; } } ?>