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