ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollResultsRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 {
24  protected int $ref_id;
25 
26  public function __construct(
27  int $ref_id
28  ) {
29  $this->ref_id = $ref_id;
30  }
31 
32  public function render(
33  ilTemplate $tpl,
35  int $presentation_mode
36  ): void {
37  if ($presentation_mode === ilObjPoll::SHOW_RESULTS_AS_PIECHART) {
38  $this->renderPieChart($tpl, $results);
39  } else {
40  $this->renderBarChart($tpl, $results);
41  }
42  }
43 
44  protected function renderPieChart(
45  ilTemplate $tpl,
47  ): void {
48  $chart = $this->getPieChart();
49  $chart->setSize("400", "200");
50  $chart->setAutoResize(true);
51 
52  $chart_data = $chart->getDataInstance();
53 
54  foreach ($results->getOrderedAnswerIds() as $id) {
55  $chart_data->addPiePoint(
56  (int) round($results->getAnswerPercentage($id)),
57  $this->specialCharsAsEntities(nl2br($results->getAnswerText($id)))
58  );
59  }
60 
61  $chart->addData($chart_data);
62 
63  $pie_legend_id = "poll_legend_" . $this->ref_id;
64  $legend = $this->getLegend();
65  $legend->setContainer($pie_legend_id);
66  $chart->setLegend($legend);
67 
68  $tpl->setVariable("PIE_LEGEND_ID", $pie_legend_id);
69  $tpl->setVariable("PIE_CHART", $chart->getHTML());
70  }
71 
72  protected function renderBarChart(
73  ilTemplate $tpl,
75  ): void {
76  $tpl->setCurrentBlock("answer_result");
77  foreach ($results->getOrderedAnswerIds() as $id) {
78  $pbar = $this->getProgressBar();
79  $pbar->setCurrent(round($results->getAnswerPercentage($id)));
80  $pbar->setCaption('(' . $results->getAnswerTotal($id) . ')');
81  $tpl->setVariable("PERC_ANSWER_RESULT", $pbar->render());
82  $tpl->setVariable(
83  "TXT_ANSWER_RESULT",
84  $this->specialCharsAsEntities(nl2br($results->getAnswerText($id)))
85  );
86  $tpl->parseCurrentBlock();
87  }
88  }
89 
90  protected function getLegend(): ilChartLegend
91  {
92  return new ilChartLegend();
93  }
94 
95  protected function getPieChart(): ilChart
96  {
99  "poll_results_pie_" . $this->ref_id
100  );
101  }
102 
103  protected function getProgressBar(): ilProgressBar
104  {
106  }
107 
108  protected function specialCharsAsEntities(string $string): string
109  {
110  // Should be replaced by a proper refinery transformation once https://github.com/ILIAS-eLearning/ILIAS/pull/6314 is merged
111  return htmlspecialchars(
112  $string,
113  ENT_QUOTES | ENT_SUBSTITUTE,
114  'utf-8'
115  );
116  }
117 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const SHOW_RESULTS_AS_PIECHART
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const TYPE_PIE
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
renderPieChart(ilTemplate $tpl, ilPollResultsHandler $results)
renderBarChart(ilTemplate $tpl, ilPollResultsHandler $results)
$results
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
render(ilTemplate $tpl, ilPollResultsHandler $results, int $presentation_mode)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByType(int $a_type, string $a_id)