ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjTestAccess.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
32
46{
52
54 private static array $settings_result_summaries_by_obj_id = [];
55
56 public function __construct()
57 {
59 global $DIC;
60 $this->db = $DIC['ilDB'];
61 $this->user = $DIC['ilUser'];
62 $this->lng = $DIC['lng'];
63 $this->rbac_system = $DIC['rbacsystem'];
64 $this->access = $DIC['ilAccess'];
65 }
66
67 public function canBeDelivered(ilWACPath $ilWACPath): bool
68 {
69 $readable = new Readable($this->access);
70
71 $can_it = $this->findMatch($ilWACPath->getPath(), [
72 new AccessFileUploadAnswer($this->user, $this->db, $readable),
73 new AccessQuestionImage($readable),
74 new AccessFileUploadPreview($this->db, $this->access),
75 ]);
76
77 return !$can_it->isOk() || $can_it->value();
78 }
79
80 private function findMatch(string $path, array $array): Result
81 {
82 return array_reduce($array, fn(Result $result, SimpleAccess $access) => $result->except(
83 fn() => $access->isPermitted($path)
84 ), new Error('Not a known path.'));
85 }
86
94 public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
95 {
96 if (is_null($user_id)) {
97 $user_id = $this->user->getId();
98 }
99
100 $is_admin = $this->rbac_system->checkAccessOfUser($user_id, 'write', $ref_id)
101 || $this->rbac_system->checkAccessOfUser($user_id, 'score_anon', $ref_id);
102
103 $is_online = !ilObject::lookupOfflineStatus($obj_id);
104
105 if (!$is_admin && !$is_online) {
106 return false;
107 }
108
109 switch ($permission) {
110 case "visible":
111 case "read":
113 !$is_admin) {
114 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("tst_warning_test_not_complete"));
115 return false;
116 }
117 break;
118 }
119
120 switch ($cmd) {
121 case "eval_stat":
123 $this->access->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $this->lng->txt("tst_warning_test_not_complete"));
124 return false;
125 }
126 break;
127 }
128
129 return true;
130 }
131
135 public static function getConditionOperators(): array
136 {
137 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
169 default:
170 return true;
171 }
172 }
173
174 public static function _getCommands(): array
175 {
176 global $DIC;
177 $DIC->language()->loadLanguageModule('assessment');
178
179 return [
180 ["permission" => "write", "cmd" => "questionsTabGateway", "lang_var" => "tst_edit_questions"],
181 ["permission" => "write", "cmd" => "ILIAS\Test\Settings\MainSettings\SettingsMainGUI::showForm", "lang_var" => "settings"],
182 ["permission" => "read", "cmd" => "ILIAS\Test\Presentation\TestScreenGUI::testScreen", "lang_var" => "tst_run", "default" => true],
183 ["permission" => "score_anon", "cmd" => "ILIAS\Test\Scoring\Manual\TestScoringByQuestionGUI::showManScoringByQuestionParticipantsTable", "lang_var" => "manscoring", "default" => true],
184 ];
185 }
186
187 //
188 // object specific access related methods
189 //
190
191 public static function getBypassActivationCheckForPermissions(): array
192 {
193 return [
194 'write',
195 'score_anon'
196 ];
197 }
198
199 private static function lookupCreationComplete(int $a_obj_id): bool
200 {
201 global $DIC;
202 $db = $DIC->database();
203 $result = $db->queryF(
204 "SELECT complete FROM tst_tests WHERE obj_fi=%s",
205 ['integer'],
206 [$a_obj_id]
207 );
208 return $result->numRows() > 0 && (bool) $db->fetchAssoc($result)['complete'];
209 }
210
218 public static function _getTestIDFromObjectID(int $object_id): int|false
219 {
220 global $DIC;
221 $ilDB = $DIC['ilDB'];
222 $test_id = false;
223 $result = $ilDB->queryF(
224 "SELECT test_id FROM tst_tests WHERE obj_fi = %s",
225 ['integer'],
226 [$object_id]
227 );
228 if ($result->numRows()) {
229 $row = $ilDB->fetchAssoc($result);
230 $test_id = $row["test_id"];
231 }
232 return $test_id;
233 }
234
242 public static function _getRandomTestsForQuestionPool(int $qpl_id): array
243 {
244 global $DIC;
245 $ilDB = $DIC['ilDB'];
246
247 $query = "
248 SELECT DISTINCT t.obj_fi
249 FROM tst_tests t
250 INNER JOIN tst_rnd_quest_set_qpls r
251 ON t.test_id = r.test_fi
252 WHERE r.pool_fi = %s
253 ";
254
255 $result = $ilDB->queryF($query, ['integer'], [$qpl_id]);
256
257 $tests = [];
258 while ($row = $ilDB->fetchAssoc($result)) {
259 $tests[] = $row['obj_fi'];
260 }
261
262 return $tests;
263 }
264
272 public static function _getParticipantData(int $active_id): string
273 {
274 global $DIC;
275 $lng = $DIC['lng'];
276 $ilDB = $DIC['ilDB'];
277
278 $result_active = $ilDB->queryF(
279 'SELECT * FROM tst_active WHERE active_id = %s',
280 ['integer'],
281 [$active_id]
282 );
283 $row_active = $ilDB->fetchAssoc($result_active);
284 $importname = $row_active['importname'];
285
286 if ($importname !== null
287 && $importname !== '') {
288 return $importname . ' (' . $lng->txt('imported') . ')';
289 }
290
291 if ($row_active['user_fi'] === ANONYMOUS_USER_ID) {
292 return '';
293 }
294
295 $uname = ilObjUser::_lookupName($row_active['user_fi']);
296
297 $result_test = $ilDB->queryF(
298 "SELECT obj_fi FROM tst_tests WHERE test_id = %s",
299 ["integer"],
300 [$row_active['test_fi']]
301 );
302 $row_test = $ilDB->fetchAssoc($result_test);
303 $obj_id = $row_test["obj_fi"];
304
305 if (ilObjTest::_lookupAnonymity($obj_id)) {
306 return $lng->txt("anonymous");
307 }
308
309 if ($uname['firstname'] . $uname['lastname'] === '') {
310 return $lng->txt('deleted_user');
311 }
312
313 return trim($uname['lastname'] . ', ' . $uname['firstname']);
314 }
315
322 public static function _getParticipantId(int $active_id): int
323 {
324 global $DIC;
325 $ilDB = $DIC['ilDB'];
326
327 $result = $ilDB->queryF(
328 'SELECT user_fi FROM tst_active WHERE active_id = %s',
329 ['integer'],
330 [$active_id]
331 );
332 $row = $ilDB->fetchAssoc($result);
333 return $row['user_fi'];
334 }
335
339 public static function _checkGoto(string $target): bool
340 {
341 global $DIC;
342 $ilAccess = $DIC['ilAccess'];
343
344 $t_arr = explode("_", $target);
345
346 if ($t_arr[0] != "tst" || ((int) $t_arr[1]) <= 0) {
347 return false;
348 }
349
350 if ($ilAccess->checkAccess("read", "", (int) $t_arr[1]) ||
351 $ilAccess->checkAccess("visible", "", (int) $t_arr[1])) {
352 return true;
353 }
354 return false;
355 }
356
362 public static function _isOffline(int $obj_id): bool
363 {
364 return ilObject::lookupOfflineStatus($obj_id);
365 }
366
367
368 public static function visibleUserResultExists(int $test_obj_id, int $user_id): bool
369 {
370 global $DIC;
371 $ilDB = $DIC['ilDB'];
372 $ilUser = $DIC['ilUser'];
373
374 $test_obj = ilObjectFactory::getInstanceByObjId($test_obj_id, false);
375
376 if (!($test_obj instanceof ilObjTest)) {
377 return false;
378 }
379
380 $test_session_factory = new ilTestSessionFactory($test_obj, $ilDB, $ilUser);
381 $test_session = $test_session_factory->getSessionByUserId($user_id);
382
383 return $test_obj->canShowTestResults($test_session);
384 }
385
386 public static function _preloadData(array $obj_ids, array $ref_ids): void
387 {
388 global $DIC;
389 if ((new ilCertificateActiveValidator())->validate()) {
390 self::$certificate_preloader = new ilCertificateObjectsForUserPreloader(new ilUserCertificateRepository());
391 self::$certificate_preloader->preLoad($DIC['ilUser']->getId(), $obj_ids);
392 self::$settings_result_summaries_by_obj_id = (new ScoreSettingsDatabaseRepository($DIC['ilDB']))
393 ->getSettingsResultSummaryByObjIds($obj_ids);
394 }
395 }
396
397 public function showCertificateFor(int $user_id, int $obj_id): bool
398 {
399 if (self::$certificate_preloader === null
400 || !self::$certificate_preloader->isPreloaded($user_id, $obj_id)
401 || !isset(self::$settings_result_summaries_by_obj_id[$obj_id])
402 || self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting()
403 === ScoreReportingTypes::SCORE_REPORTING_DISABLED) {
404 return false;
405 }
406
407 $score_reporting = self::$settings_result_summaries_by_obj_id[$obj_id]->getScoreReporting();
408 if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_IMMIDIATLY) {
409 return true;
410 }
411
412 if ($score_reporting === ScoreReportingTypes::SCORE_REPORTING_DATE
413 && self::$settings_result_summaries_by_obj_id->getReportingDate() < new \DateTimeImmutable('now', new DateTimeZone('UTC'))) {
414 return true;
415 }
416
417 return false;
418 }
419}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:32
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 _lookupAnonymity($a_obj_id)
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