public static function ajaxDeleteTemplate($data, $func)
 {
     if (!current_user_can('wpProQuiz_edit_quiz')) {
         return json_encode(array());
     }
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     $template = new WpProQuiz_Model_Template($data);
     $templateMapper->delete($template->getTemplateId());
     return json_encode(array());
 }
 private function edit()
 {
     if (!current_user_can('wpProQuiz_change_settings')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     $mapper = new WpProQuiz_Model_GlobalSettingsMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     $view = new WpProQuiz_View_GobalSettings();
     if (isset($this->_post['submit'])) {
         $mapper->save(new WpProQuiz_Model_GlobalSettings($this->_post));
         WpProQuiz_View_View::admin_notices(__('Settings saved', 'wp-pro-quiz'), 'info');
         $toplistDateFormat = $this->_post['toplist_date_format'];
         if ($toplistDateFormat == 'custom') {
             $toplistDateFormat = trim($this->_post['toplist_date_format_custom']);
         }
         $statisticTimeFormat = $this->_post['statisticTimeFormat'];
         if (add_option('wpProQuiz_toplistDataFormat', $toplistDateFormat) === false) {
             update_option('wpProQuiz_toplistDataFormat', $toplistDateFormat);
         }
         if (add_option('wpProQuiz_statisticTimeFormat', $statisticTimeFormat, '', 'no') === false) {
             update_option('wpProQuiz_statisticTimeFormat', $statisticTimeFormat);
         }
         //Email
         //$mapper->saveEmailSettiongs($this->_post['email']);
         //$mapper->saveUserEmailSettiongs($this->_post['userEmail']);
     } else {
         if (isset($this->_post['databaseFix'])) {
             WpProQuiz_View_View::admin_notices(__('Database repaired', 'wp-pro-quiz'), 'info');
             $DbUpgradeHelper = new WpProQuiz_Helper_DbUpgrade();
             $DbUpgradeHelper->databaseDelta();
         }
     }
     $view->settings = $mapper->fetchAll();
     $view->isRaw = !preg_match('[raw]', apply_filters('the_content', '[raw]a[/raw]'));
     $view->category = $categoryMapper->fetchAll();
     $view->categoryQuiz = $categoryMapper->fetchAll(WpProQuiz_Model_Category::CATEGORY_TYPE_QUIZ);
     $view->email = $mapper->getEmailSettings();
     $view->userEmail = $mapper->getUserEmailSettings();
     $view->templateQuiz = $templateMapper->fetchAll(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUIZ, false);
     $view->templateQuestion = $templateMapper->fetchAll(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUESTION, false);
     $view->toplistDataFormat = get_option('wpProQuiz_toplistDataFormat', 'Y/m/d g:i A');
     $view->statisticTimeFormat = get_option('wpProQuiz_statisticTimeFormat', 'Y/m/d g:i A');
     $view->show();
 }
 private function saveTemplate()
 {
     $questionModel = $this->getPostQuestionModel(0, 0);
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     $template = new WpProQuiz_Model_Template();
     if ($this->_post['templateSaveList'] == '0') {
         $template->setName(trim($this->_post['templateName']));
     } else {
         $template = $templateMapper->fetchById($this->_post['templateSaveList'], false);
     }
     $template->setType(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUESTION);
     $template->setData(array('question' => $questionModel));
     return $templateMapper->save($template);
 }
 /**
  * @deprecated
  */
 public function createAction()
 {
     if (!current_user_can('wpProQuiz_add_quiz')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $cateoryMapper = new WpProQuiz_Model_CategoryMapper();
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     // 		$post = null;
     // 		if(isset($this->_post['submit'])) {
     // 			$questionMapper = new WpProQuiz_Model_QuestionMapper();
     // 			$post = WpProQuiz_Controller_Request::getPost();
     // 			$post['title'] = isset($post['title']) ? trim($post['title']) : '';
     // 			$post['quizId'] = $this->_quizId;
     // 			$clearPost = $this->clearPost($post);
     // 			$post['answerData'] = $clearPost['answerData'];
     // 			if(empty($post['title'])) {
     // 				$count = $questionMapper->count($this->_quizId);
     // 				$post['title'] = sprintf(__('Question: %d', 'wp-pro-quiz'), $count+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;
     // 			$questionMapper->save(new WpProQuiz_Model_Question($post));
     // 			WpProQuiz_View_View::admin_notices(__('Question added', 'wp-pro-quiz'), 'info');
     // 			$this->showAction();
     // 			return;
     // 		}
     $this->view = new WpProQuiz_View_QuestionEdit();
     $this->view->question = new WpProQuiz_Model_Question();
     $this->view->categories = $cateoryMapper->fetchAll();
     $this->view->quiz = $quizMapper->fetch($this->_quizId);
     $this->view->data = $this->setAnswerObject();
     $this->view->templates = $templateMapper->fetchAll(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUESTION, false);
     $this->view->header = __('New question', 'wp-pro-quiz');
     if ($this->view->question->isAnswerPointsActivated()) {
         $this->view->question->setPoints(1);
     }
     $this->view->show();
 }
 private function saveTemplate()
 {
     $templateMapper = new WpProQuiz_Model_TemplateMapper();
     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);
     if ($quiz->isPrerequisite() && !empty($this->_post['prerequisiteList']) && !$quiz->isStatisticsOn()) {
         $quiz->setStatisticsOn(true);
         $quiz->setStatisticsIpLock(1440);
     }
     $form = $this->_post['form'];
     unset($form[0]);
     $forms = array();
     foreach ($form as $f) {
         $f['fieldname'] = trim($f['fieldname']);
         if (empty($f['fieldname'])) {
             continue;
         }
         if ((int) $f['form_id'] && (int) $f['form_delete']) {
             continue;
         }
         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);
     }
     WpProQuiz_View_View::admin_notices(__('Template stored', 'wp-pro-quiz'), 'info');
     $data = array('quiz' => $quiz, 'forms' => $forms, 'prerequisiteQuizList' => isset($this->_post['prerequisiteList']) ? $this->_post['prerequisiteList'] : array());
     $template = new WpProQuiz_Model_Template();
     if ($this->_post['templateSaveList'] == '0') {
         $template->setName(trim($this->_post['templateName']));
     } else {
         $template = $templateMapper->fetchById($this->_post['templateSaveList'], false);
     }
     $template->setType(WpProQuiz_Model_Template::TEMPLATE_TYPE_QUIZ);
     $template->setData($data);
     $templateMapper->save($template);
     return $template;
 }