public function quizCompleted()
 {
     $lockMapper = new WpProQuiz_Model_LockMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $statistics = new WpProQuiz_Controller_Statistics();
     $quiz = $quizMapper->fetch($this->data['quizId']);
     if ($quiz === null || $quiz->getId() <= 0) {
         return;
     }
     $categories = $categoryMapper->fetchByQuiz($quiz->getId());
     $userId = get_current_user_id();
     $resultInPercent = floor($this->data['results']['comp']['result']);
     $this->setResultCookie($quiz);
     $this->emailNote($quiz, $this->data['results']['comp'], $categories);
     if (!$this->isPreLockQuiz($quiz)) {
         $statistics->save();
         do_action('wp_pro_quiz_completed_quiz');
         do_action('wp_pro_quiz_completed_quiz_' . $resultInPercent . '_percent');
         return;
     }
     $lockMapper->deleteOldLock(60 * 60 * 24 * 7, $this->_post['quizId'], time(), WpProQuiz_Model_Lock::TYPE_QUIZ, 0);
     $lockIp = $lockMapper->isLock($this->_post['quizId'], $this->getIp(), get_current_user_id(), WpProQuiz_Model_Lock::TYPE_QUIZ);
     $lockCookie = false;
     $cookieTime = $quiz->getQuizRunOnceTime();
     $cookieJson = null;
     if (isset($this->_cookie['wpProQuiz_lock']) && get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
         $cookieJson = json_decode($this->_cookie['wpProQuiz_lock'], true);
         if ($cookieJson !== false) {
             if (isset($cookieJson[$this->_post['quizId']]) && $cookieJson[$this->_post['quizId']] == $cookieTime) {
                 $lockCookie = true;
             }
         }
     }
     if (!$lockIp && !$lockCookie) {
         $statistics->save();
         do_action('wp_pro_quiz_completed_quiz');
         do_action('wp_pro_quiz_completed_quiz_' . $resultInPercent . '_percent');
         if (get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
             $cookieData = array();
             if ($cookieJson !== null || $cookieJson !== false) {
                 $cookieData = $cookieJson;
             }
             $cookieData[$this->_post['quizId']] = $quiz->getQuizRunOnceTime();
             $url = parse_url(get_bloginfo('url'));
             setcookie('wpProQuiz_lock', json_encode($cookieData), time() + 60 * 60 * 24 * 60, empty($url['path']) ? '/' : $url['path']);
         }
         $lock = new WpProQuiz_Model_Lock();
         $lock->setUserId(get_current_user_id());
         $lock->setQuizId($this->_post['quizId']);
         $lock->setLockDate(time());
         $lock->setLockIp($this->getIp());
         $lock->setLockType(WpProQuiz_Model_Lock::TYPE_QUIZ);
         $lockMapper->insert($lock);
     }
     return;
 }
 public static function ajaxDeleteCategory($data, $func)
 {
     if (!current_user_can('wpProQuiz_edit_quiz')) {
         return json_encode(array());
     }
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $category = new WpProQuiz_Model_Category($data);
     $categoryMapper->delete($category->getCategoryId());
     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();
 }
 /**
  * @deprecated
  */
 private function show($quizId)
 {
     if (!current_user_can('wpProQuiz_show_statistics')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     $this->showNew($quizId);
     return;
     $view = new WpProQuiz_View_Statistics();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $questions = $questionMapper->fetchAll($quizId);
     $category = $categoryMapper->fetchAll();
     $categoryEmpty = new WpProQuiz_Model_Category();
     $categoryEmpty->setCategoryName(__('No category', 'wp-pro-quiz'));
     $list = array();
     $cats = array();
     foreach ($category as $c) {
         $cats[$c->getCategoryId()] = $c;
     }
     $cats[0] = $categoryEmpty;
     foreach ($questions as $q) {
         $list[$q->getCategoryId()][] = $q;
     }
     $view->quiz = $quizMapper->fetch($quizId);
     $view->questionList = $list;
     $view->categoryList = $cats;
     $view->forms = $formMapper->fetch($quizId);
     if (has_action('pre_user_query', 'ure_exclude_administrators')) {
         remove_action('pre_user_query', 'ure_exclude_administrators');
         $users = get_users(array('fields' => array('ID', 'user_login', 'display_name')));
         add_action('pre_user_query', 'ure_exclude_administrators');
     } else {
         $users = get_users(array('fields' => array('ID', 'user_login', 'display_name')));
     }
     array_unshift($users, (object) array('ID' => 0));
     $view->users = $users;
     $view->show();
 }
 public function showAction($id)
 {
     $view = new WpProQuiz_View_FrontQuiz();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $quiz = $quizMapper->fetch($id);
     if ($quiz->isShowMaxQuestion() && $quiz->getShowMaxQuestionValue() > 0) {
         $value = $quiz->getShowMaxQuestionValue();
         if ($quiz->isShowMaxQuestionPercent()) {
             $count = $questionMapper->count($id);
             $value = ceil($count * $value / 100);
         }
         $question = $questionMapper->fetchAll($id, true, $value);
     } else {
         $question = $questionMapper->fetchAll($id);
     }
     $view->quiz = $quiz;
     $view->question = $question;
     $view->category = $categoryMapper->fetchByQuiz($quiz->getId());
     $view->forms = $formMapper->fetch($quiz->getId());
     $view->show(true);
 }
 public function showAction()
 {
     if (!current_user_can('wpProQuiz_show')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     $m = new WpProQuiz_Model_QuizMapper();
     $mm = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $view = new WpProQuiz_View_QuestionOverall();
     $view->quiz = $m->fetch($this->_quizId);
     $per_page = (int) get_user_option('wp_pro_quiz_question_overview_per_page');
     if (empty($per_page) || $per_page < 1) {
         $per_page = 20;
     }
     $current_page = $this->getCurrentPage();
     $search = isset($_GET['s']) ? trim($_GET['s']) : '';
     $orderBy = isset($_GET['orderby']) ? trim($_GET['orderby']) : '';
     $order = isset($_GET['order']) ? trim($_GET['order']) : '';
     $offset = ($current_page - 1) * $per_page;
     $limit = $per_page;
     $filter = array();
     if (isset($_GET['cat'])) {
         $filter['cat'] = $_GET['cat'];
     }
     $result = $mm->fetchTable($this->_quizId, $orderBy, $order, $search, $limit, $offset, $filter);
     $view->questionItems = $result['questions'];
     $view->questionCount = $result['count'];
     $view->categoryItems = $categoryMapper->fetchAll(WpProQuiz_Model_Category::CATEGORY_TYPE_QUESTION);
     $view->perPage = $per_page;
     $view->show();
 }
 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;
 }
 public static function ajaxQuizLoadData($data)
 {
     $id = $data['quizId'];
     $view = new WpProQuiz_View_FrontQuiz();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $questionMapper = new WpProQuiz_Model_QuestionMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $quiz = $quizMapper->fetch($id);
     if ($quiz->isShowMaxQuestion() && $quiz->getShowMaxQuestionValue() > 0) {
         $value = $quiz->getShowMaxQuestionValue();
         if ($quiz->isShowMaxQuestionPercent()) {
             $count = $questionMapper->count($id);
             $value = ceil($count * $value / 100);
         }
         $question = $questionMapper->fetchAll($id, true, $value);
     } else {
         $question = $questionMapper->fetchAll($id);
     }
     if (empty($quiz) || empty($question)) {
         return null;
     }
     $view->quiz = $quiz;
     $view->question = $question;
     $view->category = $categoryMapper->fetchByQuiz($quiz->getId());
     $view->forms = $formMapper->fetch($quiz->getId());
     return json_encode($view->getQuizData());
 }
 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 static function ajaxCompletedQuiz($data)
 {
     // workaround ...
     $_POST = $_POST['data'];
     $ctr = new WpProQuiz_Controller_Quiz();
     $lockMapper = new WpProQuiz_Model_LockMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $categoryMapper = new WpProQuiz_Model_CategoryMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $is100P = $data['results']['comp']['result'] == 100;
     $quiz = $quizMapper->fetch($data['quizId']);
     if ($quiz === null || $quiz->getId() <= 0) {
         return json_encode(array());
     }
     $categories = $categoryMapper->fetchByQuiz($quiz->getId());
     $forms = $formMapper->fetch($quiz->getId());
     $ctr->setResultCookie($quiz);
     $ctr->emailNote($quiz, $data['results']['comp'], $categories, $forms, isset($data['forms']) ? $data['forms'] : array());
     if (!$ctr->isPreLockQuiz($quiz)) {
         $statistics = new WpProQuiz_Controller_Statistics();
         $statistics->save($quiz);
         do_action('wp_pro_quiz_completed_quiz');
         if ($is100P) {
             do_action('wp_pro_quiz_completed_quiz_100_percent');
         }
         return json_encode(array());
     }
     $lockMapper->deleteOldLock(60 * 60 * 24 * 7, $data['quizId'], time(), WpProQuiz_Model_Lock::TYPE_QUIZ, 0);
     $lockIp = $lockMapper->isLock($data['quizId'], $ctr->getIp(), get_current_user_id(), WpProQuiz_Model_Lock::TYPE_QUIZ);
     $lockCookie = false;
     $cookieTime = $quiz->getQuizRunOnceTime();
     $cookieJson = null;
     if (isset($ctr->_cookie['wpProQuiz_lock']) && get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
         $cookieJson = json_decode($ctr->_cookie['wpProQuiz_lock'], true);
         if ($cookieJson !== false) {
             if (isset($cookieJson[$data['quizId']]) && $cookieJson[$data['quizId']] == $cookieTime) {
                 $lockCookie = true;
             }
         }
     }
     if (!$lockIp && !$lockCookie) {
         $statistics = new WpProQuiz_Controller_Statistics();
         $statistics->save($quiz);
         do_action('wp_pro_quiz_completed_quiz');
         if ($is100P) {
             do_action('wp_pro_quiz_completed_quiz_100_percent');
         }
         if (get_current_user_id() == 0 && $quiz->isQuizRunOnceCookie()) {
             $cookieData = array();
             if ($cookieJson !== null || $cookieJson !== false) {
                 $cookieData = $cookieJson;
             }
             $cookieData[$data['quizId']] = $quiz->getQuizRunOnceTime();
             $url = parse_url(get_bloginfo('url'));
             setcookie('wpProQuiz_lock', json_encode($cookieData), time() + 60 * 60 * 24 * 60, empty($url['path']) ? '/' : $url['path']);
         }
         $lock = new WpProQuiz_Model_Lock();
         $lock->setUserId(get_current_user_id());
         $lock->setQuizId($data['quizId']);
         $lock->setLockDate(time());
         $lock->setLockIp($ctr->getIp());
         $lock->setLockType(WpProQuiz_Model_Lock::TYPE_QUIZ);
         $lockMapper->insert($lock);
     }
     return json_encode(array());
 }