コード例 #1
0
    /**
     * Handles the end of the quiz/survey.
     *
     * @since 2.0
     */
    public function finishQuiz()
    {
        global $wpdb;
        $quizName = $_SESSION['wpsqt']['current_id'];
        if (isset($_SESSION['wpsqt'][$quizName]['details']['timer']) && $_SESSION['wpsqt'][$quizName]['details']['timer'] != '0' && $_SESSION['wpsqt'][$quizName]['details']['timer'] != "") {
            ?>
					<script type="text/javascript">
						jQuery(document).ready( function(){
							jQuery(".timer").hide();
						});
					</script>
				<?php 
            $time_allowed = $_SESSION['wpsqt'][$quizName]['details']['timer'] * 60;
            $start_time = $_SESSION['wpsqt'][$quizName]['start_time'];
            // Allow an extra 5 seconds per section for loading
            $loading_allowance = count($_SESSION['wpsqt'][$quizName]['sections']) * 5;
            if ($start_time + $time_allowed + $loading_allowance < time()) {
                _e('You have taken longer than the allowed time.', 'wp-survey-and-quiz-tool');
                return;
            }
        }
        if (isset($_SESSION['wpsqt'][$quizName]['details']['use_wp']) && $_SESSION['wpsqt'][$quizName]['details']['use_wp'] == 'yes') {
            $objUser = wp_get_current_user();
            if ($objUser->data != NULL) {
                $_SESSION['wpsqt'][$quizName]['person']['name'] = $objUser->user_login;
                $_SESSION['wpsqt'][$quizName]['person']['fname'] = $objUser->first_name;
                $_SESSION['wpsqt'][$quizName]['person']['lname'] = $objUser->last_name;
                $_SESSION['wpsqt'][$quizName]['person']['email'] = $objUser->user_email;
            } else {
                $_SESSION['wpsqt'][$quizName]['person']['name'] = 'Anonymous';
            }
        }
        $personName = isset($_SESSION['wpsqt'][$quizName]['person']['name']) ? $_SESSION['wpsqt'][$quizName]['person']['name'] : 'Anonymous';
        $timeTaken = microtime(true) - $_SESSION['wpsqt'][$quizName]['start_time'];
        $totalPoints = 0;
        $correctAnswers = 0;
        $canAutoMark = true;
        if ($_SESSION['wpsqt'][$quizName]['details']['type'] == 'quiz') {
            $passMark = (int) $_SESSION['wpsqt'][$quizName]['details']['pass_mark'];
            // Set $AutoMarkWhenFreetext
            if (isset($_SESSION['wpsqt'][$quizName]['details']['automark_whenfreetxt'])) {
                $AutoMarkWhenFreetxt = $_SESSION['wpsqt'][$quizName]['details']['automark_whenfreetxt'];
            } else {
                $AutoMarkWhenFreetxt = "no";
            }
        } else {
            $AutoMarkWhenFreetxt = false;
        }
        foreach ($_SESSION['wpsqt'][$quizName]['sections'] as $quizSection) {
            if ($this->_type != "quiz" || isset($quizSection['can_automark']) && $quizSection['can_automark'] == false) {
                // Only if AutoMarkWhenFreetext is set to 'yes' will $canAutoMark be ignored
                if (preg_match("/yes/", $AutoMarkWhenFreetxt) !== 1) {
                    $canAutoMark = false;
                    break;
                }
            }
            foreach ($quizSection['questions'] as $key => $question) {
                //  AutoMarkWhenFreetext: 'no' and 'include' will mark freetext questions as 'incorrect',
                // 'exclude will ignore the freetext questions' and not add them to the $totalPoints
                if (!(preg_match("/exclude/", $AutoMarkWhenFreetxt) == 1 && $question['type'] == "Free Text")) {
                    $totalPoints += $question['points'];
                }
            }
            if (!isset($quizSection['stats'])) {
                continue;
            }
            if (isset($quizSection['stats']['correct'])) {
                $correctAnswers += $quizSection['stats']['correct'];
            }
        }
        if ($canAutoMark === true) {
            $_SESSION['wpsqt']['current_score'] = sprintf(__("%d correct out of %d", 'wp-survey-and-quiz-tool'), $correctAnswers, $totalPoints);
            $_SESSION['wpsqt']['correct_answers'] = $correctAnswers;
            $_SESSION['wpsqt']['total_points'] = $totalPoints;
        } else {
            $_SESSION['wpsqt']['current_score'] = __('Quiz can\'t be auto marked', 'wp-survey-and-quiz-tool');
        }
        if ($correctAnswers !== 0) {
            $percentRight = $correctAnswers / $totalPoints * 100;
        } else {
            $percentRight = 0;
        }
        $status = 'unviewed';
        $pass = '******';
        if ($_SESSION['wpsqt'][$quizName]['details']['type'] == 'quiz') {
            // Check if pass
            if ($percentRight >= $passMark) {
                $pass = '******';
            }
            if ($pass == '1') {
                $status = 'Accepted';
            } else {
                $status = 'unviewed';
            }
        }
        if (!isset($_SESSION['wpsqt'][$quizName]['details']['store_results']) || $_SESSION['wpsqt'][$quizName]['details']['store_results'] !== "no") {
            $wpdb->query($wpdb->prepare("INSERT INTO `" . WPSQT_TABLE_RESULTS . "` (datetaken,timetaken,person,sections,item_id,person_name,ipaddress,score,total,percentage,status,pass)\n\t\t\t\t\t\t\t\tVALUES (%s,%d,%s,%s,%d,%s,%s,%d,%d,%d,%s,%d)", array($_SESSION['wpsqt'][$quizName]['start_time'], $timeTaken, serialize($_SESSION['wpsqt'][$quizName]['person']), serialize($_SESSION['wpsqt'][$quizName]['sections']), $_SESSION['wpsqt'][$quizName]['details']['id'], $personName, $_SERVER['REMOTE_ADDR'], $correctAnswers, $totalPoints, $percentRight, $status, $pass)));
            $_SESSION['wpsqt']['result_id'] = $wpdb->insert_id;
        } else {
            $_SESSION['wpsqt']['result_id'] = null;
        }
        $emailAddress = get_option('wpsqt_contact_email');
        if (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant') {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-100' && $percentRight == 100) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-75' && $percentRight > 75) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-50' && $percentRight > 50) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && isset($_SESSION['wpsqt'][$quizName]['details']['send_user']) && $_SESSION['wpsqt'][$quizName]['details']['send_user'] == 'yes') {
            $emailTrue = true;
        }
        if (isset($emailTrue)) {
            Wpsqt_Mail::sendMail();
        }
        if ($this->_type == "survey" || $this->_type == "poll") {
            $this->_cacheSurveys();
        }
        if (isset($_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie']) && $_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie'] == 'yes') {
            // Create the cookie
            ?>
			<script type="text/javascript">
			var c_name = "wpsqt_<?php 
            echo $quizName;
            ?>
_taken";
			var value = "yes"
			var exdays = 365;
			var exdate = new Date();
			exdate.setDate(exdate.getDate() + exdays);
			var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
			document.cookie=c_name + "=" + c_value;
			</script>
			<?php 
        }
        require_once Wpsqt_Core::pageView('site/' . $this->_type . '/finished.php');
        unset($_SESSION['wpsqt']['result_id']);
    }
