Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function calculate_score_save_data_action( $post_id, $form_data ) {
- //the ID of "rate and Approve CRED Form"
- if ( $form_data['id'] == 632 ) {
- //get the current exam post
- $post = get_post($post_id);
- //get the current exams post id
- $post_in_loop_id = $post->ID;
- $post_author_id = get_post_field( 'post_author', $post_id );
- //get current exmas possible score (that numeric value is already set in the exam)
- $post_possible_exam_score = get_post_meta( $post_id, 'wpcf-possible-maximal-score-for-this-exam', true);
- // get all questions of this exam (childs of Exam)
- $all_questions = get_posts(array(
- 'numberposts' => -1,
- 'post_type' => 'question',
- 'meta_key' => '_wpcf_belongs_exam_id',
- 'meta_value' => $post_in_loop_id)
- );
- //if it returns some posts
- if( $all_questions ){
- //start the count on 0 for the scores sum later on (each question/answer has his "is" and "should" points)
- $single_question_possible_score_sum = 0;
- $single_question_real_score_sum = 0;
- //now get the single answers
- foreach( $all_questions as $single_question ){
- //get each questions post data
- $single_question_post = get_post($single_question);
- //get each ID
- $single_question_id = $single_question_post->ID;
- //get value of possible scores
- $single_question_possible_score_is = get_post_meta($single_question_id, 'wpcf-possible-maximum-score', true );
- //get value if real scores
- $single_question_real_score_is = get_post_meta($single_question_id, 'wpcf-real-maximum-score', true);
- //we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
- //Sum the possible scores of all questions
- $single_question_possible_score_sum+= $single_question_possible_score_is;
- //Sum the real scores of all questions
- $single_question_real_score_sum+= $single_question_real_score_is;
- }
- }
- //Update the Exams real score (total of all questions real score)
- update_post_meta( $post_id, 'wpcf-real-maximal-score-for-this-exam', $single_question_real_score_sum);
- //BELOW NEEDS TO BE FINSHED
- //calculate 60% of complete points
- //$percent_op_one = ($post_possible_exam_score / 100) * 60;
- //calculate the % of the reached points
- //$percent_op_two = ($single_question_real_score_sum * 100) / $post_possible_exam_score;
- //If reached %is greater or equal to the must (60%)
- //if ($percent_op_two >= $percent_op_one) {
- // Get author of current exam being reviewed (author of)
- //we already have it: $post_author_id
- //then do some magic and update the User Profile meta badges with the correct badge
- // $all_profiles = get_posts(array(
- // 'numberposts' => -1,
- // 'post_type' => 'profile',
- // 'author' => $post_author_id)
- //);
- //if it returns some posts
- //if( $all_profiles ){
- //foreach( $all_profiles as $single_profile ){
- // now check if the profiles custom field mathces the current exams $parent (real exam) slug (as exmaple wpml-basic)
- // if yes, update it to "checked"
- }
- }
- add_action('cred_save_data', 'calculate_score_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement