ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilPollAnswerTableGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
28 {
29  public function __construct(?object $a_parent_obj, string $a_parent_cmd)
30  {
31  global $DIC;
32 
33  $this->ctrl = $DIC->ctrl();
34  $this->lng = $DIC->language();
35 
36  $this->setId("ilobjpollaw");
37 
38  parent::__construct($a_parent_obj, $a_parent_cmd);
39 
40  $this->addColumn($this->lng->txt("poll_sortorder"), "pos");
41  $this->addColumn($this->lng->txt("poll_answer"), "answer");
42  $this->addColumn($this->lng->txt("poll_absolute"), "votes");
43  $this->addColumn($this->lng->txt("poll_percentage"), "percentage");
44 
45  $total = $this->getItems();
46 
47  $this->setTitle(
48  $this->lng->txt("poll_question") . ": \"" .
49  $a_parent_obj->getObject()->getQuestion() . "\""
50  );
51  $this->setDescription(sprintf($this->lng->txt("poll_population"), $total));
52 
53  $this->setRowTemplate("tpl.answer_row.html", "components/ILIAS/Poll");
54  $this->setDefaultOrderField("pos");
55  $this->setDefaultOrderDirection("asc");
56 
57  $this->setExportFormats(array(self::EXPORT_CSV, self::EXPORT_EXCEL));
58  }
59 
60  public function numericOrdering(string $a_field): bool
61  {
62  return $a_field !== "answer";
63  }
64 
65  public function getItems(): int
66  {
67  $data = $this->parent_obj->getObject()->getAnswers();
68  $perc = $this->parent_obj->getObject()->getVotePercentages();
69  $total = (int) ($perc["total"] ?? 0);
70  $perc = (array) ($perc["perc"] ?? []);
71 
72  // add current percentages
73  foreach ($data as $idx => $item) {
74  $item_id = (int) ($item['id'] ?? 0);
75  if (!isset($perc[$item_id])) {
76  $data[$idx]["percentage"] = 0;
77  $data[$idx]["votes"] = 0;
78  } else {
79  $data[$idx]["percentage"] = round((float) ($perc[$item_id]["perc"] ?? 0));
80  $data[$idx]["votes"] = (int) ($perc[$item_id]["abs"] ?? 0);
81  }
82  }
83 
84  $this->setData($data);
85 
86  return $total;
87  }
88 
89  protected function fillRow(array $a_set): void
90  {
91  $this->tpl->setVariable("VALUE_POS", (int) ($a_set["pos"] ?? 10) / 10);
92  $this->tpl->setVariable("TXT_ANSWER", nl2br((string) ($a_set["answer"] ?? '')));
93  $this->tpl->setVariable("VALUE_VOTES", (int) ($a_set["votes"] ?? 0));
94  $this->tpl->setVariable("VALUE_PERCENTAGE", (int) ($a_set["percentage"] ?? 0));
95  }
96 
97  protected function fillRowCSV(ilCSVWriter $a_csv, array $a_set): void
98  {
99  $a_csv->addColumn((string) ((int) ($a_set["pos"] ?? 10) / 10));
100  $a_csv->addColumn((string) ($a_set["answer"] ?? ''));
101  $a_csv->addColumn((string) ((int) ($a_set["votes"] ?? 0)));
102  $a_csv->addColumn((string) ((int) ($a_set["percentage"] ?? 0)));
103  $a_csv->addRow();
104  }
105 
106  protected function fillRowExcel(ilExcel $a_excel, int &$a_row, array $a_set): void
107  {
108  $a_excel->setCell($a_row, 0, (int) ($a_set["pos"] ?? 10) / 10);
109  $a_excel->setCell($a_row, 1, (string) ($a_set["answer"] ?? ''));
110  $a_excel->setCell($a_row, 2, (int) ($a_set["votes"] ?? 0));
111  $a_excel->setCell($a_row, 3, (int) ($a_set["percentage"] ?? 0) . "%");
112  }
113 }
setData(array $a_data)
addColumn(string $a_col)
setExportFormats(array $formats)
Set available export formats.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(?object $a_parent_obj, string $a_parent_cmd)
setDescription(string $a_val)
setId(string $a_val)
fillRowExcel(ilExcel $a_excel, int &$a_row, array $a_set)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCell(int $a_row, int $col, $value, ?string $datatype=null, bool $disable_strip_tags_for_strings=false)
Set cell value.
setDefaultOrderField(string $a_defaultorderfield)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
global $DIC
Definition: shib_login.php:25
setDefaultOrderDirection(string $a_defaultorderdirection)
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
__construct(Container $dic, ilPlugin $plugin)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
fillRowCSV(ilCSVWriter $a_csv, array $a_set)