ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 include_once "Modules/Exercise/classes/class." . $class . ".php";
105 return new $class;
106 }
107
108
109 //
110 // properties
111 //
112
113 public function getId()
114 {
115 return $this->id;
116 }
117
118 protected function setId($a_id)
119 {
120 $this->id = (int) $a_id;
121 }
122
123 abstract public function getType();
124
125 public function setParent($a_value)
126 {
127 $this->parent = ($a_value !== null)
128 ? (int) $a_value
129 : null;
130 }
131
132 public function getParent()
133 {
134 return $this->parent;
135 }
136
137 public function setTitle($a_value)
138 {
139 $this->title = ($a_value !== null)
140 ? trim($a_value)
141 : null;
142 }
143
144 public function getTitle()
145 {
146 return $this->title;
147 }
148
149 public function setDescription($a_value)
150 {
151 $this->desc = ($a_value !== null)
152 ? trim($a_value)
153 : null;
154 }
155
156 public function getDescription()
157 {
158 return $this->desc;
159 }
160
161 public function setRequired($a_value)
162 {
163 $this->required = (bool) $a_value;
164 }
165
166 public function isRequired()
167 {
168 return $this->required;
169 }
170
171 public function setPosition($a_value)
172 {
173 $this->pos = (int) $a_value;
174 }
175
176 public function getPosition()
177 {
178 return $this->pos;
179 }
180
181 protected function setDefinition(array $a_value = null)
182 {
183 $this->def = $a_value;
184 }
185
186 protected function getDefinition()
187 {
188 return $this->def;
189 }
190
191 public function importDefinition($a_def, $a_def_json)
192 {
193 // see #23711
194 // use json, if given
195 if ($a_def_json != "") {
196 $def = json_decode($a_def_json, true);
197 if (is_array($def)) {
198 $this->setDefinition($def);
199 }
200 return;
201 }
202
203 // use unserialize only if php > 7
204 if ($a_def != "" && version_compare(PHP_VERSION, '7.0.0') >= 0) {
205 $a_def = @unserialize($a_def, false);
206 if (is_array($a_def)) {
207 $this->setDefinition($a_def);
208 }
209 }
210 }
211
212
213 //
214 // CRUD
215 //
216
217 protected function importFromDB(array $a_row)
218 {
219 $this->setId($a_row["id"]);
220 $this->setParent($a_row["parent"]);
221 $this->setTitle($a_row["title"]);
222 $this->setDescription($a_row["descr"]);
223 $this->setRequired($a_row["required"]);
224 $this->setPosition($a_row["pos"]);
225 $this->setDefinition($a_row["def"]
226 ? unserialize($a_row["def"])
227 : null);
228 }
229
230 protected function getDBProperties()
231 {
232 return array(
233 "type" => array("text", $this->getType())
234 ,"title" => array("text", $this->getTitle())
235 ,"descr" => array("text", $this->getDescription())
236 ,"required" => array("integer", $this->isRequired())
237 ,"pos" => array("integer", $this->getPosition())
238 ,"def" => array("text", is_array($this->getDefinition())
239 ? serialize($this->getDefinition())
240 : null)
241 );
242 }
243 protected function getLastPosition()
244 {
246
247 if (!$this->getParent()) {
248 return;
249 }
250
251 $set = $ilDB->query("SELECT MAX(pos) pos" .
252 " FROM exc_crit" .
253 " WHERE parent = " . $ilDB->quote($this->getParent(), "integer"));
254 $row = $ilDB->fetchAssoc($set);
255 return (int) $row["pos"];
256 }
257
258 public function save()
259 {
261
262 if ($this->id) {
263 return $this->update();
264 }
265
266 $this->id = $ilDB->nextId("exc_crit");
267
268 $fields = $this->getDBProperties();
269
270 $fields["id"] = array("integer", $this->id);
271 $fields["type"] = array("text", $this->getType());
272 $fields["parent"] = array("integer", $this->getParent());
273 $fields["pos"] = array("integer", $this->getLastPosition() + 10);
274
275 $ilDB->insert("exc_crit", $fields);
276 }
277
278 public function update()
279 {
281
282 if (!$this->id) {
283 return $this->save();
284 }
285
286 $primary = array("id" => array("integer", $this->id));
287 $ilDB->update("exc_crit", $this->getDBProperties(), $primary);
288 }
289
290 public function delete()
291 {
293
294 if (!$this->id) {
295 return;
296 }
297
298 $ilDB->manipulate("DELETE FROM exc_crit" .
299 " WHERE id = " . $ilDB->quote($this->id, "integer"));
300 }
301
302 public static function deleteByParent($a_parent_id)
303 {
304 global $DIC;
305
306 $ilDB = $DIC->database();
307
308 if (!(int) $a_parent_id) {
309 return;
310 }
311
312 $ilDB->manipulate("DELETE FROM exc_crit" .
313 " WHERE parent = " . $ilDB->quote($a_parent_id, "integer"));
314 }
315
316 public function cloneObject($a_target_parent_id)
317 {
318 $new_obj = ilExcCriteria::getInstanceByType($this->getType());
319 $new_obj->setParent($a_target_parent_id);
320 $new_obj->setTitle($this->getTitle());
321 $new_obj->setDescription($this->getDescription());
322 $new_obj->setRequired($this->isRequired());
323 $new_obj->setPosition($this->getPosition());
324 $new_obj->setDefinition($this->getDefinition());
325 $new_obj->save();
326
327 return $new_obj->getId();
328 }
329
330
331 //
332 // ASSIGNMENT EDITOR
333 //
334
335 public function initCustomForm(ilPropertyFormGUI $a_form)
336 {
337 // type-specific
338 }
339
340 public function exportCustomForm(ilPropertyFormGUI $a_form)
341 {
342 // type-specific
343 }
344
345 public function importCustomForm(ilPropertyFormGUI $a_form)
346 {
347 // type-specific
348 }
349
350
351 // PEER REVIEW
352
353 public function setPeerReviewContext(ilExAssignment $a_ass, $a_giver_id, $a_peer_id, ilPropertyFormGUI $a_form = null)
354 {
355 $this->form = $a_form;
356 $this->ass = $a_ass;
357 $this->giver_id = $a_giver_id;
358 $this->peer_id = $a_peer_id;
359 }
360
361 abstract public function addToPeerReviewForm($a_value = null);
362
363 abstract public function importFromPeerReviewForm();
364
365 public function updateFromAjax()
366 {
367 // type-specific
368 }
369
370 public function validate($a_value)
371 {
372 return true;
373 }
374
375 abstract public function hasValue($a_value);
376
377 abstract public function getHTML($a_value);
378
379 public function resetReview()
380 {
381 // type-specific (only needed for data not kept in exc_assignment_peer)
382 }
383}
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.
$row
global $DIC
Definition: saml.php:7
$lng
foreach($_POST as $key=> $value) $res
global $ilDB
$a_type
Definition: workflow.php:92