forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
WAC Check for assessment #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lscharmer
wants to merge
1
commit into
release_7
Choose a base branch
from
hotfix/wac_assessment
base: release_7
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of ILIAS, a powerful learning management system | ||
| * published by ILIAS open source e-Learning e.V. | ||
| * | ||
| * ILIAS is licensed with the GPL-3.0, | ||
| * see https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| * You should have received a copy of said license along with the | ||
| * source code, too. | ||
| * | ||
| * If this is not the case or you just want to try ILIAS, you'll find | ||
| * us at: | ||
| * https://www.ilias.de | ||
| * https://github.com/ILIAS-eLearning | ||
| * | ||
| *********************************************************************/ | ||
|
|
||
| namespace ILIAS\Modules\Test; | ||
|
|
||
| use ILIAS\Data\Result; | ||
| use ILIAS\Data\Result\Ok; | ||
| use ILIAS\Data\Result\Error; | ||
| use ILIAS\DI\Container; | ||
| use ilObjTest; | ||
| use ilObject2; | ||
| use ilSession; | ||
| use ilTestSession; | ||
| use ilTestAccess; | ||
|
|
||
| class CanAccessFileUploadAnswer | ||
| { | ||
| /** @var Container */ | ||
| private $container; | ||
| /** @var callable(int) : int */ | ||
| private $object_id_of_test_id; | ||
| /** @var callable(int) : string[] */ | ||
| private $references_of; | ||
| /** @var callable(string) : mixed */ | ||
| private $session; | ||
| /** @var callable(string, int, int) : bool */ | ||
| private $checkResultsAccess; | ||
|
|
||
| /** | ||
| * @param Container $container | ||
| * @param callable(int) : int $object_id_of_test_id | ||
| * @param callable(string) : string[] $references_of | ||
| * @param callable(string) : mixed $session | ||
| * @param callable(string, int, int) : bool $checkResultsAccess | ||
| */ | ||
| public function __construct( | ||
| Container $container, | ||
| $object_id_of_test_id = [ilObjTest::class, '_getObjectIDFromTestID'], | ||
| $references_of = [ilObject2::class, '_getAllReferences'], | ||
| $session = [ilSession::class, 'get'], | ||
| callable $checkResultsAccess = null | ||
| ) { | ||
| $this->container = $container; | ||
| $this->object_id_of_test_id = $object_id_of_test_id; | ||
| $this->references_of = $references_of; | ||
| $this->session = $session; | ||
| $this->checkResultsAccess = $checkResultsAccess ?? static function (string $reference, int $test_id, int $active_id) : bool { | ||
| $access = new ilTestAccess($reference, $test_id); | ||
| return $access->checkResultsAccessForActiveId($active_id); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $path | ||
| * | ||
| * @return Result<bool> | ||
| */ | ||
| public function isTrue(string $path) : Result | ||
| { | ||
| $path_and_test = $this->pathAndTestId($path); | ||
|
|
||
| if (!$path_and_test) { | ||
| return new Error('Not a file upload path of test answers.'); | ||
|
mjansenDatabay marked this conversation as resolved.
|
||
| } | ||
| if (!$path_and_test['test']) { | ||
| return new Ok(false); | ||
|
mjansenDatabay marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $object_id = (int) ($this->object_id_of_test_id)($path_and_test['test']); | ||
| if (!$object_id) { | ||
| return new Ok(false); | ||
|
mjansenDatabay marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $references = ($this->references_of)($object_id); | ||
|
|
||
| return new Ok($this->canRead($references) && $this->roleBasedCheck($path_and_test['test'], $references, $path_and_test['path'])); | ||
|
mjansenDatabay marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private function isAnonymous() : bool | ||
| { | ||
| return $this->container->user()->isAnonymous() || !$this->container->user()->getId(); | ||
| } | ||
|
|
||
| private function accessCodeOk(string $file, int $test_id) : bool | ||
| { | ||
| $code = ($this->session)(ilTestSession::ACCESS_CODE_SESSION_INDEX)[$test_id] ?? false; | ||
|
|
||
| return $code && $this->userDidUpload($test_id, $file, $code); | ||
| } | ||
|
|
||
| private function userDidUpload(int $test_id, string $file, string $code = null) : bool | ||
| { | ||
| $where = [ | ||
| 'user_fi = %s', | ||
| 'value1 = %s', | ||
| 'anonymous_id ' . (null === $code ? 'IS' : '=') . ' %s', | ||
| 'test_fi = %s', | ||
| ]; | ||
|
|
||
| $result = $this->container->database()->queryF( | ||
| 'SELECT 1 FROM tst_solutions INNER JOIN tst_active ON active_id = active_fi WHERE ' . implode(' AND ', $where), | ||
| ['integer', 'text', 'text', 'integer'], | ||
| [$this->container->user()->getId(), $file, $code, $test_id] | ||
| ); | ||
|
|
||
| return (bool) $this->container->database()->numRows($result); | ||
| } | ||
|
|
||
| private function activeIdOfFile(string $file) : ?int | ||
| { | ||
| $result = $this->container->database()->queryF( | ||
| 'SELECT active_id FROM tst_active INNER JOIN tst_solutions WHERE value1 = %s', | ||
| ['text'], | ||
| [$file] | ||
| ); | ||
|
|
||
| $result = $this->container->database()->fetchAssoc($result)['active_id'] ?? false; | ||
| if (!$result) { | ||
| return null; | ||
| } | ||
|
|
||
| return (int) $result; | ||
| } | ||
|
|
||
| /** | ||
| * @param int $test_id | ||
| * @param string[] $references | ||
| * @param string $file | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function roleBasedCheck(int $test_id, array $references, string $file) : bool | ||
| { | ||
| return $this->isAnonymous() ? $this->accessCodeOk($file, $test_id) : $this->canAccessResults($test_id, $references, $file) || $this->userDidUpload($test_id, $file); | ||
| } | ||
|
|
||
| /** | ||
| * @param string[] $references | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function canRead(array $references) : bool | ||
| { | ||
| return $this->checkReferences(function (string $ref_id) : bool { | ||
| return $this->container->access()->checkAccess('read', '', $ref_id); | ||
| }, $references); | ||
| } | ||
|
|
||
| /** | ||
| * @param int $test_id | ||
| * @param string[] $references | ||
| * @param string $file | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function canAccessResults(int $test_id, array $references, string $file) : bool | ||
| { | ||
| $active_id = $this->activeIdOfFile($file); | ||
| if (!$active_id) { | ||
| return false; | ||
| } | ||
|
|
||
| return $this->checkReferences(function (string $reference) use ($test_id, $active_id) : bool { | ||
| return ($this->checkResultsAccess)($reference, $test_id, $active_id); | ||
| }, $references); | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(string) : bool $access | ||
| * @param string[] $references | ||
| * | ||
| * @return bool | ||
| */ | ||
| private function checkReferences(callable $check, array $references) : bool | ||
| { | ||
| foreach ($references as $ref_id) { | ||
| if ($check($ref_id)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $path | ||
| * | ||
| * @return null|array{test: int, path: string} | ||
| */ | ||
| private function pathAndTestId(string $path) : ?array | ||
| { | ||
| $results = []; | ||
| if (!preg_match(':/assessment/tst_(\d+)/.*/([^/]+)$:', $path, $results)) { | ||
| return null; | ||
| } | ||
|
|
||
| return [ | ||
| 'test' => (int) $results[1], | ||
| 'path' => $results[2], | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.