public function quizCompleted()
 {
     $lockMapper = new WpProQuiz_Model_LockMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $statistics = new WpProQuiz_Controller_Statistics();
     $quiz = $quizMapper->fetch($this->data['quizId']);
     if ($quiz === null || $quiz->getId() <= 0) {
         return;
     }
     $categories = $categoryMapper->fetchByQuiz($quiz->getId());
     $userId = get_current_user_id();
     $resultInPercent = floor($this->data['results']['comp']['result']);
     $this->setResultCookie($quiz);
     $this->emailNote($quiz, $this->data['results']['comp'], $categories);
     if (!$this->isPreLockQuiz($quiz)) {
         $statistics->save();
         do_action('wp_pro_quiz_completed_quiz');
         do_action('wp_pro_quiz_completed_quiz_' . $resultInPercent . '_percent');
         return;
     }
     $lockMapper->deleteOldLock(60 * 60 * 24 * 7, $this->_post['quizId'], time(), WpProQuiz_Model_Lock::TYPE_QUIZ, 0);
     $lockIp = $lockMapper->isLock($this->_post['quizId'], $this->getIp(), get_current_user_id(), WpProQuiz_Model_Lock::TYPE_QUIZ);
     $lockCookie = false;
     $cookieTime = $quiz->getQuizRunOnceTime();
     $cookieJson = null;
     if (isset($this->_cookie['wpProQuiz_lock']) && get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
         $cookieJson = json_decode($this->_cookie['wpProQuiz_lock'], true);
         if ($cookieJson !== false) {
             if (isset($cookieJson[$this->_post['quizId']]) && $cookieJson[$this->_post['quizId']] == $cookieTime) {
                 $lockCookie = true;
             }
         }
     }
     if (!$lockIp && !$lockCookie) {
         $statistics->save();
         do_action('wp_pro_quiz_completed_quiz');
         do_action('wp_pro_quiz_completed_quiz_' . $resultInPercent . '_percent');
         if (get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
             $cookieData = array();
             if ($cookieJson !== null || $cookieJson !== false) {
                 $cookieData = $cookieJson;
             }
             $cookieData[$this->_post['quizId']] = $quiz->getQuizRunOnceTime();
             $url = parse_url(get_bloginfo('url'));
             setcookie('wpProQuiz_lock', json_encode($cookieData), time() + 60 * 60 * 24 * 60, empty($url['path']) ? '/' : $url['path']);
         }
         $lock = new WpProQuiz_Model_Lock();
         $lock->setUserId(get_current_user_id());
         $lock->setQuizId($this->_post['quizId']);
         $lock->setLockDate(time());
         $lock->setLockIp($this->getIp());
         $lock->setLockType(WpProQuiz_Model_Lock::TYPE_QUIZ);
         $lockMapper->insert($lock);
     }
     return;
 }
 /**
  *
  * @param WpProQuiz_Model_Quiz $quiz
  * @return void|boolean
  */
 public function save($quiz = null)
 {
     $quizId = $this->_post['quizId'];
     $array = $this->_post['results'];
     $lockIp = $this->getIp();
     $userId = get_current_user_id();
     if ($lockIp === false) {
         return false;
     }
     if ($quiz === null) {
         $quizMapper = new WpProQuiz_Model_QuizMapper();
         $quiz = $quizMapper->fetch($quizId);
     }
     if (!$quiz->isStatisticsOn()) {
         return false;
     }
     $values = $this->makeDataList($quizId, $array, $quiz->getQuizModus());
     $formValues = $this->makeFormData($quiz, isset($this->_post['forms']) ? $this->_post['forms'] : null);
     if ($values === false) {
         return false;
     }
     if ($quiz->getStatisticsIpLock() > 0) {
         $lockMapper = new WpProQuiz_Model_LockMapper();
         $lockTime = $quiz->getStatisticsIpLock() * 60;
         $lockMapper->deleteOldLock($lockTime, $quiz->getId(), time(), WpProQuiz_Model_Lock::TYPE_STATISTIC);
         if ($lockMapper->isLock($quizId, $lockIp, $userId, WpProQuiz_Model_Lock::TYPE_STATISTIC)) {
             return false;
         }
         $lock = new WpProQuiz_Model_Lock();
         $lock->setQuizId($quizId)->setLockIp($lockIp)->setUserId($userId)->setLockType(WpProQuiz_Model_Lock::TYPE_STATISTIC)->setLockDate(time());
         $lockMapper->insert($lock);
     }
     $statisticRefModel = new WpProQuiz_Model_StatisticRefModel();
     $statisticRefModel->setCreateTime(time());
     $statisticRefModel->setUserId($userId);
     $statisticRefModel->setQuizId($quizId);
     $statisticRefModel->setFormData($formValues);
     $statisticRefMapper = new WpProQuiz_Model_StatisticRefMapper();
     $statisticRefMapper->statisticSave($statisticRefModel, $values);
     return true;
 }
 public static function ajaxCompletedQuiz($data)
 {
     // workaround ...
     $_POST = $_POST['data'];
     $ctr = new WpProQuiz_Controller_Quiz();
     $lockMapper = new WpProQuiz_Model_LockMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $is100P = $data['results']['comp']['result'] == 100;
     $quiz = $quizMapper->fetch($data['quizId']);
     if ($quiz === null || $quiz->getId() <= 0) {
         return json_encode(array());
     }
     $categories = $categoryMapper->fetchByQuiz($quiz->getId());
     $forms = $formMapper->fetch($quiz->getId());
     $ctr->setResultCookie($quiz);
     $ctr->emailNote($quiz, $data['results']['comp'], $categories, $forms, isset($data['forms']) ? $data['forms'] : array());
     if (!$ctr->isPreLockQuiz($quiz)) {
         $statistics = new WpProQuiz_Controller_Statistics();
         $statistics->save($quiz);
         do_action('wp_pro_quiz_completed_quiz');
         if ($is100P) {
             do_action('wp_pro_quiz_completed_quiz_100_percent');
         }
         return json_encode(array());
     }
     $lockMapper->deleteOldLock(60 * 60 * 24 * 7, $data['quizId'], time(), WpProQuiz_Model_Lock::TYPE_QUIZ, 0);
     $lockIp = $lockMapper->isLock($data['quizId'], $ctr->getIp(), get_current_user_id(), WpProQuiz_Model_Lock::TYPE_QUIZ);
     $lockCookie = false;
     $cookieTime = $quiz->getQuizRunOnceTime();
     $cookieJson = null;
     if (isset($ctr->_cookie['wpProQuiz_lock']) && get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
         $cookieJson = json_decode($ctr->_cookie['wpProQuiz_lock'], true);
         if ($cookieJson !== false) {
             if (isset($cookieJson[$data['quizId']]) && $cookieJson[$data['quizId']] == $cookieTime) {
                 $lockCookie = true;
             }
         }
     }
     if (!$lockIp && !$lockCookie) {
         $statistics = new WpProQuiz_Controller_Statistics();
         $statistics->save($quiz);
         do_action('wp_pro_quiz_completed_quiz');
         if ($is100P) {
             do_action('wp_pro_quiz_completed_quiz_100_percent');
         }
         if (get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
             $cookieData = array();
             if ($cookieJson !== null || $cookieJson !== false) {
                 $cookieData = $cookieJson;
             }
             $cookieData[$data['quizId']] = $quiz->getQuizRunOnceTime();
             $url = parse_url(get_bloginfo('url'));
             setcookie('wpProQuiz_lock', json_encode($cookieData), time() + 60 * 60 * 24 * 60, empty($url['path']) ? '/' : $url['path']);
         }
         $lock = new WpProQuiz_Model_Lock();
         $lock->setUserId(get_current_user_id());
         $lock->setQuizId($data['quizId']);
         $lock->setLockDate(time());
         $lock->setLockIp($ctr->getIp());
         $lock->setLockType(WpProQuiz_Model_Lock::TYPE_QUIZ);
         $lockMapper->insert($lock);
     }
     return json_encode(array());
 }