ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilObjTestAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31
45{
51
53 private static array $settings_result_summaries_by_obj_id = [];
54
55 public function __construct()
56 {
58 global $DIC;
59 $this->db = $DIC['ilDB'];
60 $this->user = $DIC['ilUser'];
61 $this->lng = $DIC['lng'];
62 $this->rbac_system = $DIC['rbacsystem'];
63 $this->access = $DIC['ilAccess'];
64 }
65
66 public function canBeDelivered(ilWACPath $ilWACPath): bool
67 {
68 $readable = new Readable($this->access);
69
70 $can_it = $this->findMatch($ilWACPath->getPath(), [
71 new AccessFileUploadAnswer($this->user, $this->db, $readable),
72 new AccessQuestionImage($readable),
73 new AccessFileUploadPreview($this->db, $this->access),
74 ]);
75
76 return !$can_it->isOk() || $can_it->value();
77 }
78
79 private function findMatch(string $path, array $array): Result
80 {
81 return array_reduce($array, fn(Result $result, SimpleAccess $access) => $result->except(
82 fn() => $access->isPermitted($path)
83 ), new Error('Not a known path.'));
84 }
85
93 public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
94 {
95 if (is_null($user_id)) {
96 $user_id = $this->user->getId();
97 }
98
99 $is_admin = $this->rbac_system->checkAccessOfUser($user_id, 'write', $ref_id)
100 || $this->rbac_system->checkAccessOfUser($user_id, 'score_anon', $ref_id);
101
102 $is_online = !ilObject::lookupOfflineStatus($obj_id);
103
104 if (!$is_admin && !$is_online) {
105 return false;
106 }
107
108 switch ($permission) {
109 case 'visible':
110 case 'read':
112 !$is_admin) {
113 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt('tst_warning_test_not_complete'));
114 return false;
115 }
116 break;
117 }
118
119 switch ($cmd) {
120 case 'eval_stat':
122 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt('tst_warning_test_not_complete'));
123 return false;
124 }
125 break;
126 }
127
128 return true;
129 }
130
134 public static function getConditionOperators(): array
135 {
136 return [
142 ];
143 }
144
145
151 public static function checkCondition(int $a_trigger_obj_id, string $a_operator, string $a_value, int $a_usr_id): bool
152 {
154 $test_result_repository = TestDIC::dic()['results.data.repository'];
155
156 switch ($a_operator) {
158 return $test_result_repository->isPassed($a_usr_id, $a_trigger_obj_id);
159
161 return $test_result_repository->isFailed($a_usr_id, $a_trigger_obj_id);
162
164 return $test_result_repository->hasFinished($a_usr_id, $a_trigger_obj_id);
165
167 return !$test_result_repository->hasFinished($a_usr_id, $a_trigger_obj_id);
168
170 $percentage_thresholds = self::deserializePercentageThresholds($a_value);
171 if ($percentage_thresholds === false) {
172 return false;
173 }
174 return $test_result_repository->reachedPercentage(
175 $a_usr_id,
176 $a_trigger_obj_id,
177 $percentage_thresholds['min_percentage'],
178 $percentage_thresholds['max_percentage']
179 );
180 default:
181 return true;
182 }
183 }
184
185 public static function _getCommands(): array
186 {
187 global $DIC;
188 $DIC->language()->loadLanguageModule('assessment');
189
190 return [
191 [
192 'permission' => 'write',
193 'cmd' => 'questionsTabGateway',
194 'lang_var' => 'tst_edit_questions'
195 ],
196 [
197 'permission' => 'write',
198 'cmd' => 'ILIAS\Test\Settings\MainSettings\SettingsMainGUI::showForm',
199 'lang_var' => 'settings'
200 ],
201 [
202 'permission' => 'read',
203 'cmd' => 'ILIAS\Test\Presentation\TestScreenGUI::testScreen',
204 'lang_var' => 'tst_run',
205 'default' => true
206 ],
207 [
208 'permission' => 'score_anon',
209 'cmd' => 'ILIAS\Test\Scoring\Manual\ConsecutiveScoringGUI::view',
210 'lang_var' => 'manscoring'
211 ],
212 ];
213 }
214
215 //
216 // object specific access related methods
217 //
218
219 public static function getBypassActivationCheckForPermissions(): array
220 {
221 return [
222 'write',
223 'score_anon'
224 ];
225 }
226
227 private static function lookupCreationComplete(int $a_obj_id): bool
228 {
229 global $DIC;
230 $db = $DIC->database();
231 $result = $db->queryF(
232 'SELECT complete FROM tst_tests WHERE obj_fi=%s',
234 [$a_obj_id]
235 );
236 return $result->numRows() > 0 && (bool) $db->fetchAssoc($result)['complete'];
237 }
238
246 public static function _getTestIDFromObjectID(int $object_id): int|false
247 {
248 global $DIC;
249 $ilDB = $DIC['ilDB'];
250 $test_id = false;
251 $result = $ilDB->queryF(
252 'SELECT test_id FROM tst_tests WHERE obj_fi = %s',
254 [$object_id]
255 );
256 if ($result->numRows()) {
257 $row = $ilDB->fetchAssoc($result);
258 $test_id = $row['test_id'];
259 }
260 return $test_id;
261 }
262
270 public static function _getRandomTestsForQuestionPool(int $qpl_id): array
271 {
272 global $DIC;
273 $ilDB = $DIC['ilDB'];
274
275 $query = 'SELECT DISTINCT t.obj_fi' . PHP_EOL
276 . 'FROM tst_tests t' . PHP_EOL
277 . 'INNER JOIN tst_rnd_quest_set_qpls r' . PHP_EOL
278 . 'ON t.test_id = r.test_fi' . PHP_EOL
279 . 'WHERE r.pool_fi = %s' . PHP_EOL;
280
281 $result = $ilDB->queryF($query, [ilDBConstants::T_INTEGER], [$qpl_id]);
282
283 $tests = [];
284 while ($row = $ilDB->fetchAssoc($result)) {
285 $tests[] = $row['obj_fi'];
286 }
287
288 return $tests;
289 }
290
298 public static function _getParticipantData(int $active_id): string
299 {
300 global $DIC;
301 $lng = $DIC['lng'];
302 $ilDB = $DIC['ilDB'];
303
304 $result_active = $ilDB->queryF(
305 'SELECT * FROM tst_active WHERE active_id = %s',
307 [$active_id]
308 );
309 $row_active = $ilDB->fetchAssoc($result_active);
310 $importname = $row_active['importname'];
311
312 if ($importname !== null
313 && $importname !== '') {
314 return $importname . ' (' . $lng->txt('imported') . ')';
315 }
316
317 if ($row_active['user_fi'] === ANONYMOUS_USER_ID) {
318 return '';
319 }
320
321 $uname = ilObjUser::_lookupName($row_active['user_fi']);
322
323 $result_test = $ilDB->queryF(
324 'SELECT obj_fi FROM tst_tests WHERE test_id = %s',
326 [$row_active['test_fi']]
327 );
328 $row_test = $ilDB->fetchAssoc($result_test);
329 $obj_id = $row_test['obj_fi'];
330
331 $test_obj = new ilObjTest($obj_id, false);
332 if ($test_obj->getAnonymity()) {
333 return $lng->txt('anonymous');
334 }
335
336 if ($uname['firstname'] . $uname['lastname'] === '') {
337 return $lng->txt('deleted_user');
338 }
339
340 return trim($uname['lastname'] . ', ' . $uname['firstname']);
341 }
342
349 public static function _getParticipantId(int $active_id): int
350 {
351 global $DIC;
352 $ilDB = $DIC['ilDB'];
353
354 $result = $ilDB->queryF(
355 'SELECT user_fi FROM tst_active WHERE active_id = %s',
357 [$active_id]
358 );
359 $row = $ilDB->fetchAssoc($result);
360 return $row['user_fi'];
361 }
362
366 public static function _checkGoto(string $target): bool
367 {
368 global $DIC;
369 $ilAccess = $DIC['ilAccess'];
370
371 $t_arr = explode('_', $target);
372
373 if ($t_arr[0] != 'tst' || ((int) $t_arr[1]) <= 0) {
374 return false;
375 }
376
377 if ($ilAccess->checkAccess('read', '', (int) $t_arr[1]) ||
378 $ilAccess->checkAccess('visible', '', (int) $t_arr[1])) {
379 return true;
380 }
381 return false;
382 }
383
389 public static function _isOffline(int $obj_id): bool
390 {
391 return ilObject::lookupOfflineStatus($obj_id);
392 }
393
394
395 public static function visibleUserResultExists(int $test_obj_id, int $user_id): bool
396 {
397 global $DIC;
398 $ilDB = $DIC['ilDB'];
399 $ilUser = $DIC['ilUser'];
400
401 $test_obj = ilObjectFactory::getInstanceByObjId($test_obj_id, false);
402
403 if (!($test_obj instanceof ilObjTest)) {
404 return false;
405 }
406
407 $test_session_factory = new ilTestSessionFactory($test_obj, $ilDB, $ilUser);
408 $test_session = $test_session_factory->getSessionByUserId($user_id);
409
410 return $test_obj->canShowTestResults($test_session);
411 }
412
413 public static function _preloadData(array $obj_ids, array $ref_ids): void
414 {
415 global $DIC;
416 if ((new ilCertificateActiveValidator())->validate()) {
417 self::$certificate_preloader = new ilCertificateObjectsForUserPreloader(new ilUserCertificateRepository());
418 self::$certificate_preloader->preLoad($DIC['ilUser']->getId(), $obj_ids);
419 self::$settings_result_summaries_by_obj_id = TestDIC::dic()['settings.scoring.repository']
420 ->getSettingsResultSummaryByObjIds($obj_ids);
421 }
422 }
423
424 public function showCertificateFor(int $user_id, int $obj_id): bool
425 {
426 if (self::$certificate_preloader === null
427 || !self::$certificate_preloader->isPreloaded($user_id, $obj_id)
428 || !isset(self::$settings_result_summaries_by_obj_id[$obj_id])
429 || self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting()
430 === ScoreReportingTypes::SCORE_REPORTING_DISABLED) {
431 return false;
432 }
433
434 $score_reporting = self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting();
435 if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_IMMIDIATLY) {
436 return true;
437 }
438
439 if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_DATE
440 && self::$settings_result_summaries_by_obj_id->getReportingDate() < new \DateTimeImmutable('now', new DateTimeZone('UTC'))) {
441 return true;
442 }
443
444 return false;
445 }
446
450 private static function deserializePercentageThresholds(string $value): array|false
451 {
452 $value_arr = unserialize($value);
453
454 if ($value_arr === false) {
455 return false;
456 }
457
458 return [
459 'min_percentage' => (float) ($value_arr['min_percentage'] ?? 0.0) / 100,
460 'max_percentage' => (float) ($value_arr['max_percentage'] ?? 0.0) / 100
461 ];
462 }
463}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:32
return true
const string OPERATOR_RESULT_RANGE_PERCENTAGE
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilObjTestAccess.
canBeDelivered(ilWACPath $ilWACPath)
static array $settings_result_summaries_by_obj_id
findMatch(string $path, array $array)
static getConditionOperators()
Get possible conditions operators.
static _getParticipantData(int $active_id)
Retrieves a participant name from active id.
static _isOffline(int $obj_id)
returns the objects's OFFline status
static ilCertificateObjectsForUserPreloader $certificate_preloader
static _getCommands()
get commands
static _checkGoto(string $target)
check whether goto script will succeed
showCertificateFor(int $user_id, int $obj_id)
static _getParticipantId(int $active_id)
Get user id for active id.
static visibleUserResultExists(int $test_obj_id, int $user_id)
static lookupCreationComplete(int $a_obj_id)
static _getTestIDFromObjectID(int $object_id)
Returns the ILIAS test id for a given object id.
_checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id=null)
Checks wether a user may invoke a command or not (this method is called by ilAccessHandler::checkAcce...
static _preloadData(array $obj_ids, array $ref_ids)
Preload data.
static getBypassActivationCheckForPermissions()
ilAccessHandler $access
static _getRandomTestsForQuestionPool(int $qpl_id)
Get all tests using a question pool for random selection.
static deserializePercentageThresholds(string $value)
User class.
static _lookupName(int $a_user_id)
Class ilObjectAccess.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static lookupOfflineStatus(int $obj_id)
Lookup offline status using objectDataCache.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ANONYMOUS_USER_ID
Definition: constants.php:27
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
except(callable $f)
Feed the error into a callable and replace this with the result or do nothing if this is a value.
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
Interface for condition handling.
static checkCondition(int $a_trigger_obj_id, string $a_operator, string $a_value, int $a_usr_id)
check condition for a specific user and object
Interface ilDBInterface.
fetchAssoc(ilDBStatement $statement)
queryF(string $query, array $types, array $values)
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26