ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Renderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\UI\Renderer as RendererInterface;
27
33{
37 public function render(Component\Component $component, RendererInterface $default_renderer): string
38 {
39 if ($component instanceof Component\Chart\ProgressMeter\FixedSize) {
40 return $this->renderFixedSize($component);
41 } elseif ($component instanceof Mini) {
42 return $this->renderMini($component);
43 } elseif ($component instanceof Component\Chart\ProgressMeter\Standard) {
44 return $this->renderStandard($component);
45 }
46
47 $this->cannotHandleComponent($component);
48 }
49
53 protected function renderStandard(Component\Chart\ProgressMeter\Standard $component): string
54 {
55 $hasComparison = ($component->getComparison() != null && $component->getComparison() > 0);
56 $tpl = $this->getTemplate("tpl.progressmeter.html", true, true);
57
58 $tpl = $this->getDefaultGraphicByComponent($component, $tpl, $hasComparison);
59
60 return $tpl->get();
61 }
62
66 protected function renderFixedSize(Component\Chart\ProgressMeter\FixedSize $component): string
67 {
68 $hasComparison = ($component->getComparison() != null && $component->getComparison() > 0);
69 $tpl = $this->getTemplate("tpl.progressmeter.html", true, true);
70
71 $tpl->setCurrentBlock('fixed');
72 $tpl->setVariable('FIXED_CLASS', 'fixed-size');
73 $tpl->parseCurrentBlock();
74
75 $tpl = $this->getDefaultGraphicByComponent($component, $tpl, $hasComparison);
76
77 return $tpl->get();
78 }
79
83 protected function renderMini(Mini $component): string
84 {
85 $tpl = $this->getTemplate("tpl.progressmeter_mini.html", true, true);
86
87 $main_percentage = $component->getMainValueAsPercent();
88
89 // set progress bar color class
90 $color_class = 'no-success';
91 if ($this->getIsReached($main_percentage, $component->getRequiredAsPercent())) {
92 $color_class = 'success';
93 }
94 $tpl->setVariable('COLOR_ONE_CLASS', $color_class);
95 // set width for process bars
96 $tpl->setVariable('BAR_ONE_WIDTH', (86.5 * ($main_percentage / 100)));
97 // set marker position
98 $needle_class = 'no-needle';
99 if ($component->getRequired() != $component->getMaximum()) {
100 $needle_class = '';
101 $tpl->setVariable('ROTATE_ONE', $this->getMarkerPos($component->getRequiredAsPercent()));
102 }
103 $tpl->setVariable('NEEDLE_ONE_CLASS', $needle_class);
104
105 $tpl->parseCurrentBlock();
106
107 return $tpl->get();
108 }
109
112 Template $tpl,
113 $hasComparison = false
114 ): Template {
115 $main_percentage = $component->getMainValueAsPercent();
116 // in some cases, bar needs be adjusted because of a large rounded linecap to visually hit 50% mark exactly
117 $visual_percentage = $main_percentage;
118
119 if ($hasComparison) {
120 // multicircle
121 $tpl->setCurrentBlock('multicircle');
122 // set first progress bar color class
123 $color_one_class = 'no-success';
124 if ($this->getIsReached($main_percentage, $component->getRequiredAsPercent())) {
125 $color_one_class = 'success';
126 }
127 $tpl->setVariable('COLOR_ONE_CLASS', $color_one_class);
128 // set width for first process bar
129 $tpl->setVariable('BAR_ONE_WIDTH', $visual_percentage);
130
131 // set second progress bar color class
132 $color_two_class = 'active';
133 if (!$this->getIsValueSet($component->getMainValueAsPercent()) && $this->getIsValueSet($component->getComparison())) {
134 $color_two_class = 'not-active';
135 }
136 $tpl->setVariable('COLOR_TWO_CLASS', $color_two_class);
137 // set width for second process bar
138 $tpl->setVariable('BAR_TWO_WIDTH', (88.8 * ($component->getComparisonAsPercent() / 100)));
139
140 $tpl->parseCurrentBlock();
141 } else {
142 // monocircle
143 $tpl->setCurrentBlock('monocircle');
144 // because line endcap is so large, bar value needs to be shorter to visually hit 50% mark
145 $visual_percentage = $main_percentage - 4;
146 if ($visual_percentage < 0) {
147 $visual_percentage = 0;
148 }
149 // set progress bar color class
150 $color_class = 'no-success';
151 if ($this->getIsReached($main_percentage, $component->getRequiredAsPercent())) {
152 $color_class = 'success';
153 }
154 $tpl->setVariable('COLOR_ONE_CLASS', $color_class);
155 // set width for process bars
156 $tpl->setVariable('BAR_ONE_WIDTH', $visual_percentage);
157
158 $tpl->parseCurrentBlock();
159 }
160
161 // set visible values
162 $tpl = $this->modifyVisibleValues($tpl, $component);
163
164 // set marker position
165 $needle_class = 'no-needle';
166 if ($component->getRequired() != $component->getMaximum()) {
167 $needle_class = '';
168 $needle_value = (270 / 100 * $component->getRequiredAsPercent() - 133);
169 // because meter is squashed, rotation needs to be gradually modified the further away we get from 0 degrees
170 // 0 degress is the 50% mark at which this compensation equals 0
171 $compensated_needle_value = $needle_value - (7 * (1 - (2 * $main_percentage / 100)));
172 $tpl->setVariable('ROTATE_ONE', $compensated_needle_value);
173 }
174 $tpl->setVariable('NEEDLE_ONE_CLASS', $needle_class);
175
176 $tpl->parseCurrentBlock();
177
178 return $tpl;
179 }
180
184 protected function modifyVisibleValues(Template $tpl, Component\Component $component): Template
185 {
186 $tpl->setVariable("MAIN", $component->getMainValueAsPercent() . ' %');
187 if ($component->getRequired() != $component->getMaximum()) {
188 $tpl->setVariable("REQUIRED", $component->getRequiredAsPercent() . ' %');
189 } else {
190 $tpl->setVariable("REQUIRED", '');
191 }
192
193 $main_text = '';
194 if (!is_null($component->getMainText())) {
195 $main_text = $component->getMainText();
196 }
197
198 $required_text = '';
199 if (!is_null($component->getRequiredText())) {
200 $required_text = $component->getRequiredText();
201 }
202 $tpl->setVariable("TEXT_MAIN", htmlspecialchars($main_text));
203 $tpl->setVariable("TEXT_REQUIRED", htmlspecialchars($required_text));
204 return $tpl;
205 }
206
213 protected function getMarkerPos(int $percentage): float
214 {
215 $needle_value = round((230 / 100 * ($percentage * 1)) - 115, 2, PHP_ROUND_HALF_UP);
216 $compensated_needle_value = $needle_value - (16 * (1 - (2 * $percentage / 100)));
217 return $compensated_needle_value;
218 }
219
225 protected function getIsValueSet($val): bool
226 {
227 return (isset($val) && $val > 0);
228 }
229
239 protected function getIsReached($a_val, $b_val): bool
240 {
241 return ($a_val >= $b_val);
242 }
243}
getMarkerPos(int $percentage)
get marker position by percent
Definition: Renderer.php:213
renderFixedSize(Component\Chart\ProgressMeter\FixedSize $component)
Render fixed size progressmeter.
Definition: Renderer.php:66
render(Component\Component $component, RendererInterface $default_renderer)
@inheritdocs
Definition: Renderer.php:37
getDefaultGraphicByComponent(Component\Chart\ProgressMeter\ProgressMeter $component, Template $tpl, $hasComparison=false)
Definition: Renderer.php:110
renderMini(Mini $component)
Render mini progressmeter.
Definition: Renderer.php:83
modifyVisibleValues(Template $tpl, Component\Component $component)
Modify visible template variables.
Definition: Renderer.php:184
getIsReached($a_val, $b_val)
Test if $a_val has reached $b_val.
Definition: Renderer.php:239
renderStandard(Component\Chart\ProgressMeter\Standard $component)
Render standard progressmeter.
Definition: Renderer.php:53
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
Interface to templating as it is used in the UI framework.
Definition: Template.php:29
setVariable(string $name, $value)
Set a variable in the current block.
setCurrentBlock(string $name)
Set the block to work on.
parseCurrentBlock()
Parse the block that is currently worked on.
An entity that renders components to a string output.
Definition: Renderer.php:31