public function export($ids)
 {
     $dom = new DOMDocument('1.0', 'utf-8');
     $root = $dom->createElement('wpProQuiz');
     $dom->appendChild($root);
     $header = $dom->createElement('header');
     $header->setAttribute('version', WPPROQUIZ_VERSION);
     $header->setAttribute('exportVersion', 1);
     $root->appendChild($header);
     $data = $dom->createElement('data');
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     foreach ($ids as $id) {
         $quizModel = $quizMapper->fetch($id);
         if ($quizModel->getId() <= 0) {
             continue;
         }
         $questionModel = $questionMapper->fetchAll($quizModel->getId());
         $forms = array();
         if ($quizModel->isFormActivated()) {
             $forms = $formMapper->fetch($quizModel->getId());
         }
         $quizElement = $this->getQuizElement($dom, $quizModel, $forms);
         $quizElement->appendChild($questionsElement = $dom->createElement('questions'));
         foreach ($questionModel as $model) {
             $questionElement = $this->createQuestionElement($dom, $model);
             $questionsElement->appendChild($questionElement);
         }
         $data->appendChild($quizElement);
     }
     $root->appendChild($data);
     return $dom->saveXML();
 }
 public function showAction($id)
 {
     $view = new WpProQuiz_View_FrontQuiz();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $quiz = $quizMapper->fetch($id);
     if ($quiz->isShowMaxQuestion() && $quiz->getShowMaxQuestionValue() > 0) {
         $value = $quiz->getShowMaxQuestionValue();
         if ($quiz->isShowMaxQuestionPercent()) {
             $count = $questionMapper->count($id);
             $value = ceil($count * $value / 100);
         }
         $question = $questionMapper->fetchAll($id, true, $value);
     } else {
         $question = $questionMapper->fetchAll($id);
     }
     $view->quiz = $quiz;
     $view->question = $question;
     $view->category = $categoryMapper->fetchByQuiz($quiz->getId());
     $view->forms = $formMapper->fetch($quiz->getId());
     $view->show(true);
 }
 public static function ajaxLoadCopyQuestion($data)
 {
     if (!current_user_can('wpProQuiz_edit_quiz')) {
         echo json_encode(array());
         exit;
     }
     $quizId = $data['quizId'];
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $data = array();
     $quiz = $quizMapper->fetchAll();
     foreach ($quiz as $qz) {
         if ($qz->getId() == $quizId) {
             continue;
         }
         $question = $questionMapper->fetchAll($qz->getId());
         $questionArray = array();
         foreach ($question as $qu) {
             $questionArray[] = array('name' => $qu->getTitle(), 'id' => $qu->getId());
         }
         $data[] = array('name' => $qz->getName(), 'id' => $qz->getId(), 'question' => $questionArray);
     }
     return json_encode($data);
 }
 private function makeDataList($quizId, $array, $modus)
 {
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $question = $questionMapper->fetchAllList($quizId, array('id', 'points'));
     $ids = array();
     foreach ($question as $q) {
         if (!isset($array[$q['id']])) {
             continue;
         }
         $ids[] = $q['id'];
         $v = $array[$q['id']];
         if (!isset($v) || $v['points'] > $q['points'] || $v['points'] < 0) {
             return false;
         }
     }
     $avgTime = null;
     if ($modus == WpProQuiz_Model_Quiz::QUIZ_MODUS_SINGLE) {
         $avgTime = ceil($array['comp']['quizTime'] / count($question));
     }
     unset($array['comp']);
     $ak = array_keys($array);
     if (array_diff($ids, $ak) !== array_diff($ak, $ids)) {
         return false;
     }
     $values = array();
     foreach ($array as $k => $v) {
         $s = new WpProQuiz_Model_Statistic();
         $s->setQuestionId($k);
         $s->setHintCount(isset($v['tip']) ? 1 : 0);
         $s->setSolvedCount(isset($v['solved']) && $v['solved'] ? 1 : 0);
         $s->setCorrectCount($v['correct'] ? 1 : 0);
         $s->setIncorrectCount($v['correct'] ? 0 : 1);
         $s->setPoints($v['points']);
         $s->setQuestionTime($avgTime === null ? $v['time'] : $avgTime);
         $s->setAnswerData(isset($v['data']) ? $v['data'] : null);
         $values[] = $s;
     }
     return $values;
 }
 public function saveImport($ids)
 {
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $data = $this->getImportData();
     $categoryArray = $categoryMapper->getCategoryArrayForImport();
     $categoryArrayQuiz = $categoryMapper->getCategoryArrayForImport(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
     foreach ($data['master'] as $quiz) {
         if (get_class($quiz) !== 'WpProQuiz_Model_Quiz') {
             continue;
         }
         $oldId = $quiz->getId();
         if ($ids !== false && !in_array($oldId, $ids)) {
             continue;
         }
         $quiz->setId(0);
         $quiz->setCategoryId(0);
         if (trim($quiz->getCategoryName()) != '') {
             if (isset($categoryArrayQuiz[strtolower($quiz->getCategoryName())])) {
                 $quiz->setCategoryId($categoryArrayQuiz[strtolower($quiz->getCategoryName())]);
             } else {
                 $categoryModel = new WpProQuiz_Model_Category();
                 $categoryModel->setCategoryName($quiz->getCategoryName());
                 $categoryModel->setType(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
                 $categoryMapper->save($categoryModel);
                 $quiz->setCategoryId($categoryModel->getCategoryId());
                 $categoryArrayQuiz[strtolower($quiz->getCategoryName())] = $categoryModel->getCategoryId();
             }
         }
         $quizMapper->save($quiz);
         if (isset($data['forms']) && isset($data['forms'][$oldId])) {
             $sort = 0;
             foreach ($data['forms'][$oldId] as $form) {
                 $form->setQuizId($quiz->getId());
                 $form->setSort($sort++);
             }
             $formMapper->update($data['forms'][$oldId]);
         }
         $sort = 0;
         foreach ($data['question'][$oldId] as $question) {
             if (get_class($question) !== 'WpProQuiz_Model_Question') {
                 continue;
             }
             $question->setQuizId($quiz->getId());
             $question->setId(0);
             $question->setSort($sort++);
             $question->setCategoryId(0);
             if (trim($question->getCategoryName()) != '') {
                 if (isset($categoryArray[strtolower($question->getCategoryName())])) {
                     $question->setCategoryId($categoryArray[strtolower($question->getCategoryName())]);
                 } else {
                     $categoryModel = new WpProQuiz_Model_Category();
                     $categoryModel->setCategoryName($question->getCategoryName());
                     $categoryMapper->save($categoryModel);
                     $question->setCategoryId($categoryModel->getCategoryId());
                     $categoryArray[strtolower($question->getCategoryName())] = $categoryModel->getCategoryId();
                 }
             }
             $questionMapper->save($question);
         }
     }
     return true;
 }
 public static function ajaxQuizLoadData($data)
 {
     $id = $data['quizId'];
     $view = new WpProQuiz_View_FrontQuiz();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $quiz = $quizMapper->fetch($id);
     if ($quiz->isShowMaxQuestion() && $quiz->getShowMaxQuestionValue() > 0) {
         $value = $quiz->getShowMaxQuestionValue();
         if ($quiz->isShowMaxQuestionPercent()) {
             $count = $questionMapper->count($id);
             $value = ceil($count * $value / 100);
         }
         $question = $questionMapper->fetchAll($id, true, $value);
     } else {
         $question = $questionMapper->fetchAll($id);
     }
     if (empty($quiz) || empty($question)) {
         return null;
     }
     $view->quiz = $quiz;
     $view->question = $question;
     $view->category = $categoryMapper->fetchByQuiz($quiz->getId());
     $view->forms = $formMapper->fetch($quiz->getId());
     return json_encode($view->getQuizData());
 }
 private function importData($o, $ids = false, $version = '1')
 {
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $categoryArray = $categoryMapper->getCategoryArrayForImport();
     $categoryArrayQuiz = $categoryMapper->getCategoryArrayForImport(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
     foreach ($o['master'] as $master) {
         if (get_class($master) !== 'WpProQuiz_Model_Quiz') {
             continue;
         }
         $oldId = $master->getId();
         if ($ids !== false) {
             if (!in_array($oldId, $ids)) {
                 continue;
             }
         }
         $master->setId(0);
         if ($version == 3) {
             if ($master->isQuestionOnSinglePage()) {
                 $master->setQuizModus(WpProQuiz_Model_Quiz::QUIZ_MODUS_SINGLE);
             } else {
                 if ($master->isCheckAnswer()) {
                     $master->setQuizModus(WpProQuiz_Model_Quiz::QUIZ_MODUS_CHECK);
                 } else {
                     if ($master->isBackButton()) {
                         $master->setQuizModus(WpProQuiz_Model_Quiz::QUIZ_MODUS_BACK_BUTTON);
                     } else {
                         $master->setQuizModus(WpProQuiz_Model_Quiz::QUIZ_MODUS_NORMAL);
                     }
                 }
             }
         }
         $master->setCategoryId(0);
         if (trim($master->getCategoryName()) != '') {
             if (isset($categoryArrayQuiz[strtolower($master->getCategoryName())])) {
                 $master->setCategoryId($categoryArrayQuiz[strtolower($master->getCategoryName())]);
             } else {
                 $categoryModel = new WpProQuiz_Model_Category();
                 $categoryModel->setCategoryName($master->getCategoryName());
                 $categoryModel->setType(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
                 $categoryMapper->save($categoryModel);
                 $master->setCategoryId($categoryModel->getCategoryId());
                 $categoryArrayQuiz[strtolower($master->getCategoryName())] = $categoryModel->getCategoryId();
             }
         }
         $quizMapper->save($master);
         if (isset($o['forms']) && isset($o['forms'][$oldId])) {
             foreach ($o['forms'][$oldId] as $form) {
                 /** @var WpProQuiz_Model_Form $form **/
                 $form->setFormId(0);
                 $form->setQuizId($master->getId());
             }
             $formMapper->update($o['forms'][$oldId]);
         }
         $sort = 0;
         foreach ($o['question'][$oldId] as $question) {
             if (get_class($question) !== 'WpProQuiz_Model_Question') {
                 continue;
             }
             $question->setQuizId($master->getId());
             $question->setId(0);
             $question->setSort($sort++);
             $question->setCategoryId(0);
             if (trim($question->getCategoryName()) != '') {
                 if (isset($categoryArray[strtolower($question->getCategoryName())])) {
                     $question->setCategoryId($categoryArray[strtolower($question->getCategoryName())]);
                 } else {
                     $categoryModel = new WpProQuiz_Model_Category();
                     $categoryModel->setCategoryName($question->getCategoryName());
                     $categoryMapper->save($categoryModel);
                     $question->setCategoryId($categoryModel->getCategoryId());
                     $categoryArray[strtolower($question->getCategoryName())] = $categoryModel->getCategoryId();
                 }
             }
             $questionMapper->save($question);
         }
     }
     return true;
 }
 public function getQuestion($quizId)
 {
     $m = new WpProQuiz_Model_QuestionMapper();
     return $m->fetchAll($quizId);
 }
 /**
  * @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));
 }