setType("TimestopTask"); $this->setName("Time Stop Task"); $this->setTable("timestop_task"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setElements($data->elements); $this->setTaskType($data->task_type); } } } function create() { if ($id = parent::create()) { $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, elements, task_type) values (".$id.", '".mysql_real_escape_string($this->getElements())."', ".$this->getTaskType().")"); 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())."', task_type=".$this->getTaskType()." WHERE base_id=".$this->getId()); return $this->getId(); } return false; } function copy($locked=false) { $copy = $this->create(); if ($copy && $locked) { $lock = $this->lock($copy); } return $copy; } function answer($data) { $examinee = get_examinee()->getId(); $elements = json_decode($this->getElements()); $answers = array(); foreach ($elements as $element) { $answers["ks_element_".$element->nr] = array('title'=>$element->text, 'choice'=>0, 'nr'=>$element->nr, 'time'=>0); } $start = 0; $end = 0; if (isset($data['ksa_elements']) && $data['ksa_elements']!="") { $d = json_decode($data['ksa_elements']); $start = $d->begin; $end = $d->end; $ans = $d->play; foreach ($ans as $a) { $answers[$a->id]["choice"] = 2; $answers[$a->id]["time"] = $a->time; } } $answers = serialize($answers); $insert = query_insert("INSERT INTO " . DB_PREFIX . "ks_answer (base_id, examinee, elements, start, end) values (".$this->getId().", ".$examinee.", '".mysql_real_escape_string($answers)."', ".$start.", ".$end.")"); if ($insert) { return true; } return false; } function collect($examinees) { $start = "start_time"; $end = "end_time"; $answers = array(); $answers['title'][$start] = _("Start time"); $answers['title'][$end] = _("End time"); foreach ($examinees as $examinee) { $answer = query_row("SELECT *, a.elements as an_el FROM " . DB_PREFIX . "ks_task t LEFT JOIN " . DB_PREFIX . "ks_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->start; $answers[$examinee][$end] = $answer->end; foreach (unserialize($answer->an_el) as $id => $element) { $answers['title'][$id] = $element['title']; $answers[$examinee][$id] = $element['choice']; } } else { $answers[$examinee][$start] = "-"; $answers[$examinee][$end] = "-"; foreach (json_decode($this->getElements()) as $element) { $answers['title']['ks_element_'.$element->nr] = $element->text; $answers[$examinee]['ks_element_'.$element->nr] = "-";; } } } $id = "kstask_".$this->getId(); $title = _("None")." ".$this->getId(); if ($this->getTitle()) $title = $this->getTitle(); return array('id'=>$id, 'title'=>$title, 'answers'=>$answers); } function build($data) { if (parent::build($data)) { if (isset($data["ks_task_type"]) && !empty($data["ks_task_type"])) $this->setTaskType($data["ks_task_type"]); if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } return false; } function compose($data) { if (isset($data["ks_elements"]) && !empty($data["ks_elements"])) { $this->setElements($data["ks_elements"]); if ($this->getId() > 0) { $this->save(); return true; } } return false; } function getElements() { return $this->elements; } function setElements($elements) { $this->elements = $elements; } function getTaskType() { return $this->task_type; } function setTaskType($tasktype) { $this->task_type = $tasktype; } function buildFromXML($obj, $version=1, $folder=0) { parent::buildFromXML($obj, $folder); $elements = $obj["elements"]; if ($version == 1) { $fp=popen("python 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 = "getElements()."]]>\n"; return $xml; } } ?>