public function total_percentage()
 {
     $question_id = mysql_real_escape_string($this->question->id);
     $percent = count($this->choices()) / count(SurveyAnswerChoice::find_all("survey_questions.id = '{$question_id}'"));
     $percent *= 100;
     return round($percent);
 }
 public function choices($reload = false)
 {
     if ($reload or !$this->choices_cache) {
         $id = mysql_real_escape_string($this->id);
         $this->choices_cache = SurveyAnswerChoice::find_all("survey_answers.id = '{$id}'");
     }
     return $this->choices_cache;
 }
 public function show($permalink = null)
 {
     if (isset($_GET['permalink'])) {
         $permalink = $_GET['permalink'];
     }
     $survey = Survey::find_by_permalink($permalink);
     if (!$survey or !$survey->active && Site::CurrentUser()->isAdmin() == 0) {
         Error404();
     }
     if ($survey->event->id) {
         $event_id = mysql_real_escape_string($survey->event_id);
         $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
         $result = EventSignup::find("events.id = '{$event_id}' AND users.id = '{$user_id}' AND event_signups.paid");
         if (!$result) {
             Site::Flash("error", "You must have attended {$survey->event->name} to take this survey");
             RedirectBack();
         }
     }
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     $survey_id = mysql_real_escape_string($survey->id);
     $response = SurveyResponse::find("surveys.id = '{$survey_id}' AND users.id = '{$user_id}'");
     if ($response) {
         Site::Flash("error", "You have already completed this survey");
         Redirect("surveys");
     }
     $response = new SurveyResponse();
     $response->survey = $survey;
     $response->survey_id = $survey->id;
     $response->user = Site::CurrentUser();
     $response->user_id = Site::CurrentUser()->id;
     $answers = array();
     $choices = array();
     $valid = true;
     $errors = array();
     if ($this->post) {
         foreach ($survey->questions() as $question) {
             $answer = new SurveyAnswer();
             $answer->survey_question_id = $question->id;
             if (in_array($question->type, array("sqtTextbox", "sqtTextArea"))) {
                 // Free-text input
                 if (isset($_POST['question'][$question->id])) {
                     $answer->value = $_POST['question'][$question->id];
                 } elseif (!$question->required) {
                     continue;
                 }
             } else {
                 if (isset($_POST['question'][$question->id])) {
                     $chosen = array();
                     if ($question->type == "sqtCheckbox") {
                         // Checkboxes
                         $chosen = $_POST['question'][$question->id];
                     } else {
                         // Radio/Select
                         $chosen = array($_POST['question'][$question->id]);
                     }
                     foreach ($chosen as $id) {
                         $option = SurveyQuestionOption::find_by_id($id);
                         if (!$option or $option->question->id != $question->id) {
                             if (!$question->required) {
                                 continue;
                             }
                             $valid = false;
                             $answer->add_error("Answer for question {$question->position} is invalid");
                             $errors[] = $question->id;
                             break;
                         }
                         $choice = new SurveyAnswerChoice();
                         $choice->survey_question_option_id = $option->id;
                         $choices[$question->id][$option->id] = $choice;
                     }
                 } elseif ($question->required) {
                     $valid = false;
                     $answer->add_error("You must enter an answer for question {$question->position}");
                     $errors[] = $question->id;
                 } else {
                     continue;
                 }
             }
             $result = $answer->validate();
             if (!$result) {
                 $valid = false;
             }
             $answers[$question->id] = $answer;
         }
         if ($valid) {
             // This is valid, let's save everything!
             if ($response->save()) {
                 foreach ($answers as $answer) {
                     $answer->survey_response_id = $response->id;
                     $answer->save();
                     $answers[$answer->survey_question_id] = $answer;
                 }
                 foreach ($choices as $question_id => $answer_choices) {
                     foreach ($answer_choices as $choice) {
                         $choice->survey_answer_id = $answers[$question_id]->id;
                         $choice->save();
                     }
                 }
                 $account = TwitterAccount::find_by_code('site');
                 if ($account) {
                     $message = "{$response->user->nickname} has completed the {$survey->name} Survey";
                     $account->add_tweet($message);
                 }
                 Redirect("surveys/{$survey->permalink}/complete");
             }
             $valid = false;
         }
     }
     $this->assign("answers", $answers);
     $this->assign("choices", $choices);
     $this->assign("valid", $valid);
     $this->assign("survey", $survey);
     $this->assign("response", $response);
     $this->assign("errors", $errors);
     $this->title = $survey->name;
     $this->render("survey/show.tpl");
 }