Skip to content

Commit

Permalink
Update find methods to work with multiple values in filters
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Sep 27, 2023
1 parent 7d689f9 commit 1ae868e
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 96 deletions.
8 changes: 1 addition & 7 deletions includes/class-sensei-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,7 @@ public function sensei_course_preview_titles( $title, $lesson_id ) {
$title_text = '';

if ( method_exists( 'Sensei_Utils', 'is_preview_lesson' ) && Sensei_Utils::is_preview_lesson( $lesson_id ) ) {
$is_user_taking_course = Sensei_Utils::sensei_check_for_activity(
array(
'post_id' => $course_id,
'user_id' => $current_user->ID,
'type' => 'sensei_course_status',
)
);
$is_user_taking_course = Sensei()->course_progress_repository->has( $course_id, $current_user->ID );
if ( ! $is_user_taking_course ) {
if ( method_exists( 'Sensei_Frontend', 'sensei_lesson_preview_title_text' ) ) {
$title_text = Sensei()->frontend->sensei_lesson_preview_title_text( $course_id );
Expand Down
22 changes: 21 additions & 1 deletion includes/class-sensei-quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ public function update_after_lesson_change( $post_id ) {
wp_update_post( $my_post );
}


/**
* Get the lesson this quiz belongs to.
*
Expand All @@ -255,6 +254,27 @@ public function get_lesson_id( $quiz_id = null ) {

}

/**
* Get lesson ids for given quizzes.
*
* @since $$next-version$$
*
* @param int[] $quiz_ids The quiz IDs.
* @return array Lesson ids, empty array if no lessons found.
*/
public function get_lesson_ids( array $quiz_ids ) {
$quiz_parents = get_posts(
array(
'fields' => 'post_parent',
'post_type' => 'quiz',
'post__in' => $quiz_ids,
'posts_per_page' => -1,
)
);

return wp_list_pluck( $quiz_parents, 'post_parent' );
}

/**
* This function hooks into the quiz page and accepts the answer form save post.
*
Expand Down
38 changes: 18 additions & 20 deletions includes/class-sensei-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public static function sensei_log_activity( $args = array() ) {
}
}


/**
* Check for Sensei activity.
*
Expand Down Expand Up @@ -2818,29 +2817,28 @@ public static function get_target_page_post_id_for_continue_url( $course_id, $us
return $course_id;
}
// First try to get the lesson the user started or updated last.
$activity_args = [
'post__in' => $course_lessons,
'user_id' => $user_id,
'type' => 'sensei_lesson_status',
'number' => 1,
'orderby' => 'comment_date',
'order' => 'DESC',
'status' => [ 'in-progress', 'ungraded' ],
];
$progress_args = array(
'lesson_id' => $course_lessons,
'user_id' => $user_id,
'status' => array( 'in-progress' ),
'orderby' => 'updated_at',
'order' => 'DESC',
'number' => 1,
);
$last_progress = Sensei()->lesson_progress_repository->find( $progress_args );

$last_lesson_activity = self::sensei_check_for_activity( $activity_args, true );
if ( count( $last_progress ) > 0 ) {
return $last_progress[0]->get_lesson_id();
}

if ( ! empty( $last_lesson_activity ) ) {
return $last_lesson_activity->comment_post_ID;
} else {
// If there is no such lesson, get the first lesson that the user has not yet started.
$completed_lessons = Sensei()->course->get_completed_lesson_ids( $course_id, $user_id );
$not_completed_lessons = array_diff( $course_lessons, $completed_lessons );
// If there is no such lesson, get the first lesson that the user has not yet started.
$completed_lessons = Sensei()->course->get_completed_lesson_ids( $course_id, $user_id );
$not_completed_lessons = array_diff( $course_lessons, $completed_lessons );

if ( $not_completed_lessons ) {
return current( $not_completed_lessons );
}
if ( $not_completed_lessons ) {
return current( $not_completed_lessons );
}

return $course_id;
}

Expand Down
9 changes: 5 additions & 4 deletions includes/course-theme/class-sensei-course-theme-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

use Sensei\Internal\Student_Progress\Quiz_Progress\Models\Quiz_Progress;
use Sensei\Internal\Student_Progress\Quiz_Progress\Models\Quiz_Progress_Interface;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
Expand Down Expand Up @@ -143,10 +144,10 @@ private function maybe_add_quiz_results_notice() {
/**
* Maybe add lesson quiz progress notice.
*
* @param array|false $user_answers User answers.
* @param Quiz_Progress|null $quiz_progress Quiz progress.
* @param int $quiz_id Quiz ID.
* @param Sensei_Context_Notices $notices Notices instance.
* @param array|false $user_answers User answers.
* @param Quiz_Progress_Interface|null $quiz_progress Quiz progress.
* @param int $quiz_id Quiz ID.
* @param Sensei_Context_Notices $notices Notices instance.
*
* @return bool Whether notice was added.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function get( int $course_id, int $user_id ): ?Course_Progress_Interface
$comment = reset( $comment );
}

if ( ! $comment instanceof WP_Comment ) {
return null;
}

return $this->create_progress_from_comment( $comment );
}

Expand All @@ -94,7 +98,7 @@ public function get( int $course_id, int $user_id ): ?Course_Progress_Interface
* @return Comments_Based_Course_Progress The course progress.
*/
private function create_progress_from_comment( WP_Comment $comment ): Comments_Based_Course_Progress {
$meta_start = get_comment_meta( $comment->comment_ID, 'start', true );
$meta_start = get_comment_meta( (int) $comment->comment_ID, 'start', true );
$started_at = $meta_start ? new DateTime( $meta_start, wp_timezone() ) : current_datetime();

$comment_date = new DateTime( $comment->comment_date, wp_timezone() );
Expand Down Expand Up @@ -274,7 +278,11 @@ public function find( array $args ): array {
);

if ( isset( $args['course_id'] ) ) {
$comments_args['post_id'] = $args['course_id'];
if ( is_array( $args['course_id'] ) ) {
$comments_args['post__in'] = $args['course_id'];
} else {
$comments_args['post_id'] = $args['course_id'];
}
}

if ( isset( $args['user_id'] ) ) {
Expand All @@ -285,6 +293,38 @@ public function find( array $args ): array {
$comments_args['status'] = $args['status'];
}

if ( isset( $args['order'] ) ) {
$comments_args['order'] = $args['order'];
}

if ( isset( $args['orderby'] ) ) {
switch ( $args['orderby'] ) {
case 'started_at':
throw new \InvalidArgumentException( 'Ordering by started_at is not supported in comments-based version.' );
case 'completed_at':
case 'created_at':
case 'updated_at':
$comments_args['orderby'] = 'comment_date';
break;
case 'course_id':
$comments_args['orderby'] = 'comment_post_ID';
break;
case 'id':
$comments_args['orderby'] = 'comment_ID';
break;
case 'status':
$comments_args['orderby'] = 'comment_approved';
break;
default:
$comments_args['orderby'] = $args['orderby'];
break;
}
}

if ( isset( $args['order'] ) ) {
$comments_args['order'] = $args['order'];
}

if ( isset( $args['offset'] ) ) {
$comments_args['offset'] = $args['offset'];
}
Expand All @@ -294,10 +334,12 @@ public function find( array $args ): array {
}

$comments = \Sensei_Utils::sensei_check_for_activity( $comments_args, true );
if ( ! $comments ) {
return [];
if ( empty( $comments ) ) {
return array();
}

$comments = is_array( $comments ) ? $comments : array( $comments );

$course_progresses = [];
foreach ( $comments as $comment ) {
$course_progresses[] = $this->create_progress_from_comment( $comment );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ public function find( array $args ): array {
$where_clause = array( 'type = %s' );
$query_params = array( 'course' );
if ( ! is_null( $course_id ) ) {
$query_params[] = (int) $course_id;
$where_clause[] = 'post_id = %d';
$query_params = array_merge( $query_params, (array) $course_id );
$where_clause[] = 'post_id IN (' . $this->get_placeholders( (array) $course_id ) . ')';
}

if ( ! is_null( $user_id ) ) {
Expand All @@ -302,8 +302,8 @@ public function find( array $args ): array {
}

if ( ! is_null( $status ) ) {
$query_params[] = $status;
$where_clause[] = 'status = %s';
$query_params = array_merge( $query_params, (array) $status );
$where_clause[] = 'status IN (' . $this->get_placeholders( (array) $status ) . ')';
}

$table_name = $this->wpdb->prefix . 'sensei_lms_progress';
Expand All @@ -314,14 +314,13 @@ public function find( array $args ): array {
$query_string .= 'WHERE ' . implode( ' AND ', $where_clause ) . ' ';
}

$query_string .= 'ORDER BY id ASC ';
$query_string .= 'LIMIT %d OFFSET %d';
$query_string .= 'ORDER BY id ASC ';
$query_string .= 'LIMIT %d OFFSET %d';
$query_params[] = $limit;
$query_params[] = $offset;


$query = $this->wpdb->prepare(
$query_string,
$query_string, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
...$query_params
);

Expand Down Expand Up @@ -349,4 +348,21 @@ public function find( array $args ): array {

return $course_progresses;
}

/**
* Return a string of placeholders for the given values.
*
* @param array $values The values.
* @return string The placeholders.
*/
private function get_placeholders( array $values ) {
if ( empty( $values ) ) {
return '';
}

$placeholder = is_numeric( $values[0] ) ? '%d' : '%s';
$placeholders = array_fill( 0, count( $values ), $placeholder );

return implode( ', ', $placeholders );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function get( int $lesson_id, int $user_id ): ?Lesson_Progress_Interface
'type' => 'sensei_lesson_status',
];
$comment = Sensei_Utils::sensei_check_for_activity( $activity_args, true );
if ( ! $comment ) {
if ( ! $comment instanceof WP_Comment ) {
return null;
}

Expand Down Expand Up @@ -273,7 +273,7 @@ public function find( array $args ): array {
);

if ( isset( $args['lesson_id'] ) ) {
$comments_args['post_id'] = $args['lesson_id'];
$comments_args['post__in'] = (array) $args['lesson_id'];
}

if ( isset( $args['user_id'] ) ) {
Expand All @@ -284,6 +284,38 @@ public function find( array $args ): array {
$comments_args['status'] = $args['status'];
}

if ( isset( $args['order'] ) ) {
$comments_args['order'] = $args['order'];
}

if ( isset( $args['orderby'] ) ) {
switch ( $args['orderby'] ) {
case 'started_at':
throw new \InvalidArgumentException( 'Ordering by started_at is not supported in comments-based version.' );
case 'completed_at':
case 'created_at':
case 'updated_at':
$comments_args['orderby'] = 'comment_date';
break;
case 'lesson_id':
$comments_args['orderby'] = 'comment_post_ID';
break;
case 'id':
$comments_args['orderby'] = 'comment_ID';
break;
case 'status':
$comments_args['orderby'] = 'comment_approved';
break;
default:
$comments_args['orderby'] = $args['orderby'];
break;
}
}

if ( isset( $args['order'] ) ) {
$comments_args['order'] = $args['order'];
}

if ( isset( $args['offset'] ) ) {
$comments_args['offset'] = $args['offset'];
}
Expand All @@ -293,10 +325,12 @@ public function find( array $args ): array {
}

$comments = \Sensei_Utils::sensei_check_for_activity( $comments_args, true );
if ( ! $comments ) {
return [];
if ( empty( $comments ) ) {
return array();
}

$comments = is_array( $comments ) ? $comments : array( $comments );

$lesson_progresses = [];
foreach ( $comments as $comment ) {
$lesson_progresses[] = $this->create_progress_from_comment( $comment );
Expand All @@ -313,7 +347,7 @@ public function find( array $args ): array {
*/
private function create_progress_from_comment( WP_Comment $comment ): Comments_Based_Lesson_Progress {
$comment_date = new DateTime( $comment->comment_date, wp_timezone() );
$meta_start = get_comment_meta( $comment->comment_ID, 'start', true );
$meta_start = get_comment_meta( (int) $comment->comment_ID, 'start', true );
$started_at = ! empty( $meta_start ) ? new DateTime( $meta_start, wp_timezone() ) : current_datetime();

if ( in_array( $comment->comment_approved, [ 'complete', 'passed', 'graded' ], true ) ) {
Expand Down
Loading

0 comments on commit 1ae868e

Please sign in to comment.