ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilExcelTestExport.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
27 
28  public function __construct(
29  ilObjTest $test_obj,
30  string $filter_key_participants = ilTestEvaluationData::FILTER_BY_NONE,
31  string $filtertext = '',
32  bool $passedonly = false,
33  bool $scoredonly = true,
34  ilLanguage $lng = null
35  ) {
36  $this->scoredonly = $scoredonly;
37  $this->worksheet = new ilAssExcelFormatHelper();
38  parent::__construct($test_obj, $filter_key_participants, $filtertext, $passedonly, $scoredonly, $lng);
39  }
40 
41  public function withResultsPage(): self
42  {
43  $this->worksheet->addSheet($this->lng->txt('tst_results'));
44 
45  $row = 1;
46  $header_row = $this->getHeaderRow($this->lng, $this->test_obj);
47  foreach ($header_row as $col => $value) {
48  $this->worksheet->setFormattedExcelTitle($this->worksheet->getColumnCoord($col) . $row, $value);
49  }
50  $this->worksheet->setBold('A' . $row . ':' . $this->worksheet->getColumnCoord(count($header_row) + 1) . $row);
51 
52  $datarows = $this->getDatarows($this->test_obj);
53  foreach ($datarows as $row => $data) {
54  if ($this->test_obj->isRandomTest()) {
55  if ($row % 2 === 0) {
56  foreach ($data as $col => $value) {
57  if ($value !== "" && $col >= count($header_row)) {
58  $this->worksheet->setFormattedExcelTitle(
59  $this->worksheet->getColumnCoord($col) . $row + 1,
60  $value
61  );
62  } else {
63  $this->worksheet->setCellByCoordinates($this->worksheet->getColumnCoord($col) . $row + 1, $value);
64  }
65  }
66  } elseif ($row % 2 === 1) {
67  if ($row !== 1) {
68  for ($col = 0, $colMax = count($header_row); $col < $colMax - 1; $col++) {
69  $data[$col] = "";
70  }
71  }
72  foreach ($data as $col => $value) {
73  $this->worksheet->setCellByCoordinates($this->worksheet->getColumnCoord($col) . $row + 1, $value);
74  }
75  }
76  } else {
77  foreach ($data as $col => $value) {
78  $this->worksheet->setCellByCoordinates($this->worksheet->getColumnCoord($col) . $row + 1, $value);
79  }
80  }
81  }
82 
83  return $this;
84  }
85 
87  {
88  return $this->worksheet;
89  }
90 
91  public function withUserPages(): self
92  {
93  $row = 1;
94  $usernames = [];
95  $allusersheet = false;
96  $pages = 0;
97  // test participant result export
98  $participantcount = count($this->complete_data->getParticipants());
99  foreach ($this->complete_data->getParticipants() as $active_id => $userdata) {
100  $username = (!is_null($userdata) && $userdata->getName()) ? $userdata->getName() : "ID $active_id";
101  $username = mb_substr($username, 0, 26);
102  $username_to_lower = strtolower($username);
103  if (array_key_exists($username_to_lower, $usernames)) {
104  $usernames[$username_to_lower]++;
105  $username .= " (" . $usernames[$username_to_lower] . ")";
106  } else {
107  $usernames[$username_to_lower] = 0;
108  }
109 
110  if ($participantcount > 250) {
111  if (!$allusersheet || ($pages - 1) < floor($row / 64000)) {
112  $this->worksheet->addSheet(
113  $this->lng->txt("eval_all_users") . (($pages > 0) ? " (" . ($pages + 1) . ")" : "")
114  );
115  $allusersheet = true;
116  $row = 1;
117  $pages++;
118  }
119  } else {
120  $resultsheet = $this->worksheet->addSheet($username);
121  }
122  if ($this->scoredonly) {
123  $passes = [$userdata->getScoredPassObject()];
124  } else {
125  $passes = $userdata->getPasses();
126  }
127  $col = 0;
128  foreach ($passes as $pass) {
129  $passCount = $pass->getPass();
130  $row = ($allusersheet) ? $row : 1;
131  $title = sprintf(
132  $this->lng->txt("tst_result_user_name_pass"),
133  $passCount + 1,
134  $userdata->getName()
135  ) .
136  (!$this->scoredonly && $userdata->getScoredPass() === $passCount ? " " .
137  $this->lng->txt("exp_scored_test_run") .
138  " (" . ($this->test_obj->getPassScoring() ? $this->lng->txt(
139  'tst_pass_scoring_best'
140  ) : $this->lng->txt('tst_pass_scoring_last')) . ")" : "");
141  $this->worksheet->setCell(
142  $row,
143  $col,
144  $title
145  );
146  $this->worksheet->setBold($this->worksheet->getColumnCoord($col) . $row);
147  $row += 2;
148  if (is_object($userdata) && is_array($userdata->getQuestions($passCount))) {
149  $questions = $userdata->getQuestions($passCount);
150  usort($questions, static function ($a, $b) {
151  return $a['sequence'] - $b['sequence'];
152  });
153  foreach ($questions as $question) {
154  $question = assQuestion::instantiateQuestion((int) $question["id"]);
155  if (is_object($question)) {
156  $row = $question->setExportDetailsXLSX($this->worksheet, $row, $col, $active_id, $passCount);
157  }
158  }
159  }
160  $col += 3;
161  }
162  }
163 
164  return $this;
165  }
166 
167  public function withAggregatedResultsPage(): self
168  {
169  $this->worksheet->addSheet($this->lng->txt('tst_results_aggregated'));
170 
171  $row = 1;
172  $col = 0;
173  $this->worksheet->setCell($row, $col++, $this->lng->txt('result'));
174  $this->worksheet->setCell($row, $col++, $this->lng->txt('value'));
175 
176  $this->worksheet->setBold('A' . $row . ':' . $this->worksheet->getColumnCoord($col - 1) . $row);
177 
178  $row++;
179  foreach ($this->aggregated_data['overview'] as $key => $value) {
180  $col = 0;
181  $this->worksheet->setCell($row, $col++, $key);
182  $this->worksheet->setCell($row, $col++, $value);
183  $row++;
184  }
185 
186  $row++;
187  $col = 0;
188 
189  $this->worksheet->setCell($row, $col++, $this->lng->txt('question_id'));
190  $this->worksheet->setCell($row, $col++, $this->lng->txt('question_title'));
191  $this->worksheet->setCell($row, $col++, $this->lng->txt('average_reached_points'));
192  $this->worksheet->setCell($row, $col++, $this->lng->txt('points'));
193  $this->worksheet->setCell($row, $col++, $this->lng->txt('percentage'));
194  $this->worksheet->setCell($row, $col++, $this->lng->txt('number_of_answers'));
195 
196  $this->worksheet->setBold('A' . $row . ':' . $this->worksheet->getColumnCoord($col - 1) . $row);
197 
198  $row++;
199  foreach ($this->aggregated_data['questions'] as $key => $value) {
200  $col = 0;
201  $this->worksheet->setCell($row, $col++, $key);
202  $this->worksheet->setCell($row, $col++, $value[0]);
203  $this->worksheet->setCell($row, $col++, $value[4]);
204  $this->worksheet->setCell($row, $col++, $value[5]);
205  $this->worksheet->setCell($row, $col++, $value[6]);
206  $this->worksheet->setCell($row, $col++, $value[3]);
207  $row++;
208  }
209 
210  return $this;
211  }
212 
216  public function deliver(string $title): void
217  {
218  $testname = ilFileUtils::getASCIIFilename(preg_replace("/\s/", "_", $title)) . '.xlsx';
219  $this->worksheet->sendToClient($testname);
220  }
221 }
getDatarows(ilObjTest $test_obj)
ilAssExcelFormatHelper $worksheet
static getASCIIFilename(string $a_filename)
getHeaderRow(ilLanguage $lng, ilObjTest $test_obj)
static instantiateQuestion(int $question_id)
__construct(VocabulariesInterface $vocabularies)
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(ilObjTest $test_obj, string $filter_key_participants=ilTestEvaluationData::FILTER_BY_NONE, string $filtertext='', bool $passedonly=false, bool $scoredonly=true, ilLanguage $lng=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples