function admin_script() { parent::admin_script(); ?> <script type="text/html" id="tmpl-dropdown-question-answer"> <tr> <td class="lpr-sortable-handle"> <i class="dashicons dashicons-sort"></i> </td> <td> <input type="hidden" name="lpr_question[{{data.question_id}}][answer][is_true][__INDEX__]" value="0" /> <input type="radio" data-group="lpr-question-answer-{{data.question_id}}" name="lpr_question[{{data.question_id}}][answer][is_true][__INDEX__]" value="1" /> </td> <td><input type="text" name="lpr_question[{{data.question_id}}][answer][text][__INDEX__]" value="" /></td> <td align="center"><span class=""><i class="dashicons dashicons-trash"></i></span> </td> </tr> </script> <?php }
function admin_script() { parent::admin_script(); ?> <script type="text/html" id="tmpl-multi-choice-question-answer"> <tr class="lpr-disabled"> <td class="lpr-sortable-handle"> <i class="dashicons dashicons-sort"></i> </td> <td class="lpr-is-true-answer"> <input type="hidden" name="lpr_question[{{data.question_id}}][answer][is_true][__INDEX__]" value="0" /> <input type="checkbox" name="lpr_question[{{data.question_id}}][answer][is_true][__INDEX__]" value="1" /> </td> <td><input class="lpr-answer-text" type="text" name="lpr_question[{{data.question_id}}][answer][text][__INDEX__]" value="" /></td> <td align="center" class="lpr-remove-answer"><span class=""><i class="dashicons dashicons-trash"></i></span> </td> </tr> </script> <?php }
/** * Enqueue scripts */ function enqueue_script() { if (!in_array(get_post_type(), array('lpr_question'))) { return; } ob_start(); ?> <script> var form = $('#post'); form.submit(function (evt) { var $title = $('#title'), is_error = false; if (0 == $title.val().length) { alert('<?php _e('Please enter the title of the question', 'learn_press'); ?> '); $title.focus(); is_error = true; } else if ( $('.lpr-question-types').length && ( 0 == $('.lpr-question-types').val().length ) ) { alert('<?php _e('Please a type of question', 'learn_press'); ?> '); $('.lpr-question-types').focus(); is_error = true; } if (is_error) { evt.preventDefault(); return false; } }); </script> <?php $script = ob_get_clean(); $script = preg_replace('!</?script>!', '', $script); learn_press_enqueue_script($script); ob_start(); ?> <script type="text/html" id="tmpl-form-quick-add-question"> <div id="lpr-form-quick-add-question" class="lpr-quick-add-form"> <input type="text"> <select class="lpr-question-types lpr-select2" name="lpr_question[type]" id="lpr-quiz-question-type"> <?php if ($questions = lpr_get_question_types()) { ?> <?php foreach ($questions as $type) { $question = LPR_Question_Type::instance($type); if (!$question) { continue; } ?> <option value="<?php echo $type; ?> "><?php echo $question->get_name(); ?> </option> <?php } ?> <?php } ?> </select> <button class="button" data-action="add" type="button"><?php _e('Add [Enter]', 'learn_press'); ?> </button> <button data-action="cancel" class="button" type="button"><?php _e('Cancel [ESC]', 'learn_press'); ?> </button> <span class="lpr-ajaxload">...</span> </div> </script> <?php $js_template = ob_get_clean(); learn_press_enqueue_script($js_template, true); }
static function html($meta, $field) { global $post; $post_id = $post->ID; $questions = lpr_get_question_types(); $question = get_post_meta($post_id, '_lpr_question', true); $question = wp_parse_args($question, array('type' => null)); ob_start(); //print_r($question_type); ?> <script type="text/javascript">var lpr_question_id = <?php echo intval($post_id); ?> ;</script> <div id="lpr-question-options-wrap"> <select class="lpr-question-types" name="lpr_question[type]" id="lpr_question-type" data-type="<?php echo $question['type']; ?> "> <option value=""><?php _e('Select...'); ?> </option> <?php if ($questions) { ?> <?php foreach ($questions as $type) { ?> <option value="<?php echo $type; ?> " <?php selected(isset($question['type']) && $type == $question['type'] ? 1 : 0, 1); ?> ><?php echo LPR_Question_Type::instance($type)->get_name(); ?> </option> <?php } ?> <?php } ?> </select> <div class="lpr-question-settings"> <?php if (isset($question['type'])) { ?> <?php LPR_Question_Type::instance($question['type'], array('ID' => $post_id))->admin_interface(); ?> <?php } ?> </div> <?php if (in_array($question['type'], array('true_or_false', 'multiple_choice', 'single_choice'))) { ?> <strong>Tips:</strong> <p><i>In <strong>Answer text</strong>, press Enter/Tab key to move to next</i></p> <p><i>In <strong>Answer text</strong>, when the text is empty press Delete/Back Space or click out side to remove</i></p> <p><i>In <strong>Answer text</strong>, press ESC to restore the text at the last time edited</i></p> <?php } ?> </div> <script type="text/javascript"> </script> <?php return ob_get_clean(); }
function get_current_question($quiz_id, $what = '') { $current = $this->get_current_question_id($quiz_id); echo $what; if ($what == 'id') { return $current; } else { $question = LPR_Question_Type::instance($current); switch ($what) { case 'html': if ($question) { ob_start(); $question->render(); $current = ob_get_clean(); } } } return $current; }
function enqueue_script() { if ('lpr_quiz' != get_post_type()) { return; } ob_start(); ?> <script> var form = $('#post'); form.submit(function(evt){ var $title = $('#title'), is_error = false; window.learn_press_before_update_quiz_message = []; if( 0 == $title.val().length ){ window.learn_press_before_update_quiz_message.push( '<?php _e('Please enter the title of the quiz', 'learn_press'); ?> ' ); $title.focus(); is_error = true; } /* hook */ is_error = form.triggerHandler( 'learn_press_question_before_update' ) === false; if( window.learn_press_before_update_quiz_message.length /*true == is_error*/ ){ if( window.learn_press_before_update_quiz_message.length ){ alert( "Error: \n" + window.learn_press_before_update_quiz_message.join( "\n\n" ) ) } evt.preventDefault(); return false; } }); </script> <?php $script = ob_get_clean(); $script = preg_replace('!</?script>!', '', $script); learn_press_enqueue_script($script); ob_start(); ?> <script type="text/html" id="tmpl-form-quick-add-question"> <div id="lpr-form-quick-add-question" class="lpr-quick-add-form"> <input type="text"> <select class="lpr-question-types lpr-select2" name="lpr_question[type]" id="lpr-quiz-question-type"> <?php if ($questions = lpr_get_question_types()) { ?> <?php foreach ($questions as $type) { ?> <option value="<?php echo $type; ?> "><?php echo LPR_Question_Type::instance($type)->get_name(); ?> </option> <?php } ?> <?php } ?> </select> <button class="button" data-action="add" type="button"><?php _e('Add [Enter]', 'learnpress'); ?> </button> <button data-action="cancel" class="button" type="button"><?php _e('Cancel [ESC]', 'learnpress'); ?> </button> <span class="lpr-ajaxload">...</span> </div> </script> <?php $js_template = ob_get_clean(); learn_press_enqueue_script($js_template, true); }
/** * Get the information result of a quiz * * @param int $user_id * @param int $quiz_id * @return array */ function learn_press_get_quiz_result($user_id = null, $quiz_id = null) { global $quiz; if (!$user_id) { $user_id = get_current_user_id(); } $quiz_id = $quiz_id ? $quiz_id : ($quiz ? $quiz->ID : 0); $questions = learn_press_get_quiz_questions($quiz_id); $answers = learn_press_get_question_answers($user_id, $quiz_id); $mark = 0; $correct_questions = 0; $wrong_questions = 0; $empty_questions = 0; $quiz_start = learn_press_get_user_quiz_data('_lpr_quiz_start_time', $user_id, $quiz_id); $quiz_end = learn_press_get_user_quiz_data('_lpr_quiz_completed', $user_id, $quiz_id); $mark_total = learn_press_get_quiz_mark($quiz_id); $quiz_time = $quiz_end ? $quiz_end - $quiz_start : 0; $info = false; if ($questions) { foreach ($questions as $question_id => $options) { $ques_object = LPR_Question_Type::instance($question_id); if ($ques_object && isset($answers[$question_id])) { $check = $ques_object->check(array('answer' => $answers[$question_id])); if ($check['correct']) { //$mark += $check['mark']; $correct_questions++; } else { $wrong_questions++; } $mark += isset($check['mark']) ? $check['mark'] : 0; } else { $empty_questions++; } } $question_count = count($questions); if (is_float($mark)) { $mark = round($mark, 1); } $info = array('mark' => $mark, 'correct' => $correct_questions, 'wrong' => $wrong_questions, 'empty' => $empty_questions, 'questions_count' => $question_count, 'mark_total' => round($mark_total, 2), 'mark_percent' => round($mark / $mark_total, 2), 'correct_percent' => round($correct_questions / $question_count * 100, 2), 'wrong_percent' => round($wrong_questions / $question_count * 100, 2), 'empty_percent' => round($empty_questions / $question_count * 100, 2), 'quiz_time' => $quiz_time); } return apply_filters('learn_press_get_quiz_result', $info, $user_id, $quiz_id); }
function __construct($type = null, $options = null) { parent::__construct($type, $options); }
/** * 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; // 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 = LPR_Question_Type::instance($question_id)) { ob_start(); $question->render(); $content = ob_get_clean(); learn_press_send_json(array('result' => 'success', 'content' => $content, 'permalink' => learn_press_get_user_question_url($quiz_id, $question_id))); } }
function learn_press_output_question($question_id, $with_answered = true, $quiz_id = 0) { if (!$question_id && $quiz_id) { /*if( $questions = learn_press_get_quiz_questions( $quiz_id ) ){ $question_id = reset( $questions ); }*/ } if ($question_id) { $question = LPR_Question_Type::instance($question_id); $answered = null; if ($with_answered) { $answers = learn_press_get_question_answers(null, learn_press_get_quiz_id(0)); if (isset($answers[$question_id])) { $answered = array('answer' => $answers[$question_id]); } } $question && $question->render($answered); } }
static function html($meta, $field) { global $post; $post_id = $post->ID; $current_user = get_current_user_id(); $questions = lpr_get_question_types(); $lpr_questions = (array) get_post_meta($post_id, '_lpr_quiz_questions', true); $qids = array_keys($lpr_questions); $qoptions = array_values($lpr_questions); ob_start(); ?> <script type="text/javascript">var lpr_quiz_id = <?php echo intval($post_id); ?> </script> <div id="lpr-quiz-questions-wrap"> <p align="right" class="lpr-questions-toggle"> <a href="" data-action="expand"><?php _e('Expand All', 'learn_press'); ?> </a> <a href="" data-action="collapse"><?php _e('Collapse All', 'learn_press'); ?> </a> </p> <div id="lpr-quiz-questions"> <?php if ($qids) { $index = 0; ?> <?php foreach ($qids as $question_id) { ?> <?php if ($question = LPR_Question_Type::instance($question_id)) { $question->admin_interface($qoptions[$index++]); } ?> <?php } ?> <?php } ?> </div> <p style="vertical-align: middle;"> <div class="btn-group" id="lpr-add-new-question-type"> <button type="button" class="btn btn-default" data-type="single_choice"><?php _e('Add new Question', 'learn_press'); ?> </button> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <span class="caret"></span> <span class="sr-only"><?php _e('Toggle Dropdown', 'learn_press'); ?> </span> </button> <ul class="dropdown-menu" role="menu"> <?php if ($questions) { ?> <?php foreach ($questions as $type) { ?> <li><a href="" rel="<?php echo $type; ?> "><?php echo LPR_Question_Type::instance($type)->get_name(); ?> </a> </li> <?php } ?> <?php } ?> </ul> </div> <?php _e('-Or-', 'learn_press'); ?> <select class="lpr-select2" name="" id="lpr-quiz-question-select-existing" style="width:300px"> <option value=""><?php _e('--Select existing question--', 'learn_press'); ?> </option> <?php $query_args = array('post_type' => 'lpr_question', 'post_status' => 'publish', 'author' => $current_user, 'posts_per_page' => -1, 'post__not_in' => $qids); $query = new WP_Query($query_args); if ($query->have_posts()) { while ($query->have_posts()) { $p = $query->next_post(); echo '<option value="' . $p->ID . '" data-type="">' . $p->post_title . '</option>'; } } ?> </select> </p> </div> <?php return ob_get_clean(); }
/** * Prints the header of a question in admin mode * should call this function before in the top of admin_interface in extends class * * @param array $args * * @reutrn void */ function admin_interface_head($args = array()) { $post_id = $this->get('ID'); settype($args, 'array'); $is_collapse = array_key_exists('toggle', $args) && !$args['toggle']; $questions = lpr_get_question_types(); ?> <div class="lpr-question lpr-question-<?php echo preg_replace('!_!', '-', $this->get_type()); ?> " data-id="<?php echo $this->get('ID'); ?> "> <div class="lpr-question-head"> <p> <a href="<?php echo get_edit_post_link($post_id); ?> "><?php _e('Edit', 'learn_press'); ?> </a> <a href="" data-action="remove"><?php _e('Remove', 'learn_press'); ?> </a> <a href="" data-action="expand" class="<?php echo !$is_collapse ? "hide-if-js" : ""; ?> "><?php _e('Expand', 'learn_press'); ?> </a> <a href="" data-action="collapse" class="<?php echo $is_collapse ? "hide-if-js" : ""; ?> "><?php _e('Collapse', 'learn_press'); ?> </a> </p> <select name="lpr_question[<?php echo $post_id; ?> ][type]" data-type="<?php echo $this->get_type(); ?> "> <?php if ($questions) { foreach ($questions as $type) { ?> <?php $question = LPR_Question_Type::instance($type); ?> <?php if ($question) { ?> <option value="<?php echo $type; ?> " <?php selected($this->get_type() == $type ? 1 : 0, 1); ?> > <?php echo $question->get_name(); ?> </option> <?php } ?> <?php } } ?> </select><!--<strong><i>[ <?php echo $this->get_name(); ?> ]</i></strong>--> <span class="lpr-question-title"><input class="inactive" type="text" name="lpr_question[<?php echo $this->get('ID'); ?> ][text]" value="<?php echo esc_attr($this->get('post_title')); ?> " /></span> </div> <div class="lpr-question-content<?php echo $is_collapse ? " hide-if-js" : ""; ?> "> <?php //do_action( 'learn_press_admin_before_question_answer', $this ); ?> <p class="lpr-question-option-label"><?php _e('Answer', 'learn_press'); ?> </p> <?php }