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();
 }
 public function getPluginContainer($serialize = false)
 {
     if ($this->_pluginContainer === null) {
         $this->_pluginContainer = new WpProQuiz_Model_PluginContainer();
     }
     if (is_object($this->_pluginContainer) || $this->_pluginContainer instanceof WpProQuiz_Model_PluginContainer) {
         if ($serialize) {
             return @serialize($this->_pluginContainer);
         }
     } else {
         if (!$serialize) {
             if (WpProQuiz_Helper_Until::saveUnserialize($this->_pluginContainer, $into) === false) {
                 return null;
             }
             $this->_pluginContainer = $into;
         }
     }
     return $this->_pluginContainer;
 }
    private function showFormBox()
    {
        $info = '<div class="wpProQuiz_invalidate">' . __('You must fill out this field.', 'wp-pro-quiz') . '</div>';
        $validateText = array(WpProQuiz_Model_Form::FORM_TYPE_NUMBER => __('You must specify a number.', 'wp-pro-quiz'), WpProQuiz_Model_Form::FORM_TYPE_TEXT => __('You must specify a text.', 'wp-pro-quiz'), WpProQuiz_Model_Form::FORM_TYPE_EMAIL => __('You must specify an email address.', 'wp-pro-quiz'), WpProQuiz_Model_Form::FORM_TYPE_DATE => __('You must specify a date.', 'wp-pro-quiz'));
        ?>
		<div class="wpProQuiz_forms">		 
			<table>
				<tbody>
				
			 <?php 
        $index = 0;
        foreach ($this->forms as $form) {
            /* @var $form WpProQuiz_Model_Form  */
            $id = 'forms_' . $this->quiz->getId() . '_' . $index++;
            $name = 'wpProQuiz_field_' . $form->getFormId();
            ?>
					<tr>
						<td>
							<?php 
            echo '<label for="' . $id . '">';
            echo esc_html($form->getFieldname());
            echo $form->isRequired() ? '<span class="wpProQuiz_required">*</span>' : '';
            echo '</label>';
            ?>
						</td>
						<td>
							
							<?php 
            switch ($form->getType()) {
                case WpProQuiz_Model_Form::FORM_TYPE_TEXT:
                case WpProQuiz_Model_Form::FORM_TYPE_EMAIL:
                case WpProQuiz_Model_Form::FORM_TYPE_NUMBER:
                    echo '<input name="' . $name . '" id="' . $id . '" type="text" ', 'data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" data-form_id="' . $form->getFormId() . '">';
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_TEXTAREA:
                    echo '<textarea rows="5" cols="20" name="' . $name . '" id="' . $id . '" ', 'data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" data-form_id="' . $form->getFormId() . '"></textarea>';
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_CHECKBOX:
                    echo '<input name="' . $name . '" id="' . $id . '" type="checkbox" value="1"', 'data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" data-form_id="' . $form->getFormId() . '">';
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_DATE:
                    echo '<div data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" class="wpProQuiz_formFields" data-form_id="' . $form->getFormId() . '">';
                    echo WpProQuiz_Helper_Until::getDatePicker(get_option('date_format', 'j. F Y'), $name);
                    echo '</div>';
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_RADIO:
                    echo '<div data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" class="wpProQuiz_formFields" data-form_id="' . $form->getFormId() . '">';
                    if ($form->getData() !== null) {
                        foreach ($form->getData() as $data) {
                            echo '<label>';
                            echo '<input name="' . $name . '" type="radio" value="' . esc_attr($data) . '"> ', esc_html($data);
                            echo '</label> ';
                        }
                    }
                    echo '</div>';
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_SELECT:
                    if ($form->getData() !== null) {
                        echo '<select name="' . $name . '" id="' . $id . '" ', 'data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" data-form_id="' . $form->getFormId() . '">';
                        echo '<option value=""></option>';
                        foreach ($form->getData() as $data) {
                            echo '<option value="' . esc_attr($data) . '">', esc_html($data), '</option>';
                        }
                        echo '</select>';
                    }
                    break;
                case WpProQuiz_Model_Form::FORM_TYPE_YES_NO:
                    echo '<div data-required="' . (int) $form->isRequired() . '" data-type="' . $form->getType() . '" class="wpProQuiz_formFields" data-form_id="' . $form->getFormId() . '">';
                    echo '<label>';
                    echo '<input name="' . $name . '" type="radio" value="1"> ', __('Yes', 'wp-pro-quiz');
                    echo '</label> ';
                    echo '<label>';
                    echo '<input name="' . $name . '" type="radio" value="0"> ', __('No', 'wp-pro-quiz');
                    echo '</label> ';
                    echo '</div>';
                    break;
            }
            if (isset($validateText[$form->getType()])) {
                echo '<div class="wpProQuiz_invalidate">' . $validateText[$form->getType()] . '</div>';
            } else {
                echo '<div class="wpProQuiz_invalidate">' . __('You must fill out this field.', 'wp-pro-quiz') . '</div>';
            }
            ?>
						</td>
					</tr>
				<?php 
        }
        ?>
				</tbody>
			</table>
			
		</div>
	
	<?php 
    }
 public static function ajaxShowFrontToplist($data)
 {
     // workaround ...
     $_POST = $_POST['data'];
     $quizIds = empty($data['quizIds']) ? array() : array_unique((array) $data['quizIds']);
     $toplistMapper = new WpProQuiz_Model_ToplistMapper();
     $quizMapper = new WpProQuiz_Model_QuizMapper();
     $j = array();
     foreach ($quizIds as $quizId) {
         $quiz = $quizMapper->fetch($quizId);
         if ($quiz == null || $quiz->getId() == 0) {
             continue;
         }
         $toplist = $toplistMapper->fetch($quizId, $quiz->getToplistDataShowLimit(), $quiz->getToplistDataSort());
         foreach ($toplist as $tp) {
             $j[$quizId][] = array('name' => $tp->getName(), 'date' => WpProQuiz_Helper_Until::convertTime($tp->getDate(), get_option('wpProQuiz_toplistDataFormat', 'Y/m/d g:i A')), 'points' => $tp->getPoints(), 'result' => $tp->getResult());
         }
     }
     return json_encode($j);
 }
 public static function ajaxLoadHistory($data)
 {
     if (!current_user_can('wpProQuiz_show_statistics')) {
         return json_encode(array());
     }
     $statisticRefMapper = new WpProQuiz_Model_StatisticRefMapper();
     $formMapper = new WpProQuiz_Model_FormMapper();
     $quizId = $data['quizId'];
     $forms = $formMapper->fetch($quizId);
     $page = isset($data['page']) && $data['page'] > 0 ? $data['page'] : 1;
     $limit = $data['pageLimit'];
     $start = $limit * ($page - 1);
     $startTime = (int) $data['dateFrom'];
     $endTime = (int) $data['dateTo'] ? $data['dateTo'] + 86400 : 0;
     $statisticModel = $statisticRefMapper->fetchHistory($quizId, $start, $limit, $data['users'], $startTime, $endTime);
     foreach ($statisticModel as $model) {
         /* @var $model WpProQuiz_Model_StatisticHistory */
         if (!$model->getUserId()) {
             $model->setUserName(__('Anonymous', 'wp-pro-quiz'));
         } else {
             if ($model->getUserName() == '') {
                 $model->setUserName(__('Deleted user', 'wp-pro-quiz'));
             }
         }
         $sum = $model->getCorrectCount() + $model->getIncorrectCount();
         $result = round(100 * $model->getPoints() / $model->getGPoints(), 2) . '%';
         $model->setResult($result);
         $model->setFormatTime(WpProQuiz_Helper_Until::convertTime($model->getCreateTime(), get_option('wpProQuiz_statisticTimeFormat', 'Y/m/d g:i A')));
         $model->setFormatCorrect($model->getCorrectCount() . ' (' . round(100 * $model->getCorrectCount() / $sum, 2) . '%)');
         $model->setFormatIncorrect($model->getIncorrectCount() . ' (' . round(100 * $model->getIncorrectCount() / $sum, 2) . '%)');
         $formData = $model->getFormData();
         $formOverview = array();
         foreach ($forms as $form) {
             /* @var $form WpProQuiz_Model_Form */
             if ($form->isShowInStatistic()) {
                 $formOverview[] = $formData != null && isset($formData[$form->getFormId()]) ? WpProQuiz_Helper_Form::formToString($form, $formData[$form->getFormId()]) : '----';
             }
         }
         $model->setFormOverview($formOverview);
     }
     $view = new WpProQuiz_View_StatisticsAjax();
     $view->historyModel = $statisticModel;
     $view->forms = $forms;
     $html = $view->getHistoryTable();
     $navi = null;
     if (isset($data['generateNav']) && $data['generateNav']) {
         $count = $statisticRefMapper->countHistory($quizId, $data['users'], $startTime, $endTime);
         $navi = ceil(($count > 0 ? $count : 1) / $limit);
     }
     return json_encode(array('html' => $html, 'navi' => $navi));
 }
 private static function updateV21()
 {
     global $wpdb;
     $results = $wpdb->get_results("\n\t\t\t\tSELECT id, answer_data, answer_type, answer_points_activated, points\n\t\t\t\tFROM {$wpdb->prefix}wp_pro_quiz_question", ARRAY_A);
     foreach ($results as $row) {
         if ($row['points']) {
             continue;
         }
         if (WpProQuiz_Helper_Until::saveUnserialize($row['answer_data'], $into)) {
             $points = 0;
             if ($row['answer_points_activated']) {
                 $dPoints = 0;
                 foreach ($into as $c) {
                     if ($row['answer_type'] == 'cloze_answer') {
                         preg_match_all('#\\{(.*?)(?:\\|(\\d+))?(?:[\\s]+)?\\}#im', $c->getAnswer(), $matches);
                         foreach ($matches[2] as $match) {
                             if (empty($match)) {
                                 $match = 1;
                             }
                             $dPoints += $match;
                         }
                     } else {
                         $dPoints += $c->getPoints();
                     }
                 }
                 $points = $dPoints;
             } else {
                 $points = 1;
             }
             $wpdb->update($wpdb->prefix . 'wp_pro_quiz_question', array('points' => $points), array('id' => $row['id']));
         }
     }
 }
    public function showOverviewTable()
    {
        ?>
		<table class="wp-list-table widefat">
			<thead>
				<tr>
					<th scope="col"><?php 
        _e('User', 'wp-pro-quiz');
        ?>
</th>
					<th scope="col" style="width: 100px;"><?php 
        _e('Points', 'wp-pro-quiz');
        ?>
</th>
					<th scope="col" style="width: 100px;"><?php 
        _e('Correct', 'wp-pro-quiz');
        ?>
</th>
					<th scope="col" style="width: 100px;"><?php 
        _e('Incorrect', 'wp-pro-quiz');
        ?>
</th>
					<th scope="col" style="width: 100px;"><?php 
        _e('Hints used', 'wp-pro-quiz');
        ?>
</th>
					<th scope="col" style="width: 100px;"><?php 
        _e('Time', 'wp-pro-quiz');
        ?>
 <span style="font-size: x-small;">(hh:mm:ss)</span></th>
					<th scope="col" style="width: 60px;"><?php 
        _e('Results', 'wp-pro-quiz');
        ?>
</th>
				</tr>
			</thead>
			<tbody>
				<?php 
        if (!count($this->statisticModel)) {
            ?>
				<tr>
					<td colspan="7" style="text-align: center; font-weight: bold; padding: 10px;"><?php 
            _e('No data available', 'wp-pro-quiz');
            ?>
</td>
				</tr>
				<?php 
        } else {
            ?>
				
				<?php 
            foreach ($this->statisticModel as $model) {
                /** @var WpProQuiz_Model_StatisticOverview  $model **/
                $sum = $model->getCorrectCount() + $model->getIncorrectCount();
                if (!$model->getUserId()) {
                    $model->setUserName(__('Anonymous', 'wp-pro-quiz'));
                }
                if ($sum) {
                    $points = $model->getPoints();
                    $correct = $model->getCorrectCount() . ' (' . round(100 * $model->getCorrectCount() / $sum, 2) . '%)';
                    $incorrect = $model->getIncorrectCount() . ' (' . round(100 * $model->getIncorrectCount() / $sum, 2) . '%)';
                    $hintCount = $model->getHintCount();
                    $time = WpProQuiz_Helper_Until::convertToTimeString($model->getQuestionTime());
                    $result = round(100 * $points / $model->getGPoints(), 2) . '%';
                } else {
                    $result = $time = $hintCount = $incorrect = $correct = $points = '---';
                }
                ?>
				
				<tr>
					<th>
						<?php 
                if ($sum) {
                    ?>
						<a href="#" class="user_statistic" data-user_id="<?php 
                    echo $model->getUserId();
                    ?>
"><?php 
                    echo esc_html($model->getUserName());
                    ?>
</a>
						<?php 
                } else {
                    echo esc_html($model->getUserName());
                }
                ?>
						
						<div <?php 
                echo $sum ? 'class="row-actions"' : 'style="visibility: hidden;"';
                ?>
>
							<span>
								<a style="color: red;" class="wpProQuiz_delete" href="#"><?php 
                _e('Delete', 'wp-pro-quiz');
                ?>
</a>
							</span>
						</div>
						
					</th>
					<th><?php 
                echo $points;
                ?>
</th>
					<th style="color: green;"><?php 
                echo $correct;
                ?>
</th>
					<th style="color: red;"><?php 
                echo $incorrect;
                ?>
</th>
					<th><?php 
                echo $hintCount;
                ?>
</th>
					<th><?php 
                echo $time;
                ?>
</th>
					<th style="font-weight: bold;"><?php 
                echo $result;
                ?>
</th>
				</tr>
				<?php 
            }
        }
        ?>
			</tbody>
		</table>
		
	<?php 
    }
 public function setQuestionAnswerData($_questionAnswerData)
 {
     $this->_questionAnswerData = null;
     if (WpProQuiz_Helper_Until::saveUnserialize($_questionAnswerData, $into) !== false) {
         $this->_questionAnswerData = $into;
     }
     return $this;
 }
 /**
  * @param bool|false $serialize
  * @return WpProQuiz_Model_AnswerTypes[]|null|string
  */
 public function getAnswerData($serialize = false)
 {
     if ($this->_answerData === null) {
         return null;
     }
     if (is_array($this->_answerData) || $this->_answerData instanceof WpProQuiz_Model_AnswerTypes) {
         if ($serialize) {
             return @serialize($this->_answerData);
         }
     } else {
         if (!$serialize) {
             if (WpProQuiz_Helper_Until::saveUnserialize($this->_answerData, $into) === false) {
                 return null;
             }
             $this->_answerData = $into;
         }
     }
     return $this->_answerData;
 }