Reset course on finish

Please add this code in your wp-content/plugins/wplms-customizer/wplms-customizer.php file :

add_action('wplms_submit_course','course_reset_on_finish',999,2);
function course_reset_on_finish($course_id,$user_id){
  bp_course_update_user_course_status($user_id,$course_id,0); // New function
      
  $course_curriculum = bp_course_get_curriculum($course_id);

  bp_course_update_user_progress($user_id,$course_id,0);

  //NEw drip feed use case
  delete_user_meta($user_id,'start_course_'.$course_id);
  do_action('wplms_student_course_reset',$course_id,$user_id);

  foreach($course_curriculum as $c){
    if(is_numeric($c)){
      if(bp_course_get_post_type($c) == 'quiz'){
                    
        bp_course_remove_user_quiz_status($user_id,$c);
        bp_course_reset_quiz_retakes($c,$user_id);

        $questions = bp_course_get_quiz_questions($c,$user_id);
        if(isset($questions) && is_array($questions) && is_Array($questions['ques'])){
          foreach($questions['ques'] as $question){
            global $wpdb;
            if(isset($question) && $question !='' && is_numeric($question)){
              bp_course_reset_question_marked_answer($c,$question,$user_id);
            }
          }
        }
      }else{
        bp_course_reset_unit($user_id,$c,$course_id);
      }
    }
  }
  
  /*=== Fix in 1.5 : Reset  Badges and CErtificates on Course Reset === */
  $user_badges = vibe_sanitize(get_user_meta($user_id,'badges',false));
  $user_certifications = vibe_sanitize(get_user_meta($user_id,'certificates',false));

  if(isset($user_badges) && is_Array($user_badges) && in_array($course_id,$user_badges)){
      $key=array_search($course_id,$user_badges);
      unset($user_badges[$key]);
      $user_badges = array_values($user_badges);
      update_user_meta($user_id,'badges',$user_badges);
  }

  if(isset($user_certifications) && is_Array($user_certifications) && in_array($course_id,$user_certifications)){
      $key=array_search($course_id,$user_certifications);
      unset($user_certifications[$key]);
      $user_certifications = array_values($user_certifications);

      global $wpdb;
      $certificate_name = 'certificate_'.$course_id.'_'.$user_id;
      $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_name = %s AND post_parent = %d AND post_author = %d",$certificate_name,$course_id,$user_id));
      if(is_numeric($attachment_id)){
        wp_delete_attachment($attachment_id);
      }
      
      update_user_meta($user_id,'certificates',$user_certifications);
  }
        /*==== End Fix ======*/

  echo '<p>'.__('Course Reset for User','vibe').'</p>';
    
  do_action('wplms_course_reset',$course_id,$user_id);
   
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *