public function copyQuestion($quizId) { if (!current_user_can('wpProQuiz_edit_quiz')) { wp_die(__('You do not have sufficient permissions to access this page.')); } $m = new WpProQuiz_Model_QuestionMapper(); $questions = $m->fetchById($this->_post['copyIds']); foreach ($questions as $question) { $question->setId(0); $question->setQuizId($quizId); $m->save($question); } WpProQuiz_View_View::admin_notices(__('questions copied', 'wp-pro-quiz'), 'info'); $this->showAction(); }
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; }
/** * @deprecated */ public function editPostAction($id) { $mapper = new WpProQuiz_Model_QuestionMapper(); if (isset($this->_post['submit']) && $mapper->existsAndWritable($id)) { $post = $this->_post; $post['id'] = $id; $post['title'] = isset($post['title']) ? trim($post['title']) : ''; $clearPost = $this->clearPost($post); $post['answerData'] = $clearPost['answerData']; if (empty($post['title'])) { $question = $mapper->fetch($id); $post['title'] = sprintf(__('Question: %d', 'wp-pro-quiz'), $question->getSort() + 1); } if ($post['answerType'] === 'assessment_answer') { $post['answerPointsActivated'] = 1; } if (isset($post['answerPointsActivated'])) { if (isset($post['answerPointsDiffModusActivated'])) { $post['points'] = $clearPost['maxPoints']; } else { $post['points'] = $clearPost['points']; } } $post['categoryId'] = $post['category'] > 0 ? $post['category'] : 0; $mapper->save(new WpProQuiz_Model_Question($post), true); WpProQuiz_View_View::admin_notices(__('Question edited', 'wp-pro-quiz'), 'info'); } }