Advertisement
bedas

get_posts in cred_save

Jul 19th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. function calculate_score_save_data_action( $post_id, $form_data ) {
  2.  
  3.     //the ID of "rate and Approve CRED Form"
  4.     if ( $form_data['id'] == 1205 ) {
  5.  
  6.         //get the current exam post
  7.         $post = get_post($post_id);
  8.  
  9.         //get the current exams post id
  10.         $post_in_loop_id = $post->ID;
  11.         $post_author_id = get_post_field( 'post_author', $post_id );
  12.        
  13.  
  14.         //get current exmas possible score (that numeric value is already set in the exam)
  15.         $post_possible_exam_score = get_post_meta( $post_id, 'wpcf-possible-maximal-score-for-this-exam', true);
  16.  
  17.         // get all questions of this exam (childs of Exam)
  18.         $all_questions = get_posts(array(
  19.             'numberposts'   => -1,
  20.             'post_type'     => 'question',
  21.             'meta_key'      => '_wpcf_belongs_exam_id',
  22.             'meta_value'    => $post_in_loop_id)
  23.         );
  24.  
  25.         //if it returns some posts
  26.         if( $all_questions ){
  27.  
  28.             //start the count on 0 for the scores sum later on (each question/answer has his "is" and "should" points)
  29.             $single_question_possible_score_sum = 0;
  30.             $single_question_real_score_sum = 0;
  31.  
  32.             //now get the single answers
  33.             foreach( $all_questions as $single_question ){
  34.  
  35.                 //get each questions post data
  36.                 $single_question_post = get_post($single_question);
  37.                 //get each ID
  38.                 $single_question_id = $single_question_post->ID;
  39.  
  40.                 //get value of possible scores
  41.                 $single_question_possible_score_is = get_post_meta($single_question_id, 'wpcf-possible-maximum-score', true );
  42.                 //get value if real scores
  43.                 $single_question_real_score_is = get_post_meta($single_question_id, 'wpcf-real-maximum-score', true);
  44.                 //we need to sum this up BEFORE the if is closed and BEFORE the foreach is closed
  45.                 //Sum the possible scores of all questions
  46.                 $single_question_possible_score_sum+= $single_question_possible_score_is;
  47.                 //Sum the real scores of all questions
  48.                 $single_question_real_score_sum+= $single_question_real_score_is;          
  49.             }              
  50.         }
  51.  
  52.         //Update the Exams real score (total of all questions real score)
  53.         update_post_meta( $post_id, 'wpcf-real-maximal-score-for-this-exam', $single_question_real_score_sum);
  54.  
  55.        
  56.     }
  57. }
  58. add_action('cred_save_data', 'calculate_score_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement