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