ILIAS  Release_5_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("./Services/Xml/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;
46  private $sort = null;
47 
50 
51  var $parent;
52  var $counter;
53 
54  var $mode;
55  var $grp;
56 
65  function ilGroupXMLParser($a_xml, $a_parent_id)
66  {
67  define('EXPORT_VERSION',2);
68 
69  parent::ilSaxParser(null);
70 
71  $this->mode = ilGroupXMLParser::$CREATE;
72  $this->grp = null;
73 
74  $this->setXMLContent($a_xml);
75 
76  // SET MEMBER VARIABLES
77  $this->__pushParentId($a_parent_id);
78 
79 
80  }
81 
82  function __pushParentId($a_id)
83  {
84  $this->parent[] = $a_id;
85  }
86  function __popParentId()
87  {
88  array_pop($this->parent);
89 
90  return true;
91  }
92  function __getParentId()
93  {
94  return $this->parent[count($this->parent) - 1];
95  }
96 
102  function setHandlers($a_xml_parser)
103  {
104  xml_set_object($a_xml_parser,$this);
105  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
106  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
107  }
108 
112  function startParsing()
113  {
115 
116  if ($this->mode == ilGroupXMLParser::$CREATE)
117  {
118  return is_object($this->group_obj) ? $this->group_obj->getRefId() : false;
119  }
120  else
121  {
122  return is_object($this->group_obj) ? $this->group_obj->update() : false;
123  }
124  }
125 
126 
130  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
131  {
132  global $ilErr;
133 
134  switch($a_name)
135  {
136  // GROUP DATA
137  case "group":
138  #if($a_attribs["exportVersion"] < EXPORT_VERSION)
139  #{
140  # $ilErr->raiseError("!!! This export Version isn't supported, update your ILIAS 2 installation"
141  # ,$ilErr->WARNING);
142  #}
143  // DEFAULT
144  $this->group_data["admin"] = array();
145  $this->group_data["member"] = array();
146 
147  $this->group_data["type"] = $a_attribs["type"];
148  $this->group_data["id"] = $a_attribs["id"];
149 
150  break;
151 
152  case 'title':
153  break;
154 
155  case "owner":
156  $this->group_data["owner"] = $a_attribs["id"];
157  break;
158 
159  case 'registration':
160  $this->group_data['registration_type'] = $a_attribs['type'];
161  $this->group_data['waiting_list_enabled'] = $a_attribs['waitingList'] == 'Yes' ? true : false;
162  break;
163 
164  case 'maxMembers':
165  $this->group_data['max_members_enabled'] = $a_attribs['enabled'] == 'Yes' ? true : false;
166  break;
167 
168  case "admin":
169  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
170  {
171  $this->group_data["admin"]["attach"][] = $a_attribs["id"];
172  }
173  elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
174  {
175  $this->group_data["admin"]["detach"][] = $a_attribs["id"];
176  }
177 
178  if(isset($a_attribs['notification']) and $a_attribs['notification'] == 'Yes')
179  {
180  $this->group_data['notifications'][] = $a_attribs['id'];
181  }
182 
183  break;
184 
185  case "member":
186  if (!isset($a_attribs['action']) || $a_attribs['action'] == "Attach")
187  {
188  $GLOBALS['ilLog']->write(__METHOD__.': new member with id '.$a_attribs['id']);
189  $this->group_data["member"]["attach"][] = $a_attribs["id"];
190  } elseif (isset($a_attribs['action']) || $a_attribs['action'] == "Detach")
191  {
192  $GLOBALS['ilLog']->write(__METHOD__.': deprecated member with id '.$a_attribs['id']);
193  $this->group_data["member"]["detach"][] = $a_attribs["id"];
194  }
195 
196  break;
197 
198  case "folder":
199  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
200  $this->__save();
201  break;
202 
203  case "file":
204  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
205  $this->__save();
206 
207  $this->file["fileName"] = $a_attribs["fileName"];
208  $this->file["id"] = $a_attribs["id"];
209 
210  // SAVE IT
211  $this->__saveFile();
212  break;
213 
214  case 'ContainerSetting':
215  $this->current_container_setting = $a_attribs['id'];
216  break;
217 
218  case 'Sort':
219 
220  if($this->group_imported)
221  {
222  $this->__initContainerSorting($a_attribs, $this->group_obj->getId());
223  }
224  else
225  {
226  $this->sort = $a_attribs;
227  }
228 
229  break;
230  }
231  }
232 
233 
234  function handlerEndTag($a_xml_parser, $a_name)
235  {
236  switch($a_name)
237  {
238  case "title":
239  $this->group_data["title"] = trim($this->cdata);
240  break;
241 
242  case "description":
243  $this->group_data["description"] = trim($this->cdata);
244  break;
245 
246  case 'information':
247  $this->group_data['information'] = trim($this->cdata);
248  break;
249 
250  case 'password':
251  $this->group_data['password'] = trim($this->cdata);
252  break;
253 
254  case 'maxMembers':
255  $this->group_data['max_members'] = trim($this->cdata);
256  break;
257 
258  case 'expiration':
259  $this->group_data['expiration_end'] = trim($this->cdata);
260  break;
261 
262  case 'start':
263  $this->group_data['expiration_start'] = trim($this->cdata);
264  break;
265 
266  case 'end':
267  $this->group_data['expiration_end'] = trim($this->cdata);
268  break;
269 
270  case "folder":
271  $this->__popParentId();
272  break;
273 
274  case "folderTitle":
275  $this->folder = trim($this->cdata);
276  $this->__saveFolder();
277  break;
278 
279  case "group":
280  // NOW SAVE THE NEW OBJECT (if it hasn't been imported)
281  $this->__save();
282  break;
283 
284  case 'ContainerSetting':
285  if($this->current_container_setting)
286  {
287  // #17357
288  if(!($this->group_obj instanceof ilObjGroup))
289  {
290  $this->__initGroupObject();
291  }
293  $this->group_obj->getId(),
295  $this->cdata);
296  }
297  break;
298  }
299  $this->cdata = '';
300  }
301 
302 
306  function handlerCharacterData($a_xml_parser, $a_data)
307  {
308  // i don't know why this is necessary, but
309  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
310  // in character data, but we don't want that, because it's the
311  // way we mask user html in our content, so we convert back...
312  $a_data = str_replace("<","&lt;",$a_data);
313  $a_data = str_replace(">","&gt;",$a_data);
314 
315  if(!empty($a_data))
316  {
317  $this->cdata .= $a_data;
318  }
319  }
320 
321  // PRIVATE
322  function __save()
323  {
324  if($this->group_imported)
325  {
326  return true;
327  }
328 
329  $this->__initGroupObject();
330 
331  $this->group_obj->setImportId($this->group_data["id"]);
332  $this->group_obj->setTitle($this->group_data["title"]);
333  $this->group_obj->setDescription($this->group_data["description"]);
334 
335  $ownerChanged = false;
336  if (isset($this->group_data["owner"]))
337  {
338  $owner = $this->group_data["owner"];
339  if (!is_numeric($owner))
340  {
341  $owner = ilUtil::__extractId ($owner, IL_INST_ID);
342  }
343  if (is_numeric($owner) && $owner > 0)
344  {
345  $this->group_obj->setOwner($owner);
346  $ownerChanged = true;
347  }
348  }
349 
353  if ($this->mode == ilGroupXMLParser::$CREATE)
354  {
355  $this->group_obj->create();
356  $this->group_obj->createReference();
357  $this->group_obj->putInTree($this->__getParentId());
358  $this->group_obj->setPermissions($this->__getParentId());
359  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? GRP_TYPE_PUBLIC : GRP_TYPE_CLOSED);
360  }
361  else
362  {
363  switch($this->group_data['type'])
364  {
365  case 'open':
366  $grp_status = GRP_TYPE_PUBLIC;
367  break;
368 
369  case 'closed':
370  $grp_status = GRP_TYPE_CLOSED;
371  break;
372 
373  }
374 
375  $this->group_obj->updateOwner();
376  if($this->group_obj->getGroupStatus() != $grp_status)
377  {
378  $this->group_obj->setGroupType($grp_status);
379  $this->group_obj->updateGroupType();
380  }
381  }
382 
383  // SET GROUP SPECIFIC DATA
384  switch($this->group_data['registration_type'])
385  {
386  case 'direct':
387  case 'enabled':
388  $flag = GRP_REGISTRATION_DIRECT;
389  break;
390 
391  case 'disabled':
393  break;
394 
395  case 'confirmation':
396  $flag = GRP_REGISTRATION_REQUEST;
397  break;
398 
399  case 'password':
401  break;
402 
403  default:
404  $flag = GRP_REGISTRATION_DIRECT;
405  }
406  $this->group_obj->setRegistrationType($flag);
407 
408  $end = new ilDateTime(time(),IL_CAL_UNIX);
409  if($this->group_data['expiration_end'])
410  {
411  $end = new ilDateTime($this->group_data['expiration_end'],IL_CAL_UNIX);
412  }
413 
414  $start = clone $end;
415  if($this->group_data['expiration_start'])
416  {
417  $start = new ilDateTime($this->group_data['expiration_start'],IL_CAL_UNIX);
418  }
419 
420  $this->group_obj->setRegistrationStart($start);
421  $this->group_obj->setRegistrationEnd($end);
422  $this->group_obj->setPassword($this->group_data['password']);
423  $this->group_obj->enableUnlimitedRegistration(!isset($this->group_data['expiration_end']));
424  $this->group_obj->enableMembershipLimitation($this->group_data['max_members_enabled']);
425  $this->group_obj->setMaxMembers($this->group_data['max_members'] ? $this->group_data['max_members'] : 0);
426  $this->group_obj->enableWaitingList($this->group_data['waiting_list_enabled']);
427 
428 
429  if ($this->mode == ilGroupXMLParser::$CREATE)
430  {
431  $this->group_obj->initGroupStatus($this->group_data["type"] == "open" ? 0 : 1);
432  }
433 
434  $this->group_obj->update();
435 
436  // ASSIGN ADMINS/MEMBERS
437  $this->__assignMembers();
438 
439  $this->__pushParentId($this->group_obj->getRefId());
440 
441  if($this->sort)
442  {
443  $this->__initContainerSorting($this->sort, $this->group_obj->getId());
444  }
445 
446  $this->group_imported = true;
447 
448  return true;
449  }
450 
451  function __saveFolder()
452  {
453  $this->__initFolderObject();
454 
455  $this->folder_obj->setTitle($this->folder);
456  $this->folder_obj->create();
457  $this->folder_obj->createReference();
458  $this->folder_obj->putInTree($this->__getParentId());
459 
460  $this->__pushParentId($this->folder_obj->getRefId());
461 
462  $this->__destroyFolderObject();
463 
464  return true;
465  }
466 
467  function __saveFile()
468  {
469  $this->__initFileObject();
470 
471  $this->file_obj->setType("file");
472  $this->file_obj->setTitle($this->file["fileName"]);
473  $this->file_obj->setFileName($this->file["fileName"]);
474  $this->file_obj->create();
475  $this->file_obj->createReference();
476  $this->file_obj->putInTree($this->__getParentId());
477  $this->file_obj->setPermissions($this->__getParentId());
478 
479  // COPY FILE
480  $this->file_obj->createDirectory();
481 
482  $this->__initImportFileObject();
483 
484  if($this->import_file_obj->findObjectFile($this->file["id"]))
485  {
486  $this->file_obj->copy($this->import_file_obj->getObjectFile(),$this->file["fileName"]);
487  }
488 
489  unset($this->file_obj);
490  unset($this->import_file_obj);
491 
492  return true;
493  }
494 
495  function __assignMembers()
496  {
497  global $ilias,$ilUser;
498 
499  $this->participants = new ilGroupParticipants($this->group_obj->getId());
500  $this->participants->add($ilUser->getId(),IL_GRP_ADMIN);
501  $this->participants->updateNotification($ilUser->getId(),true);
502 
503  // attach ADMINs
504  if (count($this->group_data["admin"]["attach"]))
505  {
506  foreach($this->group_data["admin"]["attach"] as $user)
507  {
508  if($id_data = $this->__parseId($user))
509  {
510  if($id_data['local'] or $id_data['imported'])
511  {
512  $this->participants->add($id_data['usr_id'],IL_GRP_ADMIN);
513  if(in_array($user,(array) $this->group_data['notifications']))
514  {
515  $this->participants->updateNotification($id_data['usr_id'],true);
516  }
517  }
518  }
519  }
520  }
521  // detach ADMINs
522  if (count($this->group_data["admin"]["detach"]))
523  {
524  foreach($this->group_data["admin"]["detach"] as $user)
525  {
526  if($id_data = $this->__parseId($user))
527  {
528  if($id_data['local'] or $id_data['imported'])
529  {
530  if($this->participants->isAssigned($id_data['usr_id']))
531  {
532  $this->participants->delete($id_data['usr_id']);
533  }
534  }
535  }
536  }
537  }
538  // MEMBER
539  if (count($this->group_data["member"]["attach"]))
540  {
541  foreach($this->group_data["member"]["attach"] as $user)
542  {
543  if($id_data = $this->__parseId($user))
544  {
545  if($id_data['local'] or $id_data['imported'])
546  {
547  $this->participants->add($id_data['usr_id'],IL_GRP_MEMBER);
548  }
549  }
550  }
551  }
552 
553  if (count($this->group_data["member"]["detach"]))
554  {
555  foreach($this->group_data["member"]["detach"] as $user)
556  {
557  if($id_data = $this->__parseId($user))
558  {
559  if($id_data['local'] or $id_data['imported'])
560  {
561  if($this->participants->isAssigned($id_data['usr_id']))
562  {
563  $this->participants->delete($id_data['usr_id']);
564  }
565  }
566  }
567  }
568  }
569  return true;
570  }
571 
572  function __initGroupObject()
573  {
574  include_once "./Modules/Group/classes/class.ilObjGroup.php";
575 
576  if ($this->mode == ilGroupXMLParser::$CREATE)
577  {
578  $this->group_obj =& new ilObjGroup();
579  } elseif ($this->mode == ilGroupXMLParser::$UPDATE) {
580  $this->group_obj = $this->grp;
581  }
582 
583  return true;
584  }
585 
587  {
588  include_once "./Modules/Folder/classes/class.ilObjFolder.php";
589 
590  $this->folder_obj =& new ilObjFolder();
591 
592  return true;
593  }
594 
596  {
597  include_once "./Modules/Group/classes/class.ilFileDataImportGroup.php";
598 
599  $this->import_file_obj =& new ilFileDataImportGroup();
600 
601  return true;
602  }
603 
604  function __initFileObject()
605  {
606  include_once "./Modules/File/classes/class.ilObjFile.php";
607 
608  $this->file_obj =& new ilObjFile();
609 
610  return true;
611  }
612 
614  {
615  unset($this->folder_obj);
616  }
617 
618  function __parseId($a_id)
619  {
620  global $ilias;
621 
622  $fields = explode('_',$a_id);
623 
624  if(!is_array($fields) or
625  $fields[0] != 'il' or
626  !is_numeric($fields[1]) or
627  $fields[2] != 'usr' or
628  !is_numeric($fields[3]))
629  {
630  return false;
631  }
632  if($id = ilObjUser::_getImportedUserId($a_id))
633  {
634  return array('imported' => true,
635  'local' => false,
636  'usr_id' => $id);
637  }
638  if(($fields[1] == $ilias->getSetting('inst_id',0)) and ($user = ilObjUser::_lookupName($fields[3])))
639  {
640  if(strlen($user['login']))
641  {
642  return array('imported' => false,
643  'local' => true,
644  'usr_id' => $fields[3]);
645  }
646  }
647  $GLOBALS['ilLog']->write(__METHOD__.' Parsing id failed: '.$a_id);
648  return false;
649  }
650 
651 
652  public function setMode($mode) {
653  $this->mode = $mode;
654  }
655 
656  public function setGroup(& $grp) {
657  $this->grp = $grp;
658  }
659 
660  function __initContainerSorting($a_attribs, $a_group_id)
661  {
662  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
664  }
665 }
666 ?>