setType("TimeelapseTask");
$this->setName("Time Elapse Task");
$this->setTable("timeelapse_task");
$this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose")));
if ($id) {
$data = $this->load($id);
if ($data) {
$this->setElements(json_decode($data->elements));
$this->setJsonElements($data->elements);
$this->setTasks($data->tasks);
}
}
}
function create() {
if ($id = parent::create()) {
$insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, elements, tasks) values (".$id.", '".mysql_real_escape_string(json_encode($this->getElements()))."', ".$this->getTasks().")");
return $id;
}
return false;
}
function save() {
if (parent::save()) {
$update = query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET elements='".mysql_real_escape_string(json_encode($this->getElements()))."', tasks=".$this->getTasks()." 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["te_element_".$element->nr] = array('title'=>$element->text, 'choice'=>0, 'nr'=>$element->nr, 'time'=>0);
}
$start = 0;
$end = 0;
if (isset($data['tea_elements']) && $data['tea_elements']!="") {
$d = json_decode($data['tea_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 . "timeelapse_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 . "timeelapse_task t LEFT JOIN " . DB_PREFIX . "timeelapse_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["te_tasks"]) && !empty($data["te_tasks"])) $this->setTasks($data["te_tasks"]);
if ($this->getId() > 0) {
return $this->save();
} else {
return $this->create();
}
}
return false;
}
function compose($data) {
$elements = array();
for ($i=0; $i<$this->getTasks(); $i++) {
$desc = "";
if (isset($data["te_element_desc_".$i]) && !empty($data["te_element_desc_".$i])) $desc = $data["te_element_desc_".$i];
$fb = 0;
if (isset($data["te_element_feedback_".$i]) && !empty($data["te_element_feedback_".$i])) $fb = $data["te_element_feedback_".$i];
$element = array('desc'=>$desc, 'feedback'=>$fb);
$elements []= $element;
}
if (count($elements)>0) {
$this->setElements($elements);
if ($this->getId() > 0) {
$this->save();
return true;
}
}
return false;
}
function getElements() {
return $this->elements;
}
function setElements($elements) {
$this->elements = $elements;
}
function getJsonElements() {
return $this->jsonelements;
}
function setJsonElements($elements) {
$this->jsonelements = $elements;
}
function getTasks() {
return $this->tasks;
}
function setTasks($tasks) {
$this->tasks = $tasks;
}
function buildFromXML($obj, $version=1, $folder=0) {
parent::buildFromXML($obj, $folder);
$elements = $obj["elements"];
//$this->setElements($els_str);
//return $this->create();
}
function getObjectsXML() {
$xml = "getTasks()."]]>\n";
$xml = "getElements()."]]>\n";
return $xml;
}
}
?>