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