ILIAS  release_8 Revision v8.24
class.ilExcCriteria.php
Go to the documentation of this file.
1<?php
2
28abstract class ilExcCriteria
29{
30 protected ilLanguage $lng;
31 protected ilCtrl $ctrl;
32 protected ilDBInterface $db;
33 protected ?int $id = null;
34 protected ?int $parent = null;
35 protected string $title = "";
36 protected string $desc = "";
37 protected bool $required = false;
38 protected int $pos = 0;
39 protected ?array $def = null;
40 protected ?ilPropertyFormGUI $form = null;
42 protected int $giver_id = 0;
43 protected int $peer_id = 0;
44
45 public function __construct()
46 {
47 global $DIC;
48
49 $this->db = $DIC->database();
50 $this->lng = $DIC->language();
51 $this->ctrl = $DIC->ctrl();
52 }
53
54 public static function getInstanceById(int $a_id): ?ilExcCriteria
55 {
56 global $DIC;
57
58 $ilDB = $DIC->database();
59
60 $set = $ilDB->query("SELECT *" .
61 " FROM exc_crit" .
62 " WHERE id = " . $ilDB->quote($a_id, "integer"));
63 if ($ilDB->numRows($set)) {
64 $row = $ilDB->fetchAssoc($set);
65 $obj = self::getInstanceByType($row["type"]);
66 $obj->importFromDB($row);
67 return $obj;
68 }
69
70 return null;
71 }
72
77 public static function getInstancesByParentId(int $a_parent_id): array
78 {
79 global $DIC;
80
81 $ilDB = $DIC->database();
82
83 $res = array();
84
85 $set = $ilDB->query("SELECT *" .
86 " FROM exc_crit" .
87 " WHERE parent = " . $ilDB->quote($a_parent_id, "integer") .
88 " ORDER BY pos");
89 while ($row = $ilDB->fetchAssoc($set)) {
90 $obj = self::getInstanceByType($row["type"]);
91 $obj->importFromDB($row);
92 $res[$obj->getId()] = $obj;
93 }
94
95 return $res;
96 }
97
98
99 //
100 // type(s)
101 //
102
103 public static function getTypesMap(): array
104 {
105 global $DIC;
106
107 $lng = $DIC->language();
108
109 return array(
110 "bool" => $lng->txt("exc_criteria_type_bool")
111 ,"rating" => $lng->txt("exc_criteria_type_rating")
112 ,"text" => $lng->txt("exc_criteria_type_text")
113 ,"file" => $lng->txt("exc_criteria_type_file")
114 );
115 }
116
117 public function getTranslatedType(): string
118 {
119 $map = $this->getTypesMap();
120 return $map[$this->getType()];
121 }
122
123 public static function getInstanceByType(string $a_type): ilExcCriteria
124 {
125 $class = "ilExcCriteria" . ucfirst($a_type);
126 return new $class();
127 }
128
129
130 //
131 // properties
132 //
133
134 public function getId(): ?int
135 {
136 return $this->id;
137 }
138
139 protected function setId(?int $a_id): void
140 {
141 $this->id = $a_id;
142 }
143
144 abstract public function getType(): string;
145
146 public function setParent(?int $a_value): void
147 {
148 $this->parent = $a_value;
149 }
150
151 public function getParent(): ?int
152 {
153 return $this->parent;
154 }
155
156 public function setTitle(?string $a_value): void
157 {
158 $this->title = $a_value;
159 }
160
161 public function getTitle(): string
162 {
163 return $this->title;
164 }
165
166 public function setDescription(?string $a_value): void
167 {
168 $this->desc = $a_value;
169 }
170
171 public function getDescription(): string
172 {
173 return $this->desc;
174 }
175
176 public function setRequired(bool $a_value): void
177 {
178 $this->required = $a_value;
179 }
180
181 public function isRequired(): bool
182 {
183 return $this->required;
184 }
185
186 public function setPosition(int $a_value): void
187 {
188 $this->pos = $a_value;
189 }
190
191 public function getPosition(): int
192 {
193 return $this->pos;
194 }
195
196 protected function setDefinition(?array $a_value = null)
197 {
198 $this->def = $a_value;
199 }
200
201 protected function getDefinition(): ?array
202 {
203 return $this->def;
204 }
205
206 public function importDefinition(string $a_def, string $a_def_json): void
207 {
208 // see #23711
209 // use json, if given
210 if ($a_def_json != "") {
211 $def = json_decode($a_def_json, true);
212 if (is_array($def)) {
213 $this->setDefinition($def);
214 }
215 return;
216 }
217
218 // use unserialize only if php > 7
219 if ($a_def != "" && version_compare(PHP_VERSION, '7.0.0') >= 0) {
220 $a_def = unserialize($a_def, ['allowed_classes' => false]);
221 if (is_array($a_def)) {
222 $this->setDefinition($a_def);
223 }
224 }
225 }
226
227
228 //
229 // CRUD
230 //
231
232 protected function importFromDB(array $a_row)
233 {
234 $this->setId((int) $a_row["id"]);
235 $this->setParent((int) $a_row["parent"]);
236 $this->setTitle((string) $a_row["title"]);
237 $this->setDescription((string) $a_row["descr"]);
238 $this->setRequired((bool) $a_row["required"]);
239 $this->setPosition((int) $a_row["pos"]);
240 $this->setDefinition((string) $a_row["def"]
241 ? unserialize($a_row["def"], ['allowed_classes' => false])
242 : null);
243 }
244
245 protected function getDBProperties(): array
246 {
247 return array(
248 "type" => array("text", $this->getType())
249 ,"title" => array("text", $this->getTitle())
250 ,"descr" => array("text", $this->getDescription())
251 ,"required" => array("integer", $this->isRequired())
252 ,"pos" => array("integer", $this->getPosition())
253 ,"def" => array("text", is_array($this->getDefinition())
254 ? serialize($this->getDefinition())
255 : null)
256 );
257 }
258
259 protected function getLastPosition(): int
260 {
262
263 if (!$this->getParent()) {
264 return 0;
265 }
266
267 $set = $ilDB->query("SELECT MAX(pos) pos" .
268 " FROM exc_crit" .
269 " WHERE parent = " . $ilDB->quote($this->getParent(), "integer"));
270 $row = $ilDB->fetchAssoc($set);
271 return (int) $row["pos"];
272 }
273
274 public function save(): void
275 {
277
278 if ($this->id) {
279 $this->update();
280 return;
281 }
282
283 $this->id = $ilDB->nextId("exc_crit");
284
285 $fields = $this->getDBProperties();
286
287 $fields["id"] = array("integer", $this->id);
288 $fields["type"] = array("text", $this->getType());
289 $fields["parent"] = array("integer", $this->getParent());
290 $fields["pos"] = array("integer", $this->getLastPosition() + 10);
291
292 $ilDB->insert("exc_crit", $fields);
293 }
294
295 public function update(): void
296 {
298
299 if (!$this->id) {
300 $this->save();
301 return;
302 }
303
304 $primary = array("id" => array("integer", $this->id));
305 $ilDB->update("exc_crit", $this->getDBProperties(), $primary);
306 }
307
308 public function delete(): void
309 {
311
312 if (!$this->id) {
313 return;
314 }
315
316 $ilDB->manipulate("DELETE FROM exc_crit" .
317 " WHERE id = " . $ilDB->quote($this->id, "integer"));
318 }
319
320 public static function deleteByParent(int $a_parent_id): void
321 {
322 global $DIC;
323
324 $ilDB = $DIC->database();
325
326 if (!$a_parent_id) {
327 return;
328 }
329
330 $ilDB->manipulate("DELETE FROM exc_crit" .
331 " WHERE parent = " . $ilDB->quote($a_parent_id, "integer"));
332 }
333
334 public function cloneObject(int $a_target_parent_id): ?int
335 {
336 $new_obj = ilExcCriteria::getInstanceByType($this->getType());
337 $new_obj->setParent($a_target_parent_id);
338 $new_obj->setTitle($this->getTitle());
339 $new_obj->setDescription($this->getDescription());
340 $new_obj->setRequired($this->isRequired());
341 $new_obj->setPosition($this->getPosition());
342 $new_obj->setDefinition($this->getDefinition());
343 $new_obj->save();
344
345 return $new_obj->getId();
346 }
347
348
349 //
350 // ASSIGNMENT EDITOR
351 //
352
353 public function initCustomForm(ilPropertyFormGUI $a_form): void
354 {
355 // type-specific
356 }
357
358 public function exportCustomForm(ilPropertyFormGUI $a_form): void
359 {
360 // type-specific
361 }
362
363 public function importCustomForm(ilPropertyFormGUI $a_form): void
364 {
365 // type-specific
366 }
367
368
369 // PEER REVIEW
370
371 public function setPeerReviewContext(
372 ilExAssignment $a_ass,
373 int $a_giver_id,
374 int $a_peer_id,
375 ilPropertyFormGUI $a_form = null
376 ) {
377 $this->form = $a_form;
378 $this->ass = $a_ass;
379 $this->giver_id = $a_giver_id;
380 $this->peer_id = $a_peer_id;
381 }
382
383 abstract public function addToPeerReviewForm($a_value = null): void;
384
385 abstract public function importFromPeerReviewForm();
386
387 public function updateFromAjax(): string
388 {
389 return "";
390 }
391
392 public function validate($a_value): bool
393 {
394 return true;
395 }
396
397 abstract public function hasValue($a_value);
398
399 abstract public function getHTML($a_value): string;
400
401 public function resetReview()
402 {
403 // type-specific (only needed for data not kept in exc_assignment_peer)
404 }
405}
Class ilCtrl provides processing control methods.
Exercise assignment.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilPropertyFormGUI $form
setDefinition(?array $a_value=null)
importFromPeerReviewForm()
importFromDB(array $a_row)
initCustomForm(ilPropertyFormGUI $a_form)
static deleteByParent(int $a_parent_id)
exportCustomForm(ilPropertyFormGUI $a_form)
setRequired(bool $a_value)
setTitle(?string $a_value)
importDefinition(string $a_def, string $a_def_json)
getHTML($a_value)
setPosition(int $a_value)
ilDBInterface $db
importCustomForm(ilPropertyFormGUI $a_form)
addToPeerReviewForm($a_value=null)
hasValue($a_value)
setDescription(?string $a_value)
cloneObject(int $a_target_parent_id)
setPeerReviewContext(ilExAssignment $a_ass, int $a_giver_id, int $a_peer_id, ilPropertyFormGUI $a_form=null)
static getInstancesByParentId(int $a_parent_id)
static getInstanceById(int $a_id)
setParent(?int $a_value)
ilExAssignment $ass
static getInstanceByType(string $a_type)
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
This class represents a property form user interface.
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
form( $class_path, string $cmd)