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