Skip to content

Commit

Permalink
Merge pull request #7525 from Automattic/fix/answer-feedback
Browse files Browse the repository at this point in the history
Fix inconsistency between Pass Required and Passing Grade for answer feedback
  • Loading branch information
renatho authored Mar 7, 2024
2 parents 735df53 + 8281657 commit 4442766
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 32 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-answer-feedback
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Ignore Passing Grade for answer feedback when Pass Required is turned off
66 changes: 34 additions & 32 deletions includes/class-sensei-question.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ public static function the_answer_feedback( $question_id ) {
$quiz_graded = $user_quiz_progress && ! in_array( $user_quiz_progress->get_status(), array( 'ungraded', 'in-progress' ) );

$quiz_required_pass_grade = intval( get_post_meta( $quiz_id, '_quiz_passmark', true ) );
$succeeded = $user_quiz_grade >= $quiz_required_pass_grade;
$succeeded = ! Sensei_Quiz::is_pass_required( $lesson_id ) || $user_quiz_grade >= $quiz_required_pass_grade;

if ( ! $quiz_graded ) {
return;
Expand Down Expand Up @@ -1038,33 +1038,35 @@ public static function the_answer_feedback( $question_id ) {
$has_answer_notes = $answer_notes && wp_strip_all_tags( $answer_notes );

?>
<div class="sensei-lms-question__answer-feedback <?php echo esc_attr( implode( ' ', $answer_notes_classnames ) ); ?>">
<?php if ( $indicate_incorrect ) { ?>
<div class="sensei-lms-question__answer-feedback__header">
<span class="sensei-lms-question__answer-feedback__icon"></span>
<span
class="sensei-lms-question__answer-feedback__title"><?php echo wp_kses_post( $answer_feedback_title ); ?></span>
<?php if ( $grade && $question_grade > 0 ) { ?>
<span class="sensei-lms-question__answer-feedback__points"><?php echo wp_kses_post( $grade ); ?></span>
<?php } ?>
</div>
<?php } ?>
<?php if ( $has_answer_notes || $correct_answer ) { ?>
<div class="sensei-lms-question__answer-feedback__content">
<?php if ( $correct_answer ) { ?>
<div class="sensei-lms-question__answer-feedback__correct-answer">
<?php echo wp_kses_post( __( 'Right Answer:', 'sensei-lms' ) ); ?>
<?php echo wp_kses_post( $correct_answer ); ?>
</div>
<?php } ?>
<?php if ( $has_answer_notes ) { ?>
<div class="sensei-lms-question__answer-feedback__answer-notes">
<?php echo wp_kses_post( $answer_notes ); ?>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<?php if ( $indicate_incorrect || $has_answer_notes || $correct_answer ) { ?>
<div class="sensei-lms-question__answer-feedback <?php echo esc_attr( implode( ' ', $answer_notes_classnames ) ); ?>">
<?php if ( $indicate_incorrect ) { ?>
<div class="sensei-lms-question__answer-feedback__header">
<span class="sensei-lms-question__answer-feedback__icon"></span>
<span
class="sensei-lms-question__answer-feedback__title"><?php echo wp_kses_post( $answer_feedback_title ); ?></span>
<?php if ( $grade && $question_grade > 0 ) { ?>
<span class="sensei-lms-question__answer-feedback__points"><?php echo wp_kses_post( $grade ); ?></span>
<?php } ?>
</div>
<?php } ?>
<?php if ( $has_answer_notes || $correct_answer ) { ?>
<div class="sensei-lms-question__answer-feedback__content">
<?php if ( $correct_answer ) { ?>
<div class="sensei-lms-question__answer-feedback__correct-answer">
<?php echo wp_kses_post( __( 'Right Answer:', 'sensei-lms' ) ); ?>
<?php echo wp_kses_post( $correct_answer ); ?>
</div>
<?php } ?>
<?php if ( $has_answer_notes ) { ?>
<div class="sensei-lms-question__answer-feedback__answer-notes">
<?php echo wp_kses_post( $answer_notes ); ?>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
<?php } ?>
<?php if ( $grade ) { ?>
<style> .question-title .grade { display: none; } </style>
<?php } ?>
Expand Down Expand Up @@ -1238,16 +1240,16 @@ public static function output_result_indication( $lesson_id, $question_id ) {
*/
public static function get_template_data( $question_id, $quiz_id ) {

$lesson_id = Sensei()->quiz->get_lesson_id( $quiz_id );
$user_id = get_current_user_id();
$lesson_id = (int) Sensei()->quiz->get_lesson_id( $quiz_id );
$user_id = (int) get_current_user_id();

$reset_allowed = get_post_meta( $quiz_id, '_enable_quiz_reset', true );
// backwards compatibility
// Backwards compatibility.
if ( 'on' === $reset_allowed ) {
$reset_allowed = 1;
}

// setup the question data
// Setup the question data.
$data = [];
$data['ID'] = $question_id;
$data['title'] = get_the_title( $question_id );
Expand Down
100 changes: 100 additions & 0 deletions tests/unit-tests/test-class-sensei-question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/**
* Tests for Sensei_Question class.
*/
class Sensei_Question_Test extends WP_UnitTestCase {
use Sensei_Test_Login_Helpers;

protected $factory;

protected function setUp(): void {
parent::setUp();

$this->factory = new Sensei_Factory();
}

/**
* This behavior comes from the UI because in the UI the settings related to answer feedback are hidden when pass is not required.
*/
public function testTheAnswerFeedback_WhenPassIsNotRequired_ReturnAnswerFeedbackRegardlessOtherSettings() {
/* Arrange */
$this->login_as_student();

$question_id = $this->create_graded_quiz_with_metas_including_one_question_and_return_question_id(
[
'_pass_required' => '',
'_quiz_passmark' => '80',
'_failed_indicate_incorrect' => 'no',
'_failed_show_correct_answers' => 'no',
'_failed_show_answer_feedback' => 'no',
]
);

/* Act */
ob_start();
Sensei_Question::the_answer_feedback( $question_id );
$output = ob_get_clean();

/* Assert */
$this->assertStringContainsString( '<div class="sensei-lms-question__answer-feedback__header">', $output );
}

public function testTheAnswerFeedback_WhenPassIsRequiredAndPassmarkIsAchieved_RespectAnswerFeedbackSettings() {
/* Arrange */
$this->login_as_student();

$question_id = $this->create_graded_quiz_with_metas_including_one_question_and_return_question_id(
[
'_pass_required' => 'on',
'_quiz_passmark' => '80',
'_failed_indicate_incorrect' => 'yes',
'_failed_show_correct_answers' => 'no',
'_failed_show_answer_feedback' => 'yes',
]
);

/* Act */
ob_start();
Sensei_Question::the_answer_feedback( $question_id );
$output = ob_get_clean();

/* Assert */
$this->assertStringContainsString( '<div class="sensei-lms-question__answer-feedback__header">', $output );
$this->assertStringNotContainsString( '<div class="sensei-lms-question__answer-feedback__correct-answer">', $output );
$this->assertStringContainsString( '<div class="sensei-lms-question__answer-feedback__answer-notes">', $output );
}

private function create_graded_quiz_with_metas_including_one_question_and_return_question_id( $metas ) {
// Create a quiz with a question.
$lesson_id = $this->factory->lesson->create();
$meta_input = wp_parse_args(
$metas,
[
'_quiz_lesson' => $lesson_id,
]
);
$quiz_id = $this->factory->quiz->create(
[
'post_parent' => $lesson_id,
'meta_input' => $meta_input,
]
);
$question_id = $this->factory->question->create( [ 'quiz_id' => $quiz_id ] );
$user_id = wp_get_current_user()->ID;

// Set current post as the quiz.
$GLOBALS['post'] = get_post( $quiz_id );

// Create quiz answers.
$user_quiz_answers = $this->factory->generate_user_quiz_answers( $quiz_id );
Sensei()->quiz->save_user_answers( $user_quiz_answers, array(), $lesson_id, $user_id );

// Grade the quiz.
$quiz_progress = Sensei()->quiz_progress_repository->get( $quiz_id, $user_id );
$quiz_progress->grade();
Sensei()->quiz_progress_repository->save( $quiz_progress );

return $question_id;
}
}

0 comments on commit 4442766

Please sign in to comment.