Skip to content

Commit

Permalink
Merge pull request #7178 from Automattic/update/split-quiz-answer-model
Browse files Browse the repository at this point in the history
Split the quiz answer model
  • Loading branch information
m1r0 authored Sep 21, 2023
2 parents cdc78f9 + 2ce417e commit 2e1247c
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 93 deletions.
2 changes: 1 addition & 1 deletion includes/class-sensei-quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ public function set_user_grades( $quiz_grades, $lesson_id, $user_id = 0 ) {

foreach ( $quiz_grades as $question_id => $points ) {
$answer = $answers_map[ $question_id ];
Sensei()->quiz_grade_repository->create( $submission, $answer->get_id(), $question_id, $points );
Sensei()->quiz_grade_repository->create( $submission, $answer, $question_id, $points );
}

$transient_key = 'quiz_grades_' . $user_id . '_' . $lesson_id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* File containing the Answer class.
* File containing the Answer_Abstract class.
*
* @package sensei
*/
Expand All @@ -14,13 +14,13 @@
}

/**
* Class Answer.
* Class Answer_Abstract.
*
* @internal
*
* @since 4.7.2
* @since $$next_version$$
*/
class Answer {
class Answer_Abstract implements Answer_Interface {
/**
* The answer ID.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* File containing the Answer_Interface.
*
* @package sensei
*/

namespace Sensei\Internal\Quiz_Submission\Answer\Models;

use DateTimeInterface;

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

/**
* Interface for the answer models.
*
* @internal
*
* @since $$next_version$$
*/
interface Answer_Interface {
/**
* Get the answer ID.
*
* @internal
*
* @return int
*/
public function get_id(): int;

/**
* Get the submission ID.
*
* @internal
*
* @return int
*/
public function get_submission_id(): int;

/**
* Get the question ID.
*
* @internal
*
* @return int
*/
public function get_question_id(): int;

/**
* Get the answer value.
*
* @internal
*
* @return string
*/
public function get_value(): string;

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

/**
* Get the updated date.
*
* @internal
*
* @return DateTimeInterface
*/
public function get_updated_at(): DateTimeInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* File containing the Comments_Based_Answer class.
*
* @package sensei
*/

namespace Sensei\Internal\Quiz_Submission\Answer\Models;

use DateTimeInterface;

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

/**
* Class Comments_Based_Answer.
*
* @internal
*
* @since $$next_version$$
*/
class Comments_Based_Answer extends Answer_Abstract {
/**
* Constructor.
*
* @internal
*
* @param int $submission_id The submission ID.
* @param int $question_id The question ID.
* @param string $value The answer value.
* @param DateTimeInterface $created_at The created date.
* @param DateTimeInterface $updated_at The updated date.
*/
public function __construct(
int $submission_id,
int $question_id,
string $value,
DateTimeInterface $created_at,
DateTimeInterface $updated_at
) {
parent::__construct( 0, $submission_id, $question_id, $value, $created_at, $updated_at );
}

/**
* Get the answer ID.
*
* @internal
*
* @throws \BadMethodCallException Comments_Based_Answer does not have an ID.
*/
public function get_id(): int {
throw new \BadMethodCallException( 'Comments_Based_Answer does not have an ID.' );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* File containing the Tables_Based_Answer class.
*
* @package sensei
*/

namespace Sensei\Internal\Quiz_Submission\Answer\Models;

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

/**
* Class Tables_Based_Answer.
*
* @internal
*
* @since $$next_version$$
*/
class Tables_Based_Answer extends Answer_Abstract {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Sensei\Internal\Quiz_Submission\Answer\Repositories;

use Sensei\Internal\Quiz_Submission\Answer\Models\Answer;
use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface;
use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface;
use Sensei\Internal\Quiz_Submission\Submission\Repositories\Tables_Based_Submission_Repository;

Expand Down Expand Up @@ -83,9 +83,9 @@ public function __construct(
* @param int $question_id The question ID.
* @param string $value The answer value.
*
* @return Answer The answer model.
* @return Answer_Interface The answer model.
*/
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer {
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface {
$answer = $this->comments_based_repository->create( $submission, $question_id, $value );

if ( $this->use_tables ) {
Expand All @@ -103,7 +103,7 @@ public function create( Submission_Interface $submission, int $question_id, stri
*
* @param int $submission_id The submission ID.
*
* @return Answer[] An array of answers.
* @return Answer_Interface[] An array of answers.
*/
public function get_all( int $submission_id ): array {
return $this->comments_based_repository->get_all( $submission_id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Sensei\Internal\Quiz_Submission\Answer\Repositories;

use Sensei\Internal\Quiz_Submission\Answer\Models\Answer;
use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface;
use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface;

if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -31,9 +31,9 @@ interface Answer_Repository_Interface {
* @param int $question_id The question ID.
* @param string $value The answer value.
*
* @return Answer The answer model.
* @return Answer_Interface The answer model.
*/
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer;
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface;

/**
* Get all answers for a quiz submission.
Expand All @@ -42,7 +42,7 @@ public function create( Submission_Interface $submission, int $question_id, stri
*
* @param int $submission_id The submission ID.
*
* @return Answer[] An array of answers.
* @return Answer_Interface[] An array of answers.
*/
public function get_all( int $submission_id ): array;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace Sensei\Internal\Quiz_Submission\Answer\Repositories;

use Sensei\Internal\Quiz_Submission\Answer\Models\Answer;
use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface;
use Sensei\Internal\Quiz_Submission\Answer\Models\Comments_Based_Answer;
use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface;

if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -31,9 +32,9 @@ class Comments_Based_Answer_Repository implements Answer_Repository_Interface {
* @param int $question_id The question ID.
* @param string $value The answer value.
*
* @return Answer The answer model.
* @return Answer_Interface The answer model.
*/
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer {
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface {
$submission_id = $submission->get_id();
$answers_map = $this->get_answers_map( $submission_id );
$answers_map[ $question_id ] = $value;
Expand All @@ -44,7 +45,7 @@ public function create( Submission_Interface $submission, int $question_id, stri

$created_at = current_datetime();

return new Answer( 0, $submission_id, $question_id, $value, $created_at, $created_at );
return new Comments_Based_Answer( $submission_id, $question_id, $value, $created_at, $created_at );
}

/**
Expand All @@ -54,14 +55,14 @@ public function create( Submission_Interface $submission, int $question_id, stri
*
* @param int $submission_id The submission ID.
*
* @return Answer[] An array of answers.
* @return Answer_Interface[] An array of answers.
*/
public function get_all( int $submission_id ): array {
$answers = [];
$created_at = current_datetime();

foreach ( $this->get_answers_map( $submission_id ) as $question_id => $value ) {
$answers[] = new Answer( 0, $submission_id, $question_id, $value, $created_at, $created_at );
$answers[] = new Comments_Based_Answer( $submission_id, $question_id, $value, $created_at, $created_at );
}

return $answers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

use DateTimeImmutable;
use DateTimeZone;
use Sensei\Internal\Quiz_Submission\Answer\Models\Answer;
use Sensei\Internal\Quiz_Submission\Answer\Models\Answer_Interface;
use Sensei\Internal\Quiz_Submission\Answer\Models\Tables_Based_Answer;
use Sensei\Internal\Quiz_Submission\Submission\Models\Submission_Interface;
use wpdb;

Expand Down Expand Up @@ -52,9 +53,9 @@ public function __construct( wpdb $wpdb ) {
* @param int $question_id The question ID.
* @param string $value The answer value.
*
* @return Answer The answer model.
* @return Answer_Interface The answer model.
*/
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer {
public function create( Submission_Interface $submission, int $question_id, string $value ): Answer_Interface {
$current_datetime = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
$date_format = 'Y-m-d H:i:s';

Expand All @@ -76,7 +77,7 @@ public function create( Submission_Interface $submission, int $question_id, stri
]
);

return new Answer(
return new Tables_Based_Answer(
$this->wpdb->insert_id,
$submission->get_id(),
$question_id,
Expand All @@ -93,7 +94,7 @@ public function create( Submission_Interface $submission, int $question_id, stri
*
* @param int $submission_id The submission ID.
*
* @return Answer[] An array of answers.
* @return Answer_Interface[] An array of answers.
*/
public function get_all( int $submission_id ): array {
$query = $this->wpdb->prepare(
Expand All @@ -105,7 +106,7 @@ public function get_all( int $submission_id ): array {
$answers = [];
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared earlier.
foreach ( $this->wpdb->get_results( $query ) as $result ) {
$answers[] = new Answer(
$answers[] = new Tables_Based_Answer(
$result->id,
$result->submission_id,
$result->question_id,
Expand Down
Loading

0 comments on commit 2e1247c

Please sign in to comment.