コード例 #1
0
    /**
     *
     * @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));
    }