ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilTestGradingMessageBuilder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private ilTemplate $tpl;
30  private array $resultData;
31  private int $activeId;
32 
36  private array $messageText = [];
37 
38  public function __construct(
39  private ilLanguage $lng,
40  private ilGlobalTemplateInterface $main_tpl,
41  private ilObjTest $testOBJ
42  ) {
43  }
44 
45  public function setActiveId($activeId)
46  {
47  $this->activeId = $activeId;
48  }
49 
50  public function getActiveId(): int
51  {
52  return $this->activeId;
53  }
54 
55  public function buildMessage()
56  {
57  $this->loadResultData();
58 
59  if ($this->testOBJ->isShowGradingStatusEnabled()) {
60  $this->addMessagePart($this->buildGradingStatusMsg());
61  }
62 
63  if ($this->testOBJ->isShowGradingMarkEnabled()) {
64  $this->addMessagePart($this->buildGradingMarkMsg());
65  }
66  }
67 
68  private function addMessagePart($msgPart)
69  {
70  $this->messageText[] = $msgPart;
71  }
72 
73  private function getFullMessage(): string
74  {
75  return implode(' ', $this->messageText);
76  }
77 
78  private function isPassed(): bool
79  {
80  return (bool) $this->resultData['passed'];
81  }
82 
83  public function sendMessage()
84  {
85  if (!$this->testOBJ->isShowGradingStatusEnabled()) {
86  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
87  } elseif ($this->isPassed()) {
88  $this->main_tpl->setOnScreenMessage('success', $this->getFullMessage());
89  } else {
90  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
91  }
92  }
93 
94  private function loadResultData()
95  {
96  $this->resultData = $this->testOBJ->getResultsForActiveId($this->getActiveId());
97  }
98 
99  private function buildGradingStatusMsg(): string
100  {
101  if ($this->isPassed()) {
102  return $this->lng->txt('grading_status_passed_msg');
103  }
104 
105  return $this->lng->txt('grading_status_failed_msg');
106  }
107 
108  private function buildGradingMarkMsg()
109  {
110  $markMsg = $this->lng->txt('grading_mark_msg');
111 
112  $markMsg = str_replace("[mark]", $this->getMarkOfficial(), $markMsg);
113  $markMsg = str_replace("[markshort]", $this->getMarkShort(), $markMsg);
114  $markMsg = str_replace("[percentage]", $this->getPercentage(), $markMsg);
115  $markMsg = str_replace("[reached]", (string) $this->getReachedPoints(), $markMsg);
116  $markMsg = str_replace("[max]", (string) $this->getMaxPoints(), $markMsg);
117 
118  return $markMsg;
119  }
120 
121  private function getMarkOfficial()
122  {
123  return $this->resultData['mark_official'];
124  }
125 
126  private function getMarkShort()
127  {
128  return $this->resultData['mark_short'];
129  }
130 
131  private function getPercentage(): string
132  {
133  $percentage = 0;
134 
135  if ($this->getMaxPoints() > 0) {
136  $percentage = $this->getReachedPoints() / $this->getMaxPoints();
137  }
138 
139  return sprintf("%.2f", $percentage);
140  }
141 
142  private function getReachedPoints()
143  {
144  return $this->resultData['reached_points'];
145  }
146 
147  private function getMaxPoints()
148  {
149  return $this->resultData['max_points'];
150  }
151 
152  public function buildList()
153  {
154  $this->loadResultData();
155 
156  $this->initListTemplate();
157 
158  if ($this->testOBJ->isShowGradingStatusEnabled()) {
159  $passedStatusLangVar = $this->isPassed() ? 'passed_official' : 'failed_official';
160 
161  $this->populateListEntry(
162  $this->lng->txt('passed_status'),
163  $this->lng->txt($passedStatusLangVar)
164  );
165  }
166 
167  if ($this->testOBJ->isShowGradingMarkEnabled()) {
168  $this->populateListEntry($this->lng->txt('tst_mark'), $this->getMarkOfficial());
169  }
170 
171  $this->parseListTemplate();
172  }
173 
174  public function initListTemplate()
175  {
176  $this->tpl = new ilTemplate('tpl.tst_grading_msg_list.html', true, true, 'components/ILIAS/Test');
177  }
178 
179  private function populateListEntry($label, $value)
180  {
181  $this->tpl->setCurrentBlock('grading_msg_entry');
182  $this->tpl->setVariable('LABEL', $label);
183  $this->tpl->setVariable('VALUE', $value);
184  $this->tpl->parseCurrentBlock();
185  }
186 
187  private function parseListTemplate()
188  {
189  $this->tpl->setCurrentBlock('grading_msg_list');
190  $this->tpl->parseCurrentBlock();
191  }
192 
193  public function getList(): string
194  {
195  return $this->tpl->get();
196  }
197 }
__construct(private ilLanguage $lng, private ilGlobalTemplateInterface $main_tpl, private ilObjTest $testOBJ)
global $lng
Definition: privfeed.php:31