ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPollDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 class ilPollDataSet extends ilDataSet
31 {
32  protected \ILIAS\Notes\Service $notes;
33 
34  public function __construct()
35  {
36  global $DIC;
37 
39  $this->notes = $DIC->notes();
40  }
41 
42  public function getSupportedVersions(): array
43  {
44  return array("4.3.0", "5.0.0");
45  }
46 
47  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
48  {
49  return "http://www.ilias.de/xml/Modules/Poll/" . $a_entity;
50  }
51 
55  protected function getTypes(string $a_entity, string $a_version): array
56  {
57  if ($a_entity === "poll") {
58  switch ($a_version) {
59  case "4.3.0":
60  return array(
61  "Id" => "integer",
62  "Title" => "text",
63  "Description" => "text",
64  "Question" => "text",
65  "Image" => "text",
66  "ViewResults" => "integer",
67  "Dir" => "directory"
68  );
69  break;
70  case "5.0.0":
71  return array(
72  "Id" => "integer",
73  "Title" => "text",
74  "Description" => "text",
75  "Question" => "text",
76  "Image" => "text",
77  "ViewResults" => "integer",
78  "Dir" => "directory",
79  "ShowResultsAs" => "integer",
80  "ShowComments" => "integer",
81  "MaxAnswers" => "integer",
82  "ResultSort" => "integer",
83  "NonAnon" => "integer",
84  "Period" => "integer",
85  "PeriodBegin" => "integer",
86  "PeriodEnd" => "integer"
87 
88  );
89  break;
90  }
91  }
92 
93  if ($a_entity === "poll_answer") {
94  switch ($a_version) {
95  case "4.3.0":
96  case "5.0.0":
97  return array(
98  "Id" => "integer",
99  "PollId" => "integer",
100  "Answer" => "text",
101  "Pos" => "integer",
102  );
103  break;
104  }
105  }
106 
107  return [];
108  }
109 
110  public function readData(string $a_entity, string $a_version, array $a_ids): void
111  {
112  $ilDB = $this->db;
113 
114  if ($a_entity === "poll") {
115  switch ($a_version) {
116  case "4.3.0":
117  $this->getDirectDataFromQuery("SELECT pl.id,od.title,od.description," .
118  "pl.question,pl.image,pl.view_results" .
119  " FROM il_poll pl" .
120  " JOIN object_data od ON (od.obj_id = pl.id)" .
121  " WHERE " . $ilDB->in("pl.id", $a_ids, false, "integer") .
122  " AND od.type = " . $ilDB->quote("poll", "text"));
123  break;
124  case "5.0.0":
125  $this->getDirectDataFromQuery("SELECT pl.id,od.title,od.description" .
126  ",pl.question,pl.image,pl.view_results,pl.show_results_as" .
127  ",pl.max_answers,pl.result_sort,pl.non_anon,pl.period,pl.period_begin,pl.period_end" .
128  " FROM il_poll pl" .
129  " JOIN object_data od ON (od.obj_id = pl.id)" .
130  " WHERE " . $ilDB->in("pl.id", $a_ids, false, "integer") .
131  " AND od.type = " . $ilDB->quote("poll", "text"));
132  break;
133  }
134  }
135 
136  if ($a_entity === "poll_answer") {
137  switch ($a_version) {
138  case "4.3.0":
139  case "5.0.0":
140  $this->getDirectDataFromQuery("SELECT id,poll_id,answer,pos" .
141  " FROM il_poll_answer WHERE " .
142  $ilDB->in("poll_id", $a_ids, false, "integer"));
143  break;
144  }
145  }
146  }
147 
148  protected function getDependencies(
149  string $a_entity,
150  string $a_version,
151  ?array $a_rec = null,
152  ?array $a_ids = null
153  ): array {
154  switch ($a_entity) {
155  case "poll":
156  return array(
157  "poll_answer" => array("ids" => $a_rec["Id"] ?? null)
158  );
159  }
160  return [];
161  }
162 
163  public function getXmlRecord(string $a_entity, string $a_version, array $a_set): array
164  {
165  if ($a_entity === "poll") {
166  $dir = ilObjPoll::initStorage((int) $a_set["Id"]);
167  $a_set["Dir"] = $dir;
168 
169  $a_set["ShowComments"] = $this->notes->domain()->commentsActive((int) $a_set["Id"]);
170  }
171 
172  return $a_set;
173  }
174 
175  public function importRecord(
176  string $a_entity,
177  array $a_types,
178  array $a_rec,
179  ilImportMapping $a_mapping,
180  string $a_schema_version
181  ): void {
182  $a_rec = $this->stripTags(
183  $a_rec,
184  [
185  'Id',
186  'MaxAnswers',
187  'ResultSort',
188  'NonAnon',
189  'ShowResultsAs',
190  'ShowComments',
191  'ViewResults',
192  'Period',
193  'PeriodBegin',
194  'PeriodEnd',
195  'PollId',
196  'pos',
197  ]
198  );
199 
200  switch ($a_entity) {
201  case "poll":
202  // container copy
203  if ($new_id = $a_mapping->getMapping("Services/Container", "objs", (string) ($a_rec["Id"] ?? "0"))) {
204  $newObj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
205  } else {
206  $newObj = new ilObjPoll();
207  $newObj->create();
208  }
209 
211  $newObj->setTitle((string) ($a_rec["Title"] ?? ''));
212  $newObj->setDescription((string) ($a_rec["Description"]));
213  if ((int) $a_rec["MaxAnswers"]) {
214  $newObj->setMaxNumberOfAnswers((int) $a_rec["MaxAnswers"]);
215  }
216  $newObj->setSortResultByVotes((bool) ($a_rec["ResultSort"] ?? false));
217  $newObj->setNonAnonymous((bool) ($a_rec["NonAnon"] ?? false));
218  if ((int) $a_rec["ShowResultsAs"]) {
219  $newObj->setShowResultsAs((int) $a_rec["ShowResultsAs"]);
220  }
221  $newObj->setShowComments((bool) ($a_rec["ShowComments"] ?? false));
222  $newObj->setQuestion((string) ($a_rec["Question"] ?? ''));
223  $newObj->setImage((string) ($a_rec["Image"] ?? ''));
224  $newObj->setViewResults((int) ($a_rec["ViewResults"] ?? ilObjPoll::VIEW_RESULTS_AFTER_VOTE));
225  $newObj->setVotingPeriod((bool) ($a_rec["Period"] ?? 0));
226  $newObj->setVotingPeriodBegin((int) ($a_rec["PeriodBegin"] ?? 0));
227  $newObj->setVotingPeriodEnd((int) ($a_rec["PeriodEnd"] ?? 0));
228  $newObj->update();
229 
230  // handle image(s)
231  if ($a_rec["Image"]) {
232  $dir = str_replace("..", "", (string) ($a_rec["Dir"] ?? ''));
233  if ($dir !== "" && $this->getImportDirectory() !== "") {
234  $source_dir = $this->getImportDirectory() . "/" . $dir;
235  $target_dir = ilObjPoll::initStorage($newObj->getId());
236  ilFileUtils::rCopy($source_dir, $target_dir);
237  }
238  }
239 
240  $a_mapping->addMapping("Modules/Poll", "poll", (string) ($a_rec["Id"] ?? "0"), (string) $newObj->getId());
241  break;
242 
243  case "poll_answer":
244  $poll_id = (int) $a_mapping->getMapping("Modules/Poll", "poll", (string) ($a_rec["PollId"] ?? "0"));
245  if ($poll_id) {
246  $poll = new ilObjPoll($poll_id, false);
247  $poll->saveAnswer((string) ($a_rec["Answer"] ?? ''), (int) ($a_rec["pos"] ?? 10));
248  }
249  break;
250  }
251  }
252 }
const VIEW_RESULTS_AFTER_VOTE
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getTypes(string $a_entity, string $a_version)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
ILIAS Notes Service $notes
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
ilDBInterface $db
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
getXmlRecord(string $a_entity, string $a_version, array $a_set)
getDirectDataFromQuery(string $a_query, bool $a_convert_to_leading_upper=true, bool $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
Class ilObjPoll.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static initStorage(int $a_id, ?string $a_subdir=null)
readData(string $a_entity, string $a_version, array $a_ids)
stripTags(array $rec, array $omit_keys=[])
Poll Dataset class.
getXmlNamespace(string $a_entity, string $a_schema_version)