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());
 }
 public function fetchById($templateId, $loadData = true)
 {
     $row = $this->_wpdb->get_row($this->_wpdb->prepare("SELECT * FROM {$this->_tableTemplate} WHERE template_id = %d ", $templateId), ARRAY_A);
     if ($row !== null) {
         $data = $row['data'];
         unset($row['data']);
         $template = new WpProQuiz_Model_Template($row);
         if ($loadData && WpProQuiz_Helper_Until::saveUnserialize($data, $into)) {
             $template->setData($into);
         }
         return $template;
     }
     return new WpProQuiz_Model_Template();
 }
 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);
 }
 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;
 }