function selectAnswersFromDatabase() { // Gets the answers $sql_answers = "SELECT answer, user_id, question_id FROM ilmo_answers WHERE (SELECT ilmo_id FROM " . "ilmo_questions WHERE id=question_id) = " . $this->getId() . " ORDER BY " . "user_id"; // Gets queue $sql_queue = "SELECT id, confirmed FROM ilmo_users WHERE ilmo_id = " . $this->getId() . " ORDER BY id"; $result_answers = $this->database->doQuery($sql_answers); $result_queue = $this->database->doQuery($sql_queue); // If there is no answers, confirmed or unconfirmed, it is useless // to go further if ($result_queue->numRows() < 1) { $this->debugger->debug("No answers found", "selectAnswersFromDatabase"); $this->answers = array(); return; } else { $this->debugger->debug("Found <b>" . $result_queue->numRows() . "</b> confirmed or unconfirmed answers", "selectAnswersFromDatabase"); } // Let's make a table which includes information if the answer is // confirmed or not $confirmed = array(); while ($row = $result_queue->fetchRow(MDB2_FETCHMODE_ASSOC)) { $uid = $row["id"]; $confirmed[$uid] = CommonTools::sqlToBoolean($row["confirmed"]); } // And then lets get the answers $answers = array(); $answersByUser = array(); $previousUID = -1; while ($row = $result_answers->fetchRow(MDB2_FETCHMODE_ASSOC)) { if ($previousUID != -1 && $row["user_id"] != $previousUID) { $answers[$previousUID] = $answersByUser; $answersByUser = array(); } // Get parameters for Answer object's constructor $question = $this->getQuestionById($row["question_id"]); $answer = null; if ($question->getType() == "checkbox") { $answer = CommonTools::sqlToArray($row["answer"]); } else { $answer = $row["answer"]; } $userId = $row["user_id"]; $answersByUser[$question->getId()] = new Answer($answer, $userId, $question); $previousUID = $row["user_id"]; } // The last one // Do not add the last one if there is no confirmed answers at all if ($previousUID != -1) { $answers[$previousUID] = $answersByUser; } // Now the answers are in answers array. Let's put the unconfirmed // to the array too foreach ($confirmed as $user => $value) { if ($value == false) { $answers[$user] = null; } } // Because the unconfirmed answers have been inserted last the // array must be sorted by key ksort($answers); // Finally, lets put the positions to answers $position = 1; foreach ($answers as $key => $answerByUser) { if ($answerByUser != null) { $answers[$key] = new UserAnswers($answerByUser, $key, $position); } else { $answers[$key] = null; } $position++; } $this->answers = $answers; }