-
-
Save 99manish/7926f77c1488f7316618f419f94b788a to your computer and use it in GitHub Desktop.
Open last viewed unit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('wplms_get_course_unfinished_unit','load_unit',99999); | |
add_filter('wplms_get_course_unfinished_unit_key','unit_key',99999,3); | |
add_action('wp_footer','script_to_save_last_unit'); | |
add_action('wp_ajax_save_last_viewed_unit','save_last_viewed_unit' ); | |
function script_to_save_last_unit(){ | |
//checking its course status page or not | |
$page_id = vibe_get_option('take_course_page'); | |
if(!is_page($page_id)) | |
return; | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$('.unit_content').on('unit_traverse',function(){ | |
var unit_id = $('.unit_line.active'); | |
var course_id = $("#course_id").val(); | |
if(typeof unit_id !== 'udefined' && unit_id){ | |
var last_unit = unit_id.find('a').data('unit'); | |
//make ajax hit to store this current unit id for the user in usermeta for this course | |
//meta_key: current_unit.{course_id} meta_value: unit_id | |
$.ajax({ | |
type: "POST", | |
url: ajaxurl, | |
data: { action: 'save_last_viewed_unit', | |
unit_id:last_unit, | |
course_id:course_id, | |
hash: $('#hash').val(), | |
}, | |
cache: false, | |
success: function(response){ | |
console.log(response); | |
} | |
}); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
function save_last_viewed_unit(){ | |
if ( !isset($_POST['hash']) || !wp_verify_nonce($_POST['hash'],'security') || !is_numeric($_POST['unit_id']) || !is_numeric($_POST['course_id'])){ | |
_e('Security check Failed. Contact Administrator.','wplms-last-unit'); | |
die(); | |
} | |
$user_id = get_current_user_id(); | |
$meta_key = 'current_unit_'.$_POST['course_id']; | |
$meta_value = $_POST['unit_id']; | |
update_user_meta( $user_id, $meta_key, $meta_value); | |
die(); | |
} | |
function load_unit($unit_id){ | |
if(!empty($_POST['load_unit'])) | |
return $unit_id; | |
$user_id = get_current_user_id(); | |
$course_id = $_POST['course_id']; | |
$last_viewed_unit = get_user_meta($user_id, 'current_unit_'.$course_id, true); | |
//echo $last_viewed_unit;die; | |
if(empty($last_viewed_unit)) | |
return $unit_id; | |
$units = bp_course_get_curriculum_units($course_id); | |
if(in_array($last_viewed_unit,$units)){ | |
return $uid; | |
} | |
return $unit_id; | |
} | |
function unit_key($key,$unit_id,$course_id){ | |
if(!empty($_POST['load_unit'])) | |
return $key; | |
$user_id = get_current_user_id(); | |
$last_viewed_unit = get_user_meta($user_id, 'current_unit_'.$course_id, true); | |
if(!empty($last_viewed_unit)) { | |
$unit_id = $last_viewed_unit; | |
} | |
$units = bp_course_get_curriculum_units($course_id); | |
$key = array_search($unit_id,$units); | |
$key++; | |
return $key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment