ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilGroupXMLParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("classes/class.ilSaxParser.php");
25 require_once('./Services/User/classes/class.ilObjUser.php');
26 include_once('./Services/Calendar/classes/class.ilDateTime.php');
27 include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
28 
29 
40 {
41  public static $CREATE = 1;
42  public static $UPDATE = 2;
43 
44  private $participants = null;
45 
48 
49  var $parent;
50  var $counter;
51 
52  var $mode;
53  var $grp;
54 
63  function ilGroupXMLParser($a_xml, $a_parent_id)
64  {
65  define('EXPORT_VERSION',2);
66 
67  parent::ilSaxParser(null);
68 
69  $this->mode = ilGroupXMLParser::$CREATE;
70  $this->grp = null;
71 
72  $this->setXMLContent($a_xml);
73 
74  // SET MEMBER VARIABLES
75  $this->__pushParentId($a_parent_id);
76 
77 
78  }
79 
80  function __pushParentId($a_id)
81  {
82  $this->parent[] = $a_id;
83  }
84  function __popParentId()
85  {
86  array_pop($this->parent);
87 
88  return true;
89  }
90  function __getParentId()
91  {
92  return $this->parent[count($this->parent) - 1];
93  }
94 
100  function setHandlers($a_xml_parser)
101  {
102  xml_set_object($a_xml_parser,$this);
103  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
104  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
105  }
106 
110  function startParsing()
111  {
113 
114  if ($this->mode == ilGroupXMLParser::$CREATE)
115  {
116  return is_object($this->group_obj) ? $this->group_obj->getRefId() : false;
117  }
118  else
119  {
120  return is_object($this->group_obj) ? $this->group_obj->update() : false;
121  }
122  }
123 
124 
128  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
129  {
130  global $ilErr;
131 
132  switch($a_name)
133  {
134  // GROUP DATA
135  case "group":
136  #if($a_attribs["exportVersion"] < EXPORT_VERSION)
137  #{
138  # $ilErr->raiseError("!!! This export Version isn't supported, update your ILIAS 2 installation"
139  # ,$ilErr->WARNING);
140  #}
141  // DEFAULT
142  $this->group_data["admin"] = array();
143  $this->group_data["member"] = array();
144 
145  $this->group_data["type"] = $a_attribs["type"];
146  $this->group_data["id"] = $a_attribs["id"];
147 
148  break;
149 
150  case 'title':
151  break;
152 
153  case "owner":
154  $this->group_data["owner"] = $a_attribs["id"];
155  break;
156 
157  case 'registration':
158  $this->group_data['registration_type'] = $a_attribs['type'];
159  $this->group_data['waiting_list_enabled'] = $a_attribs['waitingList'] == 'Yes' ? true : false;
160  break;
161 
162  case 'maxMembers':
163  $this->group_data['max_members_enabled'] = $a_attribs['enabled'] == 'Yes' ? true : false;
164  break;
165 
166  case "admin":
167  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
168  {
169  $this->group_data["admin"]["attach"][] = $a_attribs["id"];
170  }
171  elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
172  {
173  $this->group_data["admin"]["detach"][] = $a_attribs["id"];
174  }
175 
176  if(isset($a_attribs['notification']) and $a_attribs['notification'] == 'Yes')
177  {
178  $this->group_data['notifications'][] = $a_attribs['id'];
179  }
180 
181  break;
182 
183  case "member":
184  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
185  {
186  $this->group_data["member"]["attach"][] = $a_attribs["id"];
187  } elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
188  {
189  $this->group_data["member"]["detach"][] = $a_attribs["id"];
190  }
191 
192  break;
193 
194  case "folder":
195  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
196  $this->__save();
197  break;
198 
199  case "file":
200  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
201  $this->__save();
202 
203  $this->file["fileName"] = $a_attribs["fileName"];
204  $this->file["id"] = $a_attribs["id"];
205 
206  // SAVE IT
207  $this->__saveFile();
208  break;
209  }
210  }
211 
212 
213  function handlerEndTag($a_xml_parser, $a_name)
214  {
215  switch($a_name)
216  {
217  case "title":
218  $this->group_data["title"] = trim($this->cdata);
219  break;
220 
221  case "description":
222  $this->group_data["description"] = trim($this->cdata);
223  break;
224 
225  case 'information':
226  $this->group_data['information'] = trim($this->cdata);
227  break;
228 
229  case 'password':
230  $this->group_data['password'] = trim($this->cdata);
231  break;
232 
233  case 'maxMembers':
234  $this->group_data['max_members'] = trim($this->cdata);
235  break;
236 
237  case 'expiration':
238  $this->group_data['expiration_end'] = trim($this->cdata);
239  break;
240 
241  case 'start':
242  $this->group_data['expiration_start'] = trim($this->cdata);
243  break;
244 
245  case 'end':
246  $this->group_data['expiration_end'] = trim($this->cdata);
247  break;
248 
249  case "folder":
250  $this->__popParentId();
251  break;
252 
253  case "folderTitle":
254  $this->folder = trim($this->cdata);
255  $this->__saveFolder();
256  break;
257 
258  case "group":
259  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
260  $this->__save();
261  break;
262  }
263  $this->cdata = '';
264  }
265 
266 
270  function handlerCharacterData($a_xml_parser, $a_data)
271  {
272  // i don't know why this is necessary, but
273  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
274  // in character data, but we don't want that, because it's the
275  // way we mask user html in our content, so we convert back...
276  $a_data = str_replace("<","&lt;",$a_data);
277  $a_data = str_replace(">","&gt;",$a_data);
278 
279  if(!empty($a_data))
280  {
281  $this->cdata .= $a_data;
282  }
283  }
284 
285  // PRIVATE
286  function __save()
287  {
288  if($this->group_imported)
289  {
290  return true;
291  }
292 
293  $this->__initGroupObject();
294 
295  $this->group_obj->setImportId($this->group_data["id"]);
296  $this->group_obj->setTitle($this->group_data["title"]);
297  $this->group_obj->setDescription($this->group_data["description"]);
298 
299  $ownerChanged = false;
300  if (isset($this->group_data["owner"]))
301  {
302  $owner = $this->group_data["owner"];
303  if (!is_numeric($owner))
304  {
305  $owner = ilUtil::__extractId ($owner, IL_INST_ID);
306  }
307  if (is_numeric($owner) && $owner > 0)
308  {
309  $this->group_obj->setOwner($owner);
310  $ownerChanged = true;
311  }
312  }
313 
317  if ($this->mode == ilGroupXMLParser::$CREATE)
318  {
319  $this->group_obj->create();
320  $this->group_obj->createReference();
321  $this->group_obj->putInTree($this->__getParentId());
322  $this->group_obj->setPermissions($this->__getParentId());
323  $this->group_obj->initDefaultRoles();
324  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? GRP_TYPE_PUBLIC : GRP_TYPE_CLOSED);
325  }
326  else
327  {
328  switch($this->group_data['type'])
329  {
330  case 'open':
331  $grp_status = GRP_TYPE_PUBLIC;
332  break;
333 
334  case 'closed':
335  $grp_status = GRP_TYPE_CLOSED;
336  break;
337 
338  }
339 
340  $this->group_obj->updateOwner();
341  if($this->group_obj->getGroupStatus() != $grp_status)
342  {
343  $this->group_obj->setGroupType($grp_status);
344  $this->group_obj->updateGroupType();
345  }
346  }
347 
348  // SET GROUP SPECIFIC DATA
349  switch($this->group_data['registration_type'])
350  {
351  case 'direct':
352  case 'enabled':
353  $flag = GRP_REGISTRATION_DIRECT;
354  break;
355 
356  case 'disabled':
358  break;
359 
360  case 'confirmation':
361  $flag = GRP_REGISTRATION_REQUEST;
362  break;
363 
364  case 'password':
366  break;
367 
368  default:
369  $flag = GRP_REGISTRATION_DIRECT;
370  }
371  $this->group_obj->setRegistrationType($flag);
372 
373  $end = new ilDateTime(time(),IL_CAL_UNIX);
374  if($this->group_data['expiration_end'])
375  {
376  $end = new ilDateTime($this->group_data['expiration_end'],IL_CAL_UNIX);
377  }
378 
379  $start = clone $end;
380  if($this->group_data['expiration_start'])
381  {
382  $start = new ilDateTime($this->group_data['expiration_start'],IL_CAL_UNIX);
383  }
384 
385  $this->group_obj->setRegistrationStart($start);
386  $this->group_obj->setRegistrationEnd($end);
387  $this->group_obj->setPassword($this->group_data['password']);
388  $this->group_obj->enableUnlimitedRegistration(!isset($this->group_data['expiration_end']));
389  $this->group_obj->enableMembershipLimitation($this->group_data['max_members_enabled']);
390  $this->group_obj->setMaxMembers($this->group_data['max_members'] ? $this->group_data['max_members'] : 0);
391  $this->group_obj->enableWaitingList($this->group_data['waiting_list_enabled']);
392 
393 
394  if ($this->mode == ilGroupXMLParser::$CREATE)
395  {
396  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? 0 : 1);
397  }
398 
399  $this->group_obj->update();
400 
401  // ASSIGN ADMINS/MEMBERS
402  $this->__assignMembers();
403 
404  $this->__pushParentId($this->group_obj->getRefId());
405 
406 
407  $this->group_imported = true;
408 
409  return true;
410  }
411 
412  function __saveFolder()
413  {
414  $this->__initFolderObject();
415 
416  $this->folder_obj->setTitle($this->folder);
417  $this->folder_obj->create();
418  $this->folder_obj->createReference();
419  $this->folder_obj->putInTree($this->__getParentId());
420  $this->folder_obj->initDefaultRoles();
421 
422  $this->__pushParentId($this->folder_obj->getRefId());
423 
424  $this->__destroyFolderObject();
425 
426  return true;
427  }
428 
429  function __saveFile()
430  {
431  $this->__initFileObject();
432 
433  $this->file_obj->setType("file");
434  $this->file_obj->setTitle($this->file["fileName"]);
435  $this->file_obj->setFileName($this->file["fileName"]);
436  $this->file_obj->create();
437  $this->file_obj->createReference();
438  $this->file_obj->putInTree($this->__getParentId());
439  $this->file_obj->setPermissions($this->__getParentId());
440 
441  // COPY FILE
442  $this->file_obj->createDirectory();
443 
444  $this->__initImportFileObject();
445 
446  if($this->import_file_obj->findObjectFile($this->file["id"]))
447  {
448  $this->file_obj->copy($this->import_file_obj->getObjectFile(),$this->file["fileName"]);
449  }
450 
451  unset($this->file_obj);
452  unset($this->import_file_obj);
453 
454  return true;
455  }
456 
457  function __assignMembers()
458  {
459  global $ilias,$ilUser;
460 
461  $this->participants = ilGroupParticipants::_getInstanceByObjId($this->group_obj->getId());
462 
463  $this->participants->add($ilUser->getId(),IL_GRP_ADMIN);
464  $this->participants->updateNotification($ilUser->getId(),true);
465 
466 #print_r($this->group_data["admin"]["attach"]);
467  // attach ADMINs
468  if (count($this->group_data["admin"]["attach"]))
469  {
470  foreach($this->group_data["admin"]["attach"] as $user)
471  {
472  if($id_data = $this->__parseId($user))
473  {
474  if($id_data['local'] or $id_data['imported'])
475  {
476  $this->participants->add($id_data['usr_id'],IL_GRP_ADMIN);
477  if(in_array($user,(array) $this->group_data['notifications']))
478  {
479  $this->participants->updateNotification($id_data['usr_id'],true);
480  }
481  }
482  }
483  }
484  }
485  // detach ADMINs
486  if (count($this->group_data["admin"]["detach"]))
487  {
488  foreach($this->group_data["admin"]["detach"] as $user)
489  {
490  if($id_data = $this->__parseId($user))
491  {
492  if($id_data['local'] or $id_data['imported'])
493  {
494  if($this->participants->isAssigned($id_data['usr_id']))
495  {
496  $this->participants->delete($id_data['usr_id']);
497  }
498  }
499  }
500  }
501  }
502  // MEMBER
503  if (count($this->group_data["member"]["attach"]))
504  {
505  foreach($this->group_data["member"]["attach"] as $user)
506  {
507  if($id_data = $this->__parseId($user))
508  {
509  if($id_data['local'] or $id_data['imported'])
510  {
511  $this->participants->add($id_data['usr_id'],IL_GRP_MEMBER);
512  }
513  }
514  }
515  }
516 
517  if (count($this->group_data["member"]["detach"]))
518  {
519  foreach($this->group_data["member"]["detach"] as $user)
520  {
521  if($id_data = $this->__parseId($user))
522  {
523  if($id_data['local'] or $id_data['imported'])
524  {
525  if($this->participants->isAssigned($id_data['usr_id']))
526  {
527  $this->participants->delete($id_data['usr_id']);
528  }
529  }
530  }
531  }
532  }
533  return true;
534  }
535 
536  function __initGroupObject()
537  {
538  include_once "./Modules/Group/classes/class.ilObjGroup.php";
539 
540  if ($this->mode == ilGroupXMLParser::$CREATE)
541  {
542  $this->group_obj =& new ilObjGroup();
543  } elseif ($this->mode == ilGroupXMLParser::$UPDATE) {
544  $this->group_obj = $this->grp;
545  }
546 
547  return true;
548  }
549 
551  {
552  include_once "./Modules/Folder/classes/class.ilObjFolder.php";
553 
554  $this->folder_obj =& new ilObjFolder();
555 
556  return true;
557  }
558 
560  {
561  include_once "classes/class.ilFileDataImportGroup.php";
562 
563  $this->import_file_obj =& new ilFileDataImportGroup();
564 
565  return true;
566  }
567 
568  function __initFileObject()
569  {
570  include_once "./Modules/File/classes/class.ilObjFile.php";
571 
572  $this->file_obj =& new ilObjFile();
573 
574  return true;
575  }
576 
578  {
579  unset($this->folder_obj);
580  }
581 
582  function __parseId($a_id)
583  {
584  global $ilias;
585 
586  $fields = explode('_',$a_id);
587 
588  if(!is_array($fields) or
589  $fields[0] != 'il' or
590  !is_numeric($fields[1]) or
591  $fields[2] != 'usr' or
592  !is_numeric($fields[3]))
593  {
594  return false;
595  }
596  if($id = ilObjUser::_getImportedUserId($a_id))
597  {
598  return array('imported' => true,
599  'local' => false,
600  'usr_id' => $id);
601  }
602  if(($fields[1] == $ilias->getSetting('inst_id',0)) and strlen(ilObjUser::_lookupName($fields[3])))
603  {
604  return array('imported' => false,
605  'local' => true,
606  'usr_id' => $fields[3]);
607  }
608  return false;
609  }
610 
611 
612  public function setMode($mode) {
613  $this->mode = $mode;
614  }
615 
616  public function setGroup(& $grp) {
617  $this->grp = $grp;
618  }
619 }
620 ?>