ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
Participant.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ILIAS\Questions\AnswerForm\Response as AnswerFormResponse;
27use ILIAS\Questions\Attempt\Repository as AttemptRepository;
30use ILIAS\Questions\Question\Views\Participant as QuestionParticipantView;
32use ILIAS\HTTP\Services as HTTP;
34use ILIAS\Refinery\Factory as Refinery;
35use ILIAS\UI\Factory as UIFactory;
36
38{
40
41 private bool $shuffle_question_order = false;
42
43 private array $attempt_cache = [];
44
48 public function __construct(
49 private readonly Language $lng,
50 private readonly Refinery $refinery,
51 private readonly UIFactory $ui_factory,
52 private readonly HTTP $http,
53 private readonly QuestionRepository $question_repository,
54 private readonly AttemptRepository $attempt_repository,
55 private readonly CapabilitiesFactory $capabilities_factory,
56 array $capability_identifiers,
57 private readonly int $owner_object_id
58 ) {
59 $this->required_capabilities = $this->capabilities_factory->get(
60 $capability_identifiers
61 );
62 }
63
65 {
66 return $this->presentation_identifier;
67 }
68
69 public function withShuffleQuestionOrder(
71 ): self {
72 $clone = clone $this;
73 $clone->shuffle_question_order = $shuffle_question_order;
74 return $clone;
75 }
76
77 public function getQuestionView(
78 Uuid $question_id,
79 ?Uuid $attempt_id,
80 bool $interactive,
81 bool $show_marks,
82 bool $show_best_response,
83 bool $show_feedback
84 ): QuestionParticipantView {
85 $question = $this->question_repository->getForQuestionId(
86 $question_id
87 );
88
89 $marking_required = $this->required_capabilities->isMarkingRequired();
90
91 if ($attempt_id === null
92 || !isset($this->attempt_cache[$attempt_id->toString()][$question_id->toString()])) {
93 $attempt = $this->attempt_repository->getAttemptFor(
94 $attempt_id,
95 [$question]
96 );
97 $attempt_id = $attempt->getId();
98 $this->attempt_cache[$attempt_id->toString()][$question_id->toString()] = $attempt;
99 }
100
101 return $question->getParticipantView(
102 $this->lng,
103 $this->refinery,
104 $this->ui_factory,
105 $this->required_capabilities,
106 $this->attempt_cache[$attempt_id->toString()][$question_id->toString()],
107 $interactive,
108 $show_marks && $marking_required,
109 $show_best_response && $marking_required,
110 $show_feedback && $this->required_capabilities->isCapabilityRequired(
111 TextFeedback::getIdentifier()
112 )
113 );
114 }
115
116 public function persistResponse(
117 Uuid $question_id,
118 Uuid $attempt_id
119 ): void {
120 $question = $this->question_repository->getForQuestionId(
121 $question_id
122 );
123
124 $attempt_data = $this->attempt_repository->getAttemptFor(
125 $attempt_id,
126 [$question]
127 );
128
129 if ($attempt_data === null) {
130 throw new \UnexpectedValueException(
131 'The provided attempt identifier is invalid. Response cannot be persisted.'
132 );
133 }
134
135 $response = $this->attempt_repository->getNewResponseFor(
136 $question_id,
137 $attempt_id
138 );
139
140 $response_with_values_from_post = array_reduce(
141 $question->retrieveAnswerFormResponsesFromPost(
142 $this->required_capabilities,
143 $this->http->wrapper()->post(),
144 $response->getId()
145 ),
146 fn(Response $c, AnswerFormResponse $v): Response
147 => $c->withAnswerFormResponse($v),
149 );
150
151 if ($this->required_capabilities->isMarkingRequired()) {
152 $response_with_values_from_post = $question->addAwardedPointsToResponse(
153 $response_with_values_from_post
154 );
155 }
156
157 $this->attempt_repository->storeResponse(
158 $response_with_values_from_post
159 );
160 }
161
162 public function deleteResponsesFor(
163 Uuid $attempt_id,
164 Uuid $question_id
165 ): void {
166 $this->attempt_repository->deleteResponsesFor(
167 $attempt_id,
168 $this->question_repository->getForQuestionId(
169 $question_id
170 )
171 );
172 }
173}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:43
deleteResponsesFor(Uuid $attempt_id, Uuid $question_id)
withShuffleQuestionOrder(bool $shuffle_question_order)
Definition: Participant.php:69
readonly RequiredCapabilities $required_capabilities
Definition: Participant.php:39
__construct(private readonly Language $lng, private readonly Refinery $refinery, private readonly UIFactory $ui_factory, private readonly HTTP $http, private readonly QuestionRepository $question_repository, private readonly AttemptRepository $attempt_repository, private readonly CapabilitiesFactory $capabilities_factory, array $capability_identifiers, private readonly int $owner_object_id)
Definition: Participant.php:48
getQuestionView(Uuid $question_id, ?Uuid $attempt_id, bool $interactive, bool $show_marks, bool $show_best_response, bool $show_feedback)
Definition: Participant.php:77
persistResponse(Uuid $question_id, Uuid $attempt_id)
toString()
Converts this UUID into a string representation.
global $lng
Definition: privfeed.php:26
$response
Definition: xapitoken.php:92