Skip to content

Commit

Permalink
Merge pull request #7172 from Automattic/update/split-progress-models
Browse files Browse the repository at this point in the history
Split progress models
  • Loading branch information
merkushin authored Sep 21, 2023
2 parents 3a74164 + 7755ebe commit 8c54103
Show file tree
Hide file tree
Showing 52 changed files with 1,989 additions and 513 deletions.
5 changes: 2 additions & 3 deletions includes/class-sensei-utils.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Sensei\Internal\Student_Progress\Course_Progress\Models\Course_Progress;
use Sensei\Internal\Student_Progress\Quiz_Progress\Repositories\Quiz_Progress_Repository_Factory;
use Sensei\Internal\Student_Progress\Course_Progress\Models\Course_Progress_Interface;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand Down Expand Up @@ -1528,7 +1527,7 @@ public static function user_completed_course( $course, $user_id = 0 ) {
}
}

if ( $user_course_status && Course_Progress::STATUS_COMPLETE === $user_course_status ) {
if ( $user_course_status && Course_Progress_Interface::STATUS_COMPLETE === $user_course_status ) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace Sensei\Internal\Emails\Generators;

use Sensei\Internal\Emails\Email_Repository;
use Sensei\Internal\Student_Progress\Lesson_Progress\Models\Lesson_Progress;
use Sensei\Internal\Student_Progress\Lesson_Progress\Repositories\Lesson_Progress_Repository_Interface;
use Sensei\Internal\Student_Progress\Quiz_Progress\Models\Quiz_Progress;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -88,7 +86,7 @@ public function init() {
*/
public function student_completed_lesson_mail_to_teacher( $student_id, $lesson_id ) {
$lesson_progress = $this->lesson_progress_repository->get( $lesson_id, $student_id );
if ( ! $lesson_progress || ! in_array( $lesson_progress->get_status(), [ Lesson_Progress::STATUS_COMPLETE, Quiz_Progress::STATUS_PASSED ], true ) ) {
if ( ! $lesson_progress || ! $lesson_progress->is_complete() ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
}

use Sensei\Internal\Migration\Migration_Abstract;
use Sensei\Internal\Student_Progress\Course_Progress\Models\Course_Progress;
use Sensei\Internal\Student_Progress\Lesson_Progress\Models\Lesson_Progress;
use Sensei\Internal\Student_Progress\Quiz_Progress\Models\Quiz_Progress;
use Sensei\Internal\Student_Progress\Course_Progress\Models\Course_Progress_Interface;
use Sensei\Internal\Student_Progress\Lesson_Progress\Models\Lesson_Progress_Interface;
use Sensei\Internal\Student_Progress\Quiz_Progress\Models\Quiz_Progress_Interface;

/**
* Class Student_Progress_Migration.
Expand Down Expand Up @@ -248,11 +248,11 @@ private function prepare_progress_to_insert( $progress_comments, $mapped_meta ):
*/
private function prepare_course_progress_to_insert( $comment, $meta ): void {
$course_status = 'in-progress';
if ( Course_Progress::STATUS_COMPLETE === $comment->comment_approved ) {
if ( Course_Progress_Interface::STATUS_COMPLETE === $comment->comment_approved ) {
$course_status = 'complete';
}

if ( Course_Progress::STATUS_COMPLETE === $comment->comment_approved ) {
if ( Course_Progress_Interface::STATUS_COMPLETE === $comment->comment_approved ) {
$completed_at = $comment->comment_date_gmt;
} else {
$completed_at = null;
Expand Down Expand Up @@ -334,23 +334,23 @@ private function prepare_lesson_progress_to_insert( $comment, $meta ): void {
if ( isset( $meta['_lesson_quiz'] ) ) {
$quiz_id = $meta['_lesson_quiz'];
$supported_statuses = [
Quiz_Progress::STATUS_IN_PROGRESS,
Quiz_Progress::STATUS_FAILED,
Quiz_Progress::STATUS_GRADED,
Quiz_Progress::STATUS_PASSED,
Quiz_Progress::STATUS_UNGRADED,
Quiz_Progress_Interface::STATUS_IN_PROGRESS,
Quiz_Progress_Interface::STATUS_FAILED,
Quiz_Progress_Interface::STATUS_GRADED,
Quiz_Progress_Interface::STATUS_PASSED,
Quiz_Progress_Interface::STATUS_UNGRADED,
// We need to map lesson statuses to quiz' passed status.
Lesson_Progress::STATUS_COMPLETE,
Lesson_Progress_Interface::STATUS_COMPLETE,
];
$quiz_status = in_array( $comment->comment_approved, $supported_statuses, true )
? $comment->comment_approved
: Quiz_Progress::STATUS_IN_PROGRESS;
: Quiz_Progress_Interface::STATUS_IN_PROGRESS;
$quiz_completed_at = $completed_at;
if ( Quiz_Progress::STATUS_IN_PROGRESS === $quiz_status ) {
if ( Quiz_Progress_Interface::STATUS_IN_PROGRESS === $quiz_status ) {
$quiz_completed_at = null;
}
if ( Lesson_Progress::STATUS_COMPLETE === $quiz_status ) {
$quiz_status = Quiz_Progress::STATUS_PASSED;
if ( Lesson_Progress_Interface::STATUS_COMPLETE === $quiz_status ) {
$quiz_status = Quiz_Progress_Interface::STATUS_PASSED;
}

$this->progress_inserts[] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* File containing the Comments_Based_Course_Progress class.
*
* @package sensei
*/

namespace Sensei\Internal\Student_Progress\Course_Progress\Models;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Comments_Based_Course_Progress.
*
* @internal
*
* @since $$next-version$$
*/
class Comments_Based_Course_Progress extends Course_Progress_Abstract {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* File containing the Course_Progress class.
* File containing the Course_Progress_Abstract class.
*
* @package sensei
*/
Expand All @@ -14,26 +14,16 @@
}

/**
* Class Course_Progress.
* Class Course_Progress_Abstract.
*
* Shared properties and methods for course progress models.
* This class is not meant to be instantiated directly.
*
* @internal
*
* @since 4.7.2
* @since $$next-version$$
*/
class Course_Progress {
/**
* Status course in progress.
*
* @internal
*/
public const STATUS_IN_PROGRESS = 'in-progress';

/**
* Status course complete.
*
* @internal
*/
public const STATUS_COMPLETE = 'complete';
abstract class Course_Progress_Abstract implements Course_Progress_Interface {

/**
* Progress identifier.
Expand Down Expand Up @@ -124,7 +114,7 @@ public function __construct( int $id, int $course_id, int $user_id, ?string $sta
* @param DateTimeInterface|null $started_at Course start date.
*/
public function start( DateTimeInterface $started_at = null ): void {
$this->status = self::STATUS_IN_PROGRESS;
$this->status = Course_Progress_Interface::STATUS_IN_PROGRESS;
$this->started_at = $started_at ?? current_datetime();
}

Expand All @@ -136,7 +126,7 @@ public function start( DateTimeInterface $started_at = null ): void {
* @param DateTimeInterface|null $completed_at Course completion date.
*/
public function complete( DateTimeInterface $completed_at = null ): void {
$this->status = self::STATUS_COMPLETE;
$this->status = Course_Progress_Interface::STATUS_COMPLETE;
$this->completed_at = $completed_at ?? current_datetime();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* File containing the Course_Progress_Interface.
*
* @package sensei
*/

namespace Sensei\Internal\Student_Progress\Course_Progress\Models;

use DateTimeInterface;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Interfaces Course_Progress_Interface.
*
* @internal
*
* @since $$next-version$$
*/
interface Course_Progress_Interface {
/**
* Status course in progress.
*
* @internal
*/
public const STATUS_IN_PROGRESS = 'in-progress';

/**
* Status course complete.
*
* @internal
*/
public const STATUS_COMPLETE = 'complete';

/**
* Set in-progress status and start date.
*
* @internal
*
* @param DateTimeInterface|null $started_at Course start date.
*/
public function start( DateTimeInterface $started_at = null ): void;

/**
* Set complete status and completion date.
*
* @internal
*
* @param DateTimeInterface|null $completed_at Course completion date.
*/
public function complete( DateTimeInterface $completed_at = null ): void;

/**
* Returns the progress identifier.
*
* @internal
*
* @return int
*/
public function get_id(): int;

/**
* Returns the course identifier.
*
* @internal
*
* @return int
*/
public function get_course_id(): int;

/**
* Returns the user identifier.
*
* @internal
*
* @return int
*/
public function get_user_id(): int;

/**
* Returns the course progress status.
*
* @internal
*
* @return string|null
*/
public function get_status(): ?string;

/**
* Returns the course start date.
*
* @internal
*
* @return DateTimeInterface|null
*/
public function get_started_at(): ?DateTimeInterface;

/**
* Returns the course completion date.
*
* @internal
*
* @return DateTimeInterface|null
*/
public function get_completed_at(): ?DateTimeInterface;

/**
* Returns the course progress updated date.
*
* @internal
*
* @return DateTimeInterface
*/
public function get_updated_at(): DateTimeInterface;

/**
* Returns the course progress created date.
*
* @internal
*
* @return DateTimeInterface
*/
public function get_created_at(): DateTimeInterface;

/**
* Set the course progress updated date.
*
* @internal
*
* @param DateTimeInterface $updated_at Course progress updated date.
*/
public function set_updated_at( DateTimeInterface $updated_at ): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* File containing the Tables_Based_Course_Progress class.
*
* @package sensei
*/

namespace Sensei\Internal\Student_Progress\Course_Progress\Models;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Tables_Based_Course_Progress.
*
* @internal
*
* @since $$next-version$$
*/
class Tables_Based_Course_Progress extends Course_Progress_Abstract {
}
Loading

0 comments on commit 8c54103

Please sign in to comment.