public function save(WpProQuiz_Model_Question $question, $auto = false)
 {
     $sort = null;
     if ($auto && $question->getId()) {
         $statisticMapper = new WpProQuiz_Model_StatisticMapper();
         if ($statisticMapper->isStatisticByQuestionId($question->getId())) {
             $this->setOnlineOff($question->getId());
             $question->setQuizId($this->getQuizId($question->getId()));
             $sort = $this->getSortByQuestionId($question->getId());
             $question->setId(0);
         }
     }
     if ($question->getId() != 0) {
         $this->_wpdb->update($this->_table, array('title' => $question->getTitle(), 'points' => $question->getPoints(), 'question' => $question->getQuestion(), 'correct_msg' => $question->getCorrectMsg(), 'incorrect_msg' => $question->getIncorrectMsg(), 'correct_same_text' => (int) $question->isCorrectSameText(), 'tip_enabled' => (int) $question->isTipEnabled(), 'tip_msg' => $question->getTipMsg(), 'answer_type' => $question->getAnswerType(), 'show_points_in_box' => (int) $question->isShowPointsInBox(), 'answer_points_activated' => (int) $question->isAnswerPointsActivated(), 'answer_data' => $question->getAnswerData(true), 'category_id' => $question->getCategoryId(), 'answer_points_diff_modus_activated' => (int) $question->isAnswerPointsDiffModusActivated(), 'disable_correct' => (int) $question->isDisableCorrect(), 'matrix_sort_answer_criteria_width' => $question->getMatrixSortAnswerCriteriaWidth()), array('id' => $question->getId()), array('%s', '%d', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d'), array('%d'));
     } else {
         $this->_wpdb->insert($this->_table, array('quiz_id' => $question->getQuizId(), 'online' => 1, 'sort' => $sort !== null ? $sort : $this->getMaxSort($question->getQuizId()) + 1, 'title' => $question->getTitle(), 'points' => $question->getPoints(), 'question' => $question->getQuestion(), 'correct_msg' => $question->getCorrectMsg(), 'incorrect_msg' => $question->getIncorrectMsg(), 'correct_same_text' => (int) $question->isCorrectSameText(), 'tip_enabled' => (int) $question->isTipEnabled(), 'tip_msg' => $question->getTipMsg(), 'answer_type' => $question->getAnswerType(), 'show_points_in_box' => (int) $question->isShowPointsInBox(), 'answer_points_activated' => (int) $question->isAnswerPointsActivated(), 'answer_data' => $question->getAnswerData(true), 'category_id' => $question->getCategoryId(), 'answer_points_diff_modus_activated' => (int) $question->isAnswerPointsDiffModusActivated(), 'disable_correct' => (int) $question->isDisableCorrect(), 'matrix_sort_answer_criteria_width' => $question->getMatrixSortAnswerCriteriaWidth()), array('%d', '%d', '%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d'));
         $question->setId($this->_wpdb->insert_id);
     }
     return $question;
 }
 /**
  * @deprecated
  */
 public static function ajaxLoadStatistic($data, $func)
 {
     if (!current_user_can('wpProQuiz_show_statistics')) {
         return json_encode(array());
     }
     $userId = $data['userId'];
     $quizId = $data['quizId'];
     $testId = $data['testId'];
     $maxPoints = 0;
     $sumQuestion = 0;
     $inTest = false;
     $category = array();
     $categoryList = array();
     $testJson = array();
     $formData = null;
     $statisticMapper = new WpProQuiz_Model_StatisticMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $statisticRefMapper = new WpProQuiz_Model_StatisticRefMapper();
     $tests = $statisticRefMapper->fetchAll($quizId, $userId, $testId);
     $i = 1;
     foreach ($tests as $test) {
         if ($testId == $test->getStatisticRefId()) {
             $inTest = true;
         }
         $testJson[] = array('id' => $test->getStatisticRefId(), 'date' => '#' . $i++ . ' ' . WpProQuiz_Helper_Until::convertTime($test->getCreateTime(), get_option('wpProQuiz_statisticTimeFormat', 'Y/m/d g:i A')));
     }
     if (!$inTest) {
         $data['testId'] = $testId = 0;
     }
     if (!$testId) {
         $statistics = $statisticRefMapper->fetchAvg($quizId, $userId);
     } else {
         $statistics = $statisticMapper->fetchAllByRef($testId);
         $refModel = $statisticRefMapper->fetchByRefId($testId);
         $formData = $refModel->getFormData();
     }
     $questionData = $questionMapper->fetchAllList($quizId, array('id', 'category_id', 'points'));
     $empty = array('questionId' => 0, 'correct' => 0, 'incorrect' => 0, 'hint' => 0, 'points' => 0, 'result' => 0, 'questionTime' => 0);
     $ca = $sa = array();
     $ga = $empty;
     foreach ($questionData as $cc) {
         $categoryList[$cc['id']] = $cc['category_id'];
         $c =& $category[$cc['category_id']];
         if (empty($c)) {
             $c = $cc;
             $c['sum'] = 1;
         } else {
             $c['points'] += $cc['points'];
             $c['sum']++;
         }
         $maxPoints += $cc['points'];
         $sumQuestion++;
         $sa[$cc['id']] = self::calcTotal($empty);
         $sa[$cc['id']]['questionId'] = $cc['id'];
         $ca[$cc['category_id']] = self::calcTotal($empty);
     }
     foreach ($statistics as $statistic) {
         $s = $statistic->getCorrectCount() + $statistic->getIncorrectCount();
         if ($s > 0) {
             $correct = $statistic->getCorrectCount() . ' (' . round(100 * $statistic->getCorrectCount() / $s, 2) . '%)';
             $incorrect = $statistic->getIncorrectCount() . ' (' . round(100 * $statistic->getIncorrectCount() / $s, 2) . '%)';
         } else {
             $incorrect = $correct = '0 (0%)';
         }
         $ga['correct'] += $statistic->getCorrectCount();
         $ga['incorrect'] += $statistic->getIncorrectCount();
         $ga['hint'] += $statistic->getHintCount();
         $ga['points'] += $statistic->getPoints();
         $ga['questionTime'] += $statistic->getQuestionTime();
         $cats =& $ca[$categoryList[$statistic->getQuestionId()]];
         if (!is_array($cats)) {
             $cats = $empty;
         }
         $cats['correct'] += $statistic->getCorrectCount();
         $cats['incorrect'] += $statistic->getIncorrectCount();
         $cats['hint'] += $statistic->getHintCount();
         $cats['points'] += $statistic->getPoints();
         $cats['questionTime'] += $statistic->getQuestionTime();
         $sa[$statistic->getQuestionId()] = array('questionId' => $statistic->getQuestionId(), 'correct' => $correct, 'incorrect' => $incorrect, 'hint' => $statistic->getHintCount(), 'points' => $statistic->getPoints(), 'questionTime' => self::convertToTimeString($statistic->getQuestionTime()));
     }
     foreach ($ca as $catIndex => $cat) {
         $ca[$catIndex] = self::calcTotal($cat, $category[$catIndex]['points'], $category[$catIndex]['sum']);
     }
     $sa[0] = self::calcTotal($ga, $maxPoints, $sumQuestion);
     return json_encode(array('question' => $sa, 'category' => $ca, 'tests' => $testJson, 'testId' => $data['testId'], 'formData' => $formData));
 }