ILIAS  release_8 Revision v8.24
class.ilTestGradingMessageBuilder.php
Go to the documentation of this file.
1<?php
2
26{
30 private $lng;
31
35 private $testOBJ;
36
37 private $tpl;
38
42 private $resultData;
43
47 private $activeId;
51 private $container;
52
56 private array $messageText = [];
57
63 {
64 global $DIC;
65 $this->container = $DIC;
66 $this->lng = $lng;
67 $this->testOBJ = $testOBJ;
68 }
69
70 public function setActiveId($activeId)
71 {
72 $this->activeId = $activeId;
73 }
74
75 public function getActiveId(): int
76 {
77 return $this->activeId;
78 }
79
80 public function buildMessage()
81 {
82 $this->loadResultData();
83
84 if ($this->testOBJ->isShowGradingStatusEnabled()) {
86 }
87
88 if ($this->testOBJ->isShowGradingMarkEnabled()) {
89 $this->addMessagePart($this->buildGradingMarkMsg());
90 }
91
92 if ($this->testOBJ->getECTSOutput()) {
93 $this->addMessagePart($this->buildEctsGradeMsg());
94 }
95 }
96
97 private function addMessagePart($msgPart)
98 {
99 $this->messageText[] = $msgPart;
100 }
101
102 private function getFullMessage(): string
103 {
104 return implode(' ', $this->messageText);
105 }
106
107 private function isPassed(): bool
108 {
109 return (bool) $this->resultData['passed'];
110 }
111
112 public function sendMessage()
113 {
114 if (!$this->testOBJ->isShowGradingStatusEnabled()) {
115 $this->container->ui()->mainTemplate()->setOnScreenMessage('info', $this->getFullMessage());
116 } elseif ($this->isPassed()) {
117 $this->container->ui()->mainTemplate()->setOnScreenMessage('success', $this->getFullMessage());
118 } else {
119 $this->container->ui()->mainTemplate()->setOnScreenMessage('info', $this->getFullMessage());
120 }
121 }
122
123 private function loadResultData()
124 {
125 $this->resultData = $this->testOBJ->getResultsForActiveId($this->getActiveId());
126
127 if ($this->testOBJ->getECTSOutput()) {
128 $ectsMark = $this->testOBJ->getECTSGrade(
129 $this->testOBJ->getTotalPointsPassedArray(),
130 $this->resultData['reached_points'],
131 $this->resultData['max_points']
132 );
133
134 $this->resultData['ects_grade'] = strtoupper($ectsMark);
135 }
136 }
137
138 private function buildGradingStatusMsg(): string
139 {
140 if ($this->isPassed()) {
141 return $this->lng->txt('grading_status_passed_msg');
142 }
143
144 return $this->lng->txt('grading_status_failed_msg');
145 }
146
147 private function buildGradingMarkMsg()
148 {
149 $markMsg = $this->lng->txt('grading_mark_msg');
150
151 $markMsg = str_replace("[mark]", $this->getMarkOfficial(), $markMsg);
152 $markMsg = str_replace("[markshort]", $this->getMarkShort(), $markMsg);
153 $markMsg = str_replace("[percentage]", $this->getPercentage(), $markMsg);
154 $markMsg = str_replace("[reached]", $this->getReachedPoints(), $markMsg);
155 $markMsg = str_replace("[max]", $this->getMaxPoints(), $markMsg);
156
157 return $markMsg;
158 }
159
160 private function getMarkOfficial()
161 {
162 return $this->resultData['mark_official'];
163 }
164
165 private function getMarkShort()
166 {
167 return $this->resultData['mark_short'];
168 }
169
170 private function getPercentage(): string
171 {
172 $percentage = 0;
173
174 if ($this->getMaxPoints() > 0) {
175 $percentage = $this->getReachedPoints() / $this->getMaxPoints();
176 }
177
178 return sprintf("%.2f", $percentage);
179 }
180
181 private function getReachedPoints()
182 {
183 return $this->resultData['reached_points'];
184 }
185
186 private function getMaxPoints()
187 {
188 return $this->resultData['max_points'];
189 }
190
191 private function areObligationsAnswered(): bool
192 {
193 return (bool) $this->resultData['obligations_answered'];
194 }
195
196 private function buildEctsGradeMsg()
197 {
198 return str_replace('[markects]', $this->getEctsGrade(), $this->lng->txt('mark_tst_ects'));
199 }
200
201 private function getEctsGrade()
202 {
203 return $this->resultData['ects_grade'];
204 }
205
206 public function buildList()
207 {
208 $this->loadResultData();
209
210 $this->initListTemplate();
211
212 if ($this->testOBJ->isShowGradingStatusEnabled()) {
213 $passedStatusLangVar = $this->isPassed() ? 'passed_official' : 'failed_official';
214
215 $this->populateListEntry(
216 $this->lng->txt('passed_status'),
217 $this->lng->txt($passedStatusLangVar)
218 );
219 }
220
221 if ($this->testOBJ->areObligationsEnabled()) {
222 if ($this->areObligationsAnswered()) {
223 $obligAnsweredStatusLangVar = 'grading_obligations_answered_listentry';
224 } else {
225 $obligAnsweredStatusLangVar = 'grading_obligations_missing_listentry';
226 }
227
228 $this->populateListEntry(
229 $this->lng->txt('grading_obligations_listlabel'),
230 $this->lng->txt($obligAnsweredStatusLangVar)
231 );
232 }
233
234 if ($this->testOBJ->isShowGradingMarkEnabled()) {
235 $this->populateListEntry($this->lng->txt('tst_mark'), $this->getMarkOfficial());
236 }
237
238 if ($this->testOBJ->getECTSOutput()) {
239 $this->populateListEntry($this->lng->txt('ects_grade'), $this->getEctsGrade());
240 }
241
242 $this->parseListTemplate();
243 }
244
245 public function initListTemplate()
246 {
247 $this->tpl = new ilTemplate('tpl.tst_grading_msg_list.html', true, true, 'Modules/Test');
248 }
249
250 private function populateListEntry($label, $value)
251 {
252 $this->tpl->setCurrentBlock('grading_msg_entry');
253 $this->tpl->setVariable('LABEL', $label);
254 $this->tpl->setVariable('VALUE', $value);
255 $this->tpl->parseCurrentBlock();
256 }
257
258 private function parseListTemplate()
259 {
260 $this->tpl->setCurrentBlock('grading_msg_list');
261 $this->tpl->parseCurrentBlock();
262 }
263
264 public function getList(): string
265 {
266 return $this->tpl->get();
267 }
268}
language handling
special template class to simplify handling of ITX/PEAR
__construct(ilLanguage $lng, ilObjTest $testOBJ)
global $DIC
Definition: feed.php:28