setType("Questionnaire"); $this->setName("Questionnaire"); $this->setTable("questionnaire"); $this->setActions(array("view"=>_("View"), "edit"=>_("Edit"), "compose"=>_("Compose"))); if ($id) { $data = $this->load($id); if ($data) { $this->setQuestionsPerPage($data->questions_per_page); $this->setQuestionsInTable($data->questions_in_table); } } } function create() { if ($id = parent::create()) { $insert = query_insert("INSERT INTO " . DB_PREFIX . $this->getTable() . " (base_id, questions_per_page, questions_in_table) values (".$id.", ".$this->getQuestionsPerPage().", ".$this->getQuestionsInTable().")"); return $id; } return false; } function save() { if (parent::save()) { $update = query_update("UPDATE " . DB_PREFIX . $this->getTable() . " SET questions_per_page=".$this->getQuestionsPerPage().", questions_in_table=".$this->getQuestionsInTable()." WHERE base_id=".$this->getId()); return $this->getId(); } return false; } function copy($locked=false) { //muuda $copy = $this->create(); if ($copy){ if ($locked) { $lock = $this->lock($copy); } $questions = $this->getQuestions(); foreach ($questions as $question) { $question->setParent($copy); if (!$question->copy($locked)) return false; } return true; } } function build($data) { if (parent::build($data)) { $this->setQuestionsInTable(0); if (isset($data["questions_in_table"])) $this->setQuestionsInTable($data["questions_in_table"]); if (isset($data["questions_per_page"])) $this->setQuestionsPerPage($data["questions_per_page"]); if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } return false; } function answer($data) { $timer = parent::timer($data); if ($timer) { $examinee = get_examinee()->getId(); query_insert("INSERT INTO " . DB_PREFIX . "questionnaire_answer ". "(base_id, examinee, time_load, time_start, time_end, time_submit) values ". "(".$this->getId().", ".$examinee.", ". $timer["load"].", ".$timer["start"].", ".$timer["end"].", ".$timer["submit"]. ") ON DUPLICATE KEY UPDATE ". "time_load=".$timer["load"].", time_start=".$timer["start"].", time_end=".$timer["end"].", time_submit=".$timer["submit"]); } $questions = $this->getQuestions(); foreach ($questions as $question) { if (!$question->answer($data)) return false; } return true; } function collect($examinees) { $qa = array(); $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"); foreach ($examinees as $examinee) { $answer = query_row("SELECT * FROM " . DB_PREFIX . "questionnaire_answer a LEFT JOIN " . DB_PREFIX . $this->getTable() . " c ON c.base_id=a.base_id WHERE c.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; } else { $answers[$examinee][$start] = "-"; $answers[$examinee][$end] = "-"; $answers[$examinee][$submit] = "-"; } } $questions = $this->getQuestions(); foreach ($questions as $question) { $id = "question_".$question->getId(); $qa[$id] = $question->collect($examinees); } $id = "questionnaire_".$this->getId(); $title = _("None")." ".$this->getId(); if ($this->getTitle()) $title = $this->getTitle(); return array('id'=>$id, 'title'=>$title, 'timer'=>$answers, 'questions'=>$qa); } function compose($data) { if (isset($data["question_id"]) && $data["question_id"] > 0) { $question = $this->getNewQuestion($data["question_id"]); } else { $question = $this->getNewQuestion(); } return $question->build($data); } function delete() { $questions = $this->getQuestions(); if (is_admin() ||$this->getCreator()==get_logged_in_user()->getId()) { if (parent::delete()) { foreach ($questions as $question) { $question->delete(); } return true; } } return false; } function getNewQuestion($id = false) { $question = new Question($id); $question->setParent($this->getId()); return $question; } function getQuestions() { $questions = array(); if ($this->getId() > 0) { foreach (query_rows("SELECT * FROM " . DB_PREFIX . "questions WHERE parent=".$this->getId()." ORDER BY position ASC") as $data) { $questions []= new Question($data->id, $data); } } return $questions; } function getQuestionsPerPage() { return $this->questions_per_page; } function setQuestionsPerPage($questions_per_page) { if (is_numeric($questions_per_page)) { $this->questions_per_page = $questions_per_page; } } function getQuestionsInTable() { return $this->questions_in_table; } function setQuestionsInTable($qit) { if ($qit) { $this->questions_in_table = 1; } else { $this->questions_in_table = 0; } } function repositionContents($positions) { $positionCounter = 1; foreach ($positions as $position_id) { query_update("UPDATE " . DB_PREFIX . "questions SET position=" . $positionCounter . " WHERE id=" . $position_id." AND parent=".$this->getId()); $positionCounter = $positionCounter + 1; } return true; } function buildFromXML($obj, $version=1, $folder=0) { } function getObjectsXML() { $xml = "getQuestionsPerPage()."]]>\n"; return $xml; } } ?>