ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestGradingMessageBuilder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private $tpl;
30 
34  private $resultData;
35 
39  private $activeId;
43  private $container;
44 
48  private array $messageText = [];
49 
54  public function __construct(
55  private ilLanguage $lng,
56  private ilGlobalTemplateInterface $main_tpl,
57  private ilObjTest $testOBJ
58  ) {
59  }
60 
61  public function setActiveId($activeId)
62  {
63  $this->activeId = $activeId;
64  }
65 
66  public function getActiveId(): int
67  {
68  return $this->activeId;
69  }
70 
71  public function buildMessage()
72  {
73  $this->loadResultData();
74 
75  if ($this->testOBJ->isShowGradingStatusEnabled()) {
76  $this->addMessagePart($this->buildGradingStatusMsg());
77  }
78 
79  if ($this->testOBJ->isShowGradingMarkEnabled()) {
80  $this->addMessagePart($this->buildGradingMarkMsg());
81  }
82  }
83 
84  private function addMessagePart($msgPart)
85  {
86  $this->messageText[] = $msgPart;
87  }
88 
89  private function getFullMessage(): string
90  {
91  return implode(' ', $this->messageText);
92  }
93 
94  private function isPassed(): bool
95  {
96  return (bool) $this->resultData['passed'];
97  }
98 
99  public function sendMessage()
100  {
101  if (!$this->testOBJ->isShowGradingStatusEnabled()) {
102  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
103  } elseif ($this->isPassed()) {
104  $this->main_tpl->setOnScreenMessage('success', $this->getFullMessage());
105  } else {
106  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
107  }
108  }
109 
110  private function loadResultData()
111  {
112  $this->resultData = $this->testOBJ->getResultsForActiveId($this->getActiveId());
113  }
114 
115  private function buildGradingStatusMsg(): string
116  {
117  if ($this->isPassed()) {
118  return $this->lng->txt('grading_status_passed_msg');
119  }
120 
121  return $this->lng->txt('grading_status_failed_msg');
122  }
123 
124  private function buildGradingMarkMsg()
125  {
126  $markMsg = $this->lng->txt('grading_mark_msg');
127 
128  $markMsg = str_replace("[mark]", $this->getMarkOfficial(), $markMsg);
129  $markMsg = str_replace("[markshort]", $this->getMarkShort(), $markMsg);
130  $markMsg = str_replace("[percentage]", $this->getPercentage(), $markMsg);
131  $markMsg = str_replace("[reached]", (string) $this->getReachedPoints(), $markMsg);
132  $markMsg = str_replace("[max]", (string) $this->getMaxPoints(), $markMsg);
133 
134  return $markMsg;
135  }
136 
137  private function getMarkOfficial()
138  {
139  return $this->resultData['mark_official'];
140  }
141 
142  private function getMarkShort()
143  {
144  return $this->resultData['mark_short'];
145  }
146 
147  private function getPercentage(): string
148  {
149  $percentage = 0;
150 
151  if ($this->getMaxPoints() > 0) {
152  $percentage = $this->getReachedPoints() / $this->getMaxPoints();
153  }
154 
155  return sprintf("%.2f", $percentage);
156  }
157 
158  private function getReachedPoints()
159  {
160  return $this->resultData['reached_points'];
161  }
162 
163  private function getMaxPoints()
164  {
165  return $this->resultData['max_points'];
166  }
167 
168  private function areObligationsAnswered(): bool
169  {
170  return (bool) $this->resultData['obligations_answered'];
171  }
172 
173  public function buildList()
174  {
175  $this->loadResultData();
176 
177  $this->initListTemplate();
178 
179  if ($this->testOBJ->isShowGradingStatusEnabled()) {
180  $passedStatusLangVar = $this->isPassed() ? 'passed_official' : 'failed_official';
181 
182  $this->populateListEntry(
183  $this->lng->txt('passed_status'),
184  $this->lng->txt($passedStatusLangVar)
185  );
186  }
187 
188  if ($this->testOBJ->areObligationsEnabled()) {
189  if ($this->areObligationsAnswered()) {
190  $obligAnsweredStatusLangVar = 'grading_obligations_answered_listentry';
191  } else {
192  $obligAnsweredStatusLangVar = 'grading_obligations_missing_listentry';
193  }
194 
195  $this->populateListEntry(
196  $this->lng->txt('grading_obligations_listlabel'),
197  $this->lng->txt($obligAnsweredStatusLangVar)
198  );
199  }
200 
201  if ($this->testOBJ->isShowGradingMarkEnabled()) {
202  $this->populateListEntry($this->lng->txt('tst_mark'), $this->getMarkOfficial());
203  }
204 
205  $this->parseListTemplate();
206  }
207 
208  public function initListTemplate()
209  {
210  $this->tpl = new ilTemplate('tpl.tst_grading_msg_list.html', true, true, 'Modules/Test');
211  }
212 
213  private function populateListEntry($label, $value)
214  {
215  $this->tpl->setCurrentBlock('grading_msg_entry');
216  $this->tpl->setVariable('LABEL', $label);
217  $this->tpl->setVariable('VALUE', $value);
218  $this->tpl->parseCurrentBlock();
219  }
220 
221  private function parseListTemplate()
222  {
223  $this->tpl->setCurrentBlock('grading_msg_list');
224  $this->tpl->parseCurrentBlock();
225  }
226 
227  public function getList(): string
228  {
229  return $this->tpl->get();
230  }
231 }
__construct(private ilLanguage $lng, private ilGlobalTemplateInterface $main_tpl, private ilObjTest $testOBJ)
$lng