Created
June 19, 2015 09:57
-
-
Save woogist/731f421d8648342fb8dd to your computer and use it in GitHub Desktop.
To activate this you need to have Sensei installed and must be logged into admin. Next add the code below to your functions.php file. Then go to: yoursite.com/wp-admin?sensei_update_user_course_data=start
This file contains hidden or 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_action('admin_init', 'refresh_user_course_completion_data'); | |
function refresh_user_course_completion_data(){ | |
if( ! isset( $_GET['sensei_update_user_course_data'] ) ){ | |
return; | |
} | |
if( 'start'== $_GET['sensei_update_user_course_data'] ) { | |
//enqueue the scripts needed | |
wp_print_head_scripts(); | |
wp_print_footer_scripts(); | |
echo '<h3>Update user course statuses </h3>'; | |
$url = get_admin_url() . '?sensei_update_user_course_data=go'; | |
echo '<a class="button" href="'. $url . '" style=" background-color: green; color: white; padding: 0.5em 1em; | |
" >Go</h3>'; | |
} elseif( 'go'== $_GET['sensei_update_user_course_data'] ){ | |
echo '<h4>Updating all user course statuses...</h4>'; | |
echo '<h4>--------------------------------</h4>'; | |
update_all_user_course_statuses(); | |
echo '<h4>--------------------------------</h4>'; | |
echo '<h4>-- Update completed thank you --</h4>'; | |
}else{ | |
return; | |
} | |
die; | |
}// end refresh_user_course_completion_data | |
/** | |
* process all course sna update the users taking the courses status | |
*/ | |
function update_all_user_course_statuses( ){ | |
cust_print( ' Retrieving courses...' ); | |
$courses = Sensei()->course->get_all_courses(); | |
cust_print (' Retrieving all user taking these courses...'); | |
foreach( $courses as $course ){ | |
cust_print( 'Updating users for Course:'. $course->post_title ); | |
cust_print('>> retrieving course users...'); | |
//get users taking this course | |
$args = array( 'post_id' => $course->ID, 'type' => 'sensei_course_status' ); | |
$user_activities = WooThemes_Sensei_Utils::sensei_check_for_activity( $args , true); | |
//get user ids | |
$user_ids = array(); | |
if( !is_array( $user_activities ) ){ | |
$user_activities = array( $user_activities ); | |
} | |
foreach( $user_activities as $activity ){ | |
if( !get_userdata( $activity->user_id ) ){ | |
//ignore invalid users | |
continue; | |
} | |
$user_ids[] = $activity->user_id ; | |
} | |
if(empty( $user_ids )){ | |
continue; | |
cust_print('>> no users in taking this course'); | |
} | |
// lop through users and go through each of the lessons on the course to check if they have | |
// completed it | |
foreach( $user_ids as $user_id ){ | |
complete_user_course_when_all_lessons_complete( $course->ID, $user_id ); | |
} | |
} | |
}// end update_all_user_course_statuses | |
function complete_user_course_when_all_lessons_complete( $course_id , $user_id ){ | |
$user = get_userdata( $user_id ); | |
$message = '>> Processing '. $user->display_name . ' ... '; | |
$all_course_lessons_completed = true; | |
//get course lesson | |
$course_lessons = Sensei()->course->course_lessons( $course_id ); | |
foreach( $course_lessons as $lesson ){ | |
if( ! WooThemes_Sensei_Utils::user_completed_lesson( $lesson->ID, $user_id ) ){ | |
$all_course_lessons_completed = false; | |
break; | |
} | |
} | |
// what is the users current course completion status? | |
$current_user_completed_status = WooThemes_Sensei_Utils::user_completed_course($course_id, $user_id); | |
// update user status if the lessons are complete but the course is not | |
if( $all_course_lessons_completed && ! $current_user_completed_status ){ | |
WooThemes_Sensei_Utils::update_course_status( $user_id, $course_id, 'complete' ); | |
$message .= ' course completion updated'; | |
} | |
cust_print( $message ); | |
} | |
/** | |
* Custom printing function for this update script | |
* @param $string | |
* @param string $tag | |
*/ | |
function cust_print( $string, $tag="" ){ | |
if( empty( $tag ) ){ | |
$tag = 'p'; | |
} | |
echo '<'. $tag . '>'; | |
echo $string; | |
echo '</'. $tag . '>'; | |
} |
@dairdr If I'm reading this correctly, this is meant to be ran once to update course info. So, if you want, you can add it to either your parent theme's functions.php or your child's functions.php. Run it once by visiting the url mentioned above. And then you can delete the additional code.
Hope that helps!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which function.php file? in the theme function.php file?