ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilOrgUnit.php
Go to the documentation of this file.
1 <?php
2 
3 require_once('Services/OrgUnit/exceptions/class.ilOrgUnitException.php');
4 
5 class ilOrgUnit
6 {
7  private static $instance_cache = array();
8  private static $import_id_cache = array();
9 
10  private $id = 0;
11  private $title = '';
12  private $subtitle = '';
13  private $import_id = 0;
14 
15  private $parent = 0;
16  private $childs = array();
17 
18  private $assignment_list = null;
19 
20  private function __construct($ou_id = 0)
21  {
22  if( (int)$ou_id > 0 )
23  {
24  $this->id = (int)$ou_id;
25  $this->read();
26  }
27  }
28 
29  public function initAssigns()
30  {
31  require_once('Services/OrgUnit/classes/class.ilOrgUnitAssignmentList.php');
32  $this->assignment_list = new ilOrgUnitAssignmentList($this->id);
33  }
34 
35  public function assignUser($a_user_id, $a_reporting_access,
36  $a_cc_compl_invit, $a_cc_compl_not1, $a_cc_compl_not2)
37  {
38  if($this->assignment_list === null)
39  throw new ilOrgUnitException('Error: Assignment object not initialised yet!');
40 
41  $this->assignment_list->addAssignment($a_user_id, $a_reporting_access,
42  $a_cc_compl_invit, $a_cc_compl_not1, $a_cc_compl_not2);
43  }
44 
45  public function deassignUser($a_user_id)
46  {
47  if($this->assignment_list === null)
48  throw new ilOrgUnitException('Error: Assignment object not initialised yet!');
49 
50  $this->assignment_list->removeAssignment($a_user_id);
51  }
52 
53  public function isUserAssigned($a_user_id)
54  {
55  if($this->assignment_list === null)
56  throw new ilOrgUnitException('Error: Assignment object not initialised yet!');
57 
58  return $this->assignment_list->doesAssignmentExist($a_user_id);
59  }
60 
61  public function getAssignedUsers()
62  {
63  if($this->assignment_list === null)
64  throw new ilOrgUnitException('Error: Assignment object not initialised yet!');
65 
66  $assignments = array();
67  foreach($this->assignment_list as $assignment)
68  {
69  $assignments[$assignment->getUserId()] = array(
70  'reporting_access' => $assignment->hasReportingAccess(),
71  'cc_coml_invit' => $assignment->hasCcComplianceInvitation(),
72  'cc_coml_not1' => $assignment->hasCcComplianceNotify1(),
73  'cc_coml_not2' => $assignment->hasCcComplianceNotify2()
74  );
75  }
76 
77  return $assignments;
78  }
79 
80  public function read()
81  {
82  global $ilDB;
83 
84  $query = "SELECT * FROM org_unit_data WHERE ou_id = %s";
85 
86  $res = $ilDB->queryF($query, array('integer'), array($this->id));
87 
88  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
89  {
90  $this->id = $row->ou_id;
91  $this->title = $row->ou_title;
92  $this->subtitle = $row->ou_subtitle;
93  $this->import_id = $row->ou_import_id;
94 
95  return true;
96  }
97 
99  'organisation unit with id "'.$this->id.'" does not exist!'
100  );
101  }
102 
103  public function update()
104  {
105  global $ilDB;
106 
107  $ilDB->update(
108  'org_unit_data',
109  array(
110  'ou_title' => array('text', $this->title),
111  'ou_subtitle' => array('text', $this->subtitle),
112  'ou_import_id' => array('text', $this->import_id)
113  ),
114  array('ou_id' => array('integer', $this->id))
115  );
116  }
117 
118  public function create()
119  {
120  global $ilDB;
121 
122  $this->id = $ilDB->nextId('org_unit_data');
123 
124  $ilDB->insert(
125  'org_unit_data',
126  array(
127  'ou_id' => array('integer', $this->id),
128  'ou_title' => array('text', $this->title),
129  'ou_subtitle' => array('text', $this->subtitle),
130  'ou_import_id' => array('text', $this->import_id)
131  ),
132  array('ou_id' => array('integer', $this->id))
133  );
134  }
135 
136  public function delete()
137  {
138  self::deleteInstance($this->id);
139  }
140 
141  public static function deleteInstance($ou_id)
142  {
143  global $ilDB;
144 
145  $query = "DELETE FROM org_unit_data WHERE ou_id = %s";
146 
147  $ilDB->queryF($query, array('integer'), array($ou_id));
148 
149  if( isset(self::$instance_cache[$ou_id]) )
150  {
151  unset(self::$instance_cache[$ou_id]);
152  }
153  }
154 
155  public static function getInstance($ou_id)
156  {
157  if( !isset(self::$instance_cache[$ou_id]) )
158  {
159  self::$instance_cache[$ou_id] = new self($ou_id);
160  }
161 
162  return self::$instance_cache[$ou_id];
163  }
164 
165  public static function createInstance($ou_title, $ou_subtitle, $ou_import_id)
166  {
167  $unit = new self();
168 
169  $unit ->setTitle($ou_title)
170  ->setSubTitle($ou_subtitle)
171  ->setImportId($ou_import_id)
172  ->create();
173 
174  self::$instance_cache[$unit->getId()] = $unit;
175 
176  return $unit;
177  }
178 
179  public static function lookupIdByImportId($ou_import_id)
180  {
181  if( isset(self::$import_id_cache[$ou_import_id]) )
182  {
183  return self::$import_id_cache[$ou_import_id];
184  }
185 
186  global $ilDB;
187 
188  $query = "SELECT ou_id FROM org_unit_data WHERE ou_import_id = %s";
189 
190  $res = $ilDB->queryF($query, array('integer'), array($ou_import_id));
191 
192  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
193  {
194  self::$import_id_cache[$ou_import_id] = $row->ou_id;
195  return $row->ou_id;
196  }
197 
198  return null;
199  }
200 
201  public static function getInstancesByAssignedUser($a_user_id)
202  {
203  global $ilDB;
204 
205  $query = "SELECT ou_id FROM org_unit_data ".
206  "LEFT JOIN org_unit_assignments ".
207  "ON ou_id = oa_ou_id ".
208  "WHERE oa_usr_id = %s";
209 
210  $res = $ilDB->queryF($query, array('integer'), array($a_user_id));
211 
212  $units = array();
213  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
214  {
215  $units[$row->ou_id] = self::getInstance($row->ou_id);
216  }
217 
218  return $units;
219  }
220 
221  public function hasUserReportingAccess($a_user_id)
222  {
223  if($this->assignment_list === null) $this->initAssigns();
224 
225  return $this->assignment_list->hasUserReportingAccess($a_user_id);
226  }
227 
228  public function setId($ou_id)
229  {
230  $this->id = (int)$ou_id;
231  return $this;
232  }
233  public function getId()
234  {
235  return $this->id;
236  }
237  public function setTitle($ou_title)
238  {
239  $this->title = $ou_title;
240  return $this;
241  }
242  public function getTitle()
243  {
244  return $this->title;
245  }
246  public function setSubTitle($ou_subtitle)
247  {
248  $this->subtitle = $ou_subtitle;
249  return $this;
250  }
251  public function getSubTitle()
252  {
253  return $this->subtitle;
254  }
255  public function setImportId($ou_import_id)
256  {
257  $this->import_id = (int)$ou_import_id;
258  return $this;
259  }
260  public function getImportId()
261  {
262  return $this->import_id;
263  }
264 
265  public function setParent($a_parent)
266  {
267  $this->parent = (int)$a_parent;
268  return $this;
269  }
270  public function getParent()
271  {
272  return $this->parent;
273  }
274  public function addChild($a_child)
275  {
276  $this->childs[] = $a_child;
277  return $this;
278  }
279  public function getChilds()
280  {
281  return $this->childs;
282  }
283  public function hasChilds()
284  {
285  return (bool)count($this->childs);
286  }
287  public function sortChilds()
288  {
289  usort($this->childs, array($this,'sortCallback'));
290 
291  foreach($this->childs as $child)
292  {
293  if( $child->hasChilds() ) $child->sortChilds();
294  }
295  }
296  public function sortCallback($a_a, $a_b)
297  {
298  return strcmp($a_a->getTitle(), $a_b->getTitle());
299  }
300 }
301 
302 
303 ?>