id = $id; $this->load($data); } } function load($data = false) { if (!$data) $data = query_row("SELECT * FROM " . DB_PREFIX . "questions WHERE id=".$this->id); if ($data) { $this->parent = $data->parent; $this->title = $data->title; $this->text_title = $data->text_title; $this->text_input = $data->text_input; $this->text_value = $data->text_value; $this->setTextCols($data->text_cols); $this->setTextRows($data->text_rows); $this->setTextCharcount($data->text_charcount); $this->setTextCharcounter($data->text_charcounter); $this->setTextPos($data->text_pos); $this->setTextBefore($data->text_before); $this->table_has_border = $data->table_has_border; $this->multiple_choice = $data->multiple_choice; $this->answer_required = $data->answer_required; $this->format = $data->format; $this->margin_top = $data->margin_top; $this->margin_bottom = $data->margin_bottom; $this->position = $data->position; $this->use_question_title = $data->use_question_title; return true; } return false; } function create() { if(isset($this->parent)) { $text_input_col = ""; $text_input_val = ""; if ($this->text_input) { $text_input_col = ", text_title, text_input, text_value, text_cols, text_rows, text_charcount, text_charcounter, text_pos, text_before "; $text_input_val = ", '".mysql_real_escape_string($this->text_title)."', ".$this->text_input.", '".mysql_real_escape_string($this->text_value)."', ".$this->text_cols.", ".$this->text_rows.", ".$this->text_charcount.", ".$this->text_charcounter.", ".$this->text_pos.", ".$this->text_before; } $q = query_row("SELECT count(id) as count FROM " . DB_PREFIX . "questions WHERE parent=".$this->getParent()); $pos = ($q->count)+1; return query_insert("INSERT INTO " . DB_PREFIX . "questions (parent, title, format, multiple_choice, answer_required, table_has_border, margin_top, margin_bottom, use_question_title, position".$text_input_col.") values (".$this->parent.", '".mysql_real_escape_string($this->title)."', ".$this->format.", ".$this->getMultipleChoice().", ".$this->getAnswerRequired().", ".$this->getTableHasBorder().", ".$this->getMarginTop().", ".$this->getMarginBottom().", ".$this->getUseQuestionTitle().", ".$pos.$text_input_val.")"); } } function save() { $text_input = ""; if ($this->text_input) $text_input = ", text_title='".mysql_real_escape_string($this->text_title)."', text_input=".$this->text_input.", text_value='".mysql_real_escape_string($this->text_value)."', text_cols=".$this->text_cols.", text_rows=".$this->text_rows.", text_charcount=".$this->text_charcount.", text_charcounter=".$this->text_charcounter.", text_pos=".$this->text_pos.", text_before=".$this->text_before; if (query_update("UPDATE " . DB_PREFIX . "questions SET title='".mysql_real_escape_string($this->title)."', format=".$this->format.", multiple_choice=".$this->getMultipleChoice().", answer_required=".$this->getAnswerRequired().", table_has_border=".$this->getTableHasBorder().", margin_top=".$this->margin_top.", margin_bottom=".$this->margin_bottom.", use_question_title=".$this->getUseQuestionTitle().", text_input=".$this->getTextInput().$text_input." WHERE id=".$this->id)) return $this->id; return false; } function copy($locked=false) { //muuda $choices = $this->getChoices(1); $copy = $this->create(); if ($copy){ if ($locked) { $lock = $this->lock($copy); } $this->setId($copy); foreach ($choices as $choice) { $choice->setParent($this->getParent()); $choice->setQuestion($this->getId()); if (!$choice->copy($locked)) return false; } return true; } return false; } function lock($id=null) { if (!$id) $id = $this->getId(); return query_update("UPDATE " . DB_PREFIX . "questions SET locked=1 WHERE id=".$id); } function delete() { if (is_admin() || $this->creator==get_logged_in_user()->getId()) { $choices = $this->getChoices(1); $deleted = query("DELETE FROM " . DB_PREFIX . "questions WHERE id=".$this->id); if ($deleted) { foreach ($choices as $choice) { $choice->delete(); } return true; } } return false; } function answer($data) { $textfield = ""; if (isset($data['textfield_'.$this->id])) $textfield = $data['textfield_'.$this->id]; $selected_choices = array(); $choices = $this->getChoices(1); if (isset($data['ch_answer_'.$this->getId()])) { $selected_choices = $data['ch_answer_'.$this->getId()]; } $answer_required = (boolean)$this->getAnswerRequired(); $uses_textfield = (boolean)$this->getTextInput(); $empty_textfield = empty($textfield); // TODO this is correct way but couses trouble so use wrong way // checks are performed in javascript //$uses_selected_choices = !empty($choices); $uses_selected_choices = empty($choices); $empty_selected_choices = empty($selected_choices); $has_textfield_answer = true; $has_choices_answer = true; if ($uses_textfield && $empty_textfield) $has_textfield_answer = false; if ($uses_selected_choices && $empty_selected_choices) $has_choices_answer = false; // If answer required and no textfield fileld or choice made don't let do next action // TODO this one is broken //if ($answer_required && (!$has_textfield_answer || !$has_choices_answer)) { // return false; //} $examinee = get_examinee()->getId(); $insert = query_insert("INSERT INTO " . DB_PREFIX . "questions_answer (base_id, question_id, examinee, textfield) values (".$this->parent.", ".$this->id.", ".$examinee.", '".mysql_real_escape_string($textfield)."') ON DUPLICATE KEY UPDATE textfield='".mysql_real_escape_string($textfield)."'"); foreach ($choices as $choice) { $choice->setParent($this->getParent()); if (!$choice->answer($data)) return false; } // TODO cant check when duplicate update, so always true return true; } function collect($examinees) { $answers = array(); if ($this->getTextInput()) { $textfield = "textfield"; $title = _("Text"); if ($this->getTextTitle()) $title = $this->getTextTitle(); $answers['title'][$textfield] = $title; foreach ($examinees as $examinee) { $answer = query_row("SELECT textfield FROM " . DB_PREFIX . "questions q LEFT JOIN " . DB_PREFIX . "questions_answer a ON q.id=a.question_id WHERE q.id=".$this->getId()." AND a.examinee=".$examinee); if ($answer) { $answers[$examinee][$textfield] = $answer->textfield; } else { $answers[$examinee][$textfield] = "-"; } } } $ca = array(); $choices = $this->getChoices(1); foreach ($choices as $choice) { $id = "choice_".$choice->getId(); $title = _("None")." ".$choice->getId(); if ($choice->getTitle()) $title = $choice->getTitle(); $ca[$id] = array('id'=>$choice->getId(), 'title'=>$title, 'answers'=>$choice->collect($examinees)); } if ($ca && !$this->getMultipleChoice()) { $choice_counter = 1; $choice_selection_title = _("Selection"); $choice_selection_answers = array(); $choice_selection_answers["title"] = array("choice_selection_".$this->getId() => $choice_selection_title); foreach ($ca as $c) { $cid = $c["id"]; foreach ($c["answers"]as $e => $a) { if ($e != "title") { if (!array_key_exists($e, $choice_selection_answers)) $choice_selection_answers[$e]["choice_selection_".$this->getId()] = "-"; if ($a["choice_".$cid] > 0) { $choice_selection_answers[$e]["choice_selection_".$this->getId()] = $choice_counter; } } } $choice_counter++; } $choice_selection = array("id"=>$this->getId(), "title"=> $choice_selection_title, "answers" => $choice_selection_answers); $ca []= $choice_selection; } if ($answers || $ca) { $id = "question_".$this->getId(); $title = _("None")." ".$this->getId(); if ($this->getTitle()) $title = $this->getTitle(); return array('id'=>$id, 'title'=>$title, 'answers'=>$answers, 'choices'=>$ca); } return array(); } function build($data) { if (isset($data["delete_question"]) && isset($data["question_id"]) && $data["question_id"] > 0) { $this->setId($data["question_id"]); $this->delete(); } else if (isset($data["question"]) && !empty($data["question"])) { $this->title = $data["question"]; $this->setTableHasBorder(0); if (isset($data["table_has_border"])) $this->setTableHasBorder($data["table_has_border"]); $this->setMultipleChoice(0); if (isset($data["multiple_choice"])) $this->setMultipleChoice($data["multiple_choice"]); $this->setAnswerRequired(0); if (isset($data["answer_required"])) $this->setAnswerRequired($data["answer_required"]); $this->setTextInput(0); if (isset($data["text_input"])) $this->setTextInput($data["text_input"]); if (isset($data["text_title"])) $this->setTextTitle($data["text_title"]); if (isset($data["text_value"])) $this->setTextValue($data["text_value"]); if (isset($data["text_cols"])) $this->setTextCols($data["text_cols"]); if (isset($data["text_rows"])) $this->setTextRows($data["text_rows"]); if (isset($data["text_charcount"])) { if (is_numeric($data["text_charcount"])) { $this->setTextCharcount($data["text_charcount"]); } else { $this->setTextCharcount(0); } } $this->setTextCharcounter(0); if (isset($data["text_charcounter"])) $this->setTextCharcounter($data["text_charcounter"]); if (isset($data["text_pos"])) $this->setTextPos($data["text_pos"]); $this->setTextBefore(0); if (isset($data["text_before"])) $this->setTextBefore($data["text_before"]); $this->setUseQuestionTitle(0); if (isset($data["use_question_title"])) $this->setUseQuestionTitle($data["use_question_title"]); if (isset($data["margin_top"])) $this->setMarginTop($data["margin_top"]); if (isset($data["margin_bottom"])) $this->setMarginBottom($data["margin_bottom"]); if (isset($data["format"])) $this->setFormat($data["format"]); $qok = false; if ($this->id <= 0 && $qid = $this->create()) { $this->setId($qid); $qok = true; } else if ($this->save()) { $qok = true; } if ($qok) { $nr = 0; //if (isset($data["choice_order"])) $this->repositionChoices($data["choice_order"]); foreach ($this->getChoices() as $choice) { $choice->setParent($this->getParent()); $choice->setQuestion($this->getId()); $choice->build($nr, $data); $nr++; } return $this->getId(); } } return false; } function getId() { return $this->id; } function setId($id) { $this->id = $id; } function getTitle() { return $this->title; } function setTitle($title) { $this->title = $title; } function getParent() { return $this->parent; } function setParent($parent) { $this->parent = $parent; } function getTextTitle() { return $this->text_title; } function setTextTitle($text_title) { $this->text_title = $text_title; } function getTextValue() { return $this->text_value; } function setTextValue($text_value) { $this->text_value = $text_value; } function getTextInput() { return $this->text_input; } function setTextInput($text_input) { if ($text_input) { $this->text_input = 1; } else { $this->text_input = 0; } } function getTextCols() { return $this->text_cols; } function setTextCols($text_cols) { $text_cols = (int) $text_cols; if ($text_cols) { $this->text_cols = $text_cols; } } function getTextRows() { return $this->text_rows; } function setTextRows($text_rows) { $text_rows = (int) $text_rows; if ($text_rows) { $this->text_rows = $text_rows; } } function getTextCharcount() { return $this->text_charcount; } function setTextCharcount($text_charcount) { $text_charcount = (int) $text_charcount; if (is_numeric($text_charcount)) { $this->text_charcount = $text_charcount; } } function getTextCharcounter() { return $this->text_charcounter; } function setTextCharcounter($text_charcounter) { if ($text_charcounter) { $this->text_charcounter = 1; } else { $this->text_charcounter = 0; } } function getTextPos() { return $this->text_pos; } function setTextPos($text_pos) { $this->text_pos = $text_pos; } function getFormat() { return $this->format; } function setFormat($format) { $this->format = $format; } function getMultipleChoice() { return $this->multiple_choice; } function setMultipleChoice($table_has_border) { if ($table_has_border) { $this->multiple_choice = 1; } else { $this->multiple_choice = 0; } } function getAnswerRequired() { return $this->answer_required; } function setAnswerRequired($answer_required) { if ($answer_required) { $this->answer_required = 1; } else { $this->answer_required = 0; } } function getUseQuestionTitle() { return $this->use_question_title; } function setUseQuestionTitle($use_title) { if ($use_title) { $this->use_question_title = 1; } else { $this->use_question_title = 0; } } function getMarginTop() { return $this->margin_top; } function setMarginTop($margin_top) { $margin_top = (int) $margin_top; if ($margin_top) { $this->margin_top = $margin_top; } } function getMarginBottom() { return $this->margin_bottom; } function setMarginBottom($margin_bottom) { $margin_bottom = (int) $margin_bottom; if ($margin_bottom) { $this->margin_bottom = $margin_bottom; } } public function getPosition() { return $this->position; } function setPosition($pos) { $this->position = $pos; } function getTableHasBorder() { return $this->table_has_border; } function setTableHasBorder($table_has_border) { if ($table_has_border) { $this->table_has_border = 1; } else { $this->table_has_border = 0; } } function getTextBefore() { return $this->text_before; } function setTextBefore($text_before) { if ($text_before) { $this->text_before = 1; } else { $this->text_before = 0; } } function getChoices($existing = false) { $choices = array(); if ($this->getId() > 0) { foreach(query_rows("SELECT id FROM " . DB_PREFIX . "choices WHERE question=".$this->getId()." ORDER BY pos ASC") as $ch) { $choices []= new ChoiceObject($ch->id); } } if (!$existing) { for ($i=count($choices); $i<10; $i++) { $choices []= new ChoiceObject(); } } return $choices; } function repositionChoices($positions) { $choices = $this->getChoices(); $positionCounter = 1; $positions = explode(",", $positions); foreach ($positions as $position_id) { if ($choices[$position_id]->getId() > 0) { query_update("UPDATE " . DB_PREFIX . "choices SET pos=" . $positionCounter . " WHERE id=" . $choices[$position_id]->getId() . " AND question=".$this->getId()); } $positionCounter = $positionCounter + 1; } return true; } function getObjectsXML() { $xml = "getElements()."]]>\n"; return $xml; } } ?>