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; }
public function save(WpProQuiz_Model_Toplist $toplist) { $this->_wpdb->insert($this->_tableToplist, array('quiz_id' => $toplist->getQuizId(), 'user_id' => $toplist->getUserId(), 'date' => $toplist->getDate(), 'name' => $toplist->getName(), 'email' => $toplist->getEmail(), 'points' => $toplist->getPoints(), 'result' => $toplist->getResult(), 'ip' => $toplist->getIp()), array('%d', '%d', '%d', '%s', '%s', '%d', '%f', '%s')); $toplist->setToplistId($this->_wpdb->insert_id); }