Advertisement
bedas

Calculate scores

Sep 10th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2. function calculate_score_save_data_action( $post_id, $form_data ) {
  3.  
  4.     //the ID of "rate and Approve CRED Form"
  5.     if ( $form_data['id'] == 632 ) {
  6.  
  7.         //get the current exam post
  8.         $post = get_post($post_id);
  9.  
  10.         //get the current exams post id
  11.         $post_in_loop_id = $post->ID;
  12.         $post_author_id = get_post_field( 'post_author', $post_id );
  13.        
  14.  
  15.         //get current exmas possible score (that numeric value is already set in the exam)
  16.         $post_possible_exam_score = get_post_meta( $post_id, 'wpcf-possible-maximal-score-for-this-exam', true);
  17.  
  18.         // get all questions of this exam (childs of Exam)
  19.         $all_questions = get_posts(array(
  20.             'numberposts'   => -1,
  21.             'post_type'     => 'question',
  22.             'meta_key'      => '_wpcf_belongs_exam_id',
  23.             'meta_value'    => $post_in_loop_id)
  24.         );
  25.  
  26.         //if it returns some posts
  27.         if( $all_questions ){
  28.  
  29.             //start the count on 0 for the scores sum later on (each question/answer has his "is" and "should" points)
  30.             $single_question_possible_score_sum = 0;
  31.             $single_question_real_score_sum = 0;
  32.  
  33.             //now get the single answers
  34.             foreach( $all_questions as $single_question ){
  35.  
  36.                 //get each questions post data
  37.                 $single_question_post = get_post($single_question);
  38.                 //get each ID
  39.                 $single_question_id = $single_question_post->ID;
  40.  
  41.                 //get value of possible scores
  42.                 $single_question_possible_score_is = get_post_meta($single_question_id, 'wpcf-possible-maximum-score', true );
  43.                 //get value if real scores
  44.                 $single_question_real_score_is = get_post_meta($single_question_id, 'wpcf-real-maximum-score', true);
  45.                 //we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
  46.                 //Sum the possible scores of all questions
  47.                 $single_question_possible_score_sum+= $single_question_possible_score_is;
  48.                 //Sum the real scores of all questions
  49.                 $single_question_real_score_sum+= $single_question_real_score_is;          
  50.             }              
  51.         }
  52.  
  53.         //Update the Exams real score (total of all questions real score)
  54.         update_post_meta( $post_id, 'wpcf-real-maximal-score-for-this-exam', $single_question_real_score_sum);
  55.  
  56. //BELOW NEEDS TO BE FINSHED
  57.     //calculate 60% of complete points
  58.         //$percent_op_one = ($post_possible_exam_score / 100) * 60;
  59.     //calculate the % of the reached points
  60.         //$percent_op_two = ($single_question_real_score_sum * 100) / $post_possible_exam_score;
  61.     //If reached %is greater or equal to the must (60%)
  62.     //if ($percent_op_two >= $percent_op_one) {
  63.  
  64.         // Get author of current exam being reviewed (author of)
  65.             //we already have it: $post_author_id
  66.         //then do some magic and update the User Profile meta badges with the correct badge
  67.         // $all_profiles = get_posts(array(
  68.         //  'numberposts'   => -1,
  69.         //  'post_type'     => 'profile',
  70.         //  'author'        => $post_author_id)
  71.         //);
  72.             //if it returns some posts
  73.             //if( $all_profiles ){
  74.                 //foreach( $all_profiles as $single_profile ){
  75.                     // now check if the profiles custom field mathces the current exams $parent (real exam) slug (as exmaple wpml-basic)
  76.                     // if yes, update it to "checked"
  77.  
  78.  
  79.        
  80.     }
  81. }
  82. add_action('cred_save_data', 'calculate_score_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement