Skip to content

Commit

Permalink
Test: Add certificate indicator/link
Browse files Browse the repository at this point in the history
  • Loading branch information
kergomard committed Dec 15, 2024
1 parent 800b45e commit 91a5218
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 19 deletions.
39 changes: 39 additions & 0 deletions components/ILIAS/Test/classes/class.ilObjTestAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}
}
51 changes: 44 additions & 7 deletions components/ILIAS/Test/classes/class.ilObjTestListGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use ILIAS\Test\Access\ParticipantAccess;
use ILIAS\Test\TestDIC;
use ILIAS\UI\Component\Card\RepositoryObject as RepositoryObjectCard;

/**
* Class ilObjTestListGUI
Expand All @@ -36,6 +37,7 @@ class ilObjTestListGUI extends ilObjectListGUI
{
protected $command_link_params = [];
private ilTestAccess $test_access;
private ilObjTestAccess $obj_test_access;

/**
* initialisation
Expand All @@ -54,6 +56,7 @@ public function init(): void

// general commands array
$this->commands = ilObjTestAccess::_getCommands();
$this->obj_test_access = new ilObjTestAccess();
}

public function initItem(
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -194,6 +233,4 @@ protected function modifyTitleLink(string $default_link): string

return parent::modifyTitleLink($cmd_link);
}

// end-patch lok
} // END class.ilObjTestListGUI
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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']),
Expand Down Expand Up @@ -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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 91a5218

Please sign in to comment.