function learn_press_submit_answer()
{
    _deprecated_function('learn_press_submit_answer', '0.9.15', false);
    $quiz_id = !empty($_REQUEST['quiz_id']) ? intval($_REQUEST['quiz_id']) : 0;
    $question_id = !empty($_REQUEST['question_id']) ? intval($_REQUEST['question_id']) : 0;
    $next_id = !empty($_REQUEST['next_id']) ? intval($_REQUEST['next_id']) : learn_press_get_next_question($quiz_id, $question_id);
    $question_answer = isset($_REQUEST['question_answer']) ? $_REQUEST['question_answer'] : null;
    $finish = isset($_REQUEST['finish']) ? $_REQUEST['finish'] : null;
    $user_id = get_current_user_id();
    $json = array();
    $ques = lpr_get_question($question_id);
    if ($ques) {
        $ques->submit_answer($quiz_id, $question_answer);
    }
    ob_start();
    if ($next_id) {
        do_action('learn_press_submit_answer', $question_answer, $question_id, $quiz_id, $user_id, false);
        learn_press_get_template('quiz/form-question.php', array('question_id' => $next_id, 'course_id' => learn_press_get_course_by_quiz($quiz_id)));
    } else {
        $question_ids = learn_press_get_user_quiz_questions($quiz_id, $user_id);
        $quiz_completed = get_user_meta($user_id, '_lpr_quiz_completed', true);
        $quiz_completed[$quiz_id] = current_time('timestamp');
        update_user_meta($user_id, '_lpr_quiz_completed', $quiz_completed);
        $course_id = learn_press_get_course_by_quiz($quiz_id);
        if (!learn_press_user_has_finished_course($course_id)) {
            if (learn_press_user_has_completed_all_parts($course_id, $user_id)) {
                learn_press_finish_course($course_id, $user_id);
            }
        }
        learn_press_get_template('quiz/result.php');
        $json['quiz_completed'] = true;
        do_action('learn_press_submit_answer', $question_answer, $question_id, $quiz_id, $user_id, true);
    }
    $output = ob_get_clean();
    if (defined('DOING_AJAX') && DOING_AJAX) {
        $json['html'] = $output;
        $json['redirect'] = apply_filters('learn_press_submit_answer_redirect_url', get_the_permalink($quiz_id), $question_answer, $question_id, $quiz_id, $user_id);
        learn_press_send_json($json);
    }
}
示例#2
0
 function finish_quiz($quiz_id)
 {
     $quiz = LP_Quiz::get_quiz($quiz_id);
     if (!$quiz) {
         return;
     }
     $user_id = $this->id;
     $time = current_time('timestamp');
     $quiz_start = get_user_meta($user_id, '_lpr_quiz_start_time', true);
     $quiz_completed = get_user_meta($user_id, '_lpr_quiz_completed', true);
     $quiz_duration = absint(get_post_meta($quiz_id, '_lpr_duration', true)) * 60;
     if ($time - $quiz_start[$quiz_id] > $quiz_duration) {
         $time = $quiz_start[$quiz_id] - $quiz_duration;
     }
     $quiz_completed[$quiz_id] = $time;
     update_user_meta($user_id, '_lpr_quiz_completed', $quiz_completed);
     $course_id = learn_press_get_course_by_quiz($quiz_id);
     if (!learn_press_user_has_finished_course($course_id)) {
         if (learn_press_user_has_completed_all_parts($course_id, $user_id)) {
             learn_press_finish_course($course_id, $user_id);
         }
     }
     do_action('learn_press_finish_quiz', $quiz_id, $user_id);
 }
