protected static function load_option($question, $id = null)
 {
     if (!$id) {
         $id = $_GET['id'];
     }
     $object = SurveyQuestionOption::find_by_id($id);
     if ($object and $object->survey_question_id == $question->id) {
         return $object;
     } else {
         Error404();
     }
 }
 /**
  * Adds an object to the instance pool.
  *
  * Propel keeps cached copies of objects in an instance pool when they are retrieved
  * from the database.  In some cases -- especially when you override doSelect*()
  * methods in your stub classes -- you may need to explicitly add objects
  * to the cache in order to ensure that the same objects are always returned by doSelect*()
  * and retrieveByPK*() calls.
  *
  * @param      SurveyQuestionOption $value A SurveyQuestionOption object.
  * @param      string $key (optional) key to use for instance map (for performance boost if key was already calculated externally).
  */
 public static function addInstanceToPool(SurveyQuestionOption $obj, $key = null)
 {
     if (Propel::isInstancePoolingEnabled()) {
         if ($key === null) {
             $key = (string) $obj->getQuestionOptionId();
         }
         // if key === null
         self::$instances[$key] = $obj;
     }
 }
 public function options($reload = false)
 {
     if ($reload or !$this->options_cache) {
         $id = mysql_real_escape_string($this->id);
         $this->options_cache = SurveyQuestionOption::find_all("survey_questions.id = '{$id}'", "survey_question_options.position ASC");
     }
     return $this->options_cache;
 }
 public static function load_from_row($row, $recurse_limit = 1, $current_level = 0)
 {
     // When PHP 5.3.0 is in, we can shift this over too and just redefine it
     // if needed (eg. loading child objects)
     $class = __CLASS__;
     $object = new $class();
     $fields = self::fields_array();
     $fields[] = array("created_at", "datetime");
     $fields[] = array("updated_at", "datetime");
     $fields[] = array("deleted", "bool");
     foreach ($fields as $field) {
         $property = $field[0];
         if (isset($field[2])) {
             $property = $field[2];
         }
         if (isset($row[self::table . ".{$field[0]}"])) {
             if ($field[1] == "datetime") {
                 $object->{$property} = strtotime($row[self::table . ".{$field[0]}"]);
             } else {
                 $object->{$property} = $row[self::table . ".{$field[0]}"];
             }
         } else {
             $object->{$property} = null;
         }
     }
     $object->is_new = false;
     // Load child objects here
     if ($current_level < $recurse_limit) {
         $current_level++;
         $object->survey = Survey::load_from_row($row, $recurse_limit, $current_level);
         $object->user = User::load_from_row($row, $recurse_limit, $current_level);
         $object->question = SurveyQuestion::load_from_row($row, $recurse_limit, $current_level);
         $object->response = SurveyResponse::load_from_row($row, $recurse_limit, $current_level);
         $object->answer = SurveyAnswer::load_from_row($row, $recurse_limit, $current_level);
         $object->option = SurveyQuestionOption::load_from_row($row, $recurse_limit, $current_level);
     }
     return $object;
 }
 /**
  * Exclude object from result
  *
  * @param     SurveyQuestionOption $surveyQuestionOption Object to remove from the list of results
  *
  * @return    SurveyQuestionOptionQuery The current query, for fluid interface
  */
 public function prune($surveyQuestionOption = null)
 {
     if ($surveyQuestionOption) {
         $this->addUsingAlias(SurveyQuestionOptionPeer::QUESTION_OPTION_ID, $surveyQuestionOption->getQuestionOptionId(), Criteria::NOT_EQUAL);
     }
     return $this;
 }
 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");
 }