function bp_course_quiz_auto_submit($quiz_id, $user_id)
{
    $quiz_auto_evaluate = get_post_meta($quiz_id, 'vibe_quiz_auto_evaluate', true);
    if (vibe_validate($quiz_auto_evaluate)) {
        // Auto Evaluate for Quiz Enabled, Quiz auto evaluate, autoevaluate
        $total_marks = 0;
        $questions = vibe_sanitize(get_post_meta($quiz_id, 'quiz_questions' . $user_id, false));
        if (!isset($questions) || !is_array($questions)) {
            // Fallback for Older versions
            $questions = vibe_sanitize(get_post_meta($quiz_id, 'vibe_quiz_questions', false));
        }
        if (count($questions)) {
            $sum = $max_sum = 0;
            foreach ($questions['ques'] as $key => $question) {
                // Grab all the Questions
                if (isset($question) && $question) {
                    $type = get_post_meta($question, 'vibe_question_type', true);
                    $auto_evaluate_question_types = vibe_get_option('auto_eval_question_type');
                    if (isset($auto_evaluate_question_types) && is_Array($auto_evaluate_question_types) && count($auto_evaluate_question_types)) {
                        // Validated
                    } else {
                        $auto_evaluate_question_types = array('single');
                    }
                    if (isset($type) && in_array($type, $auto_evaluate_question_types)) {
                        $correct_answer = get_post_meta($question, 'vibe_question_answer', true);
                        $comments_query = new WP_Comment_Query();
                        $comments = $comments_query->query(array('post_id' => $question, 'user_id' => $user_id, 'number' => 1, 'status' => 'approve'));
                        foreach ($comments as $comment) {
                            $comment->comment_content = trim($comment->comment_content, ',');
                            if ($comment->comment_content == $correct_answer) {
                                $marks = $questions['marks'][$key];
                                $total_marks = $total_marks + $marks;
                            } else {
                                if ($type == 'multiple') {
                                    if (!strlen($comment->comment_content)) {
                                        $marks = 0;
                                    } else {
                                        $marked_answers = explode(',', $comment->comment_content);
                                        if (!is_array($marks_answers)) {
                                            // Force Array Form
                                            $marks_answers = array($marks_answers);
                                        }
                                        $correct_answers = explode(',', $correct_answer);
                                        if (!is_array($correct_answers)) {
                                            // Force Array Form
                                            $correct_answers = array($correct_answers);
                                        }
                                        sort($marked_answers);
                                        sort($correct_answers);
                                        if (array_diff($marked_answers, $correct_answers) == array_diff($correct_answers, $marked_answers)) {
                                            $marks = $questions['marks'][$key];
                                            $total_marks = $total_marks + $marks;
                                        } else {
                                            $marks = apply_filters('wplms_incorrect_quiz_answer', 0, $comment->comment_content, $question);
                                        }
                                    }
                                } elseif ($type == 'smalltext' || $type == 'fillblank') {
                                    if (strpos($correct_answer, ',')) {
                                        $correct_answers_array = explode(',', $correct_answer);
                                        foreach ($correct_answers_array as $c_answer) {
                                            if (strtolower($c_answer) == strtolower($comment->comment_content)) {
                                                $marks = $questions['marks'][$key];
                                                $total_marks = $total_marks + $marks;
                                                break;
                                            }
                                        }
                                    }
                                } else {
                                    $marks = apply_filters('wplms_incorrect_quiz_answer', 0, $comment->comment_content, $question);
                                }
                            }
                            update_comment_meta($comment->comment_ID, 'marks', $marks);
                        }
                        //END-For
                    }
                }
            }
            if (update_post_meta($quiz_id, $user_id, $total_marks)) {
                $message = __('You\'ve obtained ', 'vibe') . $total_marks . __(' out of ', 'vibe') . array_sum($questions['marks']) . __(' in quiz ', 'vibe') . ' <a href="' . get_permalink($quiz_id) . '">' . get_the_title($quiz_id) . '</a>';
                $sender_id = get_post_field('post_author', $quiz_id);
                if (!is_numeric($sender_id)) {
                    $sender_id = get_current_user_id();
                }
                if (function_exists('messages_new_message')) {
                    messages_new_message(array('sender_id' => $sender_id, 'subject' => __('Quiz results available', 'vibe'), 'content' => $message, 'recipients' => $user_id));
                }
                $max_marks = array_sum($questions['marks']);
                $activity_id = bp_course_record_activity(array('action' => __('Quiz Auto Evaluated', 'vibe'), 'content' => __('Quiz ', 'vibe') . get_the_title($quiz_id) . __(' auto evaluated for student ', 'vibe') . bp_core_get_userlink($user_id) . __(' with marks ', 'vibe') . ' = ' . $total_marks . __('/', 'vibe') . $max_marks, 'type' => 'evaluate_quiz', 'primary_link' => get_permalink($quiz_id), 'item_id' => $quiz_id, 'secondary_item_id' => $user_id));
                bp_course_record_activity_meta(array('id' => $activity_id, 'meta_key' => 'instructor', 'meta_value' => get_post_field('post_author', $quiz_id)));
                do_action('badgeos_wplms_evaluate_quiz', $quiz_id, $total_marks, $user_id);
            }
        }
    } else {
        // End Auto evaluate and Send notification to instructor
        if (function_exists('messages_new_message')) {
            $instructor_id = get_post_field('post_author', $quiz_id);
            $course_id = get_post_meta($quiz_id, 'vibe_course', true);
            if (isset($course_id) && is_numeric($course_id)) {
                $quiz_link = '<a href="' . get_permalink($course_id) . '?action=admin&submissions">' . get_the_title($quiz_id) . '</a>';
            } else {
                $quiz_link = get_the_title($quiz_id);
            }
            $message = sprintf(__('Quiz %s submitted by student %s for evaluation', 'vibe'), $quiz_link, bp_core_get_userlink($user_id));
            messages_new_message(array('sender_id' => $user_id, 'subject' => __('Quiz submitted for evaluation', 'vibe'), 'content' => $message, 'recipients' => $instructor_id));
        }
    }
}
예제 #2
0
 function evaluate_assignment($assignment_id, $marks, $user_id, $max, $message_id)
 {
     $activity_id = bp_course_record_activity(array('action' => sprintf(__('Results available for assignment %s', 'vibe'), get_the_title($assignment_id)), 'content' => sprintf(__('Student %s got marks %s out of %s in assignment %s ', 'vibe'), bp_core_get_userlink($user_id), $marks, $max, get_the_title($assignment_id)), 'type' => 'evaluate_assignment', 'primary_link' => get_permalink($assignment_id), 'item_id' => $assignment_id, 'secondary_item_id' => $user_id));
     bp_course_record_activity_meta(array('id' => $activity_id, 'meta_key' => 'remarks', 'meta_value' => $message_id));
     do_action('badgeos_wplms_evaluate_assignment', $comment->comment_post_ID, $value, $comment->user_id);
 }