Орла
Области
Компаний
Пользователи
Галерея
Фиксированные новости
Орла
Отзывы о фирмах
Тарифы
Пользователи
Области
Банкоматы
Торговые точки
Доска объявлений
Курсы валют
Скидки
организации Орла
организации области
товары
сайты
сайты Орел
Данные
Разделы
Поиск
Поиск запросы
Статистика
Показы каталога организаций
Статистика клиенты
Пользователи
Сайты
Управление сайтом
Анкета
Аптеки
Афиша
Афиша новая
Баннеры
Бизнес >
Викторина
Голосование
Гость на сайте
Истории
Календарь событий
Каталоги >
Конкурс красоты
Конкурс аудио
Консультации
Меню
Новое на сайте
Новости >
Организации >
Программа ТВ
Публикации
Разделы сайта
Рассылка
Рестораны
Секрет фирмы >
Сервисы >
Ссылки
Тесты
Файловый менеджер
Форум
Фотогалерея
Обратная связь
Недвижимость
Новый год
Тексты
Пользователи
Юридический рейтинг
Отзывы на товары
Бан пользователей
Жалобы
Новые тесты
Коронавирус
Файловый менеджер
Имя файла
Test.php
Содержимое
<?php namespace app\tests; use App; use app\core\helpers\ArrayHelper; use app\core\models\QueryBuilder; class Test { private $_testId; private $_userId = null; private $_userToken; private $_test = []; public function __construct($testId) { $this->_testId = $testId; if (App::$user->isGuest() == false) { $this->_userId = App::$user->getId(); } $this->_userToken = App::$cookies->get('user_token'); $this->_test = App::$command ->createCommand(' SELECT * FROM `tests` WHERE id = :id AND status = 1 ') ->bindValue(':id', $this->_testId) ->queryOne(); return $this; } public function isAllowForUser() { if (App::$user->isGuest() == true) { if ($this->_userToken == null) { return false; } return true; } else { return true; } } public function isTestExists() { return isset($this->_test['id']); } public function isFinishPage() { $question = $this->getActiveQuestion(); if (isset($question['id'])) { return false; } return true; } public function getTotalBalls() { if ($this->_userId > 0) { return App::$command ->createCommand(' SELECT SUM(variants.ball) as total_balls FROM tests_question_answer_results result LEFT JOIN tests_question_answer_variants variants ON variants.id = result.answer_id WHERE result.user_id = :user_id AND result.test_id = :test_id; ') ->bindValue(':user_id', $this->_userId) ->bindValue(':test_id', $this->_testId) ->queryOne('total_balls', 0); } else { return App::$command ->createCommand(' SELECT SUM(variants.ball) as total_balls FROM tests_question_answer_results result LEFT JOIN tests_question_answer_variants variants ON variants.id = result.answer_id WHERE result.user_token = :user_token AND result.test_id = :test_id; ') ->bindValue(':user_token', $this->_userToken) ->bindValue(':test_id', $this->_testId) ->queryOne('total_balls', 0); } } public function getQuestionResult() { $balls = (int) $this->getTotalBalls(); $items = App::$command ->createCommand(' SELECT * FROM tests_question_finish_results WHERE test_id = :test_id ORDER BY ball DESC ') ->bindValue(':test_id', $this->_testId) ->queryAll(); foreach ($items as $item) { if ($item['sign'] == '>=' && $balls >= $item['ball']) { return $item; } elseif ($item['sign'] == '<=' && $balls <= $item['ball']) { return $item; } elseif ($item['sign'] == '=' && $balls == $item['ball']) { return $item; } } return null; } public function isShowResult() { $item = App::$command ->createCommand(' SELECT is_show_result FROM tests WHERE id = :test_id ') ->bindValue(':test_id', $this->_testId) ->queryOne(); if ($item['is_show_result'] == 1) { return true; } else { return false; } return false; } public function getFinishTemplate() { $result = $this->getQuestionResult(); if ($result == null) { return ''; } if ($this->isShowResult()) { $precent = App::$command ->createCommand(' SELECT COUNT(tests_question_answer_results.answer_id) AS count, tests_question_answer_results.answer_id, tests_question_answer_variants.title as title FROM tests_question_answer_results LEFT JOIN tests_question_answer_variants ON tests_question_answer_results.answer_id = tests_question_answer_variants.id WHERE test_id = :test_id GROUP BY answer_id ') ->bindValue(':test_id', $this->_testId) ->queryAll(); $allCount = App::$command ->createCommand(' SELECT COUNT(answer_id) AS count FROM tests_question_answer_results WHERE test_id = :test_id ') ->bindValue(':test_id', $this->_testId) ->queryOne(); return App::$view->render('tests/result_precent', [ 'result' => $result, 'precents' => $precent, 'allCount' => $allCount['count'] ], true); } else { return App::$view->render('tests/result', [ 'result' => $result ], true); } } public function isStartPage() { if ($this->_userId != null) { $data = App::$command ->createCommand(' SELECT COUNT(*) as total FROM `tests_question_answer_results` WHERE user_id = :user_id AND test_id = :test_id ') ->bindValue(':user_id', $this->_userId) ->bindValue(':test_id', $this->_testId) ->queryOne(); } else { $data = App::$command ->createCommand(' SELECT COUNT(*) as total FROM `tests_question_answer_results` WHERE user_token = :user_token AND test_id = :test_id ') ->bindValue(':user_token', $this->_userToken) ->bindValue(':test_id', $this->_testId) ->queryOne(); } return $data['total'] == 0; } public function getStartPageTemplate() { return App::$view ->render('tests/start_page', [ 'test' => $this->_test ], true); } public function getActiveQuestionTemplate() { $question = $this->getActiveQuestion(); if (isset($question['id']) == false) { return false; } $questionsIds = $this->getQuestionsIds(); $totalQuestions = count($questionsIds); $activeQuestionIndex = 0; foreach ($questionsIds as $questionsId) { $activeQuestionIndex = $activeQuestionIndex + 1; if ($questionsId == $question['id']) { break; } } return App::$view ->render('tests/view_question', [ 'testInstance' => $this, 'test' => $this->_test, 'activeQuestionIndex' => $activeQuestionIndex, 'questionIds' => $questionsIds, 'totalQuestions' => $totalQuestions, 'question' => $question, 'answers' => $this->getAnswers($question['id']) ]); } public function getQuestionsIds() { return ArrayHelper::arrayColumn(App::$command ->createCommand(' SELECT id FROM tests_questions WHERE test_id = :test_id ') ->bindValue(':test_id', $this->_testId) ->queryAll(), 'id'); } public function getAnsweredQuestionIds() { if ($this->_userId > 0) { $data = App::$command ->createCommand(' SELECT question_id FROM tests_question_answer_results WHERE user_id = :user_id AND test_id = :test_id ') ->bindValue(':user_id', $this->_userId) ->bindValue(':test_id', $this->_testId) ->queryAll(); return ArrayHelper::arrayColumn($data, 'question_id'); } else { $data = App::$command ->createCommand(' SELECT question_id FROM tests_question_answer_results WHERE user_token = :user_token AND test_id = :test_id ') ->bindValue(':test_id', $this->_testId) ->bindValue(':user_token', $this->_userToken) ->queryAll(); return ArrayHelper::arrayColumn($data, 'question_id'); } } public function getActiveQuestion() { $answeredQuestionIds = $this->getAnsweredQuestionIds(); if (count($answeredQuestionIds) > 0) { return App::$command ->createCommand(' SELECT * FROM tests_questions WHERE test_id = :test_id AND id not in ('.implode(',', $answeredQuestionIds).') ORDER BY id ASC LIMIT 1; ') ->bindValue(':test_id', $this->_testId) ->queryOne(); } else { return App::$command ->createCommand(' SELECT * FROM tests_questions WHERE test_id = :test_id ORDER BY id ASC LIMIT 1; ') ->bindValue(':test_id', $this->_testId) ->queryOne(); } } public function getResultsAnswers() { $question = $this->getActiveQuestion(); if (isset($question['id']) == false) { return false; } $precent = App::$command ->createCommand(' SELECT COUNT(answer_id) AS count, answer_id FROM tests_question_answer_results WHERE test_id = :test_id GROUP BY answer_id ') ->bindValue(':test_id', $this->_testId) ->queryAll(); $questionsIds = $this->getQuestionsIds(); $totalQuestions = count($questionsIds); $activeQuestionIndex = 0; foreach ($questionsIds as $questionsId) { $activeQuestionIndex = $activeQuestionIndex + 1; if ($questionsId == $question['id']) { break; } } $allCount = App::$command ->createCommand(' SELECT COUNT(answer_id) AS count FROM tests_question_answer_results WHERE test_id = :test_id ') ->bindValue(':test_id', $this->_testId) ->queryOne(); return App::$view ->render('tests/resOpr', [ 'testInstance' => $this, 'test' => $this->_test, 'activeQuestionIndex' => $activeQuestionIndex, 'precent' => $precent, 'count' => $allCount, 'questionIds' => $questionsIds, 'totalQuestions' => $totalQuestions, 'question' => $question, 'answers' => $this->getAnswers($question['id']) ]); } public function getAnswers($questionId) { return App::$command ->createCommand(' SELECT * FROM tests_question_answer_variants WHERE test_question_id = :id ORDER BY id ASC ') ->bindValue(':id', $questionId) ->queryAll(); } public function isAnswered($questionId, $answerId) { $fragmentSql = ($this->_userId == null) ? 'AND user_token = :user_token' : 'AND user_id = :user_id;'; $command = App::$command ->createCommand(' SELECT COUNT(*) as total FROM `tests_question_answer_results` WHERE question_id = :question_id AND answer_id = :answer_id ' . $fragmentSql . ' '); $command->bindValue(':question_id', $questionId); $command->bindValue(':answer_id', $answerId); ($this->_userId != null) ? $command->bindValue(':user_id', $this->_userId) : $command->bindValue(':user_token', $this->_userToken); return $command->queryOne('total') > 0; } public function isExistQuestion($questionId) { return App::$command ->createCommand(' SELECT COUNT(*) as total FROM tests_questions WHERE id = :id ') ->bindValue(':id', $questionId) ->queryOne('total', 0) > 0; } public function isExistAnswer($answerId) { return App::$command ->createCommand(' SELECT COUNT(*) as total FROM tests_question_answer_variants WHERE id = :id ') ->bindValue(':id', $answerId) ->queryOne('total', 0) > 0; } public function setAnswer($questionId, $answerId) { if ($this->isExistQuestion($questionId) == false) { return false; } if ($this->isExistAnswer($answerId) == false) { return false; } if ($this->isAnswered($questionId, $answerId)) { return false; } $command = App::$command ->createCommand(' INSERT INTO `tests_question_answer_results` (created_at, test_id, user_id, user_token, question_id, answer_id) VALUES(NOW(), :test_id, :user_id, :user_token, :question_id, :answer_id) '); $command ->bindValues([ ':test_id' => $this->_testId, ':user_id' => $this->_userId, ':user_token' => $this->_userToken, ':question_id' => $questionId, ':answer_id' => $answerId ]); return $command->query(); } public function getAnswer($answerId) { return App::$command ->createCommand(' SELECT * FROM `tests_question_answer_variants` WHERE id = :id ') ->bindValue(':id', $answerId) ->queryOne(); } /** * Функция проверяет, правильно ли ответил пользователь на вопрос или нет. * @param $questionId * @return bool */ public function isUserRightQuestion($questionId) { $questionResult = App::$command ->createCommand(' SELECT answer_id FROM `tests_question_answer_results` WHERE user_token = :user_token AND question_id = :question_id ') ->bindValue(':question_id', $questionId) ->bindValue(':user_token', $this->_userToken) ->queryOne(); if (isset($questionResult['answer_id']) == false) { return false; } return $this->isRightAnswer($questionResult['answer_id']); } public function isRightAnswer($answerId) { $answer = $this->getAnswer($answerId); if (isset($answer['id']) == false) { return false; } return $answer['ball'] > 0; } }
/app/tests/
Имя
Дата
Размер
..
2026-10-01
Test.php
2021-07-28
15 кб.
Test.php~
2020-02-26
15 кб.
/app/tests
Загрузить файлы
Файл 1
Файл 2
Файл 3