예제 #1
0
<?php

// Set up the token object
require_once WPSQT_DIR . '/lib/Wpsqt/Tokens.php';
$objTokens = Wpsqt_Tokens::getTokenObject();
$objTokens->setDefaultValues();
?>

<?php 
$pollName = $quizName;
$pollId = $_SESSION['wpsqt'][$pollName]['details']['id'];
if ($_SESSION['wpsqt'][$pollName]['details']['finish_display'] == 'Poll results') {
    require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
    require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
    Wpsqt_Page_Main_Results_Poll::displayResults($pollId);
} else {
    if (!empty($_SESSION['wpsqt'][$pollName]['details']['finish_message'])) {
        $message = $_SESSION['wpsqt'][$pollName]['details']['finish_message'];
        $message = $objTokens->doReplacement($message);
        echo nl2br($message);
    } else {
        echo '<h2>';
        _e('Thank you for taking the poll.', 'wp-survey-and-quiz-tool');
        echo '</h2>';
    }
}
    public function display()
    {
        global $wpdb;
        // Check and see if there is a major issue.
        if (!empty($this->_errors)) {
            global $message;
            if (isset($this->_errors["session"])) {
                $message = __("PHP Sessions error. Check your sessions settings.", 'wp-survey-and-quiz-tool');
            } elseif (isset($this->_errors["noexist"])) {
                $message = __("No such quiz/survey/poll", 'wp-survey-and-quiz-tool');
            } elseif (isset($this->_errors['name'])) {
                $message = __("No quiz identifier/name was given", 'wp-survey-and-quiz-tool');
            } elseif (isset($this->_errors["type"])) {
                $message = __("Invalid type given", 'wp-survey-and-quiz-tool');
            }
            $message = apply_filters("wpsqt_" . $this->_type . "_error", $message, $this->_errors);
            echo $message;
            return;
        }
        $quizName = $_SESSION['wpsqt']['current_id'];
        // Checks if the quiz/survey/poll is disabled
        if (isset($_SESSION['wpsqt'][$quizName]['details']['status']) && $_SESSION['wpsqt'][$quizName]['details']['status'] == 'disabled') {
            printf(__('This %s is currently disabled.', 'wp-survey-and-quiz-tool'), $_SESSION['wpsqt'][$quizName]['details']['type']);
            return;
        }
        // Checks if limiting per IP is enabled and if the user has already taken it
        if (isset($_SESSION['wpsqt'][$quizName]['details']['limit_one']) && $_SESSION['wpsqt'][$quizName]['details']['limit_one'] == 'yes') {
            $item_id = $_SESSION['wpsqt'][$quizName]['details']['id'];
            $ip = $_SERVER['REMOTE_ADDR'];
            $results = $wpdb->get_results('SELECT * FROM `' . WPSQT_TABLE_RESULTS . '` WHERE `ipaddress` = "' . $ip . '" AND `item_id` = "' . $item_id . '"', ARRAY_A);
            if (count($results) != 0) {
                printf(__('You appear to have already taken this %s.', 'wp-survey-and-quiz-tool'), $this->_type);
                if (($this->_type == 'poll' || $this->_type == 'survey') && isset($_SESSION['wpsqt'][$quizName]['details']['show_results_limited']) && $_SESSION['wpsqt'][$quizName]['details']['show_results_limited'] == 'yes') {
                    require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
                    require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
                    Wpsqt_Page_Main_Results_Poll::displayResults($item_id);
                }
                return;
            }
        }
        // Checks if limiting per WP user is enabled and if the user has already taken it
        if (isset($_SESSION['wpsqt'][$quizName]['details']['limit_one_wp']) && $_SESSION['wpsqt'][$quizName]['details']['limit_one_wp'] == 'yes') {
            global $user_login;
            $item_id = $_SESSION['wpsqt'][$quizName]['details']['id'];
            $results = $wpdb->get_results('SELECT * FROM `' . WPSQT_TABLE_RESULTS . '` WHERE `item_id` = "' . $item_id . '"', ARRAY_A);
            foreach ($results as $result) {
                if (isset($result['person_name']) && $result['person_name'] == $user_login) {
                    printf(__('You appear to have already taken this %s.', 'wp-survey-and-quiz-tool'), $this->_type);
                    if (($this->_type == 'poll' || $this->_type == 'survey') && isset($_SESSION['wpsqt'][$quizName]['details']['show_results_limited']) && $_SESSION['wpsqt'][$quizName]['details']['show_results_limited'] == 'yes') {
                        $id = (int) $_SESSION['wpsqt']['item_id'];
                        $result = $wpdb->get_row("SELECT * FROM `" . WPSQT_TABLE_SURVEY_CACHE . "` WHERE item_id = '" . $id . "'", ARRAY_A);
                        $sections = unserialize($result['sections']);
                        require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
                        require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
                        Wpsqt_Page_Main_Results_Poll::displayResults($id);
                    }
                    return;
                }
            }
        }
        // Checks if limiting by cookie is enabled and if the user has already taken it
        if (isset($_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie']) && $_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie'] == 'yes') {
            $quizNameEscaped = str_replace(" ", "_", $quizName);
            if (isset($_COOKIE['wpsqt_' . $quizNameEscaped . '_taken']) && $_COOKIE['wpsqt_' . $quizNameEscaped . '_taken'] == 'yes') {
                printf(__('You appear to have already taken this %s.', 'wp-survey-and-quiz-tool'), $this->_type);
                if (($this->_type == 'poll' || $this->_type == 'survey') && isset($_SESSION['wpsqt'][$quizName]['details']['show_results_limited']) && $_SESSION['wpsqt'][$quizName]['details']['show_results_limited'] == 'yes') {
                    $id = (int) $_SESSION['wpsqt']['item_id'];
                    $result = $wpdb->get_row("SELECT * FROM `" . WPSQT_TABLE_SURVEY_CACHE . "` WHERE item_id = '" . $id . "'", ARRAY_A);
                    $sections = unserialize($result['sections']);
                    require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
                    require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
                    Wpsqt_Page_Main_Results_Poll::displayResults($id);
                }
                return;
            }
        }
        // handle contact form and all the stuff that comes with it.
        if (isset($_SESSION['wpsqt'][$quizName]['details']['contact']) && $_SESSION['wpsqt'][$quizName]['details']['contact'] == "yes" && $this->_step <= 1) {
            $fields = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPSQT_TABLE_FORMS . "` WHERE item_id = %d ORDER BY id ASC", array($_SESSION['wpsqt'][$quizName]['details']['id'])), ARRAY_A);
            $fields = apply_filters("wpsqt_" . $this->_type . "_form_fields", $fields);
            if ($this->_step == 1) {
                $errors = array();
                $_SESSION['wpsqt'][$quizName]['person'] = array();
                foreach ($fields as $key => $field) {
                    if (empty($field)) {
                        continue;
                    }
                    $fieldName = preg_replace('~[^a-z0-9]~i', '', $field['name']);
                    $fields[$key]['value'] = $_POST["Custom_" . $fieldName];
                    if (!isset($_POST["Custom_" . $fieldName]) || empty($_POST["Custom_" . $fieldName])) {
                        if ($field['required'] == 'yes') {
                            /* translators: %s is the question name */
                            $errors[] = sprintf(__('%s is required', 'wp-survey-and-quiz-tool'), $field['name']);
                        }
                    } else {
                        $field['value'] = $_POST["Custom_" . $fieldName];
                        $_SESSION['wpsqt'][$quizName]['person'][strtolower($field['name'])] = $_POST["Custom_" . $fieldName];
                    }
                }
                if (!empty($errors)) {
                    do_action("wpsqt_" . $this->_type . "_form", "original");
                    require Wpsqt_Core::pageView('site/shared/custom-form.php');
                    return;
                }
            } else {
                do_action("wpsqt_" . $this->_type . "_form", "original");
                require Wpsqt_Core::pageView('site/shared/custom-form.php');
                return;
            }
        }
        if (isset($_SESSION['wpsqt'][$quizName]['details']['contact']) && $_SESSION['wpsqt'][$quizName]['details']['contact'] == "yes") {
            $this->_key = $this->_step - 1;
        } else {
            $this->_key = $this->_step;
        }
        // Handles the timer if enabled
        if (is_page() || is_single()) {
            if (isset($_SESSION['wpsqt'][$quizName]['details']['timer']) && $_SESSION['wpsqt'][$quizName]['details']['timer'] != '0' && $_SESSION['wpsqt'][$quizName]['details']['timer'] != "") {
                $timerVal = (int) $_SESSION['wpsqt'][$quizName]['details']['timer'] * 60;
                if ($this->_key != 0) {
                    // Resume timer
                    $timerVal = $timerVal - $_POST['wpsqt_time_elapsed'];
                }
                echo '<div class="timer" style="float: right;">&nbsp;</div>';
                $timerStrings = array('timeleft' => __('Time Left:', 'wp-survey-and-quiz-tool'), 'mins' => __('minutes and', 'wp-survey-and-quiz-tool'), 'secs' => __('seconds', 'wp-survey-and-quiz-tool'), 'outoftime' => __('Unfortunately you have run out of time for this quiz', 'wp-survey-and-quiz-tool'));
                ?>
					<script type="text/javascript">
						jQuery(document).ready( function(){
							var timeSecs = <?php 
                echo $timerVal;
                ?>
;
							var refreshId = setInterval(function() {
								if (timeSecs != 0) {
									timeSecs = timeSecs - 1;
									var timeMins = timeSecs / 60;
									timeMins = (timeMins<0?-1:+1)*Math.floor(Math.abs(timeMins)); // Gets rid of the decimal place
									var timeSecsRem = timeSecs % 60;
									if (timeMins > 0) {
										jQuery(".timer").html("<?php 
                echo $timerStrings['timeleft'];
                ?>
 " + timeMins + " <?php 
                echo $timerStrings['mins'];
                ?>
 " + timeSecsRem + " <?php 
                echo $timerStrings['secs'];
                ?>
");
									} else {
										jQuery(".timer").html("<?php 
                echo $timerStrings['timeleft'];
                ?>
" + timeSecsRem + " <?php 
                echo $timerStrings['secs'];
                ?>
");
									}

									var timeElapsed = jQuery(".wpsqt_time_elapsed");
									timeElapsed.attr('value', parseInt(timeElapsed.attr('value')) + 1);
								} else {
									jQuery(".quiz").html("<?php 
                echo $timerStrings['outoftime'];
                ?>
");
									jQuery(".timer").hide();
								}
							}, 1000);
						});
					</script>
				<?php 
            }
        }
        // if we are still here then we are to
        // show the section with some questions and stuff.
        $requiredQuestions = array('exist' => 0, 'given' => array());
        if ($this->_key != 0) {
            // We should have data to deal with.
            $incorrect = 0;
            $correct = 0;
            $pastSectionKey = $this->_key - 1;
            if (!$this->_restore) {
                $_SESSION['wpsqt'][$quizName]['sections'][$pastSectionKey]['answers'] = array();
            }
            $canAutoMark = true;
            if (isset($_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"]) && is_array($_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"])) {
                foreach ($_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"] as $questionData) {
                    if (isset($questionData['required']) && $questionData['required'] == "yes") {
                        $requiredQuestions['exist']++;
                    }
                }
            }
            if (isset($_POST['answers'])) {
                foreach ($_POST['answers'] as $questionKey => $givenAnswers) {
                    $answerMarked = array();
                    $questionData = isset($_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"][$questionKey]) ? $_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"][$questionKey] : array();
                    $questionId = $questionData['id'];
                    if ($questionData["type"] == "Single" || $questionData["type"] == "Multiple") {
                        if (!isset($_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["questions"][$questionKey])) {
                            $incorrect++;
                            continue;
                        }
                        // END if isset question
                        $subNumOfCorrect = 0;
                        $subCorrect = 0;
                        $subIncorrect = 0;
                        foreach ($questionData["answers"] as $answerKey => $rawAnswers) {
                            $numberVarName = "";
                            if ($rawAnswers["correct"] == "yes") {
                                $numberVarName = "subCorrect";
                                $subNumOfCorrect++;
                            } else {
                                $numberVarName = "subIncorrect";
                            }
                            if (in_array($answerKey, $givenAnswers)) {
                                ${$numberVarName}++;
                            }
                        }
                        if ($subCorrect === $subNumOfCorrect && $subIncorrect === 0) {
                            $correct += $questionData["points"];
                            __('correct', 'wp-survey-and-quiz-tool');
                            $answerMarked['mark'] = 'correct';
                        } else {
                            // TODO Insert ability to set point per answer scores
                            $incorrect += $questionData["points"];
                            __('incorrect', 'wp-survey-and-quiz-tool');
                            $answerMarked['mark'] = 'incorrect';
                        }
                    } else {
                        $canAutoMark = false;
                    }
                    // END if section type == multiple
                    if (isset($questionData['required']) && $questionData['required'] == 'yes') {
                        if ($questionData['type'] == 'Free Text') {
                            if ($givenAnswers[0] != '') {
                                $requiredQuestions['given'][] = $questionId;
                            }
                        } else {
                            $requiredQuestions['given'][] = $questionId;
                        }
                    }
                    $answerMarked["given"] = $givenAnswers;
                    $_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["answers"][$questionId] = $answerMarked;
                }
                // END foreach answer
                $_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["stats"] = array("correct" => $correct, "incorrect" => $incorrect);
                $_SESSION["wpsqt"][$quizName]["sections"][$pastSectionKey]["can_automark"] = $canAutoMark;
            }
            // END if isset($_POST['answers'])
        }
        if (isset($requiredQuestions) && $requiredQuestions['exist'] > sizeof($requiredQuestions['given']) && !$this->_restore) {
            $_SESSION['wpsqt']['current_message'] = __('Not all the required questions were answered!', 'wp-survey-and-quiz-tool');
            $this->_step--;
            $this->_key--;
        }
        $_SESSION['wpsqt']['current_step'] = $this->_step;
        $_SESSION['wpsqt']['required'] = $requiredQuestions;
        // if equal or greater than so other
        // plugins can add extra steps at the end.
        if (sizeof($_SESSION["wpsqt"][$quizName]["sections"]) <= $this->_key && !isset($_POST['wpsqt-save-state'])) {
            // finished!
            do_action("wpsqt_" . $this->_type . "_finished", $this->_step);
            $this->finishQuiz();
            return;
        } else {
            // Show section.
            do_action("wpsqt_" . $this->_type . "_step", $this->_step);
            $this->showSection();
            if (isset($_SESSION['wpsqt'][$quizName]['details']['show_progress_bar']) && $_SESSION['wpsqt'][$quizName]['details']['show_progress_bar'] == 'yes') {
                // Progress bar
                $current_step = $this->_step + 1;
                // If there is a contact page then loose 1
                if (isset($_SESSION['wpsqt'][$quizName]['details']['contact']) && $_SESSION['wpsqt'][$quizName]['details']['contact'] == "yes") {
                    $current_step--;
                }
                printf(__('Page %d out of %d', 'wp-survey-and-quiz-tool'), $current_step, sizeof($_SESSION["wpsqt"][$quizName]["sections"]));
                $percentage = $current_step / sizeof($_SESSION["wpsqt"][$quizName]["sections"]) * 100;
                ?>
				<div class="wpsqt-progress">
					<div style="width: <?php 
                echo $percentage;
                ?>
%;">
					</div>
				</div>

				<?php 
            }
            return;
        }
    }
예제 #3
0
 public function shortcode_survey_results($atts)
 {
     ob_start();
     ob_clean();
     global $wpdb;
     extract(shortcode_atts(array('name' => 'false', 'show_chart' => 'false'), $atts));
     if ($name == 'false') {
         echo 'No survey name was supplied.';
     } else {
         echo 'Results for ' . $name;
         // Get the ID
         $surveyId = $wpdb->get_row("SELECT `id` FROM `" . WPSQT_TABLE_QUIZ_SURVEYS . "` WHERE `name` = '" . $name . "'", ARRAY_A);
         $surveyId = (int) $surveyId['id'];
         if ($show_chart == 'false') {
             // Just reuse the same page view that the admin thing uses
             require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
             require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
             Wpsqt_Page_Main_Results_Poll::displayResults($surveyId);
         } else {
             $result = $wpdb->get_row("SELECT * FROM `" . WPSQT_TABLE_SURVEY_CACHE . "` WHERE item_id = '" . $surveyId . "'", ARRAY_A);
             $sections = unserialize($result['sections']);
             require_once WPSQT_DIR . 'pages/admin/surveys/result.total.script.site.php';
         }
     }
     $contents = ob_get_contents();
     ob_end_clean();
     return $contents;
 }
예제 #4
0
파일: Core.php 프로젝트: uoyknaht/kc
 public function shortcode_survey_results($atts)
 {
     ob_start();
     ob_clean();
     global $wpdb;
     extract(shortcode_atts(array('name' => false), $atts));
     if ($name == false) {
         echo 'No survey name was supplied.';
     } else {
         echo 'Results for ' . $name;
         // Get the ID
         $surveyId = $wpdb->get_row("SELECT `id` FROM `" . WPSQT_TABLE_QUIZ_SURVEYS . "` WHERE `name` = '" . $name . "'", ARRAY_A);
         $surveyId = (int) $surveyId['id'];
         // Just reuse the same page view that the admin thing uses
         require_once WPSQT_DIR . '/lib/Wpsqt/Page.php';
         require_once WPSQT_DIR . '/lib/Wpsqt/Page/Main/Results/Poll.php';
         Wpsqt_Page_Main_Results_Poll::displayResults($surveyId);
     }
     $contents = ob_get_contents();
     ob_end_clean();
     return $contents;
 }