• Resolved foxybrown

    (@foxybrown)


    Hello, is it any possible way to show the right/wrong answers with pictures, instead of text?
    Right now I have some questions defined with just answer pictures. I want to show the exact correct picture in the result message page for each question. Something like %CorrectAnswer% but with the picture of the correct answer attached.

    • This topic was modified 2 years, 12 months ago by foxybrown.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @foxybrown ,

    It’s not possible with current settings. I will reach out to our second line support developers to check if this will be possible with short code snippet to achieve.

    kind regards,
    Kasia

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @foxybrown ,

    Please check this code:

    
    <?php
    add_filter( 'forminator_replace_quiz_form_data', 'wpmudev_show_quiz_result_with_pictures', 10, 4 );
    function wpmudev_show_quiz_result_with_pictures( $content, $quiz, $data, $entry ){
    	if( $quiz->id!= 1356 ){ //Please change the quiz ID
    		return $content;
    	}
    	$result_behav = isset( $quiz->settings['results_behav'] ) ? $quiz->settings['results_behav'] : '';
    	if ( stripos( $content, '{quiz_answer_pic}' ) !== false && ( empty( $result_behav ) || 'end' === $result_behav ) ) {
    		$answer_content = PHP_EOL . '<ul>' . PHP_EOL;
    		$answers        = isset( $data['answers'] ) ? $data['answers'] : array();
    		$question_ids   = array();
    		if ( is_array( $answers ) ) {
    			foreach ( $answers as $question_id => $answer_id ) {
    				// In multi-answer, $question_id looks like this "question-2051-7608-3".
    				$question_id = preg_replace( '/(-\d+$)/', '', $question_id );
    				if ( ! in_array( $question_id, $question_ids ) ) {
    					$answer                         = $quiz->getAnswer( $question_id, $answer_id );
    					$answer_text                    = isset( $answer['title'] ) ? $answer['title'] : '';
    					if( $answer_text == '' ){
    						$answer_img                 = isset( $answer['image'] ) ? $answer['image'] : '';
    						$answer_text = '<img src='.$answer_img.' class="wpmudev-answer-img">';
    					}
    					$question_ids[ $question_id ][] = $answer_text;
    				}
    			}
    			foreach ( $question_ids as $question_id => $answer_titles ) {
    				$question      = $quiz->getQuestion( $question_id );
    				$question_text = isset( $question['title'] ) ? $question['title'] : '';
    				$answer_head   = count( $answer_titles ) > 1 ? esc_html__( 'Answers : ', 'forminator' ) : esc_html__( 'Answer : ', 'forminator' );
    
    				$answer_content         .= '<li>' . PHP_EOL;
    					$answer_content     .= '<ul>' . PHP_EOL;
    						$answer_content .= '<li><b>' . esc_html__( 'Question : ', 'forminator' ) . '</b>' . esc_html( $question_text ) . '</li>' . PHP_EOL;
    						$answer_content .= '<li><b>' . $answer_head . '</b>' . wp_kses_post( implode( ', ', $answer_titles ) ) . '</li>' . PHP_EOL;
    					$answer_content     .= '</ul>' . PHP_EOL;
    				$answer_content         .= '</li>' . PHP_EOL;
    			}
    		}
    		$answer_content .= '</ul>';
    		$content = str_ireplace( '{quiz_answer_pic}', $answer_content, $content );
    
    	}
    	return $content;
    }

    change the quiz ID in this line if( $quiz->id!= 1356 ){.

    After adding the snippet, {quiz_answer_pic} macro can be used in Behavior → Final Count Message .

    Use this code as mu-plugin https://premium.wpmudev.org/docs/getting-started/download-wpmu-dev-plugins-themes/#installing-mu-plugins

    kind regards,
    Kasia

    Thread Starter foxybrown

    (@foxybrown)

    Thank you for you response.
    I’ve changed the ID, and “insert” the code in
    Plugins -> Plugin File Editor -> Forminator -> forminator.php
    (without the “<?php”), because it’s php file itself.

    It’s not working. I’m afraid this is not the right way to do it.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @foxybrown

    I hope you are doing well.

    Please, never edit the plugin core file, you need to make it as a mu-plugin, the full guide is found at https://premium.wpmudev.org/docs/getting-started/download-wpmu-dev-plugins-themes/#installing-mu-plugins

    Best Regards
    Patrick Freitas

    Thread Starter foxybrown

    (@foxybrown)

    Hello, I’ve managed to run the script, and it’s working! Thank you!
    BUT
    How can I print out only the right answer as a picture?

    Something like:
    https://i.ibb.co/r7xvyJB/correct-answer-as-a-picture.png

    • This reply was modified 2 years, 11 months ago by foxybrown.
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @foxybrown

    I hope you are doing well.

    We pinged our developers to verify this for you.

    We will keep you posted once hearing back from our Second Line Support team.

    Best Regards
    Patrick Freitas

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello @foxybrown

    Could you please edit the code above and replace the following part:

    
    if ( ! in_array( $question_id, $question_ids ) ) {
    	$answer                         = $quiz->getAnswer( $question_id, $answer_id );
    	$answer_text                    = isset( $answer['title'] ) ? $answer['title'] : '';
    	if( $answer_text == '' ){
    		$answer_img                 = isset( $answer['image'] ) ? $answer['image'] : '';
    		$answer_text = '<img src='.$answer_img.' class="wpmudev-answer-img">';
    	}
    	$question_ids[ $question_id ][] = $answer_text;
    }
    

    with the below one:

    
    if ( ! in_array( $question_id, $question_ids ) ) {
    	$answer                         = $quiz->get_correct_answers_for_question( $question_id );
    	$answer_text                    = isset( $answer[0]['title'] ) ? $answer[0]['title'] : '';
    	if( $answer_text == '' ){
    		$answer_img                 = isset( $answer[0]['image'] ) ? $answer[0]['image'] : '';
    		$answer_text = '<img src='.$answer_img.' class="wpmudev-answer-img">';
    	}
    	$question_ids[ $question_id ][] = $answer_text;
    }
    

    Thank you,
    Dimitris

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @foxybrown ,

    We haven’t heard from you for a while now, so it looks like you don’t need our assistance anymore.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Quiz Results: Showing the right/wrong answers as a pictures’ is closed to new replies.