id = $id; $this->load(); } } function load() { $data = query_row("SELECT * FROM " . DB_PREFIX . "choices WHERE id=".$this->id); if ($data) { $this->nr = $data->nr; $this->question = $data->question; $this->parent = $data->parent; $this->title = $data->title; $this->image = $data->image; $this->use_title = $data->use_title; $this->position = $data->position; $this->already_checked = $data->already_checked; $this->right_choice_1 = $data->right_choice_1; $this->right_choice_2 = $data->right_choice_2; $this->setLocked($data->locked); $this->pos = $data->pos; return true; } return false; } function create() { if(isset($this->parent)) { $q = query_row("SELECT count(id) as count FROM " . DB_PREFIX . "choices WHERE parent=".$this->parent); $pos = ($q->count)+1; $ins = query_insert("INSERT INTO " . DB_PREFIX . "choices (parent, nr, title, use_title, already_checked, right_choice_1, right_choice_2, position, pos) values (".$this->parent.", ".$this->nr.", '".mysql_real_escape_string($this->title)."', ".$this->use_title.", ".$this->already_checked.", ".$this->right_choice_1.", ".$this->right_choice_2.", ".$this->position.", ".$pos.")"); if ($ins) { $this->id = $ins; $this->save_question(); return $ins; } } return false; } function save() { $sql = "UPDATE " . DB_PREFIX . "choices SET ". "title='".mysql_real_escape_string($this->title)."'". ", nr=".$this->nr. ", use_title=".$this->use_title. ", already_checked=".$this->already_checked. ", right_choice_1=".$this->right_choice_1. ", right_choice_2=".$this->right_choice_2; if ($this->position) { $sql .= ", position=".$this->position; } if ($this->pos) { $sql .= ", pos=".$this->pos; } $sql .= " WHERE id=".$this->id; var_dump($sql); $upd = query_update($sql); if ($upd) { $this->save_question(); return $this->id; } return false; } function copy($locked=false) { if ($this->getImage()) { $image = new Image($this->getImage()); $this->setImage($image->copy($locked)); } $copy = $this->create(); if ($copy) { if ($locked) { $lock = $this->lock($copy); } $this->save_image(); return $copy; } return false; } function lock($id=null) { if (!$id) $id = $this->getId(); return query_update("UPDATE " . DB_PREFIX . "choices SET locked=1 WHERE id=".$id); } function save_image() { if ($this->image) { return query_update("UPDATE " . DB_PREFIX . "choices SET image=".$this->image." WHERE id=".$this->id); } return query_update("UPDATE " . DB_PREFIX . "choices SET image=NULL WHERE id=".$this->id); } function save_question() { if ($this->question) { return query_update("UPDATE " . DB_PREFIX . "choices SET question=".$this->question." WHERE id=".$this->id); } return query_update("UPDATE " . DB_PREFIX . "choices SET question=NULL WHERE id=".$this->id); } function delete() { if ($this->id && (is_admin() || $this->creator==get_logged_in_user()->getId())) { $image = $this->getImage(); $deleted = query("DELETE FROM " . DB_PREFIX . "choices WHERE id=".$this->id); if ($deleted && $image) { $img = new Image($image); $img->delete(); return true; } } return false; } function checkChoice($choice) { if ($choice) { if (!$this->right_choice_1 && !$this->right_choice_2) return 1; if ($this->right_choice_1 && $this->right_choice_2) return 2; if (!$this->right_choice_1 && $this->right_choice_2) return 3; if ($this->right_choice_1 && !$this->right_choice_2) return 4; } return 0; } function answer($data) { $examinee = get_examinee()->getId(); $choice = 0; $choice_name = 'ch_answer_'.$this->id; if ($this->question > 0) { if (isset($data['ch_answer_'.$this->question]) && in_array($this->id, $data['ch_answer_'.$this->question])) $choice = 1; $insert = query_insert("INSERT INTO " . DB_PREFIX . "choices_answer (base_id, question_id, choice_id, examinee, choice) values (".$this->parent.", ".$this->question.", ".$this->id.", ".$examinee.", ".$this->checkChoice($choice).") ON DUPLICATE KEY UPDATE choice=".$this->checkChoice($choice)); } else { if (isset($data['ch_answer_'.$this->parent]) && in_array($this->id, $data['ch_answer_'.$this->parent])) $choice = 1; $insert = query_insert("INSERT INTO " . DB_PREFIX . "choices_answer (base_id, choice_id, examinee, choice) values (".$this->parent.", ".$this->id.", ".$examinee.", ".$this->checkChoice($choice).") ON DUPLICATE KEY UPDATE choice=".$this->checkChoice($choice)); } return true; } function collect($examinees) { $answers = array(); $choice = "choice_".$this->id; $title = _("Choice").$this->getId(); if ($this->getTitle()) $title = $this->getTitle(); $answers["title"][$choice] = $title; foreach ($examinees as $examinee) { $answer = query_row("SELECT choice FROM " . DB_PREFIX . "choices c LEFT JOIN " . DB_PREFIX . "choices_answer a ON c.id=a.choice_id WHERE c.id=".$this->id." AND a.examinee=".$examinee); if ($answer) { $answers[$examinee][$choice] = $answer->choice; } else { $answers[$examinee][$choice] = "-"; } } return $answers; } function build($nr, $data) { if (isset($data["c_delete_".$nr]) && $data["c_delete_".$nr]) { return $this->delete(); } else if (isset($data["c_title_".$nr]) && !empty($data["c_title_".$nr])) { $this->title = $data["c_title_".$nr]; if (isset($data["choice_order"]) && $data["choice_order"] != "") { $positions = explode(",", $data["choice_order"]); $this->pos = array_search($nr, $positions)+1; } $this->setUseTitle(0); if (isset($data["c_use_title_".$nr])) $this->setUseTitle($data["c_use_title_".$nr]); $this->setAlreadyChecked(0); if (isset($data["c_already_checked_".$nr])) $this->setAlreadyChecked($data["c_already_checked_".$nr]); if (isset($data["c_rightwrong_".$nr])) $this->setRightChoice($data["c_rightwrong_".$nr]); if (isset($data["c_position_".$nr])) $this->setPosition($data["c_position_".$nr]); $chok = false; if ($this->id > 0) { $chok = $this->save(); } else { $chok = $this->create(); } $c_image = get_file("c_pic_".$nr); if ($chok && isset($c_image)) { if (!isset($c_image["error"]) || $c_image["error"] == 0) { if ($this->getImage() > 0) { $image = new Image($this->getImage()); } else { $image = new Image(); } $image_id = $image->upload($c_image); if ($image_id) { $this->setImage($image_id); $this->save_image(); } } else if (get_input("c_delete_pic_".$nr, false)) { $image = new Image($this->getImage()); if ($image->delete()) { $this->setImage(NULL); $this->save_image(); } } } } return false; } function getId() { return $this->id; } function setId($id) { $this->id = $id; } function getNr() { return $this->nr; } function setNr($nr) { if (is_numeric($nr)) $this->nr = $nr; } function getQuestion() { return $this->question; } function setQuestion($question) { if (is_numeric($question)) $this->question = $question; } function getImage() { return $this->image; } function setImage($image) { if (is_numeric($image)) $this->image = $image; } function getTitle() { return $this->title; } function setTitle($title) { $this->title = $title; } function getParent() { return $this->parent; } function setParent($parent) { $this->parent = $parent; } function getUseTitle() { return $this->use_title; } function setUseTitle($use_title) { if ($use_title) { $this->use_title = 1; } else { $this->use_title = 0; } } function getAlreadyChecked() { return $this->already_checked; } function setAlreadyChecked($already_checked) { if ($already_checked) { $this->already_checked = 1; } else { $this->already_checked = 0; } } function getRightChoice1() { return $this->right_choice_1; } function getRightChoice2() { return $this->right_choice_2; } function setRightChoice1($rc1) { $this->right_choice_1 = $rc1; } function setRightChoice2($rc2) { $this->right_choice_2 = $rc2; } function setRightChoice($answer) { if ($answer=="r1") { $this->right_choice_1 = 1; $this->right_choice_2 = 1; } else if ($answer=="r2") { $this->right_choice_1 = 1; $this->right_choice_2 = 0; } else if ($answer=="w2") { $this->right_choice_1 = 0; $this->right_choice_2 = 1; } else { $this->right_choice_1 = 0; $this->right_choice_2 = 0; } } function getPosition() { return $this->position; } function setPosition($position) { $this->position = $position; } public function getLocked() { return $this->locked; } public function setLocked($locked) { if ($locked) { $this->locked = 1; } else { $this->locked = 0; } } function getObjectsXML() { $xml = "getElements()."]]>\n"; return $xml; } function buildFromXML($obj, $parent=0, $dir, $version=1) { $this->setParent($parent); $this->setTitle($obj->title[0][0]); if (isset($obj->useTitle[0][0]) && $obj->useTitle[0][0] && $obj->useTitle[0][0] != "False") { $this->setUseTitle(1); } else { $this->setUseTitle(0); } if (isset($obj->alreadyChecked[0][0]) && $obj->alreadyChecked[0][0] && $obj->alreadyChecked[0][0] != "False") { $this->setAlreadyChecked(1); } else { $this->setAlreadyChecked(0); } if (isset($obj->rightChoice[0][0]) && $obj->rightChoice[0][0] && $obj->rightChoice[0][0] != "False") { $this->setRightChoice1(1); } else { $this->setRightChoice1(0); } if (isset($obj->rightChoice2[0][0]) && $obj->rightChoice2[0][0] && $obj->rightChoice2[0][0] != "False") { $this->setRightChoice2(1); } else { $this->setRightChoice2(0); } if ($ob_id = $this->create()) { $img = $obj->imageFile[0][0]; if ($obj->imageFile[0][0]) { $image = new Image(); $image_id = $image->save_from_dir($dir, $obj->imageFile[0][0]); if ($image_id > 0) { $this->setImage($image_id); $this->save_image(); } } return $ob_id; } return false; } } ?>