load($id); } if ($this->getLocked()) { $this->setActions(array("view"=>_("Contents"), "preview"=>_("Preview"), "results"=>_("Results"))); } else { $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "new"=>_("New test asset"), "import"=>_("Import test asset"))); } } function load($id) { $id = (int) $id; if (is_numeric($id)) { $data = query_row("SELECT * FROM " . DB_PREFIX . "test_folder WHERE id='{$id}'"); if ($data) { $this->id = $id; $this->setFolder($data->folder); $this->setTitle($data->title); $this->setDescription($data->description); $this->setCreated($data->created); $this->setModified($data->modified); $this->setCreator($data->creator); $this->setInfo($data->info); $this->setRights($data->rights); $this->setStatus($data->status); $this->setSecurity($data->security); $this->setAuthors($data->authors); $this->setPosition($data->position); $this->setLocked($data->locked); $this->setTest($data->test); //$this->setCache(unserialize(base64_decode($data->cache))); $this->setCached($data->cached); return $data; } } return false; } function create() { $this->setCreator(get_logged_in_user()->getId()); if (!$this->getFolder()) $this->setFolder("NULL"); $insert = query_insert("INSERT INTO " . DB_PREFIX . "test_folder (folder, title, description, created, creator, info, rights, authors) values (".$this->getFolder().", '".mysql_real_escape_string($this->getTitle())."', '".mysql_real_escape_string($this->getDescription())."', NOW(), '".$this->getCreator()."', '".mysql_real_escape_string($this->getInfo())."', '".mysql_real_escape_string($this->getRights())."', '".mysql_real_escape_string($this->getAuthors())."')"); return $insert; } function save() { $update = query_update("UPDATE " . DB_PREFIX . "test_folder SET title='".mysql_real_escape_string($this->getTitle())."', description='".mysql_real_escape_string($this->getDescription())."', modified=NOW(), info='".mysql_real_escape_string($this->getInfo())."', rights='".mysql_real_escape_string($this->getRights())."', authors='".mysql_real_escape_string($this->getAuthors())."' WHERE !locked AND id=".$this->getId()); if ($update) { return $this->id; } return false; } function copy($locked=false) { if (!$locked) $this->setTitle($this->getTitle()." ("._("Copy of").")"); $content = query_rows("SELECT id, type FROM " . DB_PREFIX . "base_object WHERE !locked AND folder=".$this->getId()." ORDER BY position ASC"); $some_failed = false; $copy = $this->create(); if ($copy) { if ($locked) $lock = $this->lock($copy); $psy = new Psyhvel(); foreach ($content as $cont) { $type = $psy->loadType($cont->type, $cont->id); if ($type) { $type->setFolder($copy); $test_id = $type->copy($locked); } else { $some_failed = true; } } } if ($some_failed) return false; return $copy; } function cut($locked=false) { if (!$this->getFolder()) $this->setFolder("NULL"); $cut = query_update("UPDATE " . DB_PREFIX . "test_folder SET folder=".$this->getFolder()." WHERE !locked AND id=".$this->getId()); return $cut; } function lock($id=null) { if (!$id) $id = $this->getId(); return query_update("UPDATE " . DB_PREFIX . "test_folder SET locked=1 WHERE id=".$id); } function assign($test_id) { $assigned = $this->copy(true); if ($assigned) { $max_pos = query_row("SELECT MAX(position) as max_pos FROM " . DB_PREFIX . "test_folder WHERE locked AND test=".$test_id)->max_pos; if ($max_pos) { $max_pos++; } else { $max_pos = 1; } return query_update("UPDATE " . DB_PREFIX . "test_folder SET test=".$test_id.", position=".$max_pos." WHERE id=".$assigned); } } function build($data) { if (!isset($data["title"]) || empty($data["title"])) { return false; } else { $this->setTitle($data["title"]); if (isset($data["folder"])) $this->setFolder($data["folder"]); if (isset($data["description"])) $this->setDescription($data["description"]); if (isset($data["rights"])) $this->setRights($data["rights"]); if (isset($data["info"])) $this->setInfo($data["info"]); } if ($this->id > 0) { return $this->save(); } else { return $this->create(); } } function delete() { if (is_numeric($this->getId()) && $this->getId() > 0) { if (is_admin() || $this->creator==get_logged_in_user()->getId()) { $psyhvel = new Psyhvel(); $content = query_rows("SELECT id, type FROM " . DB_PREFIX . "base_object WHERE folder=".$this->getId()); $some_failed = false; if (count($content) > 0) { foreach ($content as $cont) { if (!$psyhvel->deleteType($cont->type, $cont->id)) { $some_failed = true; } } } if (!$some_failed) { return query("DELETE FROM " . DB_PREFIX . "test_folder WHERE id=".$this->getId()); } } } return false; } function move_up() { } function move_down() { } public function getId() { return $this->id; } public function getType() { return $this->type; } public function getFolder() { return $this->folder; } function setFolder($folder) { $this->folder = $folder; } public function getName() { return $this->name; } function getActions() { return $this->actions; } function setActions($actions) { $this->actions = $actions; } public function getLocked() { return $this->locked; } public function setLocked($locked) { if ($locked) { $this->locked = 1; } else { $this->locked = 0; } } public function getPosition() { return $this->position; } function setPosition($pos) { $this->position = $pos; } 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 getAuthors() { return $this->authors; } function setAuthors($authors) { $this->authors = $authors; } public function getRights() { return $this->rights; } function setRights($rights) { $this->rights = $rights; } public function getInfo() { return $this->info; } function setInfo($info) { $this->info = $info; } 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 getTest() { return $this->test; } function setTest($test) { $this->test = $test; } public function getCache() { if (is_numeric($this->getId())) { $row = query_row("SELECT html FROM " . DB_PREFIX . "test_folder_cache WHERE test_folder=".$this->getId()); if (isset($row->html)) { return $row->html; } } return false; } public function getCacheRunning() { if (is_numeric($this->getId())) { $row = query_row("SELECT count(id) as running_count FROM " . DB_PREFIX . "test_folder_cache WHERE running=TRUE"); if (isset($row->running_count)) { return $row->running_count; } } return 0; } public function getCSVCache() { if (is_numeric($this->getId())) { $row = query_row("SELECT csv FROM " . DB_PREFIX . "test_folder_cache WHERE test_folder=".$this->getId()); if (isset($row->csv)) { return $row->csv; } } return false; } public function getCached() { return $this->cached; } function setCached($cached) { $this->cached = $cached; } function repositionContents($positions) { $positionCounter = 1; foreach ($positions as $position_id) { query_update("UPDATE " . DB_PREFIX . "base_object SET position = " . $positionCounter . " WHERE id = " . $position_id." AND folder=".$this->getId()); $positionCounter = $positionCounter + 1; } return true; } function getContent($pos) { if (is_numeric($this->getId())) { $row = query_row("SELECT id, type FROM " . DB_PREFIX . "base_object WHERE locked AND folder=".$this->getId()." ORDER BY position ASC LIMIT 1 OFFSET ".$pos); return $row; } return false; } function getContents() { if (is_numeric($this->getId())) { $rows = query_rows("SELECT * FROM " . DB_PREFIX . "base_object WHERE !locked AND folder=".$this->getId()." ORDER BY position ASC"); return $rows; } return false; } function getTestContents() { if (is_numeric($this->getId())) { $rows = query_rows("SELECT * FROM " . DB_PREFIX . "base_object WHERE locked AND folder=".$this->getId()." ORDER BY position ASC"); return $rows; } return false; } function getTestAnswers() { $psyhvel = new Psyhvel(); $contents = $this->getTestContents(); $answers = array(); $examinees = array(); $groups = query_rows("SELECT group_id FROM " . DB_PREFIX . "test_groups WHERE test_id=" . $this->getTest() . " ORDER BY group_id ASC"); foreach ($groups as $group) { foreach (query_rows("SELECT examinee_id FROM " . DB_PREFIX . "group_relations WHERE group_id=".$group->group_id." ORDER BY examinee_id") as $examinee) { $examinees []= $examinee->examinee_id; } } foreach ($contents as $content) { if (!substr_compare($content->type, "Task", -4, 4) || $content->type == "Questionnaire") { $obj = $psyhvel->loadType($content->type, $content->id); if ($obj) { $answers []= $obj->collect($examinees); } } } return $answers; } function doTimer(&$header, &$answers, $data) { if (array_key_exists("title", $data["timer"])) { $header[1][$data['id']] = array('title' => _("Timer"), 'colspan'=>3, 'rowspan'=>1); foreach($data["timer"]["title"] as $t_key => $t_val) { $header[2][$data['id']][$t_key] = array('title' => $t_val, 'colspan'=>1, 'rowspan'=>1); unset($data["timer"]["title"][$t_key]); } if (count($data["timer"]["title"])==0) unset($data["timer"]["title"]); } foreach($data["timer"] as $examinee_id => $examinee_answer) { foreach($examinee_answer as $a_key => $a_val) { $answers[$examinee_id][$data['id']][$a_key] = $a_val; } } } function doAnswers(&$header, &$answers, $data) { if (array_key_exists("title", $data["answers"])) { //var_dump($data["answers"]["title"]); foreach($data["answers"]["title"] as $t_key => $t_val) { $header[2][$data['id']][$t_key] = array('title' => $t_val, 'colspan'=>1, 'rowspan'=>1); unset($data["answers"]["title"][$t_key]); } if (count($data["answers"]["title"])==0) unset($data["answers"]["title"]); } foreach($data["answers"] as $examinee_id => $examinee_answer) { foreach($examinee_answer as $a_key => $a_val) { $answers[$examinee_id][$data['id']][$a_key] = $a_val; } } } function doChoices(&$header, &$answers, $data) { foreach ($data["choices"] as $c_1) { if (array_key_exists("answers", $data)) { $this->doAnswers($header, $answers, $c_1); } } } function doQuestions(&$header, &$answers, $data) { foreach ($data["questions"] as $q_1) { if (array_key_exists("id", $q_1)) { $header[1][$q_1['id']] = array('title' => $q_1['title'], 'colspan'=>0, 'rowspan'=>1); if (array_key_exists("choices", $q_1)) { $header[0][$data['id']]['colspan'] = $header[0][$data['id']]['colspan'] + count($q_1["choices"]); $header[1][$q_1['id']]['colspan'] = $header[1][$q_1['id']]['colspan'] + count($q_1["choices"]); $this->doChoices($header, $answers, $q_1); } if (array_key_exists("answers", $q_1)) { $answer_count = 0; if (array_key_exists("title", $q_1["answers"])) $answer_count = 1; $header[0][$data['id']]['colspan'] = $header[0][$data['id']]['colspan'] + $answer_count; $header[1][$q_1['id']]['colspan'] = $header[1][$q_1['id']]['colspan'] + $answer_count; $this->doAnswers($header, $answers, $q_1); } } } } function doGenerate(&$header, &$answers) { $data = $this->getTestAnswers(); foreach ($data as $d_1) { if (array_key_exists("id", $d_1)) { $header[0][$d_1['id']] = array('title' => $d_1['title'], 'colspan'=>0, 'rowspan'=>1); if (array_key_exists("timer", $d_1) && array_key_exists("title", $d_1["timer"])) { $header[0][$d_1['id']]['rowspan'] = 1; $header[0][$d_1['id']]['colspan'] = $header[0][$d_1['id']]['colspan'] + count($d_1["timer"]["title"]); $this->doTimer($header, $answers, $d_1); } if (array_key_exists("answers", $d_1) && array_key_exists("title", $d_1["answers"])) { $header[0][$d_1['id']]['rowspan'] = 2; $header[0][$d_1['id']]['colspan'] = $header[0][$d_1['id']]['colspan'] + count($d_1["answers"]["title"]); $this->doAnswers($header, $answers, $d_1); } if (array_key_exists("choices", $d_1)) { $header[0][$d_1['id']]['rowspan'] = 2; $header[0][$d_1['id']]['colspan'] = $header[0][$d_1['id']]['colspan'] + count($d_1["choices"]); $this->doChoices($header, $answers, $d_1); } if (array_key_exists("questions", $d_1)) { $header[0][$d_1['id']]['rowspan'] = 1; $this->doQuestions($header, $answers, $d_1); } } } } function cacheAnswers() { $cache_id = query_update("INSERT INTO " . DB_PREFIX . "test_folder_cache (test_folder, created) values (".$this->getId().", NOW()) ON DUPLICATE KEY UPDATE created=NOW(), stopped=NULL, running=TRUE, id=LAST_INSERT_ID(id)"); $cache_id = mysql_insert_id(); $header = array(array(), array(), array()); $answers = array(); $this->doGenerate(&$header, &$answers); $renderedData = $this->renderAnswersToTableFormat($header, $answers); $examinees = 0; if ($cache_id && $renderedData) { $html = $renderedData['html']; $csv = $renderedData['csv']; $cached = query_update("UPDATE " . DB_PREFIX . "test_folder_cache SET html='".mysql_real_escape_string($html)."', csv='".mysql_real_escape_string($csv)."', stopped=NOW(), running=FALSE WHERE id=" . $cache_id); query_update("UPDATE " . DB_PREFIX . "test_folder SET cached=NOW() WHERE id=".$this->getId()); } else if ($cache_id && !$renderedData) { $cached = query_update("UPDATE " . DB_PREFIX . "test_folder_cache SET (error) values ('No Data was rendered') WHERE id=" . $cache_id); } } function showRenderedResults() { $refresh = get_input('refresh', false); if ($refresh) { ignore_user_abort(true); set_time_limit(0); if (session_id()) session_write_close(); $this->cacheAnswers(); //exit; //forward("tests/TestFolder/".$this->getId()."/results"); } else if (get_input('stripHtml', false)) { return ststrip_tags($this->getCache(), "
"); } else if (get_input('ripHtml', false)) { return strip_tags($this->getCache()); } else if ($this->getCache()) { return $this->getCache(); } return "0"; } function renderAnswersToTableFormat($header, $answers) { $table = ''; $csv = ""; if (count($header[0]) > 0) { $table .= ''; $rowspan_shift = 0; if (count($header[1]) == 0) { $rowspan_shift = -1; } $table .= ''; $row = array(); $row []= '"'._("Examinee").'"'; foreach ($header[0] as $h) { $header_title = strip_tags($h['title']); $table .= ''; $row []= '"'.$header_title.'"'; for ($i=0; $i<($h['colspan']-1); $i++) { $row []= '""'; } } $table .= ''; $csv .= strip_line_breaks(implode(",", $row))."\n"; } if (count($header[1]) > 0) { $table .= ''; $row = array(); $row []= '""'; foreach ($header[1] as $h) { $header_title = strip_tags($h['title']); $table .= ''; $row []= '"'.$header_title.'"'; for ($i=0; $i<($h['colspan']-1); $i++) { $row []= '""'; } } $table .= ''; $csv .= strip_line_breaks(implode(",", $row))."\n"; } if (count($header[2]) > 0) { $table .= ''; $row = array(); $row []= '""'; foreach ($header[2] as $head) { foreach ($head as $h) { $header_title = strip_tags($h['title']); $table .= ''; $row []= '"'.$header_title.'"'; } } $table .= ''; $csv .= strip_line_breaks(implode(",", $row))."\n"; } foreach ($answers as $examinee => $ad) { $q = query_row("SELECT username FROM " . DB_PREFIX . "examinees WHERE id=".$examinee); if ($q) { $examinee = $q->username; } $table .= ''; $table .= ''; $row = array(); $row []= '"'.$examinee.'"'; foreach ($ad as $k => $a) { foreach ($a as $av) { $answerv = strip_tags($av); $table .= ''; $row []= '"'.$answerv .'"'; } } $table .= ''; $csv .= strip_line_breaks(implode(",", $row))."\n"; } $table .= '
'._("Examinee").''.$header_title.'
'.$header_title.'
'.$header_title.'
'.$examinee.''.$answerv .'
'; return array('html'=>$table, 'csv'=>stripcslashes($csv)); } function buildFromXML($obj, $folder=0) { $this->setTitle($obj->title[0][0]); $this->setDescription($obj->description[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]); return $this->create(); } public function getBaseXML() { $xml = "\n\n"; $xml .= "".$this->getType()."\n"; $xml .= "getCreated()."]]>\n"; $xml .= "<![CDATA[".$this->getTitle()."]]>\n"; $xml .= "getDescriotion()."]]>\n"; $xml .= "getCreator()."]]>\n"; $xml .= "getRights()."]]>\n"; $xml .= "getInfo()."]]>\n"; $xml .= $this->getObjectsXML(); $xml .= "\n"; return $xml; } public function getNavigationTree($handlername="library") { $nav = array(); $level=1; $psy = new Psyhvel(); if ($handlername=="library") { if ($this->getFolder()) { $parent = $psy->loadType("Folder", $this->getFolder()); $nav []= array('title' => $parent->getTitle(), 'url'=>$handlername."/".$parent->getType()."/".$parent->getId()."/view", 'current'=>false, 'level'=>1); $level=2; } $nav []= array('title' => $this->getTitle(), 'url'=>$handlername."/".$this->getType()."/".$this->getId()."/view", 'current'=>true, 'level'=>$level); } else if ($handlername=="tests") { $nav []= array('title' => $this->getTitle(), 'url'=>$handlername."/Test/".$this->getTest(), 'current'=>true, 'level'=>$level); } return $nav; } public function getTestToPlay() { return new Test($this->getId()); } public function getAssetToPlay() { $asset_id = 0; if (isset($_SESSION['test_preview']) && isset($_SESSION['test_preview'][$this->getId()]) && is_numeric($_SESSION['test_preview'][$this->getId()])) { $asset_id = $_SESSION['test_preview'][$this->getId()]; } $asset = $this->getContent($asset_id); $psy = new Psyhvel(); return $psy->loadType($asset->type, $asset->id); } public function getCurrentPage() { if (isset($_SESSION['test_preview']) && isset($_SESSION['test_preview'][$this->getId()]) && is_numeric($_SESSION['test_preview'][$this->getId()])) { return $_SESSION['test_preview'][$this->getId()]; } return 0; } public function getMaxPage() { return count($this->getTestContents()); } public function zipContents(&$zip, $dirname="") { $psy = new Psyhvel(); if ($this->getLocked()) { $conts = $this->getTestContents(); } else { $conts = $this->getContents(); } $nr = 1; foreach ($conts as $cont) { $loaded = $psy->loadType($cont->type, $cont->id); $shotname = $dirname . sanitize_file_name($nr."_".$loaded->getTitle()); if ($cont->screenshot > 0) { $screenshot = new Screenshot($cont->screenshot); if ($screenshot && $screenshot->location != "") { $zip->addFromString($shotname . ".png", file_get_contents($screenshot->location)); } else { $zip->addFromString($shotname . "_ERROR_SYSTEM_FAULT.txt", "Test: " . $loaded->getTitle() . ". Screenshot error! Screenshot removed from system!"); } } else { $test_shooter_type = get_input('type', false); if ($test_shooter_type == "TestFolder") { forward("includes/psyhvel/actions/download_file.php?type=TestFolder&id={$this->getId()}&start_shoot_screenshots=1"); } else { forward("includes/psyhvel/actions/download_file.php?type=Test&id={$this->getTest()}&start_shoot_screenshots=1"); } } $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() { $this->nextTestShooter(); } public function nextTestShooter() { $test = $this->getId(); if ($test) { unset($_SESSION['test_preview'][$test]); forward("tests/TestFolder/{$test}/preview?test_shooter_id=self"); } } } ?>