ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTestGradingMessageBuilder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 {
31  private ilTemplate $tpl;
32 
36  private array $messageText = [];
37 
38  public function __construct(
39  private ilLanguage $lng,
40  private ilGlobalTemplateInterface $main_tpl,
41  private ilObjTest $testOBJ,
42  private readonly ParticipantResult $result
43  ) {
44  }
45 
46  public function buildMessage()
47  {
48  if ($this->testOBJ->isShowGradingStatusEnabled()) {
49  $this->addMessagePart($this->buildGradingStatusMsg());
50  }
51 
52  if ($this->testOBJ->isShowGradingMarkEnabled()) {
53  $this->addMessagePart($this->buildGradingMarkMsg());
54  }
55  }
56 
57  private function addMessagePart($msgPart)
58  {
59  $this->messageText[] = $msgPart;
60  }
61 
62  private function getFullMessage(): string
63  {
64  return implode(' ', $this->messageText);
65  }
66 
67  public function sendMessage()
68  {
69  if (!$this->testOBJ->isShowGradingStatusEnabled()) {
70  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
71  } elseif ($this->result->isPassed()) {
72  $this->main_tpl->setOnScreenMessage('success', $this->getFullMessage());
73  } else {
74  $this->main_tpl->setOnScreenMessage('info', $this->getFullMessage());
75  }
76  }
77 
78  private function buildGradingStatusMsg(): string
79  {
80  if ($this->result->isPassed()) {
81  return $this->lng->txt('grading_status_passed_msg');
82  }
83 
84  return $this->lng->txt('grading_status_failed_msg');
85  }
86 
87  private function buildGradingMarkMsg()
88  {
89  $mark_msg = $this->lng->txt('grading_mark_msg');
90 
91  $mark_msg = str_replace('[mark]', $this->result->getMarkOfficial(), $mark_msg);
92  $mark_msg = str_replace('[markshort]', $this->result->getMarkShort(), $mark_msg);
93  $mark_msg = str_replace('[percentage]', sprintf('%.2f', $this->result->getPercentage()), $mark_msg);
94  $mark_msg = str_replace('[reached]', (string) $this->result->getReachedPoints(), $mark_msg);
95  $mark_msg = str_replace('[max]', (string) $this->result->getMaxPoints(), $mark_msg);
96 
97  return $mark_msg;
98  }
99 
100  public function buildList()
101  {
102  $this->initListTemplate();
103 
104  if ($this->testOBJ->isShowGradingStatusEnabled()) {
105  $passedStatusLangVar = $this->result->isPassed() ? 'passed_official' : 'failed_official';
106 
107  $this->populateListEntry(
108  $this->lng->txt('passed_status'),
109  $this->lng->txt($passedStatusLangVar)
110  );
111  }
112 
113  if ($this->testOBJ->isShowGradingMarkEnabled()) {
114  $this->populateListEntry($this->lng->txt('tst_mark'), $this->result->getMarkOfficial());
115  }
116 
117  $this->parseListTemplate();
118  }
119 
120  public function initListTemplate()
121  {
122  $this->tpl = new ilTemplate('tpl.tst_grading_msg_list.html', true, true, 'components/ILIAS/Test');
123  }
124 
125  private function populateListEntry($label, $value)
126  {
127  $this->tpl->setCurrentBlock('grading_msg_entry');
128  $this->tpl->setVariable('LABEL', $label);
129  $this->tpl->setVariable('VALUE', $value);
130  $this->tpl->parseCurrentBlock();
131  }
132 
133  private function parseListTemplate()
134  {
135  $this->tpl->setCurrentBlock('grading_msg_list');
136  $this->tpl->parseCurrentBlock();
137  }
138 
139  public function getList(): string
140  {
141  return $this->tpl->get();
142  }
143 }
__construct(private ilLanguage $lng, private ilGlobalTemplateInterface $main_tpl, private ilObjTest $testOBJ, private readonly ParticipantResult $result)
Class ParticipantResult is a model representation of an entry in the test_result_cache table...
global $lng
Definition: privfeed.php:31