setType("FlowerTask"); $this->setName("Flower task"); $this->setTable("flower_task"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setTableHeight($data->table_height); $this->setTableWidth($data->table_width); $this->setFlowerTable(unserialize($data->flower_table)); $this->setRightChoice(unserialize($data->right_choice)); } } } function create() { if ($id = parent::create()) { $this->createFlowerTable(); $serialized_table = serialize($this->flower_table); $serialized_right_choice = serialize($this->right_choice); $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, table_height, table_width, flower_table, right_choice) values (".$id.", ".$this->getTableHeight().", ".$this->getTableWidth().", '".$serialized_table."', '".$serialized_right_choice."')"); return $id; } return false; } function save() { if (parent::save()) { $this->updateFlowerTable(); $serialized_table = serialize($this->flower_table); if (query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET table_height=".$this->getTableHeight().", table_width=".$this->getTableWidth().", flower_table='".$serialized_table."' 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 compose($data) { $row = $data["row"]; $col = $data["col"]; $active_tool = $data["active_tool"]; if ($active_tool != "rc") { $this->saveFlowerTable($row, $col, $active_tool); } else { $this->saveRightChoice($row, $col); } } function saveFlowerTable($row, $col, $data) { $this->flower_table[$row][$col] = $data; $serialized_table = serialize($this->flower_table); if (query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET flower_table='".$serialized_table."' WHERE base_id=".$this->getId())) return $this->getId(); } function saveRightChoice($row, $col) { $this->right_choice = array($row, $col); $serialized_right_choice = serialize($this->right_choice); if (query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET right_choice='".$serialized_right_choice."' WHERE base_id=".$this->getId())) return $this->getId(); } function answer($data) { if (!isset($data['current_array'])) { return false; } $start = $data['answer_flower_start']; $end = $data['answer_flower_end']; $examinee = get_examinee()->getId(); $task_answer = $data['current_array']; $answer = explode(",", $task_answer); $right_choice = $this->getRightChoice(); $choice = 1; if ($answer[0] == $right_choice[0] && $answer[1] == $right_choice[1]) { $choice = 2; } $clicked_elem = $this->arrayCoordinatesAsNr($answer, $this->getTableWidth()); $timer = parent::timer($data); if ($timer) { query_insert("INSERT INTO " . DB_PREFIX . "flower_answer ". "(base_id, examinee, choice, clicked, start, end, time_load, time_start, time_end, time_submit) values ". "(".$this->getId().", ".$examinee.", ".$choice.", ".$clicked_elem.", ".$start.", ".$end.", ". $timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"]. ") ON DUPLICATE KEY UPDATE choice=".$choice .", clicked=".$clicked_elem.", start=".$start.", end=".$end.", ". "time_load=".$timer["load"].", time_start=".$timer["start"].", time_end=".$timer["end"].", time_submit=".$timer["submit"]); } return true; } function collect($examinees) { $choice = "choice"; $clicked = "clicked"; $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'][$choice] = _("Choice"); $answers['title'][$clicked] = _("Clicked"); foreach ($examinees as $examinee) { $answer = query_row("SELECT * FROM " . DB_PREFIX . "flower_task t LEFT JOIN " . DB_PREFIX . "flower_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][$choice] = $answer->choice; $answers[$examinee][$clicked] = $answer->clicked; } else { $answers[$examinee][$start] = "-"; $answers[$examinee][$end] = "-"; $answers[$examinee][$submit] = "-"; $answers[$examinee][$choice] = "-"; $answers[$examinee][$clicked] = "-"; } } $id = "flowertask_".$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["table_height"])) { if (is_numeric($data["table_height"])) { $this->setTableHeight($data["table_height"]); } else { $this->addBuildError(_("Table height")); } } if (isset($data["table_width"])) { if (is_numeric($data["table_width"])) { $this->setTableWidth($data["table_width"]); } else { $this->addBuildError(_("Table width")); } } if ($this->getBuildError()) { global $TeKe; $TeKe->add_system_message(_("Problems with fields") . ": " . implode(",", $this->getBuildError()), 'error'); return false; } if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } } function getTableHeight() { return $this->table_height; } function setTableHeight($table_height) { $this->table_height = $table_height; } function getTableWidth() { return $this->table_width; } function setTableWidth($table_width) { $this->table_width = $table_width; } function createFlowerTable() { if (!$this->flower_table) { $table = array(); $rows = $this->getTableHeight(); $cols = $this->getTableWidth(); foreach (range(0,$rows-1) as $row) { $table []= array(); foreach (range(0,$cols-1) as $col) { $table[$row][$col] = "ml"; } } $this->flower_table = $table; } } function updateFlowerTable() { $table = $this->flower_table; $rows = $this->getTableHeight(); $cols = $this->getTableWidth(); foreach (range(0,$rows-1) as $row) { if ($row > count($table)-1) { $table []= array(); } foreach (range(0,$cols-1) as $col) { if ($col > count($table[$row])-1) { $table[$row][$col] = "ml"; } } } $this->flower_table = $table; } function getFlowerTable() { return $this->flower_table; } function setFlowerTable($flower_table) { $this->flower_table = $flower_table; } function getRightChoice() { return $this->right_choice; } function setRightChoice($right_choice) { $this->right_choice = $right_choice; } function buildFromXML($obj, $version=1, $folder=0) { var_dump($obj);exit; //parent::buildFromXML($obj); } } ?>