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 ajaxResetLock($data)
 {
     if (!current_user_can('wpProQuiz_edit_quiz')) {
         return json_encode(array());
     }
     $quizId = (int) $data['quizId'];
     $lm = new WpProQuiz_Model_LockMapper();
     $qm = new WpProQuiz_Model_QuizMapper();
     $q = $qm->fetch($quizId);
     if ($q->getId() > 0) {
         $q->setQuizRunOnceTime(time());
         $qm->save($q);
         $lm->deleteByQuizId($quizId, WpProQuiz_Model_Lock::TYPE_QUIZ);
     }
     return json_encode(array());
 }
 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;
 }
 private function createAction()
 {
     if (!current_user_can('wpProQuiz_add_quiz')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     $this->view = new WpProQuiz_View_QuizEdit();
     $this->view->header = __('Create quiz', 'wp-pro-quiz');
     $forms = null;
     $prerequisiteQuizList = array();
     $m = new WpProQuiz_Model_QuizMapper();
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     if (isset($this->_post['submit'])) {
         if (isset($this->_post['resultGradeEnabled'])) {
             $this->_post['result_text'] = $this->filterResultTextGrade($this->_post);
         }
         $quiz = new WpProQuiz_Model_Quiz($this->_post);
         $quizMapper = new WpProQuiz_Model_QuizMapper();
         if ($this->checkValidit($this->_post)) {
             WpProQuiz_View_View::admin_notices(__('Create quiz', 'wp-pro-quiz'), 'info');
             $quizMapper->save($quiz);
             $id = $quizMapper->getInsertId();
             $prerequisiteMapper = new WpProQuiz_Model_PrerequisiteMapper();
             if ($quiz->isPrerequisite() && !empty($this->_post['prerequisiteList'])) {
                 $prerequisiteMapper->save($id, $this->_post['prerequisiteList']);
                 $quizMapper->activateStatitic($this->_post['prerequisiteList'], 1440);
             }
             if (!$this->formHandler($id, $this->_post)) {
                 $quiz->setFormActivated(false);
                 $quizMapper->save($quiz);
             }
             $this->showAction();
             return;
         } else {
             WpProQuiz_View_View::admin_notices(__('Quiz title or quiz description are not filled', 'wp-pro-quiz'));
         }
     } else {
         if (isset($this->_post['template']) || isset($this->_post['templateLoad'])) {
             if (isset($this->_post['template'])) {
                 $template = $this->saveTemplate();
             } else {
                 $template = $templateMapper->fetchById($this->_post['templateLoadId']);
             }
             $data = $template->getData();
             if ($data !== null) {
                 $quiz = $data['quiz'];
                 $forms = $data['forms'];
                 $prerequisiteQuizList = $data['prerequisiteQuizList'];
             }
         } else {
             $quiz = new WpProQuiz_Model_Quiz();
         }
     }
     $this->view->quiz = $quiz;
     $this->view->prerequisiteQuizList = $prerequisiteQuizList;
     $this->view->quizList = $m->fetchAllAsArray(array('id', 'name'));
     $this->view->captchaIsInstalled = class_exists('ReallySimpleCaptcha');
     $this->view->forms = $forms;
     $this->view->templates = $templateMapper->fetchAll(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUIZ, false);
     $this->view->show();
 }