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 ajaxLoadStatisticUser($data)
 {
     if (!current_user_can('wpProQuiz_show_statistics')) {
         return json_encode(array());
     }
     $quizId = $data['quizId'];
     $userId = $data['userId'];
     $refId = $data['refId'];
     $avg = (bool) $data['avg'];
     $refIdUserId = $avg ? $userId : $refId;
     $statisticRefMapper = new WpProQuiz_Model_StatisticRefMapper();
     $statisticUserMapper = new WpProQuiz_Model_StatisticUserMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $statisticUsers = $statisticUserMapper->fetchUserStatistic($refIdUserId, $quizId, $avg);
     $output = array();
     foreach ($statisticUsers as $statistic) {
         /* @var $statistic WpProQuiz_Model_StatisticUser */
         if (!isset($output[$statistic->getCategoryId()])) {
             $output[$statistic->getCategoryId()] = array('questions' => array(), 'categoryId' => $statistic->getCategoryId(), 'categoryName' => $statistic->getCategoryId() ? $statistic->getCategoryName() : __('No category', 'wp-pro-quiz'));
         }
         $o =& $output[$statistic->getCategoryId()];
         $o['questions'][] = array('correct' => $statistic->getCorrectCount(), 'incorrect' => $statistic->getIncorrectCount(), 'hintCount' => $statistic->getIncorrectCount(), 'time' => $statistic->getQuestionTime(), 'points' => $statistic->getPoints(), 'gPoints' => $statistic->getGPoints(), 'statistcAnswerData' => $statistic->getStatisticAnswerData(), 'questionName' => $statistic->getQuestionName(), 'questionAnswerData' => $statistic->getQuestionAnswerData(), 'answerType' => $statistic->getAnswerType(), 'solvedCount' => $statistic->getSolvedCount());
     }
     $view = new WpProQuiz_View_StatisticsAjax();
     $view->avg = $avg;
     $view->statisticModel = $statisticRefMapper->fetchByRefId($refIdUserId, $quizId, $avg);
     $view->userName = __('Anonymous', 'wp-pro-quiz');
     if ($view->statisticModel->getUserId()) {
         $userInfo = get_userdata($view->statisticModel->getUserId());
         if ($userInfo !== false) {
             $view->userName = $userInfo->user_login . ' (' . $userInfo->display_name . ')';
         } else {
             $view->userName = __('Deleted user', 'wp-pro-quiz');
         }
     }
     if (!$avg) {
         $view->forms = $formMapper->fetch($quizId);
     }
     $view->userStatistic = $output;
     $html = $view->getUserTable();
     return json_encode(array('html' => $html));
 }
 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 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());
 }
 private function getForms($quizId)
 {
     $formMapper = new WpProQuiz_Model_FormMapper();
     return $formMapper->fetch($quizId);
 }