Ejemplo n.º 1
0
 /**
  * Grabs all quizzes and resaves them so they get rebuilt in the DB with any new
  * options, etc
  */
 public function resave_quizzes()
 {
     // get all the quizzes
     $quiz_ids = $this->get_all_quiz_ids();
     // PDO to grab ALL unique Quiz IDs
     $save_quiz = new Enp_quiz_Save_quiz();
     // query to get ALL quizzes
     // for now,  just do one
     if (!empty($quiz_ids)) {
         foreach ($quiz_ids as $quiz_id) {
             $quiz = new Enp_quiz_Quiz($quiz_id);
             // turn it into an array
             $quiz = (array) $quiz;
             // we have to set this as the quiz created by value so it will allow us to update
             // in the future, we can set it to an admin value once we allow that
             $quiz['quiz_updated_by'] = $quiz['quiz_owner'];
             $quiz['quiz_updated_at'] = date("Y-m-d H:i:s");
             // save the quiz
             $save_quiz->save($quiz);
         }
     }
 }
    /**
     * The quiz they submit isn't in the exact format we want it in
     * so we need to reindex it to a nice format and set their values
     *
     * @param $quiz = array() in this format:
     *        $quiz = array(
     *            'quiz' => $_POST['enp_quiz'], // array of quiz values (quiz_id, quiz_title, etc)
     *            'questions' => $_POST['enp_question'], // array of questions
     *            'quiz_updated_by' => $user_id,
     *            'quiz_updated_at' => $date_time,
     *        );
     * @return nicely formatted and value validated quiz array ready for saving
     */
    protected function prepare_submitted_quiz()
    {
        $quiz_id = $this->set_quiz_value('quiz_id', 0);
        $quiz_title = $this->set_quiz_value('quiz_title', '');
        $quiz_status = $this->set_quiz_value('quiz_status', 'draft');
        $quiz_finish_message = $this->set_quiz_value('quiz_finish_message', 'Thanks for taking our quiz!');
        $quiz_updated_by = $this->set_quiz_value('quiz_updated_by', 0);
        $quiz_updated_at = $this->set_quiz_value('quiz_updated_at', date("Y-m-d H:i:s"));
        $quiz_owner = $this->set_quiz_value__object_first('quiz_owner', $quiz_updated_by);
        $quiz_created_by = $this->set_quiz_value__object_first('quiz_created_by', $quiz_updated_by);
        $quiz_created_at = $this->set_quiz_value('quiz_created_at', $quiz_updated_at);
        $quiz_is_deleted = $this->set_quiz_is_deleted();
        // Options
        $quiz_title_display = $this->set_quiz_value('quiz_title_display', 'show');
        $quiz_mc_options_order = $this->set_quiz_value('quiz_mc_options_order', 'random');
        $quiz_width = $this->set_quiz_css_measurement_value('quiz_width', '100%');
        $quiz_bg_color = $this->set_quiz_hex_value('quiz_bg_color', '#ffffff');
        $quiz_text_color = $this->set_quiz_hex_value('quiz_text_color', '#444444');
        $quiz_border_color = $this->set_quiz_hex_value('quiz_border_color', '#dddddd');
        $quiz_button_color = $this->set_quiz_hex_value('quiz_button_color', '#5887C0');
        $quiz_correct_color = $this->set_quiz_hex_value('quiz_correct_color', '#3bb275');
        $quiz_incorrect_color = $this->set_quiz_hex_value('quiz_incorrect_color', '#f14021');
        // custom css
        $quiz_custom_css = $this->set_quiz_css_value('quiz_custom_css', '', false);
        $quiz_custom_css_minified = $this->optimize_css($quiz_custom_css);
        // facebook
        $facebook_title = $this->set_quiz_value('facebook_title', $quiz_title);
        $facebook_description = $this->set_quiz_value('facebook_description', 'How well can you do?');
        $facebook_quote_end = $this->set_quiz_value('facebook_quote_end', 'I got {{score_percentage}}% right.');
        // email
        $email_subject = $facebook_title;
        $email_body_start = $facebook_description;
        $email_body_end = $facebook_quote_end . '

' . $facebook_description;
        // twitter
        $include_url = true;
        $replace_mustache = true;
        $tweet_end = $this->set_tweet_value('tweet_end', 'I got {{score_percentage}}% right on this quiz. How many can you get right?', $include_url, $replace_mustache);
        $default_quiz = array('quiz_id' => $quiz_id, 'quiz_title' => $quiz_title, 'quiz_status' => $quiz_status, 'quiz_finish_message' => $quiz_finish_message, 'quiz_owner' => $quiz_owner, 'quiz_created_by' => $quiz_created_by, 'quiz_created_at' => $quiz_created_at, 'quiz_updated_by' => $quiz_updated_by, 'quiz_updated_at' => $quiz_updated_at, 'quiz_is_deleted' => $quiz_is_deleted, 'quiz_title_display' => $quiz_title_display, 'quiz_mc_options_order' => $quiz_mc_options_order, 'quiz_width' => $quiz_width, 'quiz_bg_color' => $quiz_bg_color, 'quiz_text_color' => $quiz_text_color, 'quiz_border_color' => $quiz_border_color, 'quiz_button_color' => $quiz_button_color, 'quiz_correct_color' => $quiz_correct_color, 'quiz_incorrect_color' => $quiz_incorrect_color, 'quiz_custom_css' => $quiz_custom_css, 'quiz_custom_css_minified' => $quiz_custom_css_minified, 'facebook_title' => $facebook_title, 'facebook_description' => $facebook_description, 'facebook_quote_end' => $facebook_quote_end, 'email_subject' => $email_subject, 'email_body_start' => $email_body_start, 'email_body_end' => $email_body_end, 'tweet_end' => $tweet_end);
        // We don't want to lose anything that was in the sent quiz (like questions, etc)
        // so we'll merge them to make sure we don't lose anything
        self::$quiz = array_merge(self::$quiz, $default_quiz);
    }
 public function save_quiz()
 {
     // make sure they're logged in and own this quiz
     // returns current_user_id if valid
     $user_id = $this->validate_user();
     $params = array();
     if (isset($_POST['enp_quiz'])) {
         $posted_quiz = $_POST['enp_quiz'];
         if (isset($posted_quiz['new_quiz'])) {
             $new_quiz_flag = $posted_quiz['new_quiz'];
         } else {
             $new_quiz_flag = '0';
         }
     }
     if (isset($_POST['enp_question'])) {
         $posted_question = $_POST['enp_question'];
     }
     if (isset($_POST['enp-quiz-submit'])) {
         $posted_user_action = $_POST['enp-quiz-submit'];
     }
     if (isset($_POST['enp_quiz_nonce'])) {
         $posted_nonce = $_POST['enp_quiz_nonce'];
     }
     //Is it a POST request?
     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         //Validate the form key
         if (!isset($posted_nonce) || !self::$nonce->validate($posted_nonce)) {
             // Form key is invalid,
             // return them to the page (they're probably refreshing the page)
             self::$message['error'][] = 'Quiz was not resaved';
             return false;
         }
     }
     // initiate the save_quiz object
     // get access to wpdb
     global $wpdb;
     // set-up an empty array for our quiz submission
     $quiz = array();
     // extract values
     // set the date_time to pass
     $date_time = date("Y-m-d H:i:s");
     // build our array to save
     if (isset($posted_quiz)) {
         $quiz = array('quiz' => $posted_quiz, 'quiz_updated_by' => $user_id, 'quiz_updated_at' => $date_time);
     }
     if (isset($posted_question) && !empty($posted_question)) {
         $quiz['question'] = $posted_question;
     }
     if (isset($posted_user_action) && !empty($posted_user_action)) {
         // get the value of the button they clicked
         $quiz['user_action'] = $posted_user_action;
     } else {
         // no submit button clicked? Should never happen
         self::$message['error'][] = 'The form was not submitted right. Please contact our support and let them know how you reached this error';
         return false;
     }
     // initiate the save_quiz object
     $save_quiz = new Enp_quiz_Save_quiz();
     // save the quiz by passing our $quiz array to the save function
     $response = $save_quiz->save($quiz);
     // set it as our messages to return to the user
     self::$message = $this->set_message($response);
     // get the ID of the quiz that was just created (if there)
     $quiz_id = $response->quiz_id;
     // set-up vars for our next steps
     $save_action = $response->action;
     // set the user_action so we know what the user was wanting to do
     self::$user_action = $response->user_action;
     // check to see if we have a successful save response from the save class
     // REMEMBER: A successful save can still have an error message
     // such as "Quiz Updated. Hey! You don't have any questions though!"
     if ($response->status !== 'success') {
         // No successful save, so return them to the same page and display error messages
         return false;
     }
     //*************************//
     //  SUCCESS! Now what...?  //
     //*************************//
     if (defined('DOING_AJAX') && DOING_AJAX) {
         $json_response = $response;
         $json_response = json_encode($json_response);
         wp_send_json($json_response);
         // always end ajax with exit()
         exit;
     } elseif (self::$user_action['action'] === 'next' && self::$user_action['element'] === 'preview' && empty(self::$message['error'])) {
         // unset the cookies for the current quiz
         // in case they deleted questions and just in general
         // to make it feel as expected (starting quiz from beginning)
         $preview_quiz = new Enp_quiz_Quiz($quiz_id);
         $this->unset_quiz_take_cookies($preview_quiz);
         $this->redirect_to_quiz_preview($quiz_id);
     } elseif (self::$user_action['action'] === 'next' && self::$user_action['element'] === 'publish' && empty(self::$message['error'])) {
         // unset the cookies for the current quiz
         $published_quiz = new Enp_quiz_Quiz($quiz_id);
         $this->unset_quiz_take_cookies($published_quiz);
         // redirect to the quiz publish page
         $this->redirect_to_quiz_publish($quiz_id);
     } elseif ($save_action === 'insert' || $new_quiz_flag === '1') {
         // they don't want to move on yet, but they're inserting,
         // so we need to send them to their newly created quiz create page
         $this->redirect_to_quiz_create($quiz_id);
     } else {
         // we have errors! Oh no! Send them back to fix it
         return false;
     }
 }