/** * 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); } } }
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; } }