From 9d9d2c1a185933743daed19b5097d72c4b3e574f Mon Sep 17 00:00:00 2001 From: Mauro Cassani Date: Thu, 27 Feb 2025 17:42:09 +0100 Subject: [PATCH] Fix "Mark as complete" behaviour --- lib/Model/Chunks/ChunkCompletionEventDao.php | 12 ++++-- .../Translations/SegmentTranslationDao.php | 8 +++- .../Decorator/CatDecorator.php | 41 ++++++++++++++++++- .../Model/TranslationEventDao.php | 28 +++++++++++++ lib/Utils/CatUtils.php | 33 +++++++++++++++ .../sass/components/MarkAsCompleteButton.scss | 8 ++++ 6 files changed, 123 insertions(+), 7 deletions(-) diff --git a/lib/Model/Chunks/ChunkCompletionEventDao.php b/lib/Model/Chunks/ChunkCompletionEventDao.php index eb2facf98f..ba9390bfe1 100644 --- a/lib/Model/Chunks/ChunkCompletionEventDao.php +++ b/lib/Model/Chunks/ChunkCompletionEventDao.php @@ -90,16 +90,22 @@ public static function createFromChunk( $chunk, CompletionEventStruct $params ) return $conn->lastInsertId(); } - + /** + * @param Jobs_JobStruct $chunk + * @return string + * @throws Exception + */ public function currentPhase( Jobs_JobStruct $chunk ) { $lastTranslate = $this->lastCompletionRecord( $chunk, [ 'is_review' => false ] ); + if ( $lastTranslate ) { $lastRevise = $this->lastCompletionRecord( $chunk, [ 'is_review' => true ] ); + if ( $lastRevise && new DateTime( $lastTranslate[ 'create_date' ] ) < new DateTime( $lastRevise[ 'create_date' ] ) ) { return self::TRANSLATE; - } else { - return self::REVISE; } + + return self::REVISE; } return self::TRANSLATE; diff --git a/lib/Model/Translations/SegmentTranslationDao.php b/lib/Model/Translations/SegmentTranslationDao.php index 3849990e09..40abba62ab 100644 --- a/lib/Model/Translations/SegmentTranslationDao.php +++ b/lib/Model/Translations/SegmentTranslationDao.php @@ -806,8 +806,7 @@ static function rebuildFromReplaceEvents( $events ) { * @param $id_segment * @param $suggestions */ - public - static function updateSuggestionsArray( $id_segment, $suggestions ) { + public static function updateSuggestionsArray( $id_segment, $suggestions ) { if ( empty( $suggestions ) ) { return; @@ -826,4 +825,9 @@ static function updateSuggestionsArray( $id_segment, $suggestions ) { $stmt->execute( $params ); } + + public static function getLastModifiedSegmentEvent(int $id_job, int $ttl = 0) + { + + } } diff --git a/lib/Plugins/Features/ProjectCompletion/Decorator/CatDecorator.php b/lib/Plugins/Features/ProjectCompletion/Decorator/CatDecorator.php index abb9d5494f..4376c08ed0 100644 --- a/lib/Plugins/Features/ProjectCompletion/Decorator/CatDecorator.php +++ b/lib/Plugins/Features/ProjectCompletion/Decorator/CatDecorator.php @@ -4,7 +4,10 @@ use AbstractDecorator; use catController; +use CatUtils; use Chunks_ChunkCompletionEventDao; +use Constants_TranslationStatus; +use Features\TranslationEvents\Model\TranslationEventDao; use Projects_MetadataDao; class CatDecorator extends AbstractDecorator { @@ -73,10 +76,19 @@ private function varsForUncomplete() { private function varsForComplete() { $this->template->job_marked_complete = true; $this->template->header_main_button_class = 'isMarkedComplete'; - $this->template->header_main_button_enabled = false; - $this->template->mark_as_complete_button_enabled = false; + + if($this->markAsCompleteIsEnabled()){ + $this->template->header_main_button_enabled = true; + $this->template->mark_as_complete_button_enabled = true; + } else { + $this->template->header_main_button_enabled = false; + $this->template->mark_as_complete_button_enabled = false; + } } + /** + * @return bool + */ private function completable() { if ( $this->controller->getChunk()->getProject()->getWordCountType() != Projects_MetadataDao::WORD_COUNT_RAW ) { @@ -108,4 +120,29 @@ private function completable() { return $completable; } + /** + * @return bool + */ + private function markAsCompleteIsEnabled() + { + $revisionNumber = CatUtils::getRevisionNumberFromRequestUri(); + $lastEvent = (new TranslationEventDao())->getLatestEventForAJob($this->controller->getChunk()->id); + + if(empty($lastEvent)){ + return false; + } + + switch($lastEvent->status){ + case Constants_TranslationStatus::STATUS_TRANSLATED: + return true; + + case Constants_TranslationStatus::STATUS_APPROVED: + return $revisionNumber === 1 or $revisionNumber === 2; + + case Constants_TranslationStatus::STATUS_APPROVED2: + return $revisionNumber === 2; + } + + return false; + } } diff --git a/lib/Plugins/Features/TranslationEvents/Model/TranslationEventDao.php b/lib/Plugins/Features/TranslationEvents/Model/TranslationEventDao.php index af9379b9bd..59fb5338f4 100644 --- a/lib/Plugins/Features/TranslationEvents/Model/TranslationEventDao.php +++ b/lib/Plugins/Features/TranslationEvents/Model/TranslationEventDao.php @@ -139,6 +139,34 @@ public function getLatestEventForSegment( $id_job, $id_segment ): ?TranslationEv } + /** + * @param $id_job + * + * @return TranslationEventStruct|null + */ + public function getLatestEventForAJob( $id_job ): ?TranslationEventStruct { + + $sql = "SELECT * FROM segment_translation_events + WHERE id_job = :id_job + AND status != :draft + ORDER BY id DESC + LIMIT 1 + "; + + $conn = $this->getDatabaseHandler()->getConnection(); + $stmt = $conn->prepare( $sql ); + $stmt->setFetchMode( PDO::FETCH_CLASS, TranslationEventStruct::class ); + $stmt->execute( [ + 'id_job' => $id_job, + 'draft' => Constants_TranslationStatus::STATUS_DRAFT + ] ); + + $res = $stmt->fetchAll(); + + return $res[ 0 ] ?? null; + + } + /** * @param array $id_segment_list * @param int $id_job diff --git a/lib/Utils/CatUtils.php b/lib/Utils/CatUtils.php index a49451b4dc..728c51b3b7 100644 --- a/lib/Utils/CatUtils.php +++ b/lib/Utils/CatUtils.php @@ -744,6 +744,39 @@ private static function isARevisePath( string $path ): bool { return strpos( $path, "/revise" ) === 0; } + /** + * @return int + */ + public static function getRevisionNumberFromRequestUri(): int + { + + if ( !isset( $_SERVER[ 'REQUEST_URI' ] ) ) { + return 0; + } + + $_from_url = explode("/", @$_SERVER[ 'REQUEST_URI' ] ); + + if(empty($_from_url)){ + return 0; + } + + if(!isset($_from_url[1])){ + return 0; + } + + if($_from_url[1] === "revise"){ + return 1; + } + + if($_from_url[1] === "revise2"){ + return 2; + } + + return 0; + } + + + /** * Get a job from a combination of ID and ANY password (t,r1 or r2) * diff --git a/public/css/sass/components/MarkAsCompleteButton.scss b/public/css/sass/components/MarkAsCompleteButton.scss index e7a693775a..c82d384124 100644 --- a/public/css/sass/components/MarkAsCompleteButton.scss +++ b/public/css/sass/components/MarkAsCompleteButton.scss @@ -18,6 +18,14 @@ input#markAsCompleteButton[disabled] { background: transparent; border: none; + &:disabled { + cursor: not-allowed; + + &:hover { + opacity: 0.8; + } + } + &.isMarkedComplete { background-image: url('/public/img/icons/icon-mark-active.svg'); background-size: cover;