Esempio n. 1
0
 /**
  * Update question answers to use new data structure
  *
  * @since 1.3.0
  * @access public
  * @return boolean
  */
 public function update_question_answer_data($n = 50, $offset = 0)
 {
     // Get Total Number of Updates to run
     $quiz_count_object = wp_count_posts('quiz');
     $quiz_count_published = $quiz_count_object->publish;
     // Calculate if this is the last page
     if (0 == $offset) {
         $current_page = 1;
     } else {
         $current_page = intval($offset / $n);
     }
     // End If Statement
     $total_pages = intval($quiz_count_published / $n);
     $args = array('post_type' => 'quiz', 'posts_per_page' => $n, 'offset' => $offset, 'post_status' => 'publish', 'suppress_filters' => 0);
     $quizzes = get_posts($args);
     $old_answers = array();
     $right_answers = array();
     $old_user_answers = array();
     if (is_array($quizzes)) {
         foreach ($quizzes as $quiz) {
             $quiz_id = $quiz->ID;
             // Get current user answers
             $comments = Sensei_Utils::sensei_check_for_activity(array('post_id' => $quiz_id, 'type' => 'sensei_quiz_answers'), true);
             // Need to always return an array, even with only 1 item
             if (!is_array($comments)) {
                 $comments = array($comments);
             }
             foreach ($comments as $comment) {
                 $user_id = $comment->user_id;
                 $content = maybe_unserialize(base64_decode($comment->comment_content));
                 $old_user_answers[$quiz_id][$user_id] = $content;
             }
             // Get correct answers
             $questions = Sensei_Utils::sensei_get_quiz_questions($quiz_id);
             if (is_array($questions)) {
                 foreach ($questions as $question) {
                     $right_answer = get_post_meta($question->ID, '_question_right_answer', true);
                     $right_answers[$quiz_id][$question->ID] = $right_answer;
                 }
             }
         }
     }
     if (is_array($right_answers)) {
         foreach ($right_answers as $quiz_id => $question) {
             $count = 0;
             if (is_array($question)) {
                 foreach ($question as $question_id => $answer) {
                     ++$count;
                     if (isset($old_user_answers[$quiz_id])) {
                         $answers_linkup[$quiz_id][$count] = $question_id;
                     }
                 }
             }
         }
     }
     if (is_array($old_user_answers)) {
         foreach ($old_user_answers as $quiz_id => $user_answers) {
             foreach ($user_answers as $user_id => $answers) {
                 foreach ($answers as $answer_id => $user_answer) {
                     $question_id = $answers_linkup[$quiz_id][$answer_id];
                     $new_user_answers[$question_id] = $user_answer;
                     Sensei_Utils::sensei_grade_question_auto($question_id, '', $user_answer, $user_id);
                 }
                 $lesson_id = get_post_meta($quiz_id, '_quiz_lesson', true);
                 Sensei_Utils::sensei_start_lesson($lesson_id, $user_id);
                 Sensei_Utils::sensei_save_quiz_answers($new_user_answers, $user_id);
             }
         }
     }
     if ($current_page == $total_pages) {
         return true;
     } else {
         return false;
     }
     // End If Statement
 }