isLoggedIn() && ($languageId = (int)$auth->getData('languageId')) && array_key_exists($languageId, LanguageModel::get4Select()) ) { self::$languageId = $languageId; } } return self::$languageId; } public static function setLanguageId($languageId) { if (empty($languageId)) { self::$languageId = ($languageId === false) ? $languageId : null; } elseif (array_key_exists($languageId, LanguageModel::get4Select())) { self::$languageId = (int) $languageId; } else { self::$languageId = LanguageModel::LANG_DEFAULT; } } public static function getLangFilter($alias) { if (($languageId = self::getLanguageId())) { return ["`{$alias}`.`languageId` = ?" => $languageId]; } return null; } public static function getStartPage($userId) { if (empty($userId)) { return self::SHOW_HOME; } $testCount = Model::getTestCount(); if ($testCount == 0 || $testCount > 1) { return self::SHOW_LIST; } else { $testId = Model::getFirstTestId(); $attempt = Model::getLastAttempt($testId, $userId); if ($attempt) { if ($attempt['success'] === 'y') { return self::SHOW_HOME; } else if ((int) Model::getAttemptCount($testId, $userId) >= 2) { return self::SHOW_HOME; } else { return $testId; } } else { return $testId; } } } public static function getContinuePage($userId, $testId) { $result = [ 'target' => self::SHOW_HOME, 'showTest' => null, 'showList' => false, ]; if (empty($userId) || empty($testId)) { return $result; } $attempt = Model::getLastAttempt($testId, $userId, ['success']); $testCount = Model::getTestCount(); $result['showList'] = $testCount == 0 || $testCount > 1; if ($attempt) { if ($attempt['success'] === 'y') { $result['target'] = $result['showList'] ? self::SHOW_LIST : self::SHOW_HOME; } else if ((int) Model::getAttemptCount($testId, $userId) >= 2) { $result['target'] = $result['showList'] ? self::SHOW_LIST : self::SHOW_HOME; } else { $result['target'] = $result['showList'] ? self::SHOW_LIST : $testId; $result['showTest'] = $testId; } } else { $result['target'] = $testId; $result['showTest'] = $testId; } return $result; } public static function getHasAnotherTry($userId, $testId) { $attempt = Model::getLastAttempt($testId, $userId, ['success']); if (empty($attempt)) { return true; } elseif ($attempt['success'] === 'n') { $attemptCount = Model::getAttemptCount($testId, $userId); return $attemptCount < 2; } else { return false; } } /** * @param bool $attemptSuccess * @param int $attemptCount * * @return bool */ public static function checkHasAttempt($attemptSuccess, $attemptCount) { if (empty($attemptCount)) { return true; } elseif (!$attemptSuccess) { return (int) $attemptCount < 2; } else { return false; } } public static function getAttemptCountSelect($testId, $userId) { if (null == $testId || null == $userId) { return null; } $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Result'), []); $select->join(Qs_Db::getPair('ResultAttempt'), 'ResultAttempt.resultId = Result.id', ['count' => new Zend_Db_Expr('COUNT(*)')]); $select->where('Result.testId = ' . Qs_Db::quoteIdField($testId)); $select->where('Result.userId = ?', $userId, Qs_Db::INT_TYPE); $select->limit(1); return $select; } public static function getAttemptCount($testId, $userId) { if (($select = self::getAttemptCountSelect($testId, $userId))) { return Qs_Db::getInstance()->fetchOne($select); } return null; } public static function getLastAttemptSelect($testId, $userId, array $columns = ['*']) { if (null == $testId || null == $userId) { return null; } $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Result'), []); $select->join(Qs_Db::getPair('ResultAttempt'), 'ResultAttempt.resultId = Result.id', $columns); $select->where('Result.testId = ' . Qs_Db::quoteIdField($testId)); $select->where('Result.userId = ?', $userId); $select->limit(1); return $select; } public static function getLastAttempt($testId, $userId, array $columns = ['*']) { if (($select = self::getLastAttemptSelect($testId, $userId, $columns))) { return Qs_Db::getInstance()->fetchRow($select); } return null; } public static function getTestSlideExists($testId) { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('TestSlide'), ['found' => new Zend_Db_Expr('1')]); $select->where('TestSlide.testId = ' . Qs_Db::quoteIdField($testId)); $select->where('TestSlide.enabled = "y"'); $select->limit(1); return new Zend_Db_Expr('EXISTS (' . $select . ')'); } public static function getTestCount() { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Test'), ['count' => new Zend_Db_Expr('COUNT(*)')]); $select->where('Test.enabled = "y"'); $select->where(self::getTestSlideExists('Test.id')); if (($langFilter = self::getLangFilter('Test'))) { $select->where(Qs_Db::getWhereSql($langFilter)); } $select->limit(1); return Qs_Db::getInstance()->fetchOne($select); } public static function getFirstTestId() { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Test'), ['id']); $select->where('Test.enabled = "y"'); $select->where(self::getTestSlideExists('Test.id')); if (($langFilter = self::getLangFilter('Test'))) { $select->where(Qs_Db::getWhereSql($langFilter)); } $select->order('Test.id'); $select->limit(1); return Qs_Db::getInstance()->fetchOne($select); } public static function getDurationText($seconds) { $seconds = (int) $seconds; $periods = [ 'day' => 86400, 'hour' => 3600, 'minute' => 60, 'second' => 1, ]; $result = ''; foreach ($periods as $name => $value) { if ($seconds >= $value) { $tmp = floor($seconds / $value); $seconds -= $value * $tmp; $result .= ($result ? ' ' : '') . $tmp . ' ' . Qs_Translate::getPlural($name, $tmp); } } return $result; } public static function getTestFilter(Entity $entity, $alias = 'Test') { $result = []; $result["`{$alias}`.`id` = ?"] = $entity->getTestId(); if ($entity->getMode() === static::MODE_TRAINING) { $result["`{$alias}`.`enabled` = ?"] = 'y'; } if (($langFilter = self::getLangFilter($alias))) { $result += $langFilter; } $result[] = self::getTestSlideExists('Test.id'); return $result; } public static function getTestSlideFilter(Entity $entity, $filter = [], $alias = 'TestSlide') { $result = []; if (!array_key_exists('testId', $filter) && $entity->getTestId()) { $filter['testId'] = $entity->getTestId(); } if (!array_key_exists('id', $filter) && $entity->getSlideId()) { $filter['id'] = $entity->getSlideId(); } if (!array_key_exists('enabled', $filter) && ( $entity->getMode() === static::MODE_TRAINING || ($entity->getMode() === static::MODE_ADMIN && !$entity->getSlideId()) ) ) { $filter['enabled'] = 'y'; } foreach ($filter as $name => $value) { $result["`{$alias}`.`{$name}` = ?"] = $value; } return $result; } }