ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPollResultsRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
29 {
30  protected const float SINGLE_BAR_WIDTH = 0.65;
31  protected const string SINGLE_BAR_COLOR = '#4C6586';
32  protected const float STACKED_BAR_WIDTH = 0.95;
33  protected const array STACKED_BAR_COLORS = [
34  '#35485F',
35  '#F06B05',
36  '#374E1D',
37  '#A18BB6',
38  '#2C2C2C',
39  '#3D9FAE',
40  '#663D00',
41  '#F75E82'
42  ];
43 
44  protected int $ref_id;
45  protected Refinery $refinery;
49  protected ilLanguage $lng;
50 
51  public function __construct(
52  int $ref_id,
53  Refinery $refinery,
54  DataFactory $data_factory,
55  UIFactory $ui_factory,
56  UIRenderer $ui_renderer,
57  ilLanguage $lng
58  ) {
59  $this->ref_id = $ref_id;
60  $this->refinery = $refinery;
61  $this->data_factory = $data_factory;
62  $this->ui_factory = $ui_factory;
63  $this->ui_renderer = $ui_renderer;
64  $this->lng = $lng;
65  }
66 
67  public function render(
68  ilTemplate $tpl,
70  int $presentation_mode
71  ): void {
72  if ($presentation_mode === ilObjPoll::SHOW_RESULTS_AS_STACKED_CHART) {
73  $this->renderStackedChart($tpl, $results);
74  } else {
75  $this->renderBarChart($tpl, $results);
76  }
77  }
78 
79  protected function renderStackedChart(
80  ilTemplate $tpl,
82  ): void {
83  $votes_label = $this->lng->txt('poll_chart_votes');
84  $c_dimension = $this->data_factory->dimension()->cardinal();
85 
86  $dimensions = [];
87  $data_values = [];
88  $bar_configs = [];
89  $tooltips = [];
90  $color_index = 0;
91  foreach ($results->getOrderedAnswerIds() as $id) {
92  $label = $this->htmlSpecialCharsAsEntities($results->getAnswerText($id));
93  $total_votes = $results->getAnswerTotal($id);
94  $tooltip = $total_votes . ' (' . round($results->getAnswerPercentage($id)) . '%)';
95  $bar_config = new BarConfig();
96 
97  $dimensions[$label] = $c_dimension;
98  $data_values[$label] = $total_votes;
99  $bar_configs[$label] = $bar_config->withColor(
100  $this->data_factory->color(self::STACKED_BAR_COLORS[$color_index])
101  )->withRelativeWidth(self::STACKED_BAR_WIDTH);
102  $tooltips[$label] = $tooltip;
103 
104  $color_index = ($color_index + 1) >= count(self::STACKED_BAR_COLORS) ? 0 : ($color_index + 1);
105  }
106 
107  $dimension_group = $this->data_factory->dimension()->group();
108  $dataset = $this->data_factory->dataset(
109  $dimensions,
110  ['stacked' => $this->data_factory->dimension()->group(...array_keys($dimensions))]
111  )->withPoint(
112  $votes_label,
113  $data_values
114  )->withAlternativeInformation(
115  $votes_label,
116  $tooltips
117  );
118 
119  $group_config = new GroupConfig();
120  $group_config = $group_config->withStacked(true);
121  $chart = $this->ui_factory->chart()->bar()->horizontal('', $dataset)
122  ->withTitleVisible(false)
123  ->withLegendVisible(true)
124  ->withBarConfigs($bar_configs)
125  ->withGroupConfigs(['stacked' => $group_config]);
126  $tpl->setVariable('CHART', $this->ui_renderer->render($chart));
127  }
128 
129  protected function renderBarChart(
130  ilTemplate $tpl,
132  ): void {
133  $votes_label = $this->lng->txt('poll_chart_votes');
134  $c_dimension = $this->data_factory->dimension()->cardinal();
135  $dataset = $this->data_factory->dataset([$votes_label => $c_dimension]);
136 
137  foreach ($results->getOrderedAnswerIds() as $id) {
138  $label = $this->htmlSpecialCharsAsEntities($results->getAnswerText($id));
139  $total_votes = $results->getAnswerTotal($id);
140  $tooltip = $total_votes . ' (' . round($results->getAnswerPercentage($id)) . '%)';
141  $dataset = $dataset->withPoint($label, [$votes_label => $total_votes])
142  ->withAlternativeInformation($label, [$votes_label => $tooltip]);
143  }
144 
145  $bar_config = new BarConfig();
146  $bar_config = $bar_config->withColor($this->data_factory->color(self::SINGLE_BAR_COLOR))
147  ->withRelativeWidth(self::SINGLE_BAR_WIDTH);
148  $chart = $this->ui_factory->chart()->bar()->horizontal('', $dataset)
149  ->withTitleVisible(false)
150  ->withLegendVisible(false)
151  ->withBarConfigs([$votes_label => $bar_config]);
152  $tpl->setVariable('CHART', $this->ui_renderer->render($chart));
153  }
154 
155  protected function htmlSpecialCharsAsEntities(string $string): string
156  {
157  return $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform(nl2br($string));
158  }
159 }
const int SHOW_RESULTS_AS_STACKED_CHART
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
renderStackedChart(ilTemplate $tpl, ilPollResultsHandler $results)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
renderBarChart(ilTemplate $tpl, ilPollResultsHandler $results)
__construct(int $ref_id, Refinery $refinery, DataFactory $data_factory, UIFactory $ui_factory, UIRenderer $ui_renderer, ilLanguage $lng)
$results
render(ilTemplate $tpl, ilPollResultsHandler $results, int $presentation_mode)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23