Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgk/2c1a8f2fddf08800d95ab857ce748060 to your computer and use it in GitHub Desktop.
Save pgk/2c1a8f2fddf08800d95ab857ce748060 to your computer and use it in GitHub Desktop.
<?php
//1. paste this class definition on top of includes/class-sensei-analysis-overview-list-table.php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Sensei_Update_All_User_Course_Completions {
public static function temporarily_disable_learner_emails( $emails_instance ) {
remove_action( 'sensei_user_quiz_grade', array( $emails_instance, 'learner_graded_quiz' ) );
remove_action( 'sensei_course_status_updated', array( $emails_instance, 'learner_completed_course' ) );
}
public static function run() {
global $wpdb;
// we disable learner emails for this request
add_action( 'sensei_emails', array( __CLASS__, 'temporarily_disable_learner_emails' ) );
do_action( 'sensei_emails', Sensei()->emails );
// get all poublished courses for this site
$all_published_courses = $wpdb->get_results("SELECT * FROM `" . $wpdb->prefix . "posts` WHERE `post_type` = 'course' AND `post_status` = 'publish'");
foreach ( $all_published_courses as $course ) {
$course_args = array(
'post_id' => $course->ID,
'type' => 'sensei_course_status',
'status' => 'any',
);
// get all course learners
$course_learners = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_course_learners', $course_args, $course ), true );
if ( empty( $course_learners ) ) {
continue;
}
foreach ( $course_learners as $learner ) {
if ( null === $learner || empty( $learner ) ) {
continue;
}
$learner_id = $learner->user_id;
if ( empty( $learner_id ) ) {
continue;
}
echo 'Updating Course stats ' . $course->ID . ' for Learner ' . $learner_id . '<br />';
Sensei_Utils::user_complete_course( $course->ID, $learner_id ); // update course stats
echo 'Done<br />';
}
}
}
}
// 2. Add the line below as the first statement within function Sensei_Analysis_Overview_List_Table::prepare_items():
// Sensei_Update_All_User_Course_Completions::run();
// 3. refresh the page
// (note that if there are MANY courses/users this might crash)
// 4. When done delete the changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment