Skip to content

Commit bc7e466

Browse files
authored
Merge pull request #2897 from themeum/image-answering-result
Automatic result calculation for Image answering type quiz
2 parents d1d452b + 03315fd commit bc7e466

2 files changed

Lines changed: 32 additions & 43 deletions

File tree

classes/Quiz.php

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ public function start_the_quiz() {
561561
*
562562
* @return int inserted id|0
563563
*/
564-
public static function quiz_attempt( int $course_id, int $quiz_id, int $user_id, $attempt_status = 'attempt_started' ) {
564+
public static function quiz_attempt( int $course_id, int $quiz_id, int $user_id, $attempt_status = QuizModel::ATTEMPT_STARTED ) {
565565
global $wpdb;
566566

567567
if ( ! $course_id ) {
@@ -899,42 +899,32 @@ function ( $ans ) {
899899
$is_answer_was_correct = true;
900900
}
901901
} elseif ( QuizModel::QUESTION_TYPE_IMAGE_ANSWERING === $question_type ) {
902-
$image_inputs = tutor_utils()->avalue_dot( 'answer_id', $answers );
903-
$image_inputs = (array) array_map( 'sanitize_text_field', $image_inputs );
904-
$given_answer = maybe_serialize( $image_inputs );
905-
$is_answer_was_correct = false;
906-
/**
907-
* For the image_answering question type result
908-
* remain pending in spite of correct answer & required
909-
* review of admin/instructor. Since it's
910-
* pending we need to mark it as incorrect. Otherwise if
911-
* mark it correct then earned mark will be updated. then
912-
* again when instructor/admin review & mark it as correct
913-
* extra mark is adding. In this case, student
914-
* getting double mark for the same question.
915-
*
916-
* For now code is commenting will be removed later on
917-
*
918-
* @since 2.1.5
919-
*/
902+
$image_inputs = tutor_utils()->avalue_dot( 'answer_id', $answers );
903+
$image_inputs = (array) array_map( 'sanitize_text_field', $image_inputs );
904+
$given_answer = maybe_serialize( $image_inputs );
905+
906+
$stored_answers = $wpdb->get_results(
907+
$wpdb->prepare(
908+
"SELECT answer_id, answer_title
909+
FROM {$wpdb->prefix}tutor_quiz_question_answers
910+
WHERE belongs_question_id = %d
911+
AND belongs_question_type = %s
912+
ORDER BY answer_order ASC",
913+
$question_id,
914+
QuizModel::QUESTION_TYPE_IMAGE_ANSWERING
915+
)
916+
);
920917

921-
//phpcs:disable
922-
923-
// $db_answer = $wpdb->get_col(
924-
// $wpdb->prepare(
925-
// "SELECT answer_title
926-
// FROM {$wpdb->prefix}tutor_quiz_question_answers
927-
// WHERE belongs_question_id = %d
928-
// AND belongs_question_type = 'image_answering'
929-
// ORDER BY answer_order asc ;",
930-
// $question_id
931-
// )
932-
// );
933-
934-
// if ( is_array( $db_answer ) && count( $db_answer ) ) {
935-
// $is_answer_was_correct = ( strtolower( maybe_serialize( array_values( $image_inputs ) ) ) == strtolower( maybe_serialize( $db_answer ) ) );
936-
// }
937-
//phpcs:enable
918+
$is_answer_was_correct = true;
919+
foreach ( $stored_answers as $stored_answer ) {
920+
$user_answer = isset( $image_inputs[ $stored_answer->answer_id ] ) ? trim( wp_unslash( $image_inputs[ $stored_answer->answer_id ] ) ) : '';
921+
$correct_answer = trim( wp_unslash( $stored_answer->answer_title ) );
922+
923+
if ( 0 !== strcasecmp( $user_answer, $correct_answer ) ) {
924+
$is_answer_was_correct = false;
925+
break;
926+
}
927+
}
938928
} else {
939929
$custom_answer_data = array(
940930
'given_answer' => $given_answer,
@@ -982,12 +972,12 @@ function ( $ans ) {
982972
$attempt_info = array(
983973
'total_answered_questions' => tutor_utils()->count( $quiz_answers ),
984974
'earned_marks' => $total_marks,
985-
'attempt_status' => 'attempt_ended',
975+
'attempt_status' => QuizModel::ATTEMPT_ENDED,
986976
'attempt_ended_at' => date( 'Y-m-d H:i:s', tutor_time() ), //phpcs:ignore
987977
);
988978

989979
if ( $review_required ) {
990-
$attempt_info['attempt_status'] = 'review_required';
980+
$attempt_info['attempt_status'] = QuizModel::REVIEW_REQUIRED;
991981
}
992982

993983
$wpdb->update( $wpdb->tutor_quiz_attempts, $attempt_info, array( 'attempt_id' => $attempt_id ) );
@@ -1028,7 +1018,7 @@ public function finishing_quiz_attempt() {
10281018
$attempt_info = array(
10291019
'total_answered_questions' => 0,
10301020
'earned_marks' => 0,
1031-
'attempt_status' => 'attempt_ended',
1021+
'attempt_status' => QuizModel::ATTEMPT_ENDED,
10321022
'attempt_ended_at' => date( 'Y-m-d H:i:s', tutor_time() ), //phpcs:ignore
10331023
);
10341024

@@ -1082,7 +1072,7 @@ public function tutor_quiz_timeout() {
10821072
$attempt_id = $attempt->attempt_id;
10831073

10841074
$data = array(
1085-
'attempt_status' => 'attempt_timeout',
1075+
'attempt_status' => QuizModel::ATTEMPT_TIMEOUT,
10861076
'total_marks' => self::get_quiz_total_marks( $quiz_id ),
10871077
'earned_marks' => 0,
10881078
'attempt_ended_at' => gmdate( 'Y-m-d H:i:s', tutor_time() ),
@@ -1398,7 +1388,7 @@ private function apply_quiz_answer_review( int $attempt_id, $attempt_answer, str
13981388
}
13991389

14001390
if ( 'open_ended' === $question->question_type || 'short_answer' === $question->question_type ) {
1401-
$attempt_update_data['attempt_status'] = 'attempt_ended';
1391+
$attempt_update_data['attempt_status'] = QuizModel::ATTEMPT_ENDED;
14021392
}
14031393

14041394
if ( ! empty( $attempt_update_data ) ) {
@@ -1422,7 +1412,7 @@ private function apply_quiz_answer_review( int $attempt_id, $attempt_answer, str
14221412
}
14231413

14241414
if ( 'open_ended' === $question->question_type || 'short_answer' === $question->question_type ) {
1425-
$attempt_update_data['attempt_status'] = 'attempt_ended';
1415+
$attempt_update_data['attempt_status'] = QuizModel::ATTEMPT_ENDED;
14261416
}
14271417

14281418
if ( ! empty( $attempt_update_data ) ) {

models/QuizModel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ public static function get_manual_review_types() {
196196
return array(
197197
self::QUESTION_TYPE_OPEN_ENDED,
198198
self::QUESTION_TYPE_SHORT_ANSWER,
199-
self::QUESTION_TYPE_IMAGE_ANSWERING,
200199
);
201200
}
202201

0 commit comments

Comments
 (0)