setType("TMTTask"); $this->setName("TMT Task"); $this->setTable("tmt_task"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setElements($data->elements); } } } function create() { if ($id = parent::create()) { $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, elements) values (".$id.", '".mysql_real_escape_string($this->getElements())."')"); return $id; } return false; } function save() { if (parent::save()) { $update = query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET elements='".mysql_real_escape_string($this->getElements())."' WHERE base_id=".$this->getId()); if ($update) return $this->getId(); } return false; } function copy($locked=false) { $copy = $this->create(); if ($copy && $locked) { $lock = $this->lock($copy); } return $copy; } function build($data) { if (parent::build($data)) { if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } return false; } function compose($data) { if (isset($data["tmt_elements"]) && !empty($data["tmt_elements"])) { $this->setElements($data["tmt_elements"]); if ($this->getId() > 0) { $this->save(); return true; } } return false; } function delete() { if (is_admin() || $this->getCreator()==get_logged_in_user()->getId()) { if (parent::delete()) { return true; } } return false; } function answer($data) { if (!isset($data['tmt_task_answers'])) { return false; } $examinee = get_examinee()->getId(); $task_answer = $data['tmt_task_answers']; $load_time = $data['tmt_load_time']; $task_time = $data['tmt_task_time']; $answer = explode(",", $task_answer); $faults = 0; foreach ($answer as $ans) { if ($ans == 1) { $faults += 1; } } $timer = parent::timer($data); if ($timer && $task_answer) { query_insert("INSERT INTO " . DB_PREFIX . "tmt_answer ". "(base_id, examinee, elements, faults, load_time, task_time, time_load, time_start, time_end, time_submit) values ". "(".$this->getId().", ".$examinee.", '".mysql_real_escape_string($task_answer)."', ". "'". $task_answer ."', ". $faults .", ". $load_time .", ". $task_time ."". $timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"]. ") ON DUPLICATE KEY UPDATE elements='".mysql_real_escape_string($task_answer)."', ". "faults=".$faults.", load_time=".$load_time.", task_time=".$task_time.", ". "time_load=".$timer["load"].", time_start=".$timer["start"].", time_end=".$timer["end"].", time_submit=".$timer["submit"]); } return true; } function collect($examinees) { //$start = "start_time"; //$end = "end_time"; //$load = "load_time"; //$task = "task_time"; $elements = "elements"; $faults = "faults"; $answers = array(); $start = "start_time"; $end = "end_time"; $submit = "submit_time"; $answers['title'][$start] = _("Start time"); $answers['title'][$end] = _("End time"); $answers['title'][$submit] = _("Submit time"); //$answers['title'][$start] = _("Start time"); //$answers['title'][$end] = _("End time"); //$answers['title'][$load] = _("Load time"); //$answers['title'][$task] = _("Task time"); $answers['title'][$elements] = _("Elements"); $answers['title'][$faults] = _("Faults"); foreach ($examinees as $examinee) { $answer = query_row("SELECT * FROM " . DB_PREFIX . "tmt_task t LEFT JOIN " . DB_PREFIX . "tmt_answer a ON t.base_id=a.base_id WHERE t.base_id=".$this->getId()." AND a.examinee=".$examinee); if ($answer) { $answers[$examinee][$start] = $answer->time_start; $answers[$examinee][$end] = $answer->time_end; $answers[$examinee][$submit] = $answer->time_submit; //$answers[$examinee][$start] = round($answer->start/1000, 1); //$answers[$examinee][$end] = round($answer->end/1000, 1); //$answers[$examinee][$load] = $answer->load_time; //$answers[$examinee][$task] = $answer->task_time; $answers[$examinee][$elements] = $answer->elements; $answers[$examinee][$faults] = $answer->faults; } else { $answers[$examinee][$start] = "-"; $answers[$examinee][$end] = "-"; $answers[$examinee][$submit] = "-"; //$answers[$examinee][$load] = "-"; //$answers[$examinee][$task] = "-"; $answers[$examinee][$elements] = "-"; $answers[$examinee][$faults] = "-"; } } $id = "tmttask_".$this->getId(); $title = _("None")." ".$this->getId(); if ($this->getTitle()) $title = $this->getTitle(); return array('id'=>$id, 'title'=>$title, 'answers'=>$answers); } function getElements() { return $this->elements; } function setElements($elements) { $this->elements = $elements; } function buildFromXML($obj, $folder=0, $dir, $version=1) { parent::buildFromXML($obj, $folder, $dir, $version); $elements = $obj->elements[0][0]; if ($version == 1) { $fp=popen("./unpickle.py \"".$elements."\"","r"); $buffer = ""; while (!feof($fp)) { $buffer .= fgets($fp, 4096); } pclose($fp); $elements = json_decode($buffer); $els = array(); $c = 0; foreach ($elements as $el) { $els []= "\"".$c."\":{\"x\":".$el[0].",\"y\":".$el[1].",\"text\":\"".$el[2]."\",\"type\":\"".$el[3]."\"}"; $c++; } $els_str = "{".join($els, ",")."}"; } $this->setElements($els_str); return $this->create(); } function getObjectsXML() { $xml = "getBody()."]]>\n"; return $xml; } } ?>