public function save($quiz)
 {
     // first off, sanitize the whole submitted thang
     self::$quiz = $this->sanitize_array($quiz);
     // flatten it out to make it easier to work with
     self::$quiz = $this->flatten_quiz_array(self::$quiz);
     // Open a new response object
     self::$response_obj = new Enp_quiz_Save_quiz_Response();
     // setup the user_action response
     self::$response_obj->set_user_action_response(self::$quiz);
     // these are referenced a lot, so lets set a quick link up for them
     self::$user_action_action = self::$response_obj->get_user_action_action();
     self::$user_action_element = self::$response_obj->get_user_action_element();
     self::$user_action_details = self::$response_obj->get_user_action_details();
     // create our object
     self::$quiz_obj = new Enp_quiz_Quiz(self::$quiz['quiz_id']);
     // fill the quiz with all the values
     $this->prepare_submitted_quiz();
     // process questions
     $this->prepare_submitted_questions();
     // Check if we're allowed to save. If any glaring errors, return the errors here
     // TODO: Check to make sure we can save. If there are errors, just return to page!
     // Alrighty!
     // actually save the quiz
     $this->save_quiz();
     // if we are trying to move to preview, check
     // for error messages (like, not enough mc_options, no correct option set, no questions, etc...)
     if (self::$user_action_action === 'next') {
         // builds error messages if trying to preview quiz
         $quiz_obj_validate = new Enp_quiz_Quiz(self::$quiz['quiz_id']);
         $validate = self::$response_obj->validate_quiz_and_questions($quiz_obj_validate);
         // see if they're trying to publish the quiz
         if (self::$user_action_element === 'publish') {
             if ($validate === 'valid') {
                 // is the quiz already published?
                 if (self::$quiz_obj->get_quiz_status() !== 'published') {
                     // OK, it's good! Publish it!
                     $this->pdo_publish_quiz();
                     // now let's reset the data and responses on that quiz as well
                     // delete all the responses & reset the stats for the quiz and questions
                     $quiz_data = new Enp_quiz_Save_quiz_take_Quiz_data(self::$quiz_obj);
                     $quiz_data->delete_quiz_responses(self::$quiz_obj);
                 } else {
                     // don't worry about it, probably just clicked on the "embed" on the quiz preview/settings page
                 }
             }
         }
     }
     // check to see if we need to trigger a rescrape from Facebook to update the OG Tags
     $this->facebook_api_rescrape();
     // return the response to the user
     return self::$response_obj;
 }
 /**
  * if it's the first question, we need to save the initial quiz view
  * and question view. All other quiz and question view data is handled
  * by the response class
  * @param quiz object
  * @param question_id (to see if we're on the first question)
  */
 protected function save_initial_view_data()
 {
     if ($this->state === 'question') {
         // we're on a question. It might be the first one, let's find out!
         $question_ids = $this->quiz->get_questions();
         if ((int) $this->current_question_id === (int) $question_ids[0]) {
             // we're on the first question of the first quiz in the question state, so we can update the quiz views and first question view
             // save quiz view
             $quiz_data = new Enp_quiz_Save_quiz_take_Quiz_data($this->quiz);
             $quiz_data->update_quiz_views();
             // save question view
             $save_question_view = new Enp_quiz_Save_quiz_take_Question_view($this->current_question_id);
         }
     }
 }
 protected function update_quiz_data($data)
 {
     // if it doesn't match one of the states we need, go ahead and exit
     if (self::$return['state'] !== 'question_explanation' && self::$return['state'] !== 'quiz_end') {
         return false;
     }
     $quiz_data = new Enp_quiz_Save_quiz_take_Quiz_data(self::$quiz);
     $question_ids = self::$quiz->get_questions();
     // if the new returned state is question_explanation and the submitted question_id was the first question of the quiz, then someone Started the quiz.
     if (self::$return['state'] === 'question_explanation' && (int) $data['question_id'] === (int) $question_ids[0]) {
         $quiz_data->update_quiz_starts();
     } elseif (self::$return['state'] === 'quiz_end') {
         // update the response quiz table with the new score and state
         self::$save_response_quiz_obj->update_response_quiz_completed(self::$return);
         // if the new returnd state is quiz_end, then they finished the quiz
         // get their score
         $quiz_data->update_quiz_finishes(self::$return['quiz_end']['score']);
     }
 }
 /**
  * Connects to DB and increase the quiz finishes by one.
  * @param $response (array) data we'll be saving to the response table
  * @return builds and returns a response message
  */
 public function update_quiz_finishes($new_score)
 {
     // connect to PDO
     $pdo = new enp_quiz_Db();
     // Get our Parameters ready
     $params = array(':quiz_id' => self::$quiz->get_quiz_id(), ':new_score' => $new_score);
     // write our SQL statement
     $sql = "UPDATE " . $pdo->quiz_table . "\n                   SET  quiz_score_average = (quiz_finishes * quiz_score_average + :new_score) / (quiz_finishes + 1),\n                        quiz_finishes = quiz_finishes + 1\n                 WHERE  quiz_id = :quiz_id";
     // update the question finishes the database
     $stmt = $pdo->query($sql, $params);
     // success!
     if ($stmt !== false) {
         // set-up our response array
         $return = array('quiz_id' => self::$quiz->get_quiz_id(), 'status' => 'success', 'action' => 'update_quiz_finishes');
         // merge the response arrays
         self::$return = array_merge($return, self::$return);
         // see what type of question we're working on and save that response
     } else {
         // handle errors
         self::$return['error'][] = 'Update quiz finishes failed.';
     }
     // return response
     return self::$return;
 }