· Centre for Creative Quality Improvement

array(
‘key_title’ => ‘Yes’,
‘max’ => 5
),
330 => array(
‘key_title’ => ‘Yes’,
‘max’ => 3
),

);

public static function get_instance() {

if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Quiz_Results();
}
return self::$_instance;

}

private function __construct() {
add_action( ‘forminator_before_submit_quizzes’, array( $this, ‘submit_quizzes’ ) );
}

public function submit_quizzes( $model ) {

if ( ‘nowrong’ === $model->quiz_type ) {
$this->_process_nowrong_submit( $model );
} //else {
//$this->_process_knowledge_submit( $model );
//}

}

private function _process_nowrong_submit( $model ) {

//counting the result
$results = array();
$result_data = array();
$post_data = $this->get_post_data();
$forms_with_rules = array_keys( $this->forms_rules );
$final_res = null;
$count_occurences = 0;

if ( ! in_array( $post_data[‘form_id’], $forms_with_rules ) ) {
// Continue with default flow
return;
}

$rule = (object) $this->forms_rules[ $post_data[‘form_id’] ];
//$form_meta = get_post_meta( $this->form_id, ‘forminator_form_meta’, true );

if ( isset( $post_data[‘answers’] ) ) {
foreach ( $post_data[‘answers’] as $id => $answer ) {
$results[] = $model->getResultFromAnswer( $id, $answer );
$question = $model->getQuestion( $id );
$a = $model->getAnswer( $id, $answer );
$result_data[‘answers’][] = array(
‘question’ => $question[‘title’],
‘answer’ => $a[‘title’],
);
if ( $a[‘title’] == $rule->key_title ) {
$count_occurences ++;
// We are also setting “$final_res” here so we can have the answer key later on.
$final_res = $a[‘result’];
}
}
}

if ( $count_occurences < $rule->max ) {
return;
}

$result_data[‘result’] = $model->getResult( $final_res );
$entry_id = $this->_save_entry(
$model->id,

array(
array(
‘name’ => ‘entry’,
‘value’ => $result_data,
),
) );

$result = new Forminator_QForm_Result();
$result->set_entry( $entry_id );
$result->set_postdata( $post_data );

wp_send_json_success(
array(
‘result’ => $this->_render_nowrong_result( $model, $final_res ),
‘result_url’ => $result->build_permalink(),
‘type’ => ‘nowrong’,
) );
}

private function _render_nowrong_result( $model, $final_res ) {
$result = $model->getResult( $final_res );
ob_start();

$theme = $model->settings[‘forminator-quiz-theme’];

if ( ! $theme ) {
$theme = ‘default’;
}
?>





id, ‘quiz’ ); // WPCS: XSS ok. ?>