From 91a52187386623b9c881e2671da005fb34cf3c0f Mon Sep 17 00:00:00 2001 From: Stephan Kergomard Date: Sun, 15 Dec 2024 10:46:16 +0100 Subject: [PATCH] Test: Add certificate indicator/link See: https://mantis.ilias.de/view.php?id=37056 --- .../Test/classes/class.ilObjTestAccess.php | 39 ++++++++++++++ .../Test/classes/class.ilObjTestListGUI.php | 51 ++++++++++++++++--- .../ScoreSettingsDatabaseRepository.php | 50 +++++++++++++----- .../ScoreSettingsRepository.php | 1 + 4 files changed, 122 insertions(+), 19 deletions(-) diff --git a/components/ILIAS/Test/classes/class.ilObjTestAccess.php b/components/ILIAS/Test/classes/class.ilObjTestAccess.php index db2f13047918..a159c2e844d1 100755 --- a/components/ILIAS/Test/classes/class.ilObjTestAccess.php +++ b/components/ILIAS/Test/classes/class.ilObjTestAccess.php @@ -23,6 +23,8 @@ use ILIAS\Test\Access\AccessQuestionImage; use ILIAS\Test\Access\SimpleAccess; use ILIAS\Test\Access\Readable; +use ILIAS\Test\Settings\ScoreReporting\ScoreSettingsDatabaseRepository; +use ILIAS\Test\Settings\ScoreReporting\ScoreReportingTypes; use ILIAS\Data\Result; use ILIAS\Data\Result\Error; @@ -46,6 +48,9 @@ class ilObjTestAccess extends ilObjectAccess implements ilConditionHandling private ilRbacSystem $rbac_system; private ilAccessHandler $access; + private static ?ilCertificateObjectsForUserPreloader $certificate_preloader = null; + private static array $settings_result_summaries_by_obj_id = []; + public function __construct() { /** @var ILIAS\DI\Container $DIC */ @@ -720,4 +725,38 @@ public static function visibleUserResultExists($test_obj_id, $user_id): bool return $test_obj->canShowTestResults($test_session); } + + public static function _preloadData($obj_ids, $ref_ids): void + { + global $DIC; + if ((new ilCertificateActiveValidator())->validate()) { + self::$certificate_preloader = new ilCertificateObjectsForUserPreloader(new ilUserCertificateRepository()); + self::$certificate_preloader->preLoad($DIC['ilUser']->getId(), $obj_ids); + self::$settings_result_summaries_by_obj_id = (new ScoreSettingsDatabaseRepository($DIC['ilDB'])) + ->getSettingsResultSummaryByObjIds($obj_ids); + } + } + + public function showCertificateFor(int $user_id, int $obj_id): bool + { + if (self::$certificate_preloader === null + || !self::$certificate_preloader->isPreloaded($user_id, $obj_id) + || !isset(self::$settings_result_summaries_by_obj_id[$obj_id]) + || self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting() + === ScoreReportingTypes::SCORE_REPORTING_DISABLED) { + return false; + } + + $score_reporting = self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting(); + if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_IMMIDIATLY) { + return true; + } + + if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_DATE + && self::$settings_result_summaries_by_obj_id->getReportingDate() < new \DateTimeImmutable('now', new DateTimeZone('UTC'))) { + return true; + } + + return false; + } } diff --git a/components/ILIAS/Test/classes/class.ilObjTestListGUI.php b/components/ILIAS/Test/classes/class.ilObjTestListGUI.php index 6d0b3a98fb1f..96ee22b3d16f 100755 --- a/components/ILIAS/Test/classes/class.ilObjTestListGUI.php +++ b/components/ILIAS/Test/classes/class.ilObjTestListGUI.php @@ -20,6 +20,7 @@ use ILIAS\Test\Access\ParticipantAccess; use ILIAS\Test\TestDIC; +use ILIAS\UI\Component\Card\RepositoryObject as RepositoryObjectCard; /** * Class ilObjTestListGUI @@ -36,6 +37,7 @@ class ilObjTestListGUI extends ilObjectListGUI { protected $command_link_params = []; private ilTestAccess $test_access; + private ilObjTestAccess $obj_test_access; /** * initialisation @@ -54,6 +56,7 @@ public function init(): void // general commands array $this->commands = ilObjTestAccess::_getCommands(); + $this->obj_test_access = new ilObjTestAccess(); } public function initItem( @@ -104,12 +107,33 @@ public function getProperties(): array $this->user->getId() ); - if ($participant_access === ParticipantAccess::ALLOWED) { + if ($participant_access !== ParticipantAccess::ALLOWED) { + $props[] = ['alert' => true, 'property' => $this->lng->txt('status'), + 'value' => $participant_access->getAccessForbiddenMessage($this->lng)]; return $props; } - $props[] = ['alert' => true, 'property' => $this->lng->txt('status'), - 'value' => $participant_access->getAccessForbiddenMessage($this->lng)]; + if ($this->obj_test_access->showCertificateFor($this->user->getId(), $this->obj_id)) { + $this->lng->loadLanguageModule('certificate'); + $this->ctrl->setParameterByClass(ilTestEvaluationGUI::class, 'ref_id', $this->ref_id); + $props[] = [ + 'alert' => false, + 'property' => $this->lng->txt('certificate'), + 'value' => $this->ui->renderer()->render( + $this->ui->factory()->link()->standard( + $this->lng->txt('download_certificate'), + $this->ctrl->getLinkTargetByClass( + [ + ilObjTestGUI::class, + ilTestEvaluationGUI::class + ], + 'outCertificate' + ) + ) + ) + ]; + $this->ctrl->setParameterByClass(ilTestEvaluationGUI::class, 'ref_id', null); + } return $props; } @@ -170,7 +194,22 @@ public function addCommandLinkParameter($a_param) $this->command_link_params = $a_param; } - // begin-patch lok + public function getAsCard( + int $ref_id, + int $obj_id, + string $type, + string $title, + string $description + ): ?RepositoryObjectCard { + /** @var \ILIAS\UI\Component\Card\RepositoryObject $card */ + $card = parent::getAsCard($ref_id, $obj_id, $type, $title, $description); + if ($this->obj_test_access->showCertificateFor($this->user->getId(), $obj_id)) { + $card = $card->withCertificateIcon(true); + } + + return $card; + } + protected function modifyTitleLink(string $default_link): string { if (!ilLOSettings::isObjectiveTest($this->ref_id)) { @@ -194,6 +233,4 @@ protected function modifyTitleLink(string $default_link): string return parent::modifyTitleLink($cmd_link); } - - // end-patch lok -} // END class.ilObjTestListGUI +} diff --git a/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsDatabaseRepository.php b/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsDatabaseRepository.php index 5c4d798ab9b6..dbe70712af62 100755 --- a/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsDatabaseRepository.php +++ b/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsDatabaseRepository.php @@ -69,17 +69,6 @@ protected function doSelect(string $where_part): ScoreSettings $row = $this->db->fetchAssoc($res); - $reporting_date = $row['reporting_date']; - if ($reporting_date) { - $reporting_date = \DateTimeImmutable::createFromFormat( - self::STORAGE_DATE_FORMAT, - $reporting_date, - new \DateTimeZone('UTC') - ); - } else { - $reporting_date = null; - } - $test_id = (int) $row['test_id']; $settings = new ScoreSettings( @@ -90,7 +79,7 @@ protected function doSelect(string $where_part): ScoreSettings ->withPassScoring((int) $row['pass_scoring']), (new SettingsResultSummary($test_id)) ->withScoreReporting(ScoreReportingTypes::from($row['score_reporting'])) - ->withReportingDate($reporting_date) + ->withReportingDate($this->buildDateFromString($row['reporting_date'])) ->withShowGradingStatusEnabled((bool) $row['show_grading_status']) ->withShowGradingMarkEnabled((bool) $row['show_grading_mark']) ->withPassDeletionAllowed((bool) $row['pass_deletion_allowed']), @@ -132,4 +121,41 @@ public function store(ScoreSettings $settings): void ['test_id' => ['integer', $settings->getTestId()]] ); } + + + public function getSettingsResultSummaryByObjIds(array $obj_ids): array + { + $result = $this->db->query( + 'SELECT ' . PHP_EOL + . 'test_id, obj_fi, score_reporting, reporting_date,' . PHP_EOL + . 'show_grading_status, show_grading_mark, pass_deletion_allowed' . PHP_EOL + . 'FROM ' . self::TABLE_NAME . PHP_EOL + . 'WHERE ' . $this->db->in('obj_fi', $obj_ids, false, \ilDBConstants::T_INTEGER) + ); + + $settings_summary = []; + while (($row = $this->db->fetchAssoc($result)) !== null) { + $settings_summary[$row['obj_fi']] = (new SettingsResultSummary($row['test_id'])) + ->withScoreReporting(ScoreReportingTypes::from($row['score_reporting'])) + ->withReportingDate($this->buildDateFromString($row['reporting_date'])) + ->withShowGradingStatusEnabled((bool) $row['show_grading_status']) + ->withShowGradingMarkEnabled((bool) $row['show_grading_mark']) + ->withPassDeletionAllowed((bool) $row['pass_deletion_allowed']); + } + return $settings_summary; + } + + private function buildDateFromString(?string $reporting_date): ?\DateTimeImmutable + { + if ($reporting_date === null + || $reporting_date === '') { + return null; + } + + return \DateTimeImmutable::createFromFormat( + self::STORAGE_DATE_FORMAT, + $reporting_date, + new \DateTimeZone('UTC') + ); + } } diff --git a/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsRepository.php b/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsRepository.php index fff5b20015e0..ce3e91288bb1 100755 --- a/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsRepository.php +++ b/components/ILIAS/Test/src/Settings/ScoreReporting/ScoreSettingsRepository.php @@ -24,4 +24,5 @@ interface ScoreSettingsRepository { public function getFor(int $test_id): ScoreSettings; public function store(ScoreSettings $settings): void; + public function getSettingsResultSummaryByObjIds(array $obj_ids): array; }