static function html($meta, $field) { global $post; $post_id = $post->ID; $question = get_post_meta($post_id, '_lpr_question', true); $question = wp_parse_args($question, array('type' => null)); ob_start(); if ($q = LP_Question_Factory::get_question($post_id)) { $q->admin_interface(); } return ob_get_clean(); }
function learn_press_question_class($question = null) { if (!$question) { $question = LP_Question_Factory::get_question(get_the_ID()); } elseif (is_numeric($question)) { $question = LP_Question_Factory::get_question($question); } if ($question) { $class = "learn-press-question-wrap question-type-{$question->type} question-{$question->id}"; $class = apply_filters('learn_press_question_class', $class, $question); $class = explode(" ", $class); $class = array_unique($class); echo "class=\"" . join(' ', $class) . "\""; } }
/** * __get function. * * @param string $key * * @return mixed */ public function __get($key) { if (isset($this->{$key})) { return $this->{$key}; } $value = null; switch ($key) { case 'current_question': if (($question = learn_press_get_request('question')) && is_quiz()) { $value = LP_Question_Factory::get_question($question); } break; case 'questions': $value = $this->get_questions(); break; default: if (strpos($key, '_lp_') === false) { $key = '_lp_' . $key; } $value = get_post_meta($this->id, $key, true); if ($key == '_lp_duration') { $value = absint($value) * 60; } } if (!empty($value)) { $this->{$key} = $value; } return $value; }
_e('Collapse All', 'learn_press'); ?> "></a> </p> </h3> <div id="learn-press-list-questions"> <?php if ($questions) { $index = 0; ?> <?php foreach ($questions as $question) { ?> <?php $question = LP_Question_Factory::get_question($question); $question_view = learn_press_get_admin_view('meta-boxes/quiz/question.php'); include $question_view; ?> <?php } ?> <?php $exclude_ids = array_keys($questions); } ?> </div> <div class="question-actions"> <input type="text" class="regular-text no-submit" name="lp-new-question-name" placeholder="<?php _e('The name of new question and hit enter', 'learn_press'); ?>
static function save() { global $wpdb, $post; $questions = learn_press_get_request('learn_press_question'); if ($all_questions = LP_Quiz::get_quiz($post->ID)->get_questions()) { $all_questions = array_keys($all_questions); } // remove questions if it has removed from $_POST if ($all_questions && $questions && ($remove_ids = array_diff($all_questions, array_keys($questions)))) { $query = $wpdb->prepare("\n\t\t\t\t\tDELETE\n\t\t\t\t\tFROM {$wpdb->learnpress_quiz_questions}\n\t\t\t\t\tWHERE quiz_id = %d\n\t\t\t\t\tAND question_id IN(" . join(',', $remove_ids) . ")\n\t\t\t\t", $post->ID); $wpdb->query($query); do_action('learn_press_remove_quiz_questions', $remove_ids, $post->ID); } if (!$questions) { return; } $titles = learn_press_get_request('learn-press-question-name'); // update the title of questions and save all data foreach ($questions as $id => $data) { $question = LP_Question_Factory::get_question($id); if (!empty($titles[$id])) { $wpdb->update($wpdb->posts, array('post_title' => $titles[$id]), array('ID' => $id), array('%s')); } $question->save($data); } // if there are new questions then insert into quiz if ($new_ids = array_diff(array_keys($questions), $all_questions)) { $values = array(); foreach ($new_ids as $id) { $insert_data = apply_filters('learn_press_quiz_question_insert_data', array('question_id' => $id, 'quiz_id' => $post->ID, 'params' => '')); $values[] = $wpdb->prepare("(%d, %d, %s)", $insert_data['quiz_id'], $insert_data['question_id'], $insert_data['param']); } $query = "\n\t\t\t\t\tINSERT INTO {$wpdb->learnpress_quiz_questions}(`quiz_id`, `question_id`, `params`)\n\t\t\t\t\tVALUES " . join(',', $values) . "\n\t\t\t\t"; $wpdb->query($query); do_action('learn_press_insert_quiz_questions', $new_ids, $post->ID); } }
static function convert_question($id, $from, $to) { $question = LP_Question_Factory::get_question($id); $question_meta = (array) get_post_meta($id, '_lp_question', true); switch ($from) { case 'true_or_false': case 'single_choice': if ($to == 'multi_choice') { } break; case 'multi_choice': $count = 0; $true_option = 0; $answers = $question->answers; if ($to == 'true_or_false') { $first_option = reset($answers); $check_seconds_option = false; if ($first_option['is_true'] != 1) { $check_seconds_option = true; } foreach ($answers as $answer) { $count++; if ($answer['is_true'] == 1) { $true_option++; } if ($true_option > 1) { $answer['is_true'] = 0; } if ($count == 2 && $check_seconds_option) { $answer['is_true'] = 0; } if ($count >= 2) { break; } } } elseif ($to == 'single_choice') { foreach ($answers as $answer) { if ($answer['is_true'] == 1) { $true_option++; } if ($true_option > 2) { $answer['is_true'] = 0; } } } $question_meta['answer'] = $answers; } $question_meta['type'] = $to; update_post_meta($id, '_lpr_question', $question_meta); }
static function init() { if (is_admin()) { add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_assets')); add_action('save_post', array(__CLASS__, 'save')); add_action('admin_print_footer_scripts', array(__CLASS__, 'admin_template')); add_action('learn_press_convert_question_type', array(__CLASS__, 'convert_question'), 5, 4); add_filter('learn_press_question_answers_data', array(__CLASS__, 'sanitize_answers'), 10, 2); } else { } LP_Question_Factory::add_template('multi-choice-option', LP_Question_Multi_Choice::admin_js_template()); LP_Question_Factory::add_template('single-choice-option', LP_Question_Single_Choice::admin_js_template()); do_action('learn_press_question_factory_init', __CLASS__); }
/** * Get all types of question supported * * @return mixed */ function learn_press_question_types() { return LP_Question_Factory::get_types(); }
function evaluate_quiz_results($quiz_id, $progress) { $quiz = LP_Quiz::get_quiz($quiz_id); $results = array('mark' => 0, 'correct' => 0, 'wrong' => 0, 'empty' => 0, 'mark_percent' => 0, 'correct_percent' => 0, 'wrong_percent' => 0, 'empty_percent' => 0, 'quiz_time' => $quiz->duration, 'quiz_mark' => $quiz->get_mark(), 'user_time' => 0, 'questions' => array()); $questions = $quiz->questions; if ($questions) { $questions = array_keys($questions); } if (!empty($questions)) { foreach ($questions as $question_id) { if (!empty($progress->question_answers[$question_id])) { $question = LP_Question_Factory::get_question($question_id); $check = $question->check($progress->question_answers[$question_id]); if ($check['correct']) { $results['correct']++; } else { $results['wrong']++; } $results['mark'] += $check['mark']; } else { $check = false; $results['empty']++; } $results['questions'][$question_id] = $check; } } $results['user_time'] = $progress->end - $progress->start; return $results; }
static function save($new, $old, $post_id, $field) { global $wpdb, $post; //LP_Debug::instance()->add( __CLASS__ . '::' . __FUNCTION__ . '(' . join( ',', func_get_args() ) . ')' ); $questions = learn_press_get_request('learn_press_question'); /*if( $all_questions = LP_Quiz::get_quiz( $post->ID )->get_questions() ){ $all_questions = array_keys( $all_questions ); }*/ // Get all ids of questions stored $remove_ids = $wpdb->get_col($wpdb->prepare("\n\t\t\t\t\tSELECT question_id\n\t\t\t\t\tFROM {$wpdb->prefix}learnpress_quiz_questions\n\t\t\t\t\tWHERE quiz_id = %d\n\t\t\t\t", $post->ID)); // delete all questions stored $query = $wpdb->prepare("\n\t\t\t\tDELETE\n\t\t\t\tFROM {$wpdb->prefix}learnpress_quiz_questions\n\t\t\t\tWHERE quiz_id = %d\n\t\t\t", $post->ID, 1); $wpdb->query($query); learn_press_reset_auto_increment('learnpress_quiz_questions'); do_action('learn_press_remove_quiz_questions', $remove_ids, $post->ID); if (!$questions) { return; } $titles = learn_press_get_request('learn-press-question-name'); $values = array(); $order = 1; // update the title of questions and save all data foreach ($questions as $id => $data) { $question = LP_Question_Factory::get_question($id); if (!empty($titles[$id])) { $wpdb->update($wpdb->posts, array('post_title' => $titles[$id]), array('ID' => $id), array('%s')); } $question->save($data); $insert_data = apply_filters('learn_press_quiz_question_insert_data', array('question_id' => $id, 'quiz_id' => $post->ID, 'params' => '')); $values[] = $wpdb->prepare("(%d, %d, %s, %d)", $insert_data['quiz_id'], $insert_data['question_id'], $insert_data['param'], $order++); } $query = "\n\t\t\t\tINSERT INTO {$wpdb->learnpress_quiz_questions}(`quiz_id`, `question_id`, `params`, `question_order`)\n\t\t\t\tVALUES " . join(',', $values) . "\n\t\t\t"; $wpdb->query($query); do_action('learn_press_insert_quiz_questions', $questions, $post->ID); }
public static function convert_question_type() { if (($from = learn_press_get_request('from')) && ($to = learn_press_get_request('to')) && ($question_id = learn_press_get_request('question_id'))) { $data = array(); parse_str($_POST['data'], $data); do_action('learn_press_convert_question_type', $question_id, $from, $to, $data); $question = LP_Question_Factory::get_question($question_id, array('type' => $to)); learn_press_send_json(array('html' => $question->admin_interface(array('echo' => false)), 'icon' => $question->get_icon())); } else { throw new Exception(__('Convert question type must be specify the id, source and destination type', 'learn_press')); } die; }
/** * Load quiz question */ public static function load_quiz_question() { $quiz_id = !empty($_REQUEST['quiz_id']) ? absint($_REQUEST['quiz_id']) : 0; $question_id = !empty($_REQUEST['question_id']) ? absint($_REQUEST['question_id']) : 0; $user_id = !empty($_REQUEST['user_id']) ? absint($_REQUEST['user_id']) : 0; global $quiz; $quiz = LP_Quiz::get_quiz($quiz_id); // save question if find it self::save_question_if_needed(); //self::update_time_remaining(); $user = learn_press_get_current_user(); if ($user->id != $user_id) { learn_press_send_json(array('result' => 'error', 'message' => __('Load question error. Try again!', 'learn_press'))); } if (!$quiz_id || !$question_id) { learn_press_send_json(array('result' => 'error', 'message' => __('Something is wrong. Try again!', 'learn_press'))); } if ($question = LP_Question_Factory::get_question($question_id)) { ob_start(); if ($progress = $user->get_quiz_progress($quiz->id)) { learn_press_update_user_quiz_meta($progress->history_id, 'current_question', $question_id); } $question_answers = $user->get_question_answers($quiz->id, $question_id); $question->render(array('answered' => $question_answers)); $content = ob_get_clean(); learn_press_send_json(array('result' => 'success', 'content' => $content, 'permalink' => learn_press_get_user_question_url($quiz_id, $question_id))); } }