private function handleAddInToplist(WpProQuiz_Model_Quiz $quiz) { if (!wp_verify_nonce($this->_post['token'], 'wpProQuiz_toplist')) { return array('text' => __('An error has occurred.', 'wp-pro-quiz'), 'clear' => true); } if (!isset($this->_post['points']) || !isset($this->_post['totalPoints'])) { return array('text' => __('An error has occurred.', 'wp-pro-quiz'), 'clear' => true); } $quizId = $quiz->getId(); $userId = get_current_user_id(); $points = (int) $this->_post['points']; $totalPoints = (int) $this->_post['totalPoints']; $name = !empty($this->_post['name']) ? trim($this->_post['name']) : ''; $email = !empty($this->_post['email']) ? trim($this->_post['email']) : ''; $ip = filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP); $captchaAnswer = !empty($this->_post['captcha']) ? trim($this->_post['captcha']) : ''; $prefix = !empty($this->_post['prefix']) ? trim($this->_post['prefix']) : ''; $quizMapper = new WpProQuiz_Model_QuizMapper(); $toplistMapper = new WpProQuiz_Model_ToplistMapper(); if ($quiz == null || $quiz->getId() == 0 || !$quiz->isToplistActivated()) { return array('text' => __('An error has occurred.', 'wp-pro-quiz'), 'clear' => true); } if (!$this->preCheck($quiz->getToplistDataAddPermissions(), $userId)) { return array('text' => __('An error has occurred.', 'wp-pro-quiz'), 'clear' => true); } $numPoints = $quizMapper->sumQuestionPoints($quizId); if ($totalPoints > $numPoints || $points > $numPoints) { return array('text' => __('An error has occurred.', 'wp-pro-quiz'), 'clear' => true); } $clearTime = null; if ($quiz->isToplistDataAddMultiple()) { $clearTime = $quiz->getToplistDataAddBlock() * 60; } if ($userId > 0) { if ($toplistMapper->countUser($quizId, $userId, $clearTime)) { return array('text' => __('You can not enter again.', 'wp-pro-quiz'), 'clear' => true); } $user = wp_get_current_user(); $email = $user->user_email; $name = $user->display_name; } else { if ($toplistMapper->countFree($quizId, $name, $email, $ip, $clearTime)) { return array('text' => __('You can not enter again.', 'wp-pro-quiz'), 'clear' => true); } if (empty($name) || empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL) === false) { return array('text' => __('No name or e-mail entered.', 'wp-pro-quiz'), 'clear' => false); } if (strlen($name) > 15) { return array('text' => __('Your name can not exceed 15 characters.', 'wp-pro-quiz'), 'clear' => false); } if ($quiz->isToplistDataCaptcha()) { $captcha = WpProQuiz_Helper_Captcha::getInstance(); if ($captcha->isSupported()) { if (!$captcha->check($prefix, $captchaAnswer)) { return array('text' => __('You entered wrong captcha code.', 'wp-pro-quiz'), 'clear' => false); } } } } $toplist = new WpProQuiz_Model_Toplist(); $toplist->setQuizId($quizId)->setUserId($userId)->setDate(time())->setName($name)->setEmail($email)->setPoints($points)->setResult(round($points / $totalPoints * 100, 2))->setIp($ip); $toplistMapper->save($toplist); return true; }
/** * @param DOMDocument $dom * @param WpProQuiz_Model_Quiz $quiz * @param $forms * @return DOMElement */ private function getQuizElement($dom, $quiz, $forms) { $quizElement = $dom->createElement('quiz'); $title = $dom->createElement('title'); $title->appendChild($dom->createCDATASection($quiz->getName())); $title->setAttribute('titleHidden', $this->booleanToTrueOrFalse($quiz->isTitleHidden())); $quizElement->appendChild($title); $quizElement->appendChild($text = $dom->createElement('text')); $text->appendChild($dom->createCDATASection($quiz->getText())); $quizElement->appendChild($dom->createElement('category', $quiz->getCategoryName())); if (is_array($quiz->getResultText())) { $resultArray = $quiz->getResultText(); $result = $dom->createElement('resultText'); $result->setAttribute('gradeEnabled', $this->booleanToTrueOrFalse($quiz->isResultGradeEnabled())); for ($i = 0; $i < count($resultArray); $i++) { $r = $dom->createElement('text'); $r->appendChild($dom->createCDATASection($resultArray['text'][$i])); $r->setAttribute('prozent', $resultArray['prozent'][$i]); $result->appendChild($r); } $quizElement->appendChild($result); } else { $result = $dom->createElement('resultText'); $result->setAttribute('gradeEnabled', $this->booleanToTrueOrFalse($quiz->isResultGradeEnabled())); $result->appendChild($dom->createCDATASection($quiz->getResultText())); $quizElement->appendChild($result); } $quizElement->appendChild($dom->createElement('btnRestartQuizHidden', $this->booleanToTrueOrFalse($quiz->isBtnRestartQuizHidden()))); $quizElement->appendChild($dom->createElement('btnViewQuestionHidden', $this->booleanToTrueOrFalse($quiz->isBtnViewQuestionHidden()))); $quizElement->appendChild($dom->createElement('questionRandom', $this->booleanToTrueOrFalse($quiz->isQuestionRandom()))); $quizElement->appendChild($dom->createElement('answerRandom', $this->booleanToTrueOrFalse($quiz->isAnswerRandom()))); $quizElement->appendChild($dom->createElement('timeLimit', $quiz->getTimeLimit())); $quizElement->appendChild($dom->createElement('showPoints', $this->booleanToTrueOrFalse($quiz->isShowPoints()))); $statistic = $dom->createElement('statistic'); $statistic->setAttribute('activated', $this->booleanToTrueOrFalse($quiz->isStatisticsOn())); $statistic->setAttribute('ipLock', $quiz->getStatisticsIpLock()); $quizElement->appendChild($statistic); $quizElement->appendChild($quizRunOnce = $dom->createElement('quizRunOnce', $this->booleanToTrueOrFalse($quiz->isQuizRunOnce()))); $quizRunOnce->setAttribute('type', $quiz->getQuizRunOnceType()); $quizRunOnce->setAttribute('cookie', $this->booleanToTrueOrFalse($quiz->isQuizRunOnceCookie())); $quizRunOnce->setAttribute('time', $quiz->getQuizRunOnceTime()); $quizElement->appendChild($dom->createElement('numberedAnswer', $this->booleanToTrueOrFalse($quiz->isNumberedAnswer()))); $quizElement->appendChild($dom->createElement('hideAnswerMessageBox', $this->booleanToTrueOrFalse($quiz->isHideAnswerMessageBox()))); $quizElement->appendChild($dom->createElement('disabledAnswerMark', $this->booleanToTrueOrFalse($quiz->isDisabledAnswerMark()))); $quizElement->appendChild($showMaxQuestion = $dom->createElement('showMaxQuestion', $this->booleanToTrueOrFalse($quiz->isShowMaxQuestion()))); $showMaxQuestion->setAttribute('showMaxQuestionValue', $quiz->getShowMaxQuestionValue()); $showMaxQuestion->setAttribute('showMaxQuestionPercent', $this->booleanToTrueOrFalse($quiz->isShowMaxQuestionPercent())); //Toplist $toplist = $dom->createElement('toplist'); $toplist->setAttribute('activated', $this->booleanToTrueOrFalse($quiz->isToplistActivated())); $toplist->appendChild($dom->createElement('toplistDataAddPermissions', $quiz->getToplistDataAddPermissions())); $toplist->appendChild($dom->createElement('toplistDataSort', $quiz->getToplistDataSort())); $toplist->appendChild($dom->createElement('toplistDataAddMultiple', $this->booleanToTrueOrFalse($quiz->isToplistDataAddMultiple()))); $toplist->appendChild($dom->createElement('toplistDataAddBlock', $quiz->getToplistDataAddBlock())); $toplist->appendChild($dom->createElement('toplistDataShowLimit', $quiz->getToplistDataShowLimit())); $toplist->appendChild($dom->createElement('toplistDataShowIn', $quiz->getToplistDataShowIn())); $toplist->appendChild($dom->createElement('toplistDataCaptcha', $this->booleanToTrueOrFalse($quiz->isToplistDataCaptcha()))); $toplist->appendChild($dom->createElement('toplistDataAddAutomatic', $this->booleanToTrueOrFalse($quiz->isToplistDataAddAutomatic()))); $quizElement->appendChild($toplist); $quizElement->appendChild($dom->createElement('showAverageResult', $this->booleanToTrueOrFalse($quiz->isShowAverageResult()))); $quizElement->appendChild($dom->createElement('prerequisite', $this->booleanToTrueOrFalse($quiz->isPrerequisite()))); $quizElement->appendChild($dom->createElement('showReviewQuestion', $this->booleanToTrueOrFalse($quiz->isShowReviewQuestion()))); $quizElement->appendChild($dom->createElement('quizSummaryHide', $this->booleanToTrueOrFalse($quiz->isQuizSummaryHide()))); $quizElement->appendChild($dom->createElement('skipQuestionDisabled', $this->booleanToTrueOrFalse($quiz->isSkipQuestionDisabled()))); $quizElement->appendChild($dom->createElement('emailNotification', $quiz->getEmailNotification())); $quizElement->appendChild($dom->createElement('userEmailNotification', $this->booleanToTrueOrFalse($quiz->isUserEmailNotification()))); $quizElement->appendChild($dom->createElement('showCategoryScore', $this->booleanToTrueOrFalse($quiz->isShowCategoryScore()))); $quizElement->appendChild($dom->createElement('hideResultCorrectQuestion', $this->booleanToTrueOrFalse($quiz->isHideResultCorrectQuestion()))); $quizElement->appendChild($dom->createElement('hideResultQuizTime', $this->booleanToTrueOrFalse($quiz->isHideResultQuizTime()))); $quizElement->appendChild($dom->createElement('hideResultPoints', $this->booleanToTrueOrFalse($quiz->isHideResultPoints()))); $quizElement->appendChild($dom->createElement('autostart', $this->booleanToTrueOrFalse($quiz->isAutostart()))); $quizElement->appendChild($dom->createElement('forcingQuestionSolve', $this->booleanToTrueOrFalse($quiz->isForcingQuestionSolve()))); $quizElement->appendChild($dom->createElement('hideQuestionPositionOverview', $this->booleanToTrueOrFalse($quiz->isHideQuestionPositionOverview()))); $quizElement->appendChild($dom->createElement('hideQuestionNumbering', $this->booleanToTrueOrFalse($quiz->isHideQuestionNumbering()))); //0.27 $quizElement->appendChild($dom->createElement('sortCategories', $this->booleanToTrueOrFalse($quiz->isSortCategories()))); $quizElement->appendChild($dom->createElement('showCategory', $this->booleanToTrueOrFalse($quiz->isShowCategory()))); $quizModus = $dom->createElement('quizModus', $quiz->getQuizModus()); $quizModus->setAttribute('questionsPerPage', $quiz->getQuestionsPerPage()); $quizElement->appendChild($quizModus); $quizElement->appendChild($dom->createElement('startOnlyRegisteredUser', $this->booleanToTrueOrFalse($quiz->isStartOnlyRegisteredUser()))); $formsElement = $dom->createElement('forms'); $formsElement->setAttribute('activated', $this->booleanToTrueOrFalse($quiz->isFormActivated())); $formsElement->setAttribute('position', $quiz->getFormShowPosition()); //0.29 if ($quiz->getAdminEmail() !== null) { /* @var $adminEmail WpProQuiz_Model_Email */ $adminEmail = $quiz->getAdminEmail(); $adminEmailXml = $dom->createElement('adminEmail'); /* * $qElement->appendChild($title = $dom->createElement('title')); $title->appendChild($dom->createCDATASection($question->getTitle())); */ $adminEmailXml->appendChild($dom->createElement('to', $adminEmail->getTo())); $adminEmailXml->appendChild($dom->createElement('form', $adminEmail->getFrom())); $adminEmailXml->appendChild($dom->createElement('subject', $adminEmail->getSubject())); $adminEmailXml->appendChild($dom->createElement('html', $this->booleanToTrueOrFalse($adminEmail->isHtml()))); $adminEmailXml->appendChild($message = $dom->createElement('message')); $message->appendChild($dom->createCDATASection($adminEmail->getMessage())); $quizElement->appendChild($adminEmailXml); } if ($quiz->getUserEmail() !== null) { /* @var $adminEmail WpProQuiz_Model_Email */ $userEmail = $quiz->getUserEmail(); $userEmaillXml = $dom->createElement('userEmail'); $userEmaillXml->appendChild($dom->createElement('to', $userEmail->getTo())); $userEmaillXml->appendChild($dom->createElement('toUser', $this->booleanToTrueOrFalse($userEmail->isToUser()))); $userEmaillXml->appendChild($dom->createElement('toForm', $this->booleanToTrueOrFalse($userEmail->isToForm()))); $userEmaillXml->appendChild($dom->createElement('form', $userEmail->getFrom())); $userEmaillXml->appendChild($dom->createElement('subject', $userEmail->getSubject())); $userEmaillXml->appendChild($dom->createElement('html', $this->booleanToTrueOrFalse($userEmail->isHtml()))); $userEmaillXml->appendChild($message = $dom->createElement('message')); $message->appendChild($dom->createCDATASection($userEmail->getMessage())); $quizElement->appendChild($userEmaillXml); } foreach ($forms as $form) { /** @var WpProQuiz_Model_Form $form * */ $formElement = $dom->createElement('form'); $formElement->setAttribute('type', $form->getType()); $formElement->setAttribute('required', $this->booleanToTrueOrFalse($form->isRequired())); $formElement->setAttribute('fieldname', $form->getFieldname()); if ($form->getData() !== null) { $data = $form->getData(); foreach ($data as $d) { $formDataElement = $dom->createElement('formData', $d); $formElement->appendChild($formDataElement); } } $formsElement->appendChild($formElement); } $quizElement->appendChild($formsElement); return $quizElement; }
private function leaderboardOptions() { ?> <div class="postbox"> <h3 class="hndle"><?php _e('Leaderboard', 'wp-pro-quiz'); ?> <?php _e('(optional)', 'wp-pro-quiz'); ?> </h3> <div class="inside"> <p> <?php _e('The leaderboard allows users to enter results in public list and to share the result this way.', 'wp-pro-quiz'); ?> </p> <p> <?php _e('The leaderboard works independent from internal statistics function.', 'wp-pro-quiz'); ?> </p> <table class="form-table"> <tbody id="toplistBox"> <tr> <th scope="row"> <?php _e('Leaderboard', 'wp-pro-quiz'); ?> </th> <td> <label> <input type="checkbox" name="toplistActivated" value="1" <?php echo $this->quiz->isToplistActivated() ? 'checked="checked"' : ''; ?> > <?php _e('Activate', 'wp-pro-quiz'); ?> </label> </td> </tr> <tr> <th scope="row"> <?php _e('Who can sign up to the list', 'wp-pro-quiz'); ?> </th> <td> <label> <input name="toplistDataAddPermissions" type="radio" value="1" <?php echo $this->quiz->getToplistDataAddPermissions() == 1 ? 'checked="checked"' : ''; ?> > <?php _e('all users', 'wp-pro-quiz'); ?> </label> <label> <input name="toplistDataAddPermissions" type="radio" value="2" <?php echo $this->quiz->getToplistDataAddPermissions() == 2 ? 'checked="checked"' : ''; ?> > <?php _e('registered useres only', 'wp-pro-quiz'); ?> </label> <label> <input name="toplistDataAddPermissions" type="radio" value="3" <?php echo $this->quiz->getToplistDataAddPermissions() == 3 ? 'checked="checked"' : ''; ?> > <?php _e('anonymous users only', 'wp-pro-quiz'); ?> </label> <p class="description"> <?php _e('Not registered users have to enter name and e-mail (e-mail won\'t be displayed)', 'wp-pro-quiz'); ?> </p> </td> </tr> <tr> <th scope="row"> <?php _e('insert automatically', 'wp-pro-quiz'); ?> </th> <td> <label> <input name="toplistDataAddAutomatic" type="checkbox" value="1" <?php $this->checked($this->quiz->isToplistDataAddAutomatic()); ?> > <?php _e('Activate', 'wp-pro-quiz'); ?> </label> <p class="description"> <?php _e('If you enable this option, logged in users will be automatically entered into leaderboard', 'wp-pro-quiz'); ?> </p> </td> </tr> <tr> <th scope="row"> <?php _e('display captcha', 'wp-pro-quiz'); ?> </th> <td> <label> <input type="checkbox" name="toplistDataCaptcha" value="1" <?php echo $this->quiz->isToplistDataCaptcha() ? 'checked="checked"' : ''; ?> <?php echo $this->captchaIsInstalled ? '' : 'disabled="disabled"'; ?> > <?php _e('Activate', 'wp-pro-quiz'); ?> </label> <p class="description"> <?php _e('If you enable this option, additional captcha will be displayed for users who are not registered.', 'wp-pro-quiz'); ?> </p> <p class="description" style="color: red;"> <?php _e('This option requires additional plugin:', 'wp-pro-quiz'); ?> <a href="http://wordpress.org/extend/plugins/really-simple-captcha/" target="_blank">Really Simple CAPTCHA</a> </p> <?php if ($this->captchaIsInstalled) { ?> <p class="description" style="color: green;"> <?php _e('Plugin has been detected.', 'wp-pro-quiz'); ?> </p> <?php } else { ?> <p class="description" style="color: red;"> <?php _e('Plugin is not installed.', 'wp-pro-quiz'); ?> </p> <?php } ?> </td> </tr> <tr> <th scope="row"> <?php _e('Sort list by', 'wp-pro-quiz'); ?> </th> <td> <label> <input name="toplistDataSort" type="radio" value="1" <?php echo $this->quiz->getToplistDataSort() == 1 ? 'checked="checked"' : ''; ?> > <?php _e('best user', 'wp-pro-quiz'); ?> </label> <label> <input name="toplistDataSort" type="radio" value="2" <?php echo $this->quiz->getToplistDataSort() == 2 ? 'checked="checked"' : ''; ?> > <?php _e('newest entry', 'wp-pro-quiz'); ?> </label> <label> <input name="toplistDataSort" type="radio" value="3" <?php echo $this->quiz->getToplistDataSort() == 3 ? 'checked="checked"' : ''; ?> > <?php _e('oldest entry', 'wp-pro-quiz'); ?> </label> </td> </tr> <tr> <th scope="row"> <?php _e('Users can apply multiple times', 'wp-pro-quiz'); ?> </th> <td> <div> <label> <input type="checkbox" name="toplistDataAddMultiple" value="1" <?php echo $this->quiz->isToplistDataAddMultiple() ? 'checked="checked"' : ''; ?> > <?php _e('Activate', 'wp-pro-quiz'); ?> </label> </div> <div id="toplistDataAddBlockBox" style="display: none;"> <label> <?php _e('User can apply after:', 'wp-pro-quiz'); ?> <input type="number" min="0" class="small-text" name="toplistDataAddBlock" value="<?php echo $this->quiz->getToplistDataAddBlock(); ?> "> <?php _e('minute', 'wp-pro-quiz'); ?> </label> </div> </td> </tr> <tr> <th scope="row"> <?php _e('How many entries should be displayed', 'wp-pro-quiz'); ?> </th> <td> <div> <label> <input type="number" min="0" class="small-text" name="toplistDataShowLimit" value="<?php echo $this->quiz->getToplistDataShowLimit(); ?> "> <?php _e('Entries', 'wp-pro-quiz'); ?> </label> </div> </td> </tr> <tr> <th scope="row"> <?php _e('Automatically display leaderboard in quiz result', 'wp-pro-quiz'); ?> </th> <td> <div style="margin-top: 6px;"> <?php _e('Where should leaderboard be displayed:', 'wp-pro-quiz'); ?> <label style="margin-right: 5px; margin-left: 5px;"> <input type="radio" name="toplistDataShowIn" value="0" <?php echo $this->quiz->getToplistDataShowIn() == 0 ? 'checked="checked"' : ''; ?> > <?php _e('don\'t display', 'wp-pro-quiz'); ?> </label> <label> <input type="radio" name="toplistDataShowIn" value="1" <?php echo $this->quiz->getToplistDataShowIn() == 1 ? 'checked="checked"' : ''; ?> > <?php _e('below the "result text"', 'wp-pro-quiz'); ?> </label> <span class="wpProQuiz_demoBox" style="margin-right: 5px;"> <a href="#"><?php _e('Demo', 'wp-pro-quiz'); ?> </a> <span style="z-index: 9999999; position: absolute; background-color: #E9E9E9; padding: 10px; box-shadow: 0px 0px 10px 4px rgb(44, 44, 44); display: none; "> <img alt="" src="<?php echo WPPROQUIZ_URL . '/img/leaderboardInResultText.png'; ?> "> </span> </span> <label> <input type="radio" name="toplistDataShowIn" value="2" <?php echo $this->quiz->getToplistDataShowIn() == 2 ? 'checked="checked"' : ''; ?> > <?php _e('in a button', 'wp-pro-quiz'); ?> </label> <span class="wpProQuiz_demoBox"> <a href="#"><?php _e('Demo', 'wp-pro-quiz'); ?> </a> <span style="z-index: 9999999; position: absolute; background-color: #E9E9E9; padding: 10px; box-shadow: 0px 0px 10px 4px rgb(44, 44, 44); display: none; "> <img alt="" src="<?php echo WPPROQUIZ_URL . '/img/leaderboardInButton.png'; ?> "> </span> </span> </div> </td> </tr> </tbody> </table> </div> </div> <?php }