diff --git a/plugin/whispeakauth/Entity/LogEvent.php b/plugin/whispeakauth/Entity/LogEvent.php index 0890cb9adb..4047a2d759 100644 --- a/plugin/whispeakauth/Entity/LogEvent.php +++ b/plugin/whispeakauth/Entity/LogEvent.php @@ -24,7 +24,6 @@ use Doctrine\ORM\Mapping as ORM; */ class LogEvent { - const STATUS_REQUIRED = -1; const STATUS_FAILED = 0; const STATUS_SUCCESS = 1; diff --git a/plugin/whispeakauth/WhispeakAuthPlugin.php b/plugin/whispeakauth/WhispeakAuthPlugin.php index 78b8a7291d..71a6c1cd1b 100644 --- a/plugin/whispeakauth/WhispeakAuthPlugin.php +++ b/plugin/whispeakauth/WhispeakAuthPlugin.php @@ -692,43 +692,7 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface } /** - * @param int $userId - * @param int $lpItemId - * @param int $lpId - * - * @throws \Doctrine\ORM\NonUniqueResultException - * - * @return LogEventLp|null - */ - public function getLastRequiredAttemptInLearningPath($userId, $lpItemId, $lpId) - { - $query = Database::getManager() - ->createQuery( - 'SELECT log FROM ChamiloPluginBundle:WhispeakAuth\LogEventLp log - WHERE - log.user = :user AND - log.lp = :lp AND - log.lpItem = :lp_item AND - log.actionStatus = :action_status - ORDER BY log.datetime DESC' - ) - ->setMaxResults(1) - ->setParameters( - [ - 'user' => $userId, - 'lp' => $lpId, - 'lp_item' => $lpItemId, - 'action_status' => LogEvent::STATUS_REQUIRED, - ] - ); - - /** @var LogEventLp|null $logEvent */ - $logEvent = $query->getOneOrNullResult(); - - return $logEvent; - } - - /** + * @param int $status * @param int $userId * @param int $lpItemId * @param int $lpId @@ -739,7 +703,7 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface * * @return LogEventLp|null */ - public function addAttemptInLearningPath($userId, $lpItemId, $lpId) + public function addAttemptInLearningPath($status, $userId, $lpItemId, $lpId) { $em = Database::getManager(); @@ -759,7 +723,7 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface ->setDatetime( api_get_utc_datetime(null, false, true) ) - ->setActionStatus($logEvent::STATUS_REQUIRED); + ->setActionStatus($status); $em->persist($logEvent); $em->flush(); @@ -769,73 +733,6 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface /** * @param int $status - * @param int $userId - * @param int $lpItemId - * @param int $lpId - * - * @throws \Doctrine\ORM\NonUniqueResultException - * @throws \Doctrine\ORM\OptimisticLockException - * - * @return LogEventLp|null - */ - public function updateAttemptInLearningPath($status, $userId, $lpItemId, $lpId) - { - $em = Database::getManager(); - - $logEvent = $this->getLastRequiredAttemptInLearningPath($userId, $lpItemId, $lpId); - - if (empty($logEvent)) { - return null; - } - - if ($logEvent->getActionStatus() !== $status) { - $logEvent->setActionStatus($status); - - $em->persist($logEvent); - $em->flush(); - } - - return $logEvent; - } - - /** - * @param int $userId - * @param int $questionId - * @param int $quizId - * - * @throws \Doctrine\ORM\NonUniqueResultException - * - * @return LogEventQuiz|null - */ - public function getLastRequiredAttemptInQuiz($userId, $questionId, $quizId) - { - $query = Database::getManager() - ->createQuery( - 'SELECT log FROM ChamiloPluginBundle:WhispeakAuth\LogEventQuiz log - WHERE - log.user = :user AND - log.quiz = :quiz AND - log.question = :question AND - log.actionStatus = :action_status - ORDER BY log.datetime DESC' - ) - ->setMaxResults(1) - ->setParameters( - [ - 'user' => $userId, - 'quiz' => $quizId, - 'question' => $questionId, - 'action_status' => LogEvent::STATUS_REQUIRED, - ] - ); - - /** @var LogEventQuiz|null $logEvent */ - $logEvent = $query->getOneOrNullResult(); - - return $logEvent; - } - - /** * @param int $userId * @param int $questionId * @param int $quizId @@ -846,7 +743,7 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface * * @return LogEventQuiz|null */ - public function addAttemptInQuiz($userId, $questionId, $quizId) + public function addAttemptInQuiz($status, $userId, $questionId, $quizId) { $em = Database::getManager(); @@ -866,7 +763,7 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface ->setDatetime( api_get_utc_datetime(null, false, true) ) - ->setActionStatus($logEvent::STATUS_REQUIRED); + ->setActionStatus($status); $em->persist($logEvent); $em->flush(); @@ -874,36 +771,6 @@ class WhispeakAuthPlugin extends Plugin implements HookPluginInterface return $logEvent; } - /** - * @param int $status - * @param int $userId - * @param int $questionId - * @param int $quizId - * - * @throws \Doctrine\ORM\NonUniqueResultException - * @throws \Doctrine\ORM\OptimisticLockException - * - * @return LogEventQuiz|null - */ - public function updateAttemptInQuiz($status, $userId, $questionId, $quizId) - { - $logEvent = $this->getLastRequiredAttemptInQuiz($userId, $questionId, $quizId); - - if (empty($logEvent)) { - return null; - } - - if ($logEvent->getActionStatus() !== $status) { - $logEvent->setActionStatus($status); - - $em = Database::getManager(); - $em->persist($logEvent); - $em->flush(); - } - - return $logEvent; - } - /** * @param int $lpId * @param int $userId diff --git a/plugin/whispeakauth/ajax/authentify_password.php b/plugin/whispeakauth/ajax/authentify_password.php index 6aeedd7196..de4ad04006 100644 --- a/plugin/whispeakauth/ajax/authentify_password.php +++ b/plugin/whispeakauth/ajax/authentify_password.php @@ -51,6 +51,22 @@ $isExpired = empty($user->getExpirationDate()) || $user->getExpirationDate() > a $userPass = true; if (!$isValidPassword || !$isActive || !$isExpired) { + if (!empty($lpItemInfo)) { + $plugin->addAttemptInLearningPath( + LogEvent::STATUS_FAILED, + $user->getId(), + $lpItemInfo['lp_item'], + $lpItemInfo['lp'] + ); + } elseif (!empty($quizQuestionInfo)) { + $plugin->addAttemptInQuiz( + LogEvent::STATUS_FAILED, + $user->getId(), + $quizQuestionInfo['question'], + $quizQuestionInfo['quiz'] + ); + } + $userPass = false; $message = $plugin->get_lang('AuthentifyFailed'); @@ -76,34 +92,18 @@ if (!$isValidPassword || !$isActive || !$isExpired) { if (!$maxAttempts || ($maxAttempts && $failedLogins >= $maxAttempts) ) { - if (!empty($lpItemInfo)) { - $plugin->updateAttemptInLearningPath( - LogEvent::STATUS_FAILED, - $user->getId(), - $lpItemInfo['lp_item'], - $lpItemInfo['lp'] - ); - } elseif (!empty($quizQuestionInfo)) { - $plugin->updateAttemptInQuiz( - LogEvent::STATUS_FAILED, - $user->getId(), - $quizQuestionInfo['question'], - $quizQuestionInfo['quiz'] - ); - } - $userPass = true; } } elseif ($isValidPassword) { if (!empty($lpItemInfo)) { - $plugin->updateAttemptInLearningPath( + $plugin->addAttemptInLearningPath( LogEvent::STATUS_SUCCESS, $user->getId(), $lpItemInfo['lp_item'], $lpItemInfo['lp'] ); } elseif (!empty($quizQuestionInfo)) { - $plugin->updateAttemptInQuiz( + $plugin->addAttemptInQuiz( LogEvent::STATUS_SUCCESS, $user->getId(), $quizQuestionInfo['question'], diff --git a/plugin/whispeakauth/ajax/record_audio.php b/plugin/whispeakauth/ajax/record_audio.php index 88bcb2b246..9b594bd711 100644 --- a/plugin/whispeakauth/ajax/record_audio.php +++ b/plugin/whispeakauth/ajax/record_audio.php @@ -174,6 +174,24 @@ if ($isAuthentify) { $message = $plugin->get_lang('AuthentifySuccess'); if (!$success) { + if (!empty($lpItemInfo)) { + $plugin->addAttemptInLearningPath( + LogEvent::STATUS_FAILED, + $user->getId(), + $lpItemInfo['lp_item'], + $lpItemInfo['lp'] + ); + } + + if (!empty($quizQuestionInfo)) { + $plugin->addAttemptInQuiz( + LogEvent::STATUS_FAILED, + $user->getId(), + $quizQuestionInfo['question'], + $quizQuestionInfo['quiz'] + ); + } + $message = $plugin->get_lang('AuthentifyFailed'); ChamiloSession::write(WhispeakAuthPlugin::SESSION_FAILED_LOGINS, ++$failedLogins); @@ -249,7 +267,7 @@ if ($isAuthentify) { ChamiloSession::erase(WhispeakAuthPlugin::SESSION_LP_ITEM); ChamiloSession::erase(WhispeakAuthPlugin::SESSION_2FA_USER); - $plugin->updateAttemptInLearningPath( + $plugin->addAttemptInLearningPath( LogEvent::STATUS_SUCCESS, $user->getId(), $lpItemInfo['lp_item'], @@ -267,7 +285,7 @@ if ($isAuthentify) { ChamiloSession::write(WhispeakAuthPlugin::SESSION_QUIZ_QUESTION, $quizQuestionInfo); - $plugin->updateAttemptInQuiz( + $plugin->addAttemptInQuiz( LogEvent::STATUS_SUCCESS, $user->getId(), $quizQuestionInfo['question'], diff --git a/plugin/whispeakauth/authentify.php b/plugin/whispeakauth/authentify.php index 095a041061..28a839b491 100644 --- a/plugin/whispeakauth/authentify.php +++ b/plugin/whispeakauth/authentify.php @@ -44,20 +44,6 @@ if (ChamiloSession::read(WhispeakAuthPlugin::SESSION_AUTH_PASSWORD, false)) { exit; } -if (!empty($lpItemInfo)) { - $plugin->addAttemptInLearningPath( - $userId, - $lpItemInfo['lp_item'], - $lpItemInfo['lp'] - ); -} elseif (!empty($lpQuestionInfo)) { - $plugin->addAttemptInQuiz( - $userId, - $lpQuestionInfo['question'], - $lpQuestionInfo['quiz'] - ); -} - if ($userId) { $wsid = WhispeakAuthPlugin::getAuthUidValue($userId);