コード例 #2
0
    /**
     * Handles the end of the quiz/survey.
     *
     * @since 2.0
     */
    public function finishQuiz()
    {
        global $wpdb;
        $quizName = $_SESSION['wpsqt']['current_id'];
        if (isset($_SESSION['wpsqt'][$quizName]['details']['timer']) && $_SESSION['wpsqt'][$quizName]['details']['timer'] != '0' && $_SESSION['wpsqt'][$quizName]['details']['timer'] != "") {
            ?>
					<script type="text/javascript">
						jQuery(document).ready( function(){
							jQuery(".timer").hide();
						});
					</script>
				<?php 
        }
        if (isset($_SESSION['wpsqt'][$quizName]['details']['use_wp']) && $_SESSION['wpsqt'][$quizName]['details']['use_wp'] == 'yes') {
            $objUser = wp_get_current_user();
            $_SESSION['wpsqt'][$quizName]['person']['name'] = $objUser->user_login;
            $_SESSION['wpsqt'][$quizName]['person']['fname'] = $objUser->first_name;
            $_SESSION['wpsqt'][$quizName]['person']['lname'] = $objUser->last_name;
            $_SESSION['wpsqt'][$quizName]['person']['email'] = $objUser->user_email;
        }
        $personName = isset($_SESSION['wpsqt'][$quizName]['person']['name']) ? $_SESSION['wpsqt'][$quizName]['person']['name'] : 'Anonymous';
        $timeTaken = microtime(true) - $_SESSION['wpsqt'][$quizName]['start_time'];
        $totalPoints = 0;
        $correctAnswers = 0;
        $canAutoMark = true;
        if ($_SESSION['wpsqt'][$quizName]['details']['type'] == 'quiz') {
            $passMark = (int) $_SESSION['wpsqt'][$quizName]['details']['pass_mark'];
        }
        foreach ($_SESSION['wpsqt'][$quizName]['sections'] as $quizSection) {
            if ($this->_type != "quiz" || isset($quizSection['can_automark']) && $quizSection['can_automark'] == false) {
                $canAutoMark = false;
                break;
            }
            foreach ($quizSection['questions'] as $key => $question) {
                $totalPoints += $question['points'];
            }
            if (!isset($quizSection['stats'])) {
                continue;
            }
            if (isset($quizSection['stats']['correct'])) {
                $correctAnswers += $quizSection['stats']['correct'];
            }
        }
        if ($canAutoMark === true) {
            $_SESSION['wpsqt']['current_score'] = $correctAnswers . " correct out of " . $totalPoints;
        } else {
            $_SESSION['wpsqt']['current_score'] = "quiz can't be auto marked";
        }
        if ($correctAnswers !== 0) {
            $percentRight = $correctAnswers / $totalPoints * 100;
        } else {
            $percentRight = 0;
        }
        $status = 'unviewed';
        $pass = '******';
        if ($_SESSION['wpsqt'][$quizName]['details']['type'] == 'quiz') {
            // Check if pass
            if ($percentRight >= $passMark) {
                $pass = '******';
            }
            if ($pass == '1') {
                $status = 'Accepted';
            } else {
                $status = 'unviewed';
            }
        }
        if (!isset($_SESSION['wpsqt'][$quizName]['details']['store_results']) || $_SESSION['wpsqt'][$quizName]['details']['store_results'] !== "no") {
            $wpdb->query($wpdb->prepare("INSERT INTO `" . WPSQT_TABLE_RESULTS . "` (datetaken,timetaken,person,sections,item_id,person_name,ipaddress,score,total,percentage,status,pass)\n\t\t\t\t\t\t\t\tVALUES (%s,%d,%s,%s,%d,%s,%s,%d,%d,%d,%s,%d)", array($_SESSION['wpsqt'][$quizName]['start_time'], $timeTaken, serialize($_SESSION['wpsqt'][$quizName]['person']), serialize($_SESSION['wpsqt'][$quizName]['sections']), $_SESSION['wpsqt'][$quizName]['details']['id'], $personName, $_SERVER['REMOTE_ADDR'], $correctAnswers, $totalPoints, $percentRight, $status, $pass)));
            $_SESSION['wpsqt']['result_id'] = $wpdb->insert_id;
        } else {
            $_SESSION['wpsqt']['result_id'] = null;
        }
        $emailAddress = get_option('wpsqt_contact_email');
        if (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant') {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-100' && $percentRight == 100) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-75' && $percentRight > 75) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && $_SESSION['wpsqt'][$quizName]['details']['notificaton_type'] == 'instant-50' && $percentRight > 50) {
            $emailTrue = true;
        } elseif (isset($_SESSION['wpsqt'][$quizName]['details']['notificaton_type']) && isset($_SESSION['wpsqt'][$quizName]['details']['send_user']) && $_SESSION['wpsqt'][$quizName]['details']['send_user'] == 'yes') {
            $emailTrue = true;
        }
        if (isset($emailTrue)) {
            Wpsqt_Mail::sendMail();
        }
        if ($this->_type == "survey" || $this->_type == "poll") {
            $this->_cacheSurveys();
        }
        if (isset($_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie']) && $_SESSION['wpsqt'][$quizName]['details']['limit_one_cookie'] == 'yes') {
            // Create the cookie
            ?>
			<script type="text/javascript">
			var c_name = "wpsqt_<?php 
            echo $quizName;
            ?>
_taken";
			var value = "yes"
			var exdays = 365;
			var exdate = new Date();
			exdate.setDate(exdate.getDate() + exdays);
			var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
			document.cookie=c_name + "=" + c_value;
			</script>
			<?php 
        }
        require_once Wpsqt_Core::pageView('site/' . $this->_type . '/finished.php');
        unset($_SESSION['wpsqt']['result_id']);
    }