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 upgradeDbV23()
    {
        $this->_wpdb->query('
			ALTER TABLE  ' . $this->_wpdb->prefix . 'wp_pro_quiz_master  
				ADD  `admin_email` TEXT NOT NULL ,
				ADD  `user_email` TEXT NOT NULL ;
		');
        $mapper = new WpProQuiz_Model_GlobalSettingsMapper();
        $adminEmail = $mapper->getEmailSettings();
        $userEmail = $mapper->getUserEmailSettings();
        $adminEmailNew = new WpProQuiz_Model_Email();
        $adminEmailNew->setTo($adminEmail['to']);
        $adminEmailNew->setFrom($adminEmail['from']);
        $adminEmailNew->setSubject($adminEmail['subject']);
        $adminEmailNew->setHtml($adminEmail['html']);
        $adminEmailNew->setMessage($adminEmail['message']);
        $userEmailNew = new WpProQuiz_Model_Email();
        $userEmailNew->setFrom($userEmail['from']);
        $userEmailNew->setToUser(true);
        $userEmailNew->setSubject($userEmail['subject']);
        $userEmailNew->setHtml($userEmail['html']);
        $userEmailNew->setMessage($userEmail['message']);
        $this->_wpdb->update($this->_wpdb->prefix . 'wp_pro_quiz_master', array('admin_email' => @serialize($adminEmailNew)), array('email_notification' => 1), array('%s'), array('%d'));
        $this->_wpdb->update($this->_wpdb->prefix . 'wp_pro_quiz_master', array('admin_email' => @serialize($adminEmailNew)), array('email_notification' => 2), array('%s'), array('%d'));
        $this->_wpdb->update($this->_wpdb->prefix . 'wp_pro_quiz_master', array('user_email' => @serialize($userEmailNew)), array('user_email_notification' => 1), array('%s'), array('%d'));
        return 24;
    }
 private function loadSettings()
 {
     $mapper = new WpProQuiz_Model_GlobalSettingsMapper();
     $this->_settings = $mapper->fetchAll();
 }
 private function emailNote(WpProQuiz_Model_Quiz $quiz, $result, $categories)
 {
     $globalMapper = new WpProQuiz_Model_GlobalSettingsMapper();
     $adminEmail = $globalMapper->getEmailSettings();
     $userEmail = $globalMapper->getUserEmailSettings();
     $user = wp_get_current_user();
     $r = array('$userId' => $user->ID, '$username' => $user->display_name, '$quizname' => $quiz->getName(), '$result' => $result['result'] . '%', '$points' => $result['points'], '$ip' => filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP), '$categories' => empty($result['cats']) ? '' : $this->setCategoryOverview($result['cats'], $categories));
     if ($user->ID == 0) {
         $r['$username'] = $r['$ip'];
     }
     if ($quiz->isUserEmailNotification()) {
         $msg = str_replace(array_keys($r), $r, $userEmail['message']);
         $headers = '';
         if (!empty($userEmail['from'])) {
             $headers = 'From: ' . $userEmail['from'];
         }
         if ($userEmail['html']) {
             add_filter('wp_mail_content_type', array($this, 'htmlEmailContent'));
         }
         wp_mail($user->user_email, $userEmail['subject'], $msg, $headers);
         if ($userEmail['html']) {
             remove_filter('wp_mail_content_type', array($this, 'htmlEmailContent'));
         }
     }
     if ($quiz->getEmailNotification() == WpProQuiz_Model_Quiz::QUIZ_EMAIL_NOTE_ALL || get_current_user_id() > 0 && $quiz->getEmailNotification() == WpProQuiz_Model_Quiz::QUIZ_EMAIL_NOTE_REG_USER) {
         $msg = str_replace(array_keys($r), $r, $adminEmail['message']);
         $headers = '';
         if (!empty($adminEmail['from'])) {
             $headers = 'From: ' . $adminEmail['from'];
         }
         if (isset($adminEmail['html']) && $adminEmail['html']) {
             add_filter('wp_mail_content_type', array($this, 'htmlEmailContent'));
         }
         wp_mail($adminEmail['to'], $adminEmail['subject'], $msg, $headers);
         if (isset($adminEmail['html']) && $adminEmail['html']) {
             remove_filter('wp_mail_content_type', array($this, 'htmlEmailContent'));
         }
     }
 }