Skip to content

Commit

Permalink
WPML: Shared student progress and quiz submission (#7492)
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Apr 9, 2024
1 parent 9410aa2 commit 7bba262
Show file tree
Hide file tree
Showing 33 changed files with 1,608 additions and 142 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-wpml-progress
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Share student progress and quiz submission across translations
17 changes: 15 additions & 2 deletions includes/blocks/class-sensei-block-take-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@ public function render_take_course_block( $attributes, $content ): string {
}

$course_id = $post->ID;
$html = '';

/**
* Filters the course ID for the take course block.
*
* @hook sensei_block_take_course_course_id
*
* @since $$next-version$$
*
* @param {int} $course_id The course ID.
* @return {int} The course ID.
*/
$course_id = apply_filters( 'sensei_block_take_course_course_id', $course_id );

$html = '';

if ( 'course' !== get_post_type( $course_id ) ) {
return '';
Expand Down Expand Up @@ -117,7 +130,7 @@ private function render_with_start_course_form( $course_id, $button ) {
*
* @return string The html with the added classes.
*/
private function add_button_classes( $button ) : string {
private function add_button_classes( $button ): string {
wp_enqueue_script( 'sensei-stop-double-submission' );

if ( preg_match( '/<button(.*)class="(.*)"/', $button ) ) {
Expand Down
1 change: 0 additions & 1 deletion includes/blocks/class-sensei-course-progress-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function register_block() {
public function render_course_progress( $attributes ): string {

$course_id = $attributes['postId'] ?? get_the_ID();

if ( 'course' !== get_post_type( $course_id ) ) {
return '';
}
Expand Down
38 changes: 24 additions & 14 deletions includes/class-sensei-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,19 @@ public static function get_course_settings_sidebar_vars() {
* @return bool
*/
public static function is_user_enrolled( $course_id, $user_id = null ) {

/**
* Filters the course ID for the `Sensei_Course::is_user_enrolled` method.
*
* @hook sensei_block_course_progress_course_id
*
* @since $$next-version$$
*
* @param {int} $course_id The course ID.
* @return {int} The course ID.
*/
$course_id = (int) apply_filters( 'sensei_course_is_user_enrolled_course_id', $course_id );

if ( empty( $course_id ) ) {
return false;
}
Expand Down Expand Up @@ -2362,7 +2375,7 @@ public static function get_all_courses() {
* Generate the course meter component
*
* @since 1.8.0
* @param int $progress_percentage 0 - 100
* @param int|float $progress_percentage 0 - 100
* @return string $progress_bar_html
*/
public function get_progress_meter( $progress_percentage ) {
Expand Down Expand Up @@ -2416,7 +2429,6 @@ public function get_progress_statement( $course_id, $user_id ) {
* @return {string} Filtered course completion statement.
*/
return apply_filters( 'sensei_course_completion_statement', $statement );

}

/**
Expand Down Expand Up @@ -2458,8 +2470,8 @@ public function get_progress_stats( int $course_id ): array {
/**
* Output the course progress statement
*
* @param $course_id
* @return void
* @param int $course_id The course ID.
* @param int $user_id The user ID.
*/
public function the_progress_statement( $course_id = 0, $user_id = 0 ) {
if ( empty( $course_id ) ) {
Expand Down Expand Up @@ -2515,8 +2527,8 @@ public function the_progress_meter( $course_id = 0, $user_id = 0 ) {
*
* @since 1.8.0
*
* @param int $course_id
* @param int $user_id
* @param int $course_id The course ID.
* @param int $user_id The user ID.
* @return array $completed_lesson_ids
*/
public function get_completed_lesson_ids( $course_id, $user_id = 0 ) {
Expand All @@ -2538,17 +2550,16 @@ public function get_completed_lesson_ids( $course_id, $user_id = 0 ) {
}

return $completed_lesson_ids;

}

/**
* Calculate the perceantage completed in the course
*
* @since 1.8.0
*
* @param int $course_id
* @param int $user_id
* @return int $percentage
* @param int $course_id The course ID.
* @param int $user_id The user ID.
* @return float Percentage of the course completed.
*/
public function get_completion_percentage( $course_id, $user_id = 0 ) {

Expand Down Expand Up @@ -2579,15 +2590,14 @@ public function get_completion_percentage( $course_id, $user_id = 0 ) {
* @return {float} Filtered percentage.
*/
return apply_filters( 'sensei_course_completion_percentage', $percentage, $course_id, $user_id );

}

/**
* Block email notifications for the specific courses
* that the user disabled the notifications.
* Block email notifications for the specific courses that the user disabled the notifications.
*
* @since 1.8.0
* @param $should_send
*
* @param bool $should_send Whether the email should be sent, initial value.
* @return bool
*/
public function block_notification_emails( $should_send ) {
Expand Down
130 changes: 77 additions & 53 deletions includes/class-sensei-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1206,69 +1206,93 @@ public function sensei_lesson_preview_title( $title = '', $id = 0 ) {
return $title;
} // sensei_lesson_preview_title

/**
* Start a course.
*/
public function sensei_course_start() {
global $post, $current_user;

// Handle user starting the course.
if (
is_singular( 'course' )
&& isset( $_POST['course_start'] )
&& wp_verify_nonce( $_POST['woothemes_sensei_start_course_noonce'], 'woothemes_sensei_start_course_noonce' )
&& Sensei_Course::can_current_user_manually_enrol( $post->ID )
&& Sensei_Course::is_prerequisite_complete( $post->ID )
&& ! post_password_required( $post->ID )
/**
* Filter the course ID for the course being started.
*
* @since $$next-version$$
*
* @hook sensei_course_start_course_id
*
* @param {int} $course_id Course post ID.
* @return {int} Filtered course ID.
*/
$course_id = (int) apply_filters( 'sensei_course_start_course_id', $post->ID ?? 0 );

$nonce = isset( $_POST['woothemes_sensei_start_course_noonce'] )
? sanitize_text_field( wp_unslash( (string) $_POST['woothemes_sensei_start_course_noonce'] ) )
: '';

if ( ! is_singular( 'course' )
|| ! isset( $_POST['course_start'] )
|| ! wp_verify_nonce( $nonce, 'woothemes_sensei_start_course_noonce' )
|| ! Sensei_Course::can_current_user_manually_enrol( $course_id )
|| ! Sensei_Course::is_prerequisite_complete( $course_id )
|| post_password_required( $course_id )
) {
return;
}

/**
* Lets providers give their own course sign-up handler.
*
* @since 3.0.0
*
* @hook sensei_frontend_learner_enrolment_handler
*
* @param {callback(int, int):bool} $handler Frontend enrolment handler. Takes two arguments: $user_id and $course_id. Returns `true` if successful; `false` if not.
* @param {int} $user_id User ID.
* @param {int} $course_id Course post ID.
* @return {callback(int, int):bool} Filtered handler.
*/
$learner_enrollment_handler = apply_filters( 'sensei_frontend_learner_enrolment_handler', [ $this, 'manually_enrol_learner' ], $current_user->ID, $post->ID );

$student_enrolled = false;
if ( is_callable( $learner_enrollment_handler ) ) {
$student_enrolled = call_user_func( $learner_enrollment_handler, $current_user->ID, $post->ID );
}
/**
* Lets providers give their own course sign-up handler.
*
* @since 3.0.0
*
* @hook sensei_frontend_learner_enrolment_handler
*
* @param {callback(int, int):bool} $handler Frontend enrolment handler. Takes two arguments: $user_id and $course_id. Returns `true` if successful; `false` if not.
* @param {int} $user_id User ID.
* @param {int} $course_id Course post ID.
* @return {callback(int, int):bool} Filtered handler.
*/
$learner_enrollment_handler = apply_filters(
'sensei_frontend_learner_enrolment_handler',
array( $this, 'manually_enrol_learner' ),
$current_user->ID,
$course_id
);

$this->data = new stdClass();
$this->data->is_user_taking_course = false;
if ( $student_enrolled ) {
$this->data->is_user_taking_course = true;
$student_enrolled = false;
if ( is_callable( $learner_enrollment_handler ) ) {
$student_enrolled = call_user_func( $learner_enrollment_handler, $current_user->ID, $course_id );
}

// Refresh page to avoid re-posting.
/**
* Filter the URL that students are redirected to after starting a course.
*
* @since 1.10.0
*
* @hook sensei_start_course_redirect_url
*
* @param {string|bool} $redirect_url URL to redirect students to after starting course. Return `false` to prevent redirect.
* @param {WP_Post} $post Post object for course.
* @return {string|bool} Filtered URL to redirect students to after starting course. Return `false` to prevent redirect.
*/
$redirect_url = apply_filters( 'sensei_start_course_redirect_url', get_permalink( $post->ID ), $post );
$this->data = new stdClass();
$this->data->is_user_taking_course = false;
if ( ! $student_enrolled ) {
return;
}

if ( 'publish' !== get_post_status( $post ) ) {
wp_safe_redirect( add_query_arg( 'draftcourse', 'true', $redirect_url ) );
exit();
}
if ( false !== $redirect_url ) {
?>
$this->data->is_user_taking_course = true;

<script type="text/javascript"> window.location = '<?php echo esc_url_raw( $redirect_url ); ?>'; </script>
// Refresh page to avoid re-posting.
/**
* Filter the URL that students are redirected to after starting a course.
*
* @since 1.10.0
*
* @hook sensei_start_course_redirect_url
*
* @param {string|bool} $redirect_url URL to redirect students to after starting course. Return `false` to prevent redirect.
* @param {WP_Post} $post Post object for course.
* @return {string|bool} Filtered URL to redirect students to after starting course. Return `false` to prevent redirect.
*/
$redirect_url = apply_filters( 'sensei_start_course_redirect_url', get_permalink( $post->ID ), $post );

if ( 'publish' !== get_post_status( $post ) ) {
wp_safe_redirect( add_query_arg( 'draftcourse', 'true', $redirect_url ) );
exit();
}

<?php
}
}
if ( false !== $redirect_url ) {
?>
<script type="text/javascript"> window.location = '<?php echo esc_url_raw( $redirect_url ); ?>'; </script>
<?php
}
}

Expand Down

0 comments on commit 7bba262

Please sign in to comment.