ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPollDataSet.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Poll\Image\I\FactoryInterface as PollImageFactoryInterface;
22use ILIAS\Poll\Image\Factory as PollImageFactory;
23use ILIAS\Data\Factory as DataFactory;
24use ILIAS\Notes\Service as NotesService;
25
36{
37 protected const string ENTITY = "poll";
38 protected NotesService $notes;
39 protected DataFactory $data_factory;
40 protected ilObjuser $user;
41 protected PollImageFactoryInterface $poll_image_factory;
42
43 public function __construct()
44 {
45 global $DIC;
46
48 $this->notes = $DIC->notes();
49 $this->data_factory = new DataFactory();
50 $this->user = $DIC->user();
51 $this->poll_image_factory = new PollImageFactory();
52 }
53
54 public function getSupportedVersions(): array
55 {
56 return array("4.3.0", "5.0.0", "10.0");
57 }
58
59 protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
60 {
61 return "http://www.ilias.de/xml/Modules/Poll/" . $a_entity;
62 }
63
67 protected function getTypes(string $a_entity, string $a_version): array
68 {
69 if ($a_entity === self::ENTITY) {
70 switch ($a_version) {
71 case "4.3.0":
72 return array(
73 "Id" => "integer",
74 "Title" => "text",
75 "Description" => "text",
76 "Question" => "text",
77 "Image" => "text",
78 "ViewResults" => "integer",
79 "Dir" => "directory"
80 );
81 break;
82 case "5.0.0":
83 return array(
84 "Id" => "integer",
85 "Title" => "text",
86 "Description" => "text",
87 "Question" => "text",
88 "Image" => "text",
89 "ViewResults" => "integer",
90 "Dir" => "directory",
91 "ShowResultsAs" => "integer",
92 "ShowComments" => "integer",
93 "MaxAnswers" => "integer",
94 "ResultSort" => "integer",
95 "NonAnon" => "integer",
96 "Period" => "integer",
97 "PeriodBegin" => "integer",
98 "PeriodEnd" => "integer"
99
100 );
101 break;
102 case "10.0":
103 return array(
104 "Id" => "integer",
105 "Title" => "text",
106 "Description" => "text",
107 "Question" => "text",
108 "Image" => "text", // Image now contains the full path, not just the name
109 "ViewResults" => "integer",
110 "ShowResultsAs" => "integer",
111 "ShowComments" => "integer",
112 "MaxAnswers" => "integer",
113 "ResultSort" => "integer",
114 "NonAnon" => "integer",
115 "Period" => "integer",
116 "PeriodBegin" => "integer",
117 "PeriodEnd" => "integer"
118 );
119 break;
120 }
121 }
122
123 if ($a_entity === "poll_answer") {
124 switch ($a_version) {
125 case "4.3.0":
126 case "5.0.0":
127 case "10.0":
128 return array(
129 "Id" => "integer",
130 "PollId" => "integer",
131 "Answer" => "text",
132 "Pos" => "integer",
133 );
134 break;
135 }
136 }
137
138 return [];
139 }
140
141 public function readData(string $a_entity, string $a_version, array $a_ids): void
142 {
144
145 if ($a_entity === self::ENTITY) {
146 switch ($a_version) {
147 case "4.3.0":
148 $this->getDirectDataFromQuery("SELECT pl.id,od.title,od.description," .
149 "pl.question,pl.image,pl.view_results" .
150 " FROM il_poll pl" .
151 " JOIN object_data od ON (od.obj_id = pl.id)" .
152 " WHERE " . $ilDB->in("pl.id", $a_ids, false, "integer") .
153 " AND od.type = " . $ilDB->quote("poll", "text"));
154 break;
155 case "5.0.0":
156 $this->getDirectDataFromQuery("SELECT pl.id,od.title,od.description" .
157 ",pl.question,pl.image,pl.view_results,pl.show_results_as" .
158 ",pl.max_answers,pl.result_sort,pl.non_anon,pl.period,pl.period_begin,pl.period_end" .
159 " FROM il_poll pl" .
160 " JOIN object_data od ON (od.obj_id = pl.id)" .
161 " WHERE " . $ilDB->in("pl.id", $a_ids, false, "integer") .
162 " AND od.type = " . $ilDB->quote("poll", "text"));
163 break;
164 case "10.0":
165 $this->getDirectDataFromQuery("SELECT pl.id,od.title,od.description" .
166 ",pl.question,pl.view_results,pl.show_results_as" .
167 ",pl.max_answers,pl.result_sort,pl.non_anon,pl.period,pl.period_begin,pl.period_end" .
168 " FROM il_poll pl" .
169 " JOIN object_data od ON (od.obj_id = pl.id)" .
170 " WHERE " . $ilDB->in("pl.id", $a_ids, false, "integer") .
171 " AND od.type = " . $ilDB->quote("poll", "text"));
172 break;
173 }
174 }
175
176 if ($a_entity === "poll_answer") {
177 switch ($a_version) {
178 case "4.3.0":
179 case "5.0.0":
180 case "10.0":
181 $this->getDirectDataFromQuery("SELECT id,poll_id,answer,pos" .
182 " FROM il_poll_answer WHERE " .
183 $ilDB->in("poll_id", $a_ids, false, "integer"));
184 break;
185 }
186 }
187 }
188
189 protected function getDependencies(
190 string $a_entity,
191 string $a_version,
192 ?array $a_rec = null,
193 ?array $a_ids = null
194 ): array {
195 switch ($a_entity) {
196 case "poll":
197 return array(
198 "poll_answer" => array("ids" => $a_rec["Id"] ?? null)
199 );
200 }
201 return [];
202 }
203
204 public function getXmlRecord(string $a_entity, string $a_version, array $a_set): array
205 {
206 if ($a_entity === self::ENTITY) {
207 $resource = $this->poll_image_factory->handler()->getRessource(
208 $this->data_factory->objId((int) $a_set["Id"])
209 );
210 if ($resource !== null) {
211 $title = $resource->getTitle();
212 $path_in_container = ltrim($this->export->getExportDirInContainer(), '/') . '/image/' . $title;
213 $this->export->getExportWriter()->writeFilesByResourceId(
214 $resource->getIdentification()->serialize(),
215 $path_in_container
216 );
217 /*
218 * For some reason, the name of the import file itself is included in getImportDirectory,
219 * so has to be removed here. Might need to take this out again if anything changes.
220 */
221 $path_to_image = explode('/', $path_in_container);
222 unset($path_to_image[0]);
223 $path_to_image = implode('/', $path_to_image);
224 $a_set["Image"] = $path_to_image;
225 } else {
226 $a_set["Image"] = '';
227 }
228
229 $a_set["ShowComments"] = $this->notes->domain()->commentsActive((int) $a_set["Id"]);
230 }
231 return $a_set;
232 }
233
234 public function importRecord(
235 string $a_entity,
236 array $a_types,
237 array $a_rec,
238 ilImportMapping $a_mapping,
239 string $a_schema_version
240 ): void {
241 $a_rec = $this->stripTags(
242 $a_rec,
243 [
244 'Id',
245 'MaxAnswers',
246 'ResultSort',
247 'NonAnon',
248 'ShowResultsAs',
249 'ShowComments',
250 'ViewResults',
251 'Period',
252 'PeriodBegin',
253 'PeriodEnd',
254 'PollId',
255 'pos',
256 ]
257 );
258
259 switch ($a_entity) {
260 case "poll":
261 // container copy
262 if ($new_id = $a_mapping->getMapping("components/ILIAS/Container", "objs", (string) ($a_rec["Id"] ?? "0"))) {
263 $newObj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
264 } else {
265 $newObj = new ilObjPoll();
266 $newObj->create();
267 }
268
270 $newObj->setTitle((string) ($a_rec["Title"] ?? ''));
271 $newObj->setDescription((string) ($a_rec["Description"]));
272 if ((int) $a_rec["MaxAnswers"]) {
273 $newObj->setMaxNumberOfAnswers((int) $a_rec["MaxAnswers"]);
274 }
275 $newObj->setSortResultByVotes((bool) ($a_rec["ResultSort"] ?? false));
276 $newObj->setNonAnonymous((bool) ($a_rec["NonAnon"] ?? false));
277 if ((int) $a_rec["ShowResultsAs"]) {
278 $newObj->setShowResultsAs((int) $a_rec["ShowResultsAs"]);
279 }
280 $newObj->setShowComments((bool) ($a_rec["ShowComments"] ?? false));
281 $newObj->setQuestion((string) ($a_rec["Question"] ?? ''));
282 $newObj->setViewResults((int) ($a_rec["ViewResults"] ?? ilObjPoll::VIEW_RESULTS_AFTER_VOTE));
283 $newObj->setVotingPeriod((bool) ($a_rec["Period"] ?? 0));
284 $newObj->setVotingPeriodBegin((int) ($a_rec["PeriodBegin"] ?? 0));
285 $newObj->setVotingPeriodEnd((int) ($a_rec["PeriodEnd"] ?? 0));
286 $newObj->update();
287
288 // handle image(s)
289 if ($a_rec["Image"] && $this->getImportDirectory() !== "") {
290 $dir = str_replace("..", "", (string) ($a_rec["Dir"] ?? ''));
291 if ($a_schema_version === "4.3.0" || $a_schema_version === "5.0.0") {
292 $source = $this->getImportDirectory() . "/" . $dir . "/org_" . $a_rec["Image"];
293 $name = $a_rec["Image"];
294 } else {
295 $source = $this->getImportDirectory() . "/" . $a_rec["Image"];
296 $name = basename($source);
297 }
298 $this->poll_image_factory->handler()->uploadImage(
299 $this->data_factory->objId($newObj->getId()),
300 $source,
301 $name,
302 $this->user->getId()
303 );
304 }
305
306 $a_mapping->addMapping("components/ILIAS/Poll", "poll", (string) ($a_rec["Id"] ?? "0"), (string) $newObj->getId());
307 break;
308
309 case "poll_answer":
310 $poll_id = (int) $a_mapping->getMapping("components/ILIAS/Poll", "poll", (string) ($a_rec["PollId"] ?? "0"));
311 if ($poll_id) {
312 $poll = new ilObjPoll($poll_id, false);
313 $poll->saveAnswer((string) ($a_rec["Answer"] ?? ''), (int) ($a_rec["pos"] ?? 10));
314 }
315 break;
316 }
317 }
318}
Builds data types.
Definition: Factory.php:36
A dataset contains in data in a common structure that can be shared and transformed for different pur...
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 ...
ilDBInterface $db
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
Class ilObjPoll.
const int VIEW_RESULTS_AFTER_VOTE
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Poll Dataset class.
PollImageFactoryInterface $poll_image_factory
getXmlNamespace(string $a_entity, string $a_schema_version)
getTypes(string $a_entity, string $a_version)
Get (abstract) types for (abstract) field names.Please note that the abstract fields/types only depen...
getXmlRecord(string $a_entity, string $a_version, array $a_set)
Get xml record for version.
readData(string $a_entity, string $a_version, array $a_ids)
Read data from DB.
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
DataFactory $data_factory
NotesService $notes
const string ENTITY
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26