/** * Set the data context for the question. * Decide which question we need based on current quiz state. * * @param $response (array) response from server, if present * @param $quiz (object) Enp_quiz_Quiz() * @return $question (array) The question we need to display */ public function set_question() { // if we have a question id, get the question data for it if (!empty($this->qt->current_question_id)) { $options = array('mc_options_order' => $this->qt->quiz->get_quiz_mc_options_order()); $question = new Enp_quiz_Question($this->qt->current_question_id, $options); } $question_type = $question->get_question_type(); $this->question = $question; }
public function __construct($question_id, $ab_test_id = false) { parent::__construct($question_id); if ($ab_test_id === false) { return false; } $this->get_ab_test_question_results($question_id, $ab_test_id); }
/** * Build question object by id * * @param $question_id = question_id that you want to select * @return question object, false if not found **/ public function get_question_by_id($question_id) { self::$question = $this->select_question_by_id($question_id); if (self::$question !== false) { self::$question = $this->set_question_object_values(); } return self::$question; }
/** * Checks if the response is valid or not * @param $response = submitted response * @return (mixed) 'invalid' if not valid, Enp_quiz_Slider object if valid */ protected function validate_slider_response($response) { // validate that this ID is attached to this question $question = new Enp_quiz_Question($response['question_id']); $slider_id = $question->get_slider(); $slider = new Enp_quiz_Slider($slider_id); // set it up for future use self::$slider = $slider; // see if their response is within the range $slider_response = (double) $response['question_response']; $slider_range_low = (double) $slider->get_slider_range_low(); $slider_range_high = (double) $slider->get_slider_range_high(); if ($slider_range_low <= $slider_response && $slider_response <= $slider_range_high) { // it's valid! $valid = true; } else { $valid = false; } return $valid; }
<?php // get our question object $question = new Enp_quiz_Question($question_id); if ($question_id === '{{question_id}}') { $question_i = '{{question_position}}'; } else { $question_i = $question_i; } $question_image = $question->get_question_image(); ?> <section id="enp-question--<?php echo $question_id; ?> " class="enp-question-content"> <input class="enp-question-id" type="hidden" name="enp_question[<?php echo $question_i; ?> ][question_id]" value="<?php echo $question_id; ?> " /> <?php echo $Quiz_create->get_question_delete_button($question_id); ?> <div class="enp-question-inner enp-question"> <label class="enp-label enp-question-title__label" for="question-title-<?php
/** * If you ever need the entirely built quiz at once with all questions * and all MC Option/Slider data * @return array of quiz and questions */ public function get_quiz_with_full_questions_array() { $quiz = $this->get_take_quiz_array(); $question_ids = $this->get_questions(); // create a blank question array // remove what we don't need unset($quiz['questions']); $quiz['question'] = array(); // loop questions if (!empty($question_ids)) { foreach ($question_ids as $question_id) { // get question object $question = new Enp_quiz_Question($question_id); $question_array = $question->get_take_question_array(); // add this question to the array we'll send via json $quiz['question'][] = $question_array; } } return $quiz; }
protected function reset_all_quiz_questions_data($question_ids) { if (!empty($question_ids)) { foreach ($question_ids as $question_id) { // soft delete responses $this->delete_question_responses($question_id); // reset stats $this->reset_question_data($question_id); // create the question object $question = new Enp_quiz_Question($question_id); // get the question type $question_type = $question->get_question_type(); if ($question_type === 'mc') { $mc_option_ids = $question->get_mc_options(); foreach ($mc_option_ids as $mc_option_id) { // reset compiled mc option data $this->reset_mc_option_data($mc_option_id); // soft delete mc option response $this->delete_mc_option_responses($mc_option_id); } } elseif ($question_type === 'slider') { // get the slider id $slider_id = $question->get_slider(); // soft delete the responses $this->delete_slider_responses($slider_id); } } } }
public function quiz_object_to_array($quiz_obj) { $quiz_array = (array) $quiz_obj; $new_questions_array = array(); if (!empty($quiz_array)) { $question_ids = $quiz_obj->get_questions(); if (!empty($question_ids)) { foreach ($question_ids as $question_id) { // generate the object $question_obj = new Enp_quiz_Question($question_id); // arrayify the question $question_array = (array) $question_obj; // check if mc or slider and add that object as an array if ($question_obj->get_question_type() === 'mc') { $mc_option_ids = $question_obj->get_mc_options(); if (!empty($mc_option_ids)) { foreach ($question_obj->get_mc_options() as $mc_option_id) { $mc_option_object = new Enp_quiz_MC_option($mc_option_id); $mc_option_array = (array) $mc_option_object; $question_array['mc_option'][] = $mc_option_array; } } } elseif ($question_obj->get_question_type() === 'slider') { // get the slider ID $slider_id = $question_obj->get_slider(); // get the slider object $slider_object = new Enp_quiz_Slider($slider_id); // cast it to an array $slider_array = (array) $slider_object; // add it to the question $question_array['slider'] = $slider_array; } // add it to our questions array $quiz_array['question'][] = $question_array; } } } return $quiz_array; }