/**
  *
  * @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;
 }
    /**
     *
     * @param WpProQuiz_Model_StatisticRefModel $statisticRefModel
     * @param WpProQuiz_Model_Statistic[] $statisticModel
     */
    public function statisticSave($statisticRefModel, $statisticModel)
    {
        $values = array();
        $refId = null;
        $isOld = false;
        // 		if(!$statisticRefModel->getUserId()) {
        // 			$isOld = true;
        // 			$refId = $this->_wpdb->get_var(
        // 					$this->_wpdb->prepare('
        // 						SELECT statistic_ref_id
        // 						FROM '.$this->_tableStatisticRef.'
        // 						WHERE quiz_id = %d AND user_id = %d
        // 				', $statisticRefModel->getQuizId(), $statisticRefModel->getUserId())
        // 			);
        // 		}
        if ($refId === null) {
            $refData = array('quiz_id' => $statisticRefModel->getQuizId(), 'user_id' => $statisticRefModel->getUserId(), 'create_time' => $statisticRefModel->getCreateTime(), 'is_old' => (int) $isOld);
            $refFormat = array('%d', '%d', '%d', '%d');
            if ($statisticRefModel->getFormData() !== null && is_array($statisticRefModel->getFormData())) {
                $refData['form_data'] = @json_encode($statisticRefModel->getFormData());
                $refFormat[] = '%s';
            }
            $this->_wpdb->insert($this->_tableStatisticRef, $refData, $refFormat);
            $refId = $this->_wpdb->insert_id;
        }
        foreach ($statisticModel as $d) {
            $answerData = $d->getAnswerData() === null ? 'NULL' : $this->_wpdb->prepare('%s', json_encode($d->getAnswerData()));
            $values[] = '( ' . implode(', ', array('statistic_ref_id' => $refId, 'question_id' => $d->getQuestionId(), 'correct_count' => $d->getCorrectCount(), 'incorrect_count' => $d->getIncorrectCount(), 'hint_count' => $d->getHintCount(), 'solved_count' => $d->getSolvedCount(), 'points' => $d->getPoints(), 'question_time' => $d->getQuestionTime(), 'answer_data' => $answerData)) . ' )';
        }
        $this->_wpdb->query('INSERT INTO
				' . $this->_tableStatistic . ' (
					statistic_ref_id, question_id, correct_count, incorrect_count, hint_count, solved_count, points, question_time, answer_data
				)
			VALUES
				' . implode(', ', $values));
    }