ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilExcCriteriaCatalogue.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 
12 {
16  protected $db;
17 
18  protected $id; // [int]
19  protected $parent; // [int]
20  protected $title; // [string]
21  protected $pos; // [int]
22 
23  public function __construct($a_id = null)
24  {
25  global $DIC;
26 
27  $this->db = $DIC->database();
28  $this->read($a_id);
29  }
30 
31  public static function getInstancesByParentId($a_parent_id)
32  {
33  global $DIC;
34 
35  $ilDB = $DIC->database();
36 
37  $res = array();
38 
39  $set = $ilDB->query("SELECT *" .
40  " FROM exc_crit_cat" .
41  " WHERE parent = " . $ilDB->quote($a_parent_id, "integer") .
42  " ORDER BY pos");
43  while ($row = $ilDB->fetchAssoc($set)) {
44  $obj = new self();
45  $obj->importFromDB($row);
46  $res[$obj->getId()] = $obj;
47  }
48 
49  return $res;
50  }
51 
52 
53  //
54  // properties
55  //
56 
57  public function getId()
58  {
59  return $this->id;
60  }
61 
62  protected function setId($a_id)
63  {
64  $this->id = (int) $a_id;
65  }
66 
67  public function setParent($a_value)
68  {
69  $this->parent = ($a_value !== null)
70  ? (int) $a_value
71  : null;
72  }
73 
74  public function getParent()
75  {
76  return $this->parent;
77  }
78 
79  public function setTitle($a_value)
80  {
81  $this->title = ($a_value !== null)
82  ? trim($a_value)
83  : null;
84  }
85 
86  public function getTitle()
87  {
88  return $this->title;
89  }
90 
91  public function setPosition($a_value)
92  {
93  $this->pos = (int) $a_value;
94  }
95 
96  public function getPosition()
97  {
98  return $this->pos;
99  }
100 
101 
102  //
103  // CRUD
104  //
105 
106  protected function importFromDB(array $a_row)
107  {
108  $this->setId($a_row["id"]);
109  $this->setParent($a_row["parent"]);
110  $this->setTitle($a_row["title"]);
111  $this->setPosition($a_row["pos"]);
112  }
113 
114  protected function getDBProperties()
115  {
116  return array(
117  "title" => array("text", $this->getTitle())
118  ,"pos" => array("integer", $this->getPosition())
119  );
120  }
121  protected function getLastPosition()
122  {
123  $ilDB = $this->db;
124 
125  if (!$this->getParent()) {
126  return;
127  }
128 
129  $set = $ilDB->query("SELECT MAX(pos) pos" .
130  " FROM exc_crit_cat" .
131  " WHERE parent = " . $ilDB->quote($this->getParent(), "integer"));
132  $row = $ilDB->fetchAssoc($set);
133  return (int) $row["pos"];
134  }
135 
136  protected function read($a_id)
137  {
138  $ilDB = $this->db;
139 
140  $a_id = (int) $a_id;
141  if ($a_id) {
142  $set = $ilDB->query("SELECT *" .
143  " FROM exc_crit_cat" .
144  " WHERE id = " . $ilDB->quote($a_id, "integer"));
145  if ($ilDB->numRows($set)) {
146  $row = $ilDB->fetchAssoc($set);
147  $this->importFromDB($row);
148  }
149  }
150  }
151 
152  public function save()
153  {
154  $ilDB = $this->db;
155 
156  if ($this->id) {
157  return $this->update();
158  }
159 
160  $this->id = $ilDB->nextId("exc_crit_cat");
161 
162  $fields = $this->getDBProperties();
163  $fields["parent"] = array("integer", $this->getParent());
164  $fields["pos"] = array("integer", $this->getLastPosition() + 10);
165  $fields["id"] = array("integer", $this->id);
166 
167  $ilDB->insert("exc_crit_cat", $fields);
168  }
169 
170  public function update()
171  {
172  $ilDB = $this->db;
173 
174  if (!$this->id) {
175  return $this->save();
176  }
177 
178  $primary = array("id" => array("integer", $this->id));
179  $ilDB->update("exc_crit_cat", $this->getDBProperties(), $primary);
180  }
181 
182  public function delete()
183  {
184  $ilDB = $this->db;
185 
186  if (!$this->id) {
187  return;
188  }
189 
190  include_once "Modules/Exercise/classes/class.ilExcCriteria.php";
192 
193  $ilDB->manipulate("DELETE FROM exc_crit_cat" .
194  " WHERE id = " . $ilDB->quote($this->id, "integer"));
195  }
196 
197  public static function deleteByParent($a_parent_id)
198  {
199  global $DIC;
200 
201  $ilDB = $DIC->database();
202 
203  if (!(int) $a_parent_id) {
204  return;
205  }
206 
207  $ilDB->manipulate("DELETE FROM exc_crit" .
208  " WHERE parent = " . $ilDB->quote($a_parent_id, "integer"));
209  }
210 
211  public function cloneObject($a_target_parent_id)
212  {
213  $new_obj = new self();
214  $new_obj->setParent($a_target_parent_id);
215  $new_obj->setTitle($this->getTitle());
216  $new_obj->setPosition($this->getPosition());
217  $new_obj->save();
218 
219  include_once "Modules/Exercise/classes/class.ilExcCriteria.php";
220  foreach (ilExcCriteria::getInstancesByParentId($this->getId()) as $crit) {
221  $crit->cloneObject($new_obj->getId());
222  }
223 
224  return $new_obj->getId();
225  }
226 }
Class ilExcCriteriaCatalogue.
global $DIC
Definition: saml.php:7
static deleteByParent($a_parent_id)
foreach($_POST as $key=> $value) $res
static getInstancesByParentId($a_parent_id)
$row
static getInstancesByParentId($a_parent_id)
global $ilDB