setType("KSTask"); $this->setName("KS Task"); $this->setTable("ks_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); } } //$val['ksa_elements']='{"load":1305129692821,"begin":2,"end":4,"play":[{"id":"ks_element_0","time":0,"title":"Text 1"},{"id":"ks_element_1","time":1,"title":"Text 2"},{"id":"ks_element_2","time":1,"title":"Text 3"},{"id":"ks_element_3","time":2,"title":"Hästi suur"}]}'; //$this->answer($val); } 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); $timer = parent::timer($data); if ($timer && $answers) { query_insert("INSERT INTO " . DB_PREFIX . "ks_answer ". "(base_id, examinee, elements, time_load, time_start, time_end, time_submit) values ". "(".$this->getId().", ".$examinee.", '".mysql_real_escape_string($answers)."', ". $timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"]. ") ON DUPLICATE KEY UPDATE elements='".mysql_real_escape_string($answers)."', ". "time_load=".$timer["load"].", time_start=".$timer["start"].", time_end=".$timer["end"].", time_submit=".$timer["submit"]); } return true; } function collect($examinees) { $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"); 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->time_start; $answers[$examinee][$end] = $answer->time_end; $answers[$examinee][$submit] = $answer->time_submit; 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] = "-"; $answers[$examinee][$submit] = "-"; 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; } } ?>