public function save(WpProQuiz_Model_Quiz $data)
 {
     if ($data->isResultGradeEnabled()) {
         $resultText = serialize($data->getResultText());
     } else {
         $resultText = $data->getResultText();
     }
     $set = array('name' => $data->getName(), 'text' => $data->getText(), 'result_text' => $resultText, 'title_hidden' => (int) $data->isTitleHidden(), 'btn_restart_quiz_hidden' => (int) $data->isBtnRestartQuizHidden(), 'btn_view_question_hidden' => (int) $data->isBtnViewQuestionHidden(), 'question_random' => (int) $data->isQuestionRandom(), 'answer_random' => (int) $data->isAnswerRandom(), 'time_limit' => (int) $data->getTimeLimit(), 'statistics_on' => (int) $data->isStatisticsOn(), 'statistics_ip_lock' => (int) $data->getStatisticsIpLock(), 'result_grade_enabled' => (int) $data->isResultGradeEnabled(), 'show_points' => (int) $data->isShowPoints(), 'quiz_run_once' => (int) $data->isQuizRunOnce(), 'quiz_run_once_type' => $data->getQuizRunOnceType(), 'quiz_run_once_cookie' => (int) $data->isQuizRunOnceCookie(), 'quiz_run_once_time' => (int) $data->getQuizRunOnceTime(), 'numbered_answer' => (int) $data->isNumberedAnswer(), 'hide_answer_message_box' => (int) $data->isHideAnswerMessageBox(), 'disabled_answer_mark' => (int) $data->isDisabledAnswerMark(), 'show_max_question' => (int) $data->isShowMaxQuestion(), 'show_max_question_value' => (int) $data->getShowMaxQuestionValue(), 'show_max_question_percent' => (int) $data->isShowMaxQuestionPercent(), 'toplist_activated' => (int) $data->isToplistActivated(), 'toplist_data' => $data->getToplistData(), 'show_average_result' => (int) $data->isShowAverageResult(), 'prerequisite' => (int) $data->isPrerequisite(), 'quiz_modus' => (int) $data->getQuizModus(), 'show_review_question' => (int) $data->isShowReviewQuestion(), 'quiz_summary_hide' => (int) $data->isQuizSummaryHide(), 'skip_question_disabled' => (int) $data->isSkipQuestionDisabled(), 'email_notification' => $data->getEmailNotification(), 'user_email_notification' => (int) $data->isUserEmailNotification(), 'show_category_score' => (int) $data->isShowCategoryScore(), 'hide_result_correct_question' => (int) $data->isHideResultCorrectQuestion(), 'hide_result_quiz_time' => (int) $data->isHideResultQuizTime(), 'hide_result_points' => (int) $data->isHideResultPoints(), 'autostart' => (int) $data->isAutostart(), 'forcing_question_solve' => (int) $data->isForcingQuestionSolve(), 'hide_question_position_overview' => (int) $data->isHideQuestionPositionOverview(), 'hide_question_numbering' => (int) $data->isHideQuestionNumbering(), 'form_activated' => (int) $data->isFormActivated(), 'form_show_position' => $data->getFormShowPosition(), 'start_only_registered_user' => (int) $data->isStartOnlyRegisteredUser(), 'questions_per_page' => $data->getQuestionsPerPage(), 'sort_categories' => (int) $data->isSortCategories(), 'show_category' => (int) $data->isShowCategory(), 'category_id' => (int) $data->getCategoryId(), 'admin_email' => $data->getAdminEmail(true), 'user_email' => $data->getUserEmail(true), 'plugin_container' => $data->getPluginContainer(true));
     if ($data->getId() != 0) {
         $result = $this->_wpdb->update($this->_table, $set, array('id' => $data->getId()), array('%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s'), array('%d'));
     } else {
         $result = $this->_wpdb->insert($this->_table, $set, array('%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s'));
         $data->setId($this->_wpdb->insert_id);
     }
     if ($result === false) {
         return null;
     }
     return $data;
 }
 private function addEditQuiz()
 {
     $quizId = isset($_GET['quizId']) ? (int) $_GET['quizId'] : 0;
     if ($quizId) {
         if (!current_user_can('wpProQuiz_edit_quiz')) {
             wp_die(__('You do not have sufficient permissions to access this page.'));
         }
     } else {
         if (!current_user_can('wpProQuiz_add_quiz')) {
             wp_die(__('You do not have sufficient permissions to access this page.'));
         }
     }
     $prerequisiteMapper = new WpProQuiz_Model_PrerequisiteMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     $cateoryMapper = new WpProQuiz_Model_CategoryMapper();
     $quiz = new WpProQuiz_Model_Quiz();
     $forms = null;
     $prerequisiteQuizList = array();
     if ($quizId && $quizMapper->exists($quizId) == 0) {
         WpProQuiz_View_View::admin_notices(__('Quiz not found', 'wp-pro-quiz'), 'error');
         return;
     }
     if (isset($this->_post['template']) || isset($this->_post['templateLoad']) && isset($this->_post['templateLoadId'])) {
         if (isset($this->_post['template'])) {
             $template = $this->saveTemplate();
         } else {
             $template = $templateMapper->fetchById($this->_post['templateLoadId']);
         }
         $data = $template->getData();
         if ($data !== null) {
             /** @var WpProQuiz_Model_Quiz $quiz */
             $quiz = $data['quiz'];
             $quiz->setId($quizId);
             $forms = $data['forms'];
             $prerequisiteQuizList = $data['prerequisiteQuizList'];
         }
     } else {
         if (isset($this->_post['submit'])) {
             if (isset($this->_post['resultGradeEnabled'])) {
                 $this->_post['result_text'] = $this->filterResultTextGrade($this->_post);
             }
             $this->_post['categoryId'] = $this->_post['category'] > 0 ? $this->_post['category'] : 0;
             $this->_post['adminEmail'] = new WpProQuiz_Model_Email($this->_post['adminEmail']);
             $this->_post['userEmail'] = new WpProQuiz_Model_Email($this->_post['userEmail']);
             $quiz = new WpProQuiz_Model_Quiz($this->_post);
             $quiz->setId($quizId);
             if (isset($this->_post['plugin'])) {
                 $quiz->getPluginContainer()->set($this->_post['plugin']);
             }
             if ($this->checkValidit($this->_post)) {
                 if ($quizId) {
                     WpProQuiz_View_View::admin_notices(__('Quiz edited', 'wp-pro-quiz'), 'info');
                 } else {
                     WpProQuiz_View_View::admin_notices(__('quiz created', 'wp-pro-quiz'), 'info');
                 }
                 $quizMapper->save($quiz);
                 $quizId = $quiz->getId();
                 $prerequisiteMapper->delete($quizId);
                 if ($quiz->isPrerequisite() && !empty($this->_post['prerequisiteList'])) {
                     $prerequisiteMapper->save($quizId, $this->_post['prerequisiteList']);
                     $quizMapper->activateStatitic($this->_post['prerequisiteList'], 1440);
                 }
                 if (!$this->formHandler($quiz->getId(), $this->_post)) {
                     $quiz->setFormActivated(false);
                     $quizMapper->save($quiz);
                 }
                 $forms = $formMapper->fetch($quizId);
                 $prerequisiteQuizList = $prerequisiteMapper->fetchQuizIds($quizId);
             } else {
                 WpProQuiz_View_View::admin_notices(__('Quiz title or quiz description are not filled', 'wp-pro-quiz'));
             }
         } else {
             if ($quizId) {
                 $quiz = $quizMapper->fetch($quizId);
                 $forms = $formMapper->fetch($quizId);
                 $prerequisiteQuizList = $prerequisiteMapper->fetchQuizIds($quizId);
             }
         }
     }
     $view = new WpProQuiz_View_QuizEdit();
     $view->quiz = $quiz;
     $view->forms = $forms;
     $view->prerequisiteQuizList = $prerequisiteQuizList;
     $view->templates = $templateMapper->fetchAll(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUIZ, false);
     $view->quizList = $quizMapper->fetchAllAsArray(array('id', 'name'), $quizId ? array($quizId) : array());
     $view->captchaIsInstalled = class_exists('ReallySimpleCaptcha');
     $view->categories = $cateoryMapper->fetchAll(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
     $view->header = $quizId ? __('Edit quiz', 'wp-pro-quiz') : __('Create quiz', 'wp-pro-quiz');
     $view->show();
 }