setType("SliderTask"); $this->setName("Slider Task"); $this->setTable("slider_task"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"))); if ($id) { $data = $this->load($id); if ($data) { $this->setOptions($data->options); $this->setCorrects($data->corrects); } } } function create() { if ($id = parent::create()) { $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, options, corrects) values (".$id.", '".mysql_real_escape_string($this->getOptions())."', '".mysql_real_escape_string($this->getCorrects())."')"); return $id; } return false; } function save() { if (parent::save()) { query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET options='".mysql_real_escape_string($this->getOptions())."', corrects='".mysql_real_escape_string($this->getCorrects())."' 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(); $slider_data = json_decode($this->getCorrects(), true); $answer = array(); if (isset($data['slider_token']) && $data['slider_token']!="") { $token = base64_decode($data['slider_token']); $correct = 0; $offset = $slider_data["offset"]; $correct_value = $slider_data["correct"]; if ($correct_value - $offset <= $token && $correct_value + $offset >= $token) { $correct = 1; } $answer = array('answer'=>$token, 'correct'=>$correct); } $answer = serialize($answer); $timer = parent::timer($data); if ($timer && $answer) { query_insert("INSERT INTO " . DB_PREFIX . "slider_answer ". "(base_id, examinee, answer, time_load, time_start, time_end, time_submit) values ". "(".$this->getId().", ".$examinee.", '".mysql_real_escape_string($answer)."', ". $timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"]. ") ON DUPLICATE KEY UPDATE answer='".mysql_real_escape_string($answer)."', ". "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"; $submit = "submit_time"; $answers = array(); $answers['title'][$start] = _("Start time"); $answers['title'][$end] = _("End time"); $answers['title'][$submit] = _("Submit time"); foreach ($examinees as $examinee) { $answer = query_row("SELECT *, a.answer as answer_data FROM " . DB_PREFIX . "slider_task t LEFT JOIN " . DB_PREFIX . "slider_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; $answer_data = unserialize($answer->answer_data); $answers['title']["answer"] = _("Answer"); $answers[$examinee]["answer"] = $answer_data['answer']; $answers['title']["correct"] = _("Correct"); $answers[$examinee]["correct"] = $answer_data['correct']; } else { $answers[$examinee][$start] = "-"; $answers[$examinee][$end] = "-"; $answers[$examinee][$submit] = "-"; $answers['title']["answer"] = _("Answer"); $answers[$examinee]["answer"] = "-"; $answers['title']["correct"] = _("Correct"); $answers[$examinee]["correct"] = "-"; } } $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)) { $opts = array(); $cor = array(); if ((isset($data["max"])) && is_numeric($data["max"])) { $opts["max"] = $data["max"]; } if (isset($data["min"]) && is_numeric($data["min"])) { $opts["min"] = $data["min"]; } if (isset($data["value"]) && is_numeric($data["value"])) { $opts["value"] = $data["value"]; } if (isset($data["values"]) && !empty($data["values"]) && is_array($data["values"]) && count($data["values"]) == 2) { $opts["values"] = $data["values"]; } if (isset($data["range"]) && is_numeric($data["range"])) { $opts["range"] = $data["range"]; } if (isset($data["offset"]) && is_numeric($data["offset"])) { $cor["offset"] = $data["offset"]; } if (isset($data["scale_start"])) { $opts["scale_start"] = 1; } else { $opts["scale_start"] = 0; } if (isset($data["scale_end"])) { $opts["scale_end"] = 1; } else { $opts["scale_end"] = 0; } if (isset($data["pips"])) { $opts["pips"] = 1; } else { $opts["pips"] = 0; } if (isset($data["labels"])) { $opts["labels"] = 1; } else { $opts["labels"] = 0; } if (isset($data["correct"]) && is_numeric($data["correct"])) { $cor["correct"] = $data["correct"]; } if (isset($data["corrects"]) && !empty($data["corrects"]) && is_array($data["corrects"]) && count($data["corrects"]) == 2) { $cor["corrects"] = $data["corrects"][0].", ".$data["corrects"][1]."]"; } if (isset($data["orientation"]) && !empty($data["orientation"])) { $opts["orientation"] = $data["orientation"]; } if (isset($data["optional_labels"]) && !empty($data["optional_labels"])) { //$opts["optional_labels"] = explode(",", $data["optional_labels"]); $opts["optional_labels"] = array_map('trim', explode(',', $data["optional_labels"])); } $this->setOptions(json_encode($opts)); $this->setCorrects(json_encode($cor)); if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } return false; } function getOptionsEditable() { return json_decode($this->options); } function getOptions() { return $this->options; } function setOptions($data) { $this->options = $data; } function getCorrectsEditable() { return json_decode($this->corrects); } function getCorrects() { return $this->corrects; } function setCorrects($data) { $this->corrects = $data; } function buildFromXML($obj, $version=1, $folder=0) { parent::buildFromXML($obj, $folder); $this->setOptions($obj["options"]); return $this->create(); } function getObjectsXML() { $xml = "getOptions()."]]>\n"; return $xml; } } ?>