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 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;
 }
 private function formHandler($quizId, $post)
 {
     if (!isset($post['form'])) {
         return false;
     }
     $form = $post['form'];
     unset($form[0]);
     if (empty($form)) {
         return false;
     }
     $formMapper = new WpProQuiz_Model_FormMapper();
     $deleteIds = array();
     $forms = array();
     $sort = 0;
     foreach ($form as $f) {
         $f['fieldname'] = trim($f['fieldname']);
         if (empty($f['fieldname'])) {
             continue;
         }
         if ((int) $f['form_id'] && (int) $f['form_delete']) {
             $deleteIds[] = (int) $f['form_id'];
             continue;
         }
         $f['sort'] = $sort++;
         $f['quizId'] = $quizId;
         if ($f['type'] == WpProQuiz_Model_Form::FORM_TYPE_SELECT || $f['type'] == WpProQuiz_Model_Form::FORM_TYPE_RADIO) {
             if (!empty($f['data'])) {
                 $items = explode("\n", $f['data']);
                 $f['data'] = array();
                 foreach ($items as $item) {
                     $item = trim($item);
                     if (!empty($item)) {
                         $f['data'][] = $item;
                     }
                 }
             }
         }
         if (empty($f['data']) || !is_array($f['data'])) {
             $f['data'] = null;
         }
         $forms[] = new WpProQuiz_Model_Form($f);
     }
     if (!empty($deleteIds)) {
         $formMapper->deleteForm($deleteIds, $quizId);
     }
     $formMapper->update($forms);
     return !empty($forms);
 }