ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
Factory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Test\Results\Data;
22 
27 
28 class Factory
29 {
30  /*
31  * @var array<int test_obj_id, \ilTestEvaluationData> $test_data
32  */
33  private array $test_data = [];
34  public function __construct(
35  protected \ilTestShuffler $shuffler,
36  protected UIFactory $ui_factory,
37  protected UIRenderer $ui_renderer
38  ) {
39  }
40 
44  public function getAttemptIdsArrayFor(
45  \ilObjTest $test_obj,
46  int $active_id
47  ): array {
48  $eval = $this->retrieveResultData($test_obj);
49  return array_keys($eval->getParticipant($active_id)->getPasses());
50  }
51 
52  public function getOverviewDataForTest(
53  \ilObjTest $test_obj
54  ): ?TestOverview {
55  $eval = $this->retrieveResultData($test_obj);
56  $found_participants = $eval->getParticipants();
57  if ($found_participants === []) {
58  return null;
59  }
60 
61  $total_passed = 0;
62  $total_passed_reached = 0.0;
63  $total_passed_max = 0.0;
64  $total_passed_time = 0;
65  foreach ($found_participants as $userdata) {
66  if ($userdata->getMark()?->getPassed()) {
67  $total_passed++;
68  $total_passed_reached += $userdata->getReached();
69  $total_passed_max += $userdata->getMaxpoints();
70  $total_passed_time += $userdata->getTimeOnTask();
71  }
72  }
73 
74  return new TestOverview(
75  $test_obj->getId(),
76  count($found_participants),
77  $eval->getTotalFinishedParticipants(),
78  $total_passed,
79  $test_obj->evalTotalStartedAverageTime($eval->getParticipantIds()),
80  $total_passed_time,
81  $eval->getStatistics()->rankMedian(),
82  $eval->getStatistics()->getEvaluationDataOfMedianUser()?->getMark()?->getShortName() ?? '',
83  $eval->getStatistics()->median(),
84  $total_passed === 0 ? 0 : $total_passed_reached / $total_passed
85  );
86  }
87 
88  public function getAttemptOverviewFor(
90  \ilObjTest $test_obj,
91  int $active_id,
92  ?int $attempt_id
93  ): ?AttemptOverview {
94  $eval = $this->retrieveResultData($test_obj);
95  $found_participants = $eval->getParticipants();
96  $participant_data = $eval->getParticipant($active_id);
97  if ($attempt_id === null) {
98  $attempt_id = $participant_data?->getScoredPass();
99  }
100  if ($found_participants === []
101  || $attempt_id === null) {
102  return null;
103  }
104 
105  $attempt_data = $participant_data?->getPass($attempt_id);
106  if ($attempt_data === null) {
107  return null;
108  }
109 
110  return new AttemptOverview(
111  $active_id,
112  $attempt_id,
113  $settings,
114  $attempt_data->getExamId(),
115  $attempt_data->getReachedPoints(),
116  $attempt_data->getMaxPoints(),
117  $attempt_data->getMark(),
118  $attempt_data->getAnsweredQuestionCount(),
119  $attempt_data->getQuestionCount(),
120  $attempt_data->getRequestedHintsCount(),
121  $attempt_data->getWorkingTime(),
122  $participant_data->getTimeOnTask(),
123  $attempt_data->getStartTime(),
124  $attempt_data->getLastAccessTime(),
125  $participant_data->getPassCount(),
126  $participant_data->getScoredPass(),
127  $eval->getStatistics()->rank($participant_data->getReached()),
128  $attempt_data->getStatusOfAttempt()
129  );
130  }
131 
138  ResultPresentationSettings $settings,
139  \ilObjTest $test_obj,
140  array $participants
141  ): array {
142  return array_map(
143  function (Participant $v) use ($settings, $test_obj, $participants): Participant {
144  if ($v->getActiveId() === null) {
145  return $v;
146  }
147 
148  $scored_attempt = $this->getAttemptOverviewFor(
149  $settings,
150  $test_obj,
151  $v->getActiveId(),
152  null
153  );
154 
155  if ($scored_attempt !== null
156  && $scored_attempt->getStatusOfAttempt() === StatusOfAttempt::RUNNING
157  && $scored_attempt->getStartedDate() !== null) {
158  $v = $v->withRunningAttemptStart($scored_attempt->getStartedDate());
159  }
160 
162  $scored_attempt
163  );
164  },
165  $participants
166  );
167  }
168 
169  public function getAttemptResultsFor(
170  ResultPresentationSettings $settings,
171  \ilObjTest $test_obj,
172  int $active_id,
173  int $attempt_id,
174  bool $is_user_output
175  ): AttemptResult {
176  return $this->buildAttemptResults(
177  $settings,
178  $test_obj,
179  $active_id,
180  $attempt_id,
181  $is_user_output
182  );
183  }
184 
185  private function buildAttemptResults(
186  ResultPresentationSettings $settings,
187  \ilObjTest $test_obj,
188  int $active_id,
189  int $attempt_id,
190  bool $is_user_output
191  ): AttemptResult {
192  $question_results = [];
193 
194  $results = $test_obj->getTestResult(
195  $active_id,
196  $attempt_id,
197  false, //$ordered_sequence
198  $settings->getShowHiddenQuestions(),
199  $settings->getShowOptionalQuestions()
200  );
201 
202  // params of getSolutionOutput
203  $graphical_output = false;
204  $result_output = false;
205  $show_question_only = $settings->getQuestionTextOnly();
206  $show_feedback = false; //general
207  $show_correct_solution = false;
208  $show_manual_scoring = false;
209  $show_question_text = true;
210  $show_inline_feedback = true;
211 
212  foreach ($results as $idx => $qresult) {
213  if (!is_numeric($idx)) {
214  continue;
215  }
216 
217  $qid = $qresult['qid'];
218  $type = $qresult['type'];
219  $title = $qresult['title'];
220  $question_score = $qresult['max'];
221  $usr_score = $qresult['reached'];
222  $workedthrough = (bool) $qresult['workedthrough'];
223  $answered = (bool) $qresult['answered'];
224  $requested_hints = (int) $qresult['requested_hints'];
225 
226 
227  $question_gui = $test_obj->createQuestionGUI('', $qid);
228  $shuffle_trafo = $this->shuffler->getAnswerShuffleFor($qid, $active_id, $attempt_id);
229  $question = $question_gui->getObject();
230  $question->setShuffler($shuffle_trafo);
231  $question_gui->setObject($question);
232 
233  $graphical_output = true;
234  $show_correct_solution = false;
235  $show_feedback = $settings->getShowFeedback();
236  $usr_solution = $question_gui->getSolutionOutput(
237  $active_id,
238  $attempt_id,
239  $graphical_output,
240  $result_output,
241  $show_question_only,
242  $show_feedback,
243  $show_correct_solution,
244  $show_manual_scoring,
245  $show_question_text,
246  $show_inline_feedback
247  );
248 
249  $autosave_output = null;
250  $show_autosave_title = false;
251  if ($test_obj->getAutosave()) {
252  $autosave_output = $question_gui->getAutoSavedSolutionOutput(
253  $active_id,
254  $attempt_id,
255  $graphical_output,
256  $result_output,
257  $show_question_only,
258  $show_feedback,
259  $show_correct_solution,
260  $show_manual_scoring,
261  $show_question_text,
262  $show_autosave_title,
263  $show_inline_feedback
264  );
265  }
266 
267  $graphical_output = false;
268  $show_correct_solution = true;
269  $show_feedback = false;
270  $show_inline_feedback = false;
271  $best_solution = $question_gui->getSolutionOutput(
272  $active_id,
273  $attempt_id,
274  $graphical_output,
275  $result_output,
276  $show_question_only,
277  $show_feedback,
278  $show_correct_solution,
279  $show_manual_scoring,
280  $show_question_text,
281  $show_inline_feedback
282  );
283 
284  if ($show_question_only) {
285  $usr_solution = $this->ui_renderer->render($this->ui_factory->legacy('<div class="ilc_question_Standard">' . $usr_solution . '</div>'));
286  $best_solution = $this->ui_renderer->render($this->ui_factory->legacy('<div class="ilc_question_Standard">' . $best_solution . '</div>'));
287  }
288 
289  $feedback = $question_gui->getGenericFeedbackOutput($active_id, $attempt_id);
290 
291  $recapitulation = null;
292  if ($is_user_output && $settings->getShowRecapitulation()) {
293  $recapitulation = $question_gui->getObject()->getSuggestedSolutionOutput();
294  }
295 
296  $question_results[] = new QuestionResult(
297  $qid,
298  $type,
299  $title,
300  $question_score,
301  $usr_score,
302  $usr_solution,
303  $best_solution,
304  $feedback,
305  $workedthrough,
306  $answered,
307  $requested_hints,
308  $recapitulation,
309  $autosave_output,
310  $idx
311  );
312  }
313 
314  return new AttemptResult(
315  $active_id,
316  $attempt_id,
317  $question_results
318  );
319  }
320 
321  private function retrieveResultData(\ilObjTest $test_obj): \ilTestEvaluationData
322  {
323  if (!isset($this->test_data[$test_obj->getId()])) {
324  $this->test_data[$test_obj->getId()] = $test_obj->getCompleteEvaluationData();
325  }
326 
327  return $this->test_data[$test_obj->getId()];
328  }
329 }
withRunningAttemptStart(\DateTimeImmutable $start_date)
retrieveResultData(\ilObjTest $test_obj)
Definition: Factory.php:321
getOverviewDataForTest(\ilObjTest $test_obj)
Definition: Factory.php:52
getAttemptIdsArrayFor(\ilObjTest $test_obj, int $active_id)
Definition: Factory.php:44
withAttemptOverviewInformation(?AttemptOverview $attempt_overview)
createQuestionGUI($question_type, $question_id=-1)
Creates a question GUI instance of a given question type.
evalTotalStartedAverageTime(?array $active_ids_to_filter=null)
getTestResult(int $active_id, ?int $pass=null, bool $ordered_sequence=false, bool $consider_hidden_questions=true, bool $consider_optional_questions=true)
Calculates the results of a test for a given user and returns an array with all test results...
getAttemptOverviewFor(ResultPresentationSettings $settings, \ilObjTest $test_obj, int $active_id, ?int $attempt_id)
Definition: Factory.php:88
$results
getCompleteEvaluationData($filterby='', $filtertext='')
buildAttemptResults(ResultPresentationSettings $settings, \ilObjTest $test_obj, int $active_id, int $attempt_id, bool $is_user_output)
Definition: Factory.php:185
__construct(protected \ilTestShuffler $shuffler, protected UIFactory $ui_factory, protected UIRenderer $ui_renderer)
Definition: Factory.php:34
getAttemptResultsFor(ResultPresentationSettings $settings, \ilObjTest $test_obj, int $active_id, int $attempt_id, bool $is_user_output)
Definition: Factory.php:169
addAttemptOverviewInformationToParticipants(ResultPresentationSettings $settings, \ilObjTest $test_obj, array $participants)
Definition: Factory.php:137