示例#3
0
 /**
  * Finish course
  */
 public static function finish_course()
 {
     $user_id = get_current_user_id();
     $course_id = !empty($_POST['course_id']) ? $_POST['course_id'] : 0;
     if (!$user_id || !$course_id) {
         wp_die(__('Access denied!', 'learn_press'));
     }
     $finish = false;
     $json = array('finish' => true);
     $assessment = get_post_meta($course_id, '_lpr_course_final', true);
     $pass = floatval(get_post_meta($course_id, '_lpr_course_condition', true));
     if ($assessment == 'yes') {
         $final_quiz = lpr_get_final_quiz($course_id);
         $final_result = learn_press_get_quiz_result($user_id, $final_quiz);
         // lpr_get_quiz_result( $final_quiz );
         if (!empty($final_result) && !empty($final_result['mark_percent']) && $final_result['mark_percent'] * 100 >= $pass) {
             $finish = true;
         }
     } else {
         $progress = lpr_course_evaluation($course_id);
         if ($progress >= $pass) {
             $finish = true;
         }
     }
     if ($finish) {
         learn_press_finish_course($course_id, $user_id);
         $json['message'] = __('Congratulation ! You have finished this course', 'learn_press');
     } else {
         $json['finish'] = false;
         $json['message'] = __('Sorry! You can not finish this course now', 'learn_press');
     }
     wp_send_json($json);
     die;
 }
/**
 * Auto eval the result of the course and mark it as finished if the time is over
 */
function learn_press_auto_evaluation_course()
{
    $user_id = get_current_user_id();
    $courses = learn_press_get_user_courses($user_id);
    if (!$courses) {
        return;
    }
    $now = time();
    foreach ($courses as $course_id) {
        if (learn_press_user_has_finished_course($course_id)) {
            continue;
        }
        $course_duration = intval(get_post_meta($course_id, '_lpr_course_duration', true)) * 7 * 24 * 3600;
        $course_time = get_user_meta($user_id, '_lpr_course_time', true);
        if (empty($course_time[$course_id])) {
            $course_time[$course_id] = array('start' => time(), 'end' => null);
            update_user_meta($user_id, '_lpr_course_time', $course_time);
        }
        $course_time = $course_time[$course_id];
        $start_time = intval($course_time['start']);
        if ($course_duration && $start_time + $course_duration <= $now) {
            learn_press_finish_course($course_id, $user_id);
        } else {
            //echo "Time to finish: " . ( ( $start_time + $course_duration - $now ) / ( 7 * 24 * 3600 ) );
        }
    }
}
function learn_press_submit_answer()
{
    $quiz_id = !empty($_REQUEST['quiz_id']) ? intval($_REQUEST['quiz_id']) : 0;
    $question_id = !empty($_REQUEST['question_id']) ? intval($_REQUEST['question_id']) : 0;
    $next_id = !empty($_REQUEST['next_id']) ? intval($_REQUEST['next_id']) : 0;
    $question_answer = isset($_REQUEST['question_answer']) ? $_REQUEST['question_answer'] : null;
    $finish = isset($_REQUEST['finish']) ? $_REQUEST['finish'] : null;
    $user_id = get_current_user_id();
    $json = array();
    ob_start();
    $ques = lpr_get_question($question_id);
    if ($ques) {
        $ques->submit_answer($quiz_id, $question_answer);
    }
    if ($next_id) {
        /*$ques = lpr_get_question( $next_id );
        
                if( $ques ){
                    $quiz_answers = learn_press_get_question_answers(null, $quiz_id );
                    $ques->render( array(
                        'answer' => isset( $quiz_answers[$next_id] ) ? $quiz_answers[$next_id] : null
                    ));
                }*/
        learn_press_get_template('quiz/form-question.php', array('question_id' => $next_id, 'course_id' => learn_press_get_course_by_quiz($quiz_id)));
    } else {
        $question_ids = learn_press_get_user_quiz_questions($quiz_id, $user_id);
        $quiz_completed = get_user_meta($user_id, '_lpr_quiz_completed', true);
        $quiz_completed[$quiz_id] = time();
        update_user_meta($user_id, '_lpr_quiz_completed', $quiz_completed);
        $course_id = learn_press_get_course_by_quiz($quiz_id);
        if (!learn_press_user_has_finished_course($course_id)) {
            if (learn_press_user_has_completed_all_parts($course_id, $user_id)) {
                learn_press_finish_course($course_id, $user_id);
            }
        }
        learn_press_get_template('quiz/result.php');
        $json['quiz_completed'] = true;
    }
    $json['html'] = ob_get_clean();
    wp_send_json($json);
    die;
}