Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculate_score_save_data_action( $post_id, $form_data ) {
- //the ID of "rate and Approve CRED Form"
- if ( $form_data['id'] == 1205 ) {
- //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);
- }
- }
- add_action('cred_save_data', 'calculate_score_save_data_action',